syslinux.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. member of the Free Pascal development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { These things are set in the makefile, }
  13. { But you can override them here.}
  14. { If you want to link to the C library, set the conditional crtlib }
  15. { $define crtlib}
  16. { If you use an aout system, set the conditional AOUT}
  17. { $Define AOUT}
  18. Unit SysLinux;
  19. Interface
  20. {$ifdef m68k}
  21. { used for single computations }
  22. const
  23. BIAS4 = $7f-1;
  24. {$endif}
  25. {$ifdef BSD}
  26. {$define newsignal}
  27. {$endif}
  28. {$I systemh.inc}
  29. {$I heaph.inc}
  30. const
  31. UnusedHandle = -1;
  32. StdInputHandle = 0;
  33. StdOutputHandle = 1;
  34. StdErrorHandle = 2;
  35. var
  36. argc : longint;
  37. argv : ppchar;
  38. envp : ppchar;
  39. Implementation
  40. {$I system.inc}
  41. {$ifdef crtlib}
  42. Procedure _rtl_exit(l: longint); cdecl;
  43. Function _rtl_paramcount: longint; cdecl;
  44. Procedure _rtl_paramstr(st: pchar; l: longint); cdecl;
  45. Function _rtl_open(f: pchar; flags: longint): longint; cdecl;
  46. Procedure _rtl_close(h: longint); cdecl;
  47. Procedure _rtl_write(h: longint; addr: longInt; len : longint); cdecl;
  48. Procedure _rtl_erase(p: pchar); cdecl;
  49. Procedure _rtl_rename(p1: pchar; p2 : pchar); cdecl;
  50. Function _rtl_read(h: longInt; addr: longInt; len : longint) : longint; cdecl;
  51. Function _rtl_filepos(Handle: longint): longint; cdecl;
  52. Procedure _rtl_seek(Handle: longint; pos:longint); cdecl;
  53. Function _rtl_filesize(Handle:longint): longInt; cdecl;
  54. Procedure _rtl_rmdir(buffer: pchar); cdecl;
  55. Procedure _rtl_mkdir(buffer: pchar); cdecl;
  56. Procedure _rtl_chdir(buffer: pchar); cdecl;
  57. {$else}
  58. { used in syscall to report errors.}
  59. var
  60. Errno : longint;
  61. { Include constant and type definitions }
  62. {$i errno.inc } { Error numbers }
  63. {$i sysnr.inc } { System call numbers }
  64. {$i sysconst.inc } { Miscellaneous constants }
  65. {$i systypes.inc } { Types needed for system calls }
  66. { Read actual system call definitions. }
  67. {$i syscalls.inc }
  68. {$endif}
  69. {*****************************************************************************
  70. Misc. System Dependent Functions
  71. *****************************************************************************}
  72. procedure prthaltproc;external name '_haltproc';
  73. procedure System_exit;
  74. begin
  75. {$ifdef i386}
  76. asm
  77. jmp prthaltproc
  78. end;
  79. {$else}
  80. asm
  81. jmp prthaltproc
  82. end;
  83. {$endif}
  84. End;
  85. Function ParamCount: Longint;
  86. Begin
  87. Paramcount:=argc-1
  88. End;
  89. Function ParamStr(l: Longint): String;
  90. var
  91. link,
  92. hs : string;
  93. i : longint;
  94. begin
  95. if l=0 then
  96. begin
  97. str(sys_getpid,hs);
  98. hs:='/proc/'+hs+'/exe'#0;
  99. i:=Sys_readlink(@hs[1],@link[1],high(link));
  100. if i>0 then
  101. begin
  102. link[0]:=chr(i);
  103. paramstr:=link;
  104. end
  105. else
  106. paramstr:=strpas(argv[0]);
  107. end
  108. else
  109. if (l>0) and (l<argc) then
  110. paramstr:=strpas(argv[l])
  111. else
  112. paramstr:='';
  113. end;
  114. Procedure Randomize;
  115. Begin
  116. randseed:=sys_time;
  117. End;
  118. {*****************************************************************************
  119. Heap Management
  120. *****************************************************************************}
  121. var
  122. _HEAP : longint;external name 'HEAP';
  123. _HEAPSIZE : longint;external name 'HEAPSIZE';
  124. function getheapstart:pointer;assembler;
  125. {$ifdef i386}
  126. asm
  127. leal _HEAP,%eax
  128. end ['EAX'];
  129. {$else}
  130. asm
  131. lea.l _HEAP,a0
  132. move.l a0,d0
  133. end;
  134. {$endif}
  135. function getheapsize:longint;assembler;
  136. {$ifdef i386}
  137. asm
  138. movl _HEAPSIZE,%eax
  139. end ['EAX'];
  140. {$else}
  141. asm
  142. move.l _HEAPSIZE,d0
  143. end ['D0'];
  144. {$endif}
  145. {$ifdef bsd}
  146. Function sbrk(size : longint) : Longint;
  147. CONST MAP_PRIVATE =2;
  148. MAP_ANONYMOUS =$1000; {$20 under linux}
  149. begin
  150. Sbrk:=do_syscall(syscall_nr_mmap,0,size,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0,0);
  151. if ErrNo<>0 then
  152. Sbrk:=0;
  153. end;
  154. {$else}
  155. Function sbrk(size : longint) : Longint;
  156. type
  157. tmmapargs=packed record
  158. address : longint;
  159. size : longint;
  160. prot : longint;
  161. flags : longint;
  162. fd : longint;
  163. offset : longint;
  164. end;
  165. var
  166. t : syscallregs;
  167. mmapargs : tmmapargs;
  168. begin
  169. mmapargs.address:=0;
  170. mmapargs.size:=Size;
  171. mmapargs.prot:=3;
  172. mmapargs.flags:=$22;
  173. mmapargs.fd:=-1;
  174. mmapargs.offset:=0;
  175. t.reg2:=longint(@mmapargs);
  176. Sbrk:=syscall(syscall_nr_mmap,t);
  177. if ErrNo<>0 then
  178. Sbrk:=0;
  179. end;
  180. {$endif}
  181. { include standard heap management }
  182. {$I heap.inc}
  183. {*****************************************************************************
  184. Low Level File Routines
  185. *****************************************************************************}
  186. {
  187. The lowlevel file functions should take care of setting the InOutRes to the
  188. correct value if an error has occured, else leave it untouched
  189. }
  190. Procedure Errno2Inoutres;
  191. {
  192. Convert ErrNo error to the correct Inoutres value
  193. }
  194. begin
  195. if ErrNo=0 then { Else it will go through all the cases }
  196. exit;
  197. case ErrNo of
  198. Sys_ENFILE,
  199. Sys_EMFILE : Inoutres:=4;
  200. Sys_ENOENT : Inoutres:=2;
  201. Sys_EBADF : Inoutres:=6;
  202. Sys_ENOMEM,
  203. Sys_EFAULT : Inoutres:=217;
  204. Sys_EINVAL : Inoutres:=218;
  205. Sys_EPIPE,
  206. Sys_EINTR,
  207. Sys_EIO,
  208. Sys_EAGAIN,
  209. Sys_ENOSPC : Inoutres:=101;
  210. Sys_ENAMETOOLONG,
  211. Sys_ELOOP,
  212. Sys_ENOTDIR : Inoutres:=3;
  213. Sys_EROFS,
  214. Sys_EEXIST,
  215. Sys_EACCES : Inoutres:=5;
  216. Sys_ETXTBSY : Inoutres:=162;
  217. end;
  218. end;
  219. Procedure Do_Close(Handle:Longint);
  220. Begin
  221. {$ifdef crtlib}
  222. _rtl_close(Handle);
  223. {$else}
  224. sys_close(Handle);
  225. {$endif}
  226. End;
  227. Procedure Do_Erase(p:pchar);
  228. Begin
  229. {$ifdef crtlib}
  230. _rtl_erase(p);
  231. {$else}
  232. sys_unlink(p);
  233. Errno2Inoutres;
  234. {$endif}
  235. End;
  236. Procedure Do_Rename(p1,p2:pchar);
  237. Begin
  238. {$ifdef crtlib}
  239. _rtl_rename(p1,p2);
  240. {$else }
  241. sys_rename(p1,p2);
  242. Errno2Inoutres;
  243. {$endif}
  244. End;
  245. Function Do_Write(Handle,Addr,Len:Longint):longint;
  246. Begin
  247. {$ifdef crtlib}
  248. _rtl_write(Handle,addr,len);
  249. Do_Write:=Len;
  250. {$else}
  251. Do_Write:=sys_write(Handle,pchar(addr),len);
  252. Errno2Inoutres;
  253. {$endif}
  254. if Do_Write<0 then
  255. Do_Write:=0;
  256. End;
  257. Function Do_Read(Handle,Addr,Len:Longint):Longint;
  258. Begin
  259. {$ifdef crtlib}
  260. Do_Read:=_rtl_read(Handle,addr,len);
  261. {$else}
  262. Do_Read:=sys_read(Handle,pchar(addr),len);
  263. Errno2Inoutres;
  264. {$endif}
  265. if Do_Read<0 then
  266. Do_Read:=0;
  267. End;
  268. Function Do_FilePos(Handle: Longint): Longint;
  269. Begin
  270. {$ifdef crtlib}
  271. Do_FilePos:=_rtl_filepos(Handle);
  272. {$else}
  273. Do_FilePos:=sys_lseek(Handle, 0, Seek_Cur);
  274. Errno2Inoutres;
  275. {$endif}
  276. End;
  277. Procedure Do_Seek(Handle,Pos:Longint);
  278. Begin
  279. {$ifdef crtlib}
  280. _rtl_seek(Handle, Pos);
  281. {$else}
  282. sys_lseek(Handle, pos, Seek_set);
  283. {$endif}
  284. End;
  285. Function Do_SeekEnd(Handle:Longint): Longint;
  286. begin
  287. {$ifdef crtlib}
  288. Do_SeekEnd:=_rtl_filesize(Handle);
  289. {$else}
  290. Do_SeekEnd:=sys_lseek(Handle,0,Seek_End);
  291. {$endif}
  292. end;
  293. {$ifdef BSD}
  294. Function Do_FileSize(Handle:Longint): Longint;
  295. {$ifndef crtlib}
  296. var
  297. Info : Stat;
  298. {$endif}
  299. Begin
  300. {$ifdef crtlib}
  301. Do_FileSize:=_rtl_filesize(Handle);
  302. {$else}
  303. if do_SysCall(syscall_nr_fstat,handle,longint(@info))=0 then
  304. Do_FileSize:=Info.Size
  305. else
  306. Do_FileSize:=0;
  307. Errno2Inoutres;
  308. {$endif}
  309. End;
  310. {$ELSE}
  311. Function Do_FileSize(Handle:Longint): Longint;
  312. {$ifndef crtlib}
  313. var
  314. regs : Syscallregs;
  315. Info : Stat;
  316. {$endif}
  317. Begin
  318. {$ifdef crtlib}
  319. Do_FileSize:=_rtl_filesize(Handle);
  320. {$else}
  321. regs.reg2:=Handle;
  322. regs.reg3:=longint(@Info);
  323. if SysCall(SysCall_nr_fstat,regs)=0 then
  324. Do_FileSize:=Info.Size
  325. else
  326. Do_FileSize:=0;
  327. Errno2Inoutres;
  328. {$endif}
  329. End;
  330. {$endif}
  331. Procedure Do_Truncate(Handle,Pos:longint);
  332. {$ifndef crtlib}
  333. {$ifndef bsd}
  334. var
  335. sr : syscallregs;
  336. {$endif}
  337. {$endif}
  338. begin
  339. {$ifndef crtlib}
  340. {$ifdef bsd}
  341. do_syscall(syscall_nr_ftruncate,handle,pos,0);
  342. {$else}
  343. sr.reg2:=Handle;
  344. sr.reg3:=Pos;
  345. syscall(syscall_nr_ftruncate,sr);
  346. {$endif}
  347. Errno2Inoutres;
  348. {$endif}
  349. end;
  350. Procedure Do_Open(var f;p:pchar;flags:longint);
  351. {
  352. FileRec and textrec have both Handle and mode as the first items so
  353. they could use the same routine for opening/creating.
  354. when (flags and $100) the file will be append
  355. when (flags and $1000) the file will be truncate/rewritten
  356. when (flags and $10000) there is no check for close (needed for textfiles)
  357. }
  358. var
  359. {$ifndef crtlib}
  360. oflags : longint;
  361. {$endif}
  362. Begin
  363. { close first if opened }
  364. if ((flags and $10000)=0) then
  365. begin
  366. case FileRec(f).mode of
  367. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  368. fmclosed : ;
  369. else
  370. begin
  371. inoutres:=102; {not assigned}
  372. exit;
  373. end;
  374. end;
  375. end;
  376. { reset file Handle }
  377. FileRec(f).Handle:=UnusedHandle;
  378. { We do the conversion of filemodes here, concentrated on 1 place }
  379. case (flags and 3) of
  380. 0 : begin
  381. oflags :=Open_RDONLY;
  382. FileRec(f).mode:=fminput;
  383. end;
  384. 1 : begin
  385. oflags :=Open_WRONLY;
  386. FileRec(f).mode:=fmoutput;
  387. end;
  388. 2 : begin
  389. oflags :=Open_RDWR;
  390. FileRec(f).mode:=fminout;
  391. end;
  392. end;
  393. if (flags and $1000)=$1000 then
  394. oflags:=oflags or (Open_CREAT or Open_TRUNC)
  395. else
  396. if (flags and $100)=$100 then
  397. oflags:=oflags or (Open_APPEND);
  398. { empty name is special }
  399. if p[0]=#0 then
  400. begin
  401. case FileRec(f).mode of
  402. fminput :
  403. FileRec(f).Handle:=StdInputHandle;
  404. fminout, { this is set by rewrite }
  405. fmoutput :
  406. FileRec(f).Handle:=StdOutputHandle;
  407. fmappend :
  408. begin
  409. FileRec(f).Handle:=StdOutputHandle;
  410. FileRec(f).mode:=fmoutput; {fool fmappend}
  411. end;
  412. end;
  413. exit;
  414. end;
  415. { real open call }
  416. {$ifdef crtlib}
  417. FileRec(f).Handle:=_rtl_open(p, oflags);
  418. if FileRec(f).Handle<0 then
  419. InOutRes:=2
  420. else
  421. InOutRes:=0;
  422. {$else}
  423. FileRec(f).Handle:=sys_open(p,oflags,438);
  424. if (ErrNo=Sys_EROFS) and ((OFlags and Open_RDWR)<>0) then
  425. begin
  426. Oflags:=Oflags and not(Open_RDWR);
  427. FileRec(f).Handle:=sys_open(p,oflags,438);
  428. end;
  429. Errno2Inoutres;
  430. {$endif}
  431. End;
  432. Function Do_IsDevice(Handle:Longint):boolean;
  433. {
  434. Interface to Unix ioctl call.
  435. Performs various operations on the filedescriptor Handle.
  436. Ndx describes the operation to perform.
  437. Data points to data needed for the Ndx function. The structure of this
  438. data is function-dependent.
  439. }
  440. var
  441. {$ifndef BSD}
  442. sr: SysCallRegs;
  443. {$endif}
  444. Data : array[0..255] of byte; {Large enough for termios info}
  445. begin
  446. {$ifdef BSD}
  447. Do_IsDevice:=(do_SysCall(syscall_nr_ioctl,handle,$5401,longint(@data))=0);
  448. {$else}
  449. sr.reg2:=Handle;
  450. sr.reg3:=$5401; {=TCGETS}
  451. sr.reg4:=Longint(@Data);
  452. Do_IsDevice:=(SysCall(Syscall_nr_ioctl,sr)=0);
  453. {$endif}
  454. end;
  455. {*****************************************************************************
  456. UnTyped File Handling
  457. *****************************************************************************}
  458. {$i file.inc}
  459. {*****************************************************************************
  460. Typed File Handling
  461. *****************************************************************************}
  462. {$i typefile.inc}
  463. {*****************************************************************************
  464. Text File Handling
  465. *****************************************************************************}
  466. {$DEFINE SHORT_LINEBREAK}
  467. {$DEFINE EXTENDED_EOF}
  468. {$i text.inc}
  469. {*****************************************************************************
  470. Directory Handling
  471. *****************************************************************************}
  472. Procedure MkDir(Const s: String);[IOCheck];
  473. Var
  474. Buffer: Array[0..255] of Char;
  475. Begin
  476. If InOutRes <> 0 then exit;
  477. Move(s[1], Buffer, Length(s));
  478. Buffer[Length(s)] := #0;
  479. {$ifdef crtlib}
  480. _rtl_mkdir(@buffer);
  481. {$else}
  482. sys_mkdir(@buffer, 511);
  483. Errno2Inoutres;
  484. {$endif}
  485. End;
  486. Procedure RmDir(Const s: String);[IOCheck];
  487. Var
  488. Buffer: Array[0..255] of Char;
  489. Begin
  490. If InOutRes <> 0 then exit;
  491. Move(s[1], Buffer, Length(s));
  492. Buffer[Length(s)] := #0;
  493. {$ifdef crtlib}
  494. _rtl_rmdir(@buffer);
  495. {$else}
  496. sys_rmdir(@buffer);
  497. Errno2Inoutres;
  498. {$endif}
  499. End;
  500. Procedure ChDir(Const s: String);[IOCheck];
  501. Var
  502. Buffer: Array[0..255] of Char;
  503. Begin
  504. If InOutRes <> 0 then exit;
  505. Move(s[1], Buffer, Length(s));
  506. Buffer[Length(s)] := #0;
  507. {$ifdef crtlib}
  508. _rtl_chdir(@buffer);
  509. {$else}
  510. sys_chdir(@buffer);
  511. Errno2Inoutres;
  512. {$endif}
  513. End;
  514. procedure getdir(drivenr : byte;var dir : shortstring);
  515. {$ifndef crtlib}
  516. var
  517. thisdir : stat;
  518. rootino,
  519. thisino,
  520. dotdotino : longint;
  521. rootdev,
  522. thisdev,
  523. dotdotdev : word;
  524. thedir,dummy : string[255];
  525. dirstream : pdir;
  526. d : pdirent;
  527. mountpoint,validdir : boolean;
  528. predot : string[255];
  529. {$endif}
  530. begin
  531. drivenr:=0;
  532. dir:='';
  533. {$ifndef crtlib}
  534. thedir:='/'#0;
  535. if sys_stat(@thedir[1],thisdir)<0 then
  536. exit;
  537. rootino:=thisdir.ino;
  538. rootdev:=thisdir.dev;
  539. thedir:='.'#0;
  540. if sys_stat(@thedir[1],thisdir)<0 then
  541. exit;
  542. thisino:=thisdir.ino;
  543. thisdev:=thisdir.dev;
  544. { Now we can uniquely identify the current and root dir }
  545. thedir:='';
  546. predot:='';
  547. while not ((thisino=rootino) and (thisdev=rootdev)) do
  548. begin
  549. { Are we on a mount point ? }
  550. dummy:=predot+'..'#0;
  551. if sys_stat(@dummy[1],thisdir)<0 then
  552. exit;
  553. dotdotino:=thisdir.ino;
  554. dotdotdev:=thisdir.dev;
  555. mountpoint:=(thisdev<>dotdotdev);
  556. { Now, Try to find the name of this dir in the previous one }
  557. dirstream:=opendir (@dummy[1]);
  558. if dirstream=nil then
  559. exit;
  560. repeat
  561. d:=sys_readdir (dirstream);
  562. validdir:=false;
  563. if (d<>nil) and
  564. (not ((d^.name[0]='.') and ((d^.name[1]=#0) or ((d^.name[1]='.')
  565. and (d^.name[2]=#0))))) and
  566. (mountpoint or (d^.ino=thisino)) then
  567. begin
  568. dummy:=predot+'../'+strpas(@(d^.name[0]))+#0;
  569. validdir:=not (sys_stat (@(dummy[1]),thisdir)<0);
  570. end
  571. else
  572. validdir:=false;
  573. until (d=nil) or
  574. ((validdir) and (thisdir.dev=thisdev) and (thisdir.ino=thisino) );
  575. if (closedir(dirstream)<0) or (d=nil) then
  576. exit;
  577. { At this point, d.name contains the name of the current dir}
  578. thedir:='/'+strpas(@(d^.name[0]))+thedir;
  579. thisdev:=dotdotdev;
  580. thisino:=dotdotino;
  581. predot:=predot+'../';
  582. end;
  583. { Now rootino=thisino and rootdev=thisdev so we've reached / }
  584. dir:=thedir
  585. {$endif}
  586. end;
  587. {*****************************************************************************
  588. SystemUnit Initialization
  589. *****************************************************************************}
  590. {$ifdef I386}
  591. { this should be defined in i386 directory !! PM }
  592. const
  593. fpucw : word = $1332;
  594. FPU_Invalid = 1;
  595. FPU_Denormal = 2;
  596. FPU_DivisionByZero = 4;
  597. FPU_Overflow = 8;
  598. FPU_Underflow = $10;
  599. FPU_StackUnderflow = $20;
  600. FPU_StackOverflow = $40;
  601. {$endif I386}
  602. Procedure ResetFPU;
  603. begin
  604. {$ifdef I386}
  605. {$ifndef CORRECTFLDCW}
  606. {$asmmode direct}
  607. {$endif}
  608. asm
  609. fninit
  610. fldcw fpucw
  611. end;
  612. {$ifndef CORRECTFLDCW}
  613. {$asmmode att}
  614. {$endif}
  615. {$endif I386}
  616. end;
  617. {$ifndef BSD}
  618. {$ifndef newSignal}
  619. Procedure SignalToRunError(Sig:longint);
  620. begin
  621. case sig of
  622. 8 : begin
  623. { this is not allways necessary but I don't know yet
  624. how to tell if it is or not PM }
  625. ResetFPU;
  626. HandleError(200);
  627. end;
  628. 11 : HandleError(216);
  629. end;
  630. end;
  631. Procedure InstallSignals;
  632. var
  633. sr : syscallregs;
  634. begin
  635. sr.reg3:=longint(@SignalToRunError);
  636. { sigsegv }
  637. sr.reg2:=11;
  638. syscall(syscall_nr_signal,sr);
  639. { sigfpe }
  640. sr.reg2:=8;
  641. syscall(syscall_nr_signal,sr);
  642. end;
  643. {$else newSignal}
  644. {$i i386/signal.inc}
  645. procedure SignalToRunerror(Sig: longint; SigContext: SigContextRec); cdecl;
  646. var
  647. res,fpustate : word;
  648. begin
  649. case sig of
  650. 8 : begin
  651. { this is not allways necessary but I don't know yet
  652. how to tell if it is or not PM }
  653. {$ifdef I386}
  654. fpustate:=0;
  655. res:=200;
  656. if assigned(SigContext.fpstate) then
  657. fpuState:=SigContext.fpstate^.sw;
  658. {$ifdef SYSTEMDEBUG}
  659. Writeln(stderr,'FpuState = ',Hexstr(FpuState,4));
  660. {$endif SYSTEMDEBUG}
  661. if (FpuState and $7f) <> 0 then
  662. begin
  663. if (FpuState and FPU_Invalid)<>0 then
  664. res:=216
  665. else if (FpuState and FPU_Denormal)<>0 then
  666. res:=216
  667. else if (FpuState and FPU_DivisionByZero)<>0 then
  668. res:=200
  669. else if (FpuState and FPU_Overflow)<>0 then
  670. res:=205
  671. else if (FpuState and FPU_Underflow)<>0 then
  672. res:=206
  673. else
  674. res:=207; {'Coprocessor Error'}
  675. end;
  676. {$endif I386}
  677. ResetFPU;
  678. HandleError(res);
  679. end;
  680. 11 : HandleError(216);
  681. end;
  682. end;
  683. Procedure InstallSignals;
  684. const
  685. act: SigActionRec = (handler:(Sa:@SignalToRunError);sa_mask:0;sa_flags:0;
  686. Sa_restorer: NIL);
  687. oldact: PSigActionRec = Nil;
  688. begin
  689. ResetFPU;
  690. SigAction(8,@act,oldact);
  691. SigAction(11,@act,oldact);
  692. end;
  693. {$endif newSignal}
  694. {$endif bsd}
  695. procedure SetupCmdLine;
  696. var
  697. bufsize,
  698. len,j,
  699. size,i : longint;
  700. found : boolean;
  701. buf : array[0..1026] of char;
  702. procedure AddBuf;
  703. begin
  704. reallocmem(cmdline,size+bufsize);
  705. move(buf,cmdline[size],bufsize);
  706. inc(size,bufsize);
  707. bufsize:=0;
  708. end;
  709. begin
  710. size:=0;
  711. bufsize:=0;
  712. i:=0;
  713. while (i<argc) do
  714. begin
  715. len:=strlen(argv[i]);
  716. if len>sizeof(buf)-2 then
  717. len:=sizeof(buf)-2;
  718. found:=false;
  719. for j:=1 to len do
  720. if argv[i][j]=' ' then
  721. begin
  722. found:=true;
  723. break;
  724. end;
  725. if bufsize+len>=sizeof(buf)-2 then
  726. AddBuf;
  727. if found then
  728. begin
  729. buf[bufsize]:='"';
  730. inc(bufsize);
  731. end;
  732. move(argv[i]^,buf[bufsize],len);
  733. inc(bufsize,len);
  734. if found then
  735. begin
  736. buf[bufsize]:='"';
  737. inc(bufsize);
  738. end;
  739. if i<argc then
  740. buf[bufsize]:=' '
  741. else
  742. buf[bufsize]:=#0;
  743. inc(bufsize);
  744. inc(i);
  745. end;
  746. AddBuf;
  747. end;
  748. Begin
  749. { Set up signals handlers }
  750. {$ifndef bsd}
  751. InstallSignals;
  752. {$endif}
  753. { Setup heap }
  754. InitHeap;
  755. InitExceptions;
  756. { Arguments }
  757. SetupCmdLine;
  758. { Setup stdin, stdout and stderr }
  759. OpenStdIO(Input,fmInput,StdInputHandle);
  760. OpenStdIO(Output,fmOutput,StdOutputHandle);
  761. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  762. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  763. { Reset IO Error }
  764. InOutRes:=0;
  765. End.
  766. {
  767. $Log$
  768. Revision 1.44 2000-04-14 13:04:53 marco
  769. * Merged bsd/syslinux.pp and 1.43 linux/syslinux.pp to this file with ifdefs
  770. Revision 1.43 2000/04/07 14:56:36 peter
  771. * switch to direct asm if not correctfldcw defined
  772. Revision 1.42 2000/03/31 23:26:32 pierre
  773. * FPU needs reset for all SIGFPE even from integer division by zero
  774. Revision 1.41 2000/03/31 23:21:19 pierre
  775. * multiple exception handling works
  776. (for linux only if syslinux is compiled with -dnewsignal)
  777. Revision 1.40 2000/03/31 13:24:28 jonas
  778. * signal handling using sigaction when compiled with -dnewsignal
  779. (allows multiple signals to be received in one run)
  780. Revision 1.39 2000/03/25 12:28:37 peter
  781. * patch for getdir from Pierre
  782. Revision 1.38 2000/03/23 15:24:18 peter
  783. * remove handle check for do_close
  784. Revision 1.37 2000/02/09 16:59:32 peter
  785. * truncated log
  786. Revision 1.36 2000/02/09 12:17:51 peter
  787. * moved halt to system.inc
  788. * syslinux doesn't use direct asm anymore
  789. Revision 1.35 2000/02/08 11:47:09 peter
  790. * paramstr(0) support
  791. Revision 1.34 2000/01/20 23:38:02 peter
  792. * support fm_inout as stdoutput for assign(f,'');rewrite(f,1); becuase
  793. rewrite opens always with filemode 2
  794. Revision 1.33 2000/01/16 22:25:38 peter
  795. * check handle for file closing
  796. Revision 1.32 2000/01/07 16:41:41 daniel
  797. * copyright 2000
  798. Revision 1.31 2000/01/07 16:32:28 daniel
  799. * copyright 2000 added
  800. Revision 1.30 1999/12/01 22:57:31 peter
  801. * cmdline support
  802. Revision 1.29 1999/11/06 14:39:12 peter
  803. * truncated log
  804. Revision 1.28 1999/10/28 09:50:06 peter
  805. * use mmap instead of brk
  806. Revision 1.27 1999/09/10 15:40:35 peter
  807. * fixed do_open flags to be > $100, becuase filemode can be upto 255
  808. Revision 1.26 1999/09/08 16:14:43 peter
  809. * pointer fixes
  810. Revision 1.25 1999/07/28 23:18:36 peter
  811. * closedir fixes, which now disposes the pdir itself
  812. }