system.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. This is a prototype file to show all function that need to be implemented
  6. for a new operating system (provided the processor specific
  7. function are already implemented !)
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. { no stack check in system }
  15. {$DEFINE SHORT_LINEBREAK}
  16. {$S-}
  17. unit System;
  18. interface
  19. { include system-independent routine headers }
  20. {$I systemh.inc}
  21. { include heap support headers }
  22. {$I heaph.inc}
  23. {Platform specific information}
  24. const
  25. LineEnding = #10;
  26. LFNSupport = true;
  27. DirectorySeparator = '/';
  28. DriveSeparator = ':';
  29. PathSeparator = ':';
  30. { FileNameCaseSensitive is defined separately below!!! }
  31. const
  32. FileNameCaseSensitive : boolean = true;
  33. sLineBreak : string[1] = LineEnding;
  34. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  35. var
  36. argc : longint;
  37. argv : ppchar;
  38. envp : ppchar;
  39. errno : longint; // MvdV: yuckie
  40. UnusedHandle:longint;
  41. StdInputHandle:longint;
  42. StdOutputHandle:longint;
  43. StdErrorHandle:longint;
  44. implementation
  45. {$I sysfiles.inc}
  46. function sys_unlink (a:cardinal;name:pchar):longint; cdecl; external name 'sys_unlink';
  47. function sys_rename (a:cardinal;p1:pchar;b:cardinal;p2:pchar):longint; cdecl; external name 'sys_rename';
  48. function sys_create_area (name:pchar; var start:longint; a,b,c,d:longint):longint; cdecl; external name 'sys_create_area';
  49. function sys_resize_area (handle:cardinal; size:longint):longint; cdecl; external name 'sys_resize_area';
  50. function sys_mkdir (a:cardinal; name:pchar; mode:cardinal):longint; cdecl; external name 'sys_mkdir';
  51. function sys_chdir (a:cardinal; name:pchar):longint; cdecl; external name 'sys_chdir';
  52. function sys_rmdir (a:cardinal; name:pchar):longint; cdecl; external name 'sys_rmdir';
  53. {$I system.inc}
  54. {*****************************************************************************
  55. System Dependent Exit code
  56. *****************************************************************************}
  57. procedure prthaltproc;external name '_haltproc';
  58. procedure system_exit;
  59. begin
  60. asm
  61. jmp prthaltproc
  62. end;
  63. End;
  64. {*****************************************************************************
  65. Stack check code
  66. *****************************************************************************}
  67. { cheking the stack is done system independend in 1.1
  68. procedure int_stackcheck(stack_size:longint);[public,alias:'FPC_STACKCHECK'];
  69. {
  70. called when trying to get local stack if the compiler directive $S
  71. is set this function must preserve esi !!!! because esi is set by
  72. the calling proc for methods it must preserve all registers !!
  73. With a 2048 byte safe area used to write to StdIo without crossing
  74. the stack boundary
  75. }
  76. begin
  77. end;
  78. }
  79. {*****************************************************************************
  80. ParamStr/Randomize
  81. *****************************************************************************}
  82. { number of args }
  83. function paramcount : longint;
  84. begin
  85. paramcount := argc - 1;
  86. end;
  87. { argument number l }
  88. function paramstr(l : longint) : string;
  89. begin
  90. if (l>=0) and (l+1<=argc) then
  91. paramstr:=strpas(argv[l])
  92. else
  93. paramstr:='';
  94. end;
  95. { set randseed to a new pseudo random value }
  96. procedure randomize;
  97. begin
  98. {regs.realeax:=$2c00;
  99. sysrealintr($21,regs);
  100. hl:=regs.realedx and $ffff;
  101. randseed:=hl*$10000+ (regs.realecx and $ffff);}
  102. randseed:=0;
  103. end;
  104. {*****************************************************************************
  105. Heap Management
  106. *****************************************************************************}
  107. var myheapstart:longint;
  108. myheapsize:longint;
  109. myheaprealsize:longint;
  110. heap_handle:longint;
  111. zero:longint;
  112. { first address of heap }
  113. function getheapstart:pointer;
  114. begin
  115. getheapstart:=pointer(myheapstart);
  116. end;
  117. { current length of heap }
  118. function getheapsize:longint;
  119. begin
  120. getheapsize:=myheapsize;
  121. end;
  122. { function to allocate size bytes more for the program }
  123. { must return the first address of new data space or -1 if fail }
  124. function Sbrk(size : longint):longint;
  125. var newsize,newrealsize:longint;
  126. begin
  127. if (myheapsize+size)<=myheaprealsize then begin
  128. Sbrk:=myheapstart+myheapsize;
  129. myheapsize:=myheapsize+size;
  130. exit;
  131. end;
  132. newsize:=myheapsize+size;
  133. newrealsize:=(newsize and $FFFFF000)+$1000;
  134. if sys_resize_area(heap_handle,newrealsize)=0 then begin
  135. Sbrk:=myheapstart+myheapsize;
  136. myheapsize:=newsize;
  137. myheaprealsize:=newrealsize;
  138. exit;
  139. end;
  140. Sbrk:=-1;
  141. end;
  142. { include standard heap management }
  143. {$I heap.inc}
  144. {****************************************************************************
  145. Low level File Routines
  146. All these functions can set InOutRes on errors
  147. ****************************************************************************}
  148. { close a file from the handle value }
  149. procedure do_close(handle : longint);
  150. begin
  151. { writeln ('CLOSE ',handle);}
  152. if handle<=2 then exit;
  153. InOutRes:=sys_close(handle);
  154. end;
  155. procedure do_erase(p : pchar);
  156. begin
  157. if sys_unlink($FF000000,p)<>0 then InOutRes:=1
  158. else InOutRes:=0;
  159. end;
  160. procedure do_rename(p1,p2 : pchar);
  161. begin
  162. InOutRes:=sys_rename($FF000000,p1,$FF000000,p2);
  163. end;
  164. function do_write(h,addr,len : longint) : longint;
  165. begin
  166. { if h>0 then begin
  167. sys_write ('WRITE handle=%d ',h);
  168. printf ('addr=%x ',addr);
  169. printf ('len=%d',len);
  170. printf ('%c',10);
  171. end;}
  172. do_write:=sys_write (h,pointer(addr),len,zero);
  173. if (do_write<0) then begin
  174. InOutRes:=do_write;
  175. do_write:=0;
  176. end else InOutRes:=0;
  177. end;
  178. function do_read(h,addr,len : longint) : longint;
  179. begin
  180. { if h>2 then begin
  181. printf ('READ handle=%d ',h);
  182. printf ('addr=%x ',addr);
  183. printf ('len=%d',len);
  184. end;}
  185. do_read:=sys_read (h,pointer(addr),len,zero);
  186. if (do_read<0) then begin
  187. InOutRes:=do_read;
  188. do_read:=0;
  189. end else InOutRes:=0;
  190. end;
  191. function do_filepos(handle : longint) : longint;
  192. begin
  193. do_filepos:=sys_lseek(handle,0,1); {1=SEEK_CUR}
  194. if (do_filepos<0) then begin
  195. InOutRes:=do_filepos;
  196. do_filepos:=0;
  197. end else InOutRes:=0;
  198. end;
  199. procedure do_seek(handle,pos : longint);
  200. begin
  201. InOutRes:=sys_lseek(handle,pos,0);
  202. if InOutRes>0 then InOutRes:=0;
  203. end;
  204. function do_seekend(handle:longint):longint;
  205. begin
  206. do_seekend:=sys_lseek (handle,0,2); {2=SEEK_END}
  207. if do_seekend<0 then begin
  208. InOutRes:=do_seekend;
  209. do_seekend:=0;
  210. end else InOutRes:=0;
  211. end;
  212. function do_filesize(handle : longint) : longint;
  213. var cur:longint;
  214. begin
  215. cur:=sys_lseek (handle,0,1); {1=SEEK_CUR}
  216. if cur<0 then begin
  217. InOutRes:=cur;
  218. do_filesize:=0;
  219. exit;
  220. end;
  221. do_filesize:=sys_lseek (handle,0,2); {2=SEEK_END}
  222. if do_filesize<0 then begin
  223. InOutRes:=do_filesize;
  224. do_filesize:=0;
  225. exit;
  226. end;
  227. cur:=sys_lseek (handle,cur,0); {0=SEEK_POS}
  228. if cur<0 then begin
  229. InOutRes:=cur;
  230. do_filesize:=0;
  231. exit;
  232. end;
  233. end;
  234. { truncate at a given position }
  235. procedure do_truncate (handle,pos:longint);
  236. begin
  237. InOutRes:=1;
  238. end;
  239. procedure do_open(var f;p:pchar;flags:longint);
  240. {
  241. filerec and textrec have both handle and mode as the first items so
  242. they could use the same routine for opening/creating.
  243. when (flags and $100) the file will be append
  244. when (flags and $1000) the file will be truncate/rewritten
  245. when (flags and $10000) there is no check for close (needed for textfiles)
  246. }
  247. var m:longint;
  248. mode,h:longint;
  249. begin
  250. { printf ('OPEN %d ',longint(f));
  251. printf (' %s',longint(p));
  252. printf (' %x',flags);}
  253. m:=0;
  254. case (flags and $3) of
  255. $0: begin m:=m or O_RDONLY; mode:=fminput; end;
  256. $1: begin m:=m or O_WRONLY; mode:=fmoutput;end;
  257. $2: begin m:=m or O_RDWR; mode:=fminout; end;
  258. end;
  259. if (flags and $100)<>0 then m:=m or O_APPEND;
  260. if (flags and $1000)<>0 then m:=m or O_TRUNC or O_CREAT;
  261. { if (flags and $10000)<>0 then m:=m or O_TEXT else m:=m or O_BINARY;}
  262. h:=sys_open($FF000000,p,m,0,0);
  263. if h<0 then InOutRes:=h
  264. else InOutRes:=0;
  265. if InOutRes=0 then begin
  266. FileRec(f).handle:=h;
  267. FileRec(f).mode:=mode;
  268. end;
  269. end;
  270. function do_isdevice(handle:longint):boolean;
  271. begin
  272. do_isdevice:=false;
  273. InOutRes:=0;
  274. end;
  275. {*****************************************************************************
  276. UnTyped File Handling
  277. *****************************************************************************}
  278. {$i file.inc}
  279. {*****************************************************************************
  280. Typed File Handling
  281. *****************************************************************************}
  282. {$i typefile.inc}
  283. {*****************************************************************************
  284. Text File Handling
  285. *****************************************************************************}
  286. { should we consider #26 as the end of a file ? }
  287. {?? $DEFINE EOF_CTRLZ}
  288. {$i text.inc}
  289. {*****************************************************************************
  290. Directory Handling
  291. *****************************************************************************}
  292. procedure mkdir(const s : string);[IOCheck];
  293. var t:string;
  294. begin
  295. t:=s+#0;
  296. InOutRes:=sys_mkdir ($FF000000,@t[1],493);
  297. end;
  298. procedure rmdir(const s : string);[IOCheck];
  299. var t:string;
  300. begin
  301. t:=s+#0;
  302. InOutRes:=sys_rmdir ($FF000000,@t[1]);
  303. end;
  304. procedure chdir(const s : string);[IOCheck];
  305. var t:string;
  306. begin
  307. t:=s+#0;
  308. InOutRes:=sys_chdir ($FF000000,@t[1]);
  309. end;
  310. {*****************************************************************************
  311. getdir procedure
  312. *****************************************************************************}
  313. type dirent = packed record
  314. d_dev:longint;
  315. d_pdev:longint;
  316. d_ino:int64;
  317. d_pino:int64;
  318. d_reclen:word;
  319. d_name:array[0..255] of char;
  320. end;
  321. stat = packed record
  322. dev:longint; {"device" that this file resides on}
  323. ino:int64; {this file's inode #, unique per device}
  324. mode:dword; {mode bits (rwx for user, group, etc)}
  325. nlink:longint; {number of hard links to this file}
  326. uid:dword; {user id of the owner of this file}
  327. gid:dword; {group id of the owner of this file}
  328. size:int64; {size of this file (in bytes)}
  329. rdev:longint; {device type (not used)}
  330. blksize:longint; {preferref block size for i/o}
  331. atime:longint; {last access time}
  332. mtime:longint; {last modification time}
  333. ctime:longint; {last change time, not creation time}
  334. crtime:longint; {creation time}
  335. end;
  336. pstat = ^stat;
  337. function sys_stat (a:cardinal;path:pchar;info:pstat;n:longint):longint; cdecl; external name 'sys_stat';
  338. function FStat(Path:String;Var Info:stat):Boolean;
  339. {
  340. Get all information on a file, and return it in Info.
  341. }
  342. var tmp:string;
  343. var p:pchar;
  344. begin
  345. tmp:=path+#0;
  346. p:=@tmp[1];
  347. FStat:=(sys_stat($FF000000,p,@Info,0)=0);
  348. end;
  349. function sys_opendir (a:cardinal;path:pchar;b:longint):longint; cdecl; external name 'sys_opendir';
  350. function sys_readdir (fd:longint;var de:dirent;a:longint;b:byte):longint; cdecl; external name 'sys_readdir';
  351. function parentdir(fd:longint;dev:longint;ino:int64;var err:longint):string;
  352. var len:longint;
  353. ent:dirent;
  354. name:string;
  355. begin
  356. err:=0;
  357. parentdir:='';
  358. if sys_readdir(fd,ent,$11C,1)=0 then begin
  359. err:=1;
  360. exit;
  361. end;
  362. len:=StrLen(@ent.d_name);
  363. Move(ent.d_name,name[1],len);
  364. name[0]:=chr(len);
  365. { writeln ('NAME: "',name,'" = ',ent.d_dev,',',ent.d_ino);}
  366. if (dev=ent.d_dev) and (ino=ent.d_ino) then begin
  367. err:=0;
  368. parentdir:='/'+name;
  369. exit;
  370. end;
  371. err:=0;
  372. end;
  373. function getdir2:string;
  374. var tmp:string;
  375. info:stat;
  376. info2:stat;
  377. fd:longint;
  378. name:string;
  379. cur:string;
  380. res:string;
  381. err:longint;
  382. begin
  383. res:='';
  384. cur:='';
  385. repeat
  386. FStat(cur+'.',info);
  387. FStat(cur+'..',info2);
  388. { writeln ('"." = ',info.dev,',',info.ino);}
  389. if ((info.dev=info2.dev) and (info.ino=info2.ino)) then begin
  390. if res='' then getdir2:='/' else getdir2:=res;
  391. exit;
  392. end;
  393. tmp:=cur+'..'+#0;
  394. fd:=sys_opendir ($FF000000,@tmp[1],0);
  395. repeat
  396. name:=parentdir(fd,info.dev,info.ino,err);
  397. until (err<>0) or (name<>'');
  398. if err<>0 then begin
  399. getdir2:='';
  400. exit;
  401. end;
  402. res:=name+res;
  403. { writeln(res);}
  404. cur:=cur+'../';
  405. until false;
  406. end;
  407. procedure getdir(drivenr : byte;var dir : shortstring);
  408. begin
  409. drivenr:=0;
  410. dir:=getdir2;
  411. end;
  412. {*****************************************************************************
  413. SystemUnit Initialization
  414. *****************************************************************************}
  415. procedure SysInitStdIO;
  416. begin
  417. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  418. displayed in and messagebox }
  419. StdInputHandle:=0;
  420. StdOutputHandle:=1;
  421. StdErrorHandle:=2;
  422. OpenStdIO(Input,fmInput,StdInputHandle);
  423. OpenStdIO(Output,fmOutput,StdOutputHandle);
  424. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  425. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  426. end;
  427. begin
  428. { Setup heap }
  429. zero:=0;
  430. myheapsize:=$2000;
  431. myheaprealsize:=$2000;
  432. myheapstart:=0;
  433. heap_handle:=sys_create_area('fpcheap',myheapstart,0,myheaprealsize,0,3);
  434. if heap_handle>0 then begin
  435. InitHeap;
  436. end else system_exit;
  437. SysInitExceptions;
  438. { Setup IO }
  439. SysInitStdIO;
  440. { Reset IO Error }
  441. InOutRes:=0;
  442. {$ifdef HASVARIANT}
  443. initvariantmanager;
  444. {$endif HASVARIANT}
  445. end.
  446. {
  447. $Log$
  448. Revision 1.8 2003-01-08 22:32:28 marco
  449. * Small fixes and quick merge with 1.0.x. At least the compiler builds now,
  450. but it could crash hard, since there are lots of unimplemented funcs.
  451. Revision 1.7 2003/01/05 20:22:24 florian
  452. - removed stack check, it's system independend in 1.1
  453. Revision 1.6 2003/01/05 20:06:30 florian
  454. + fixed missing SysInitStdIO
  455. Revision 1.5 2002/10/13 09:25:31 florian
  456. + call to initvariantmanager inserted
  457. Revision 1.4 2002/09/07 16:01:17 peter
  458. * old logs removed and tabs fixed
  459. }