system.pp 14 KB

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