syslinux.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. {$ifdef bsd}
  524. dotdotdev : longint;
  525. {$else}
  526. dotdotdev : word;
  527. {$endif}
  528. thedir,dummy : string[255];
  529. dirstream : pdir;
  530. d : pdirent;
  531. mountpoint,validdir : boolean;
  532. predot : string[255];
  533. {$endif}
  534. begin
  535. drivenr:=0;
  536. dir:='';
  537. {$ifndef crtlib}
  538. thedir:='/'#0;
  539. if sys_stat(@thedir[1],thisdir)<0 then
  540. exit;
  541. rootino:=thisdir.ino;
  542. rootdev:=thisdir.dev;
  543. thedir:='.'#0;
  544. if sys_stat(@thedir[1],thisdir)<0 then
  545. exit;
  546. thisino:=thisdir.ino;
  547. thisdev:=thisdir.dev;
  548. { Now we can uniquely identify the current and root dir }
  549. thedir:='';
  550. predot:='';
  551. while not ((thisino=rootino) and (thisdev=rootdev)) do
  552. begin
  553. { Are we on a mount point ? }
  554. dummy:=predot+'..'#0;
  555. if sys_stat(@dummy[1],thisdir)<0 then
  556. exit;
  557. dotdotino:=thisdir.ino;
  558. dotdotdev:=thisdir.dev;
  559. mountpoint:=(thisdev<>dotdotdev);
  560. { Now, Try to find the name of this dir in the previous one }
  561. dirstream:=opendir (@dummy[1]);
  562. if dirstream=nil then
  563. exit;
  564. repeat
  565. d:=sys_readdir (dirstream);
  566. validdir:=false;
  567. if (d<>nil) and
  568. (not ((d^.name[0]='.') and ((d^.name[1]=#0) or ((d^.name[1]='.')
  569. and (d^.name[2]=#0))))) and
  570. (mountpoint or (d^.ino=thisino)) then
  571. begin
  572. dummy:=predot+'../'+strpas(@(d^.name[0]))+#0;
  573. validdir:=not (sys_stat (@(dummy[1]),thisdir)<0);
  574. end
  575. else
  576. validdir:=false;
  577. until (d=nil) or
  578. ((validdir) and (thisdir.dev=thisdev) and (thisdir.ino=thisino) );
  579. if (closedir(dirstream)<0) or (d=nil) then
  580. exit;
  581. { At this point, d.name contains the name of the current dir}
  582. thedir:='/'+strpas(@(d^.name[0]))+thedir;
  583. thisdev:=dotdotdev;
  584. thisino:=dotdotino;
  585. predot:=predot+'../';
  586. end;
  587. { Now rootino=thisino and rootdev=thisdev so we've reached / }
  588. dir:=thedir
  589. {$endif}
  590. end;
  591. {*****************************************************************************
  592. SystemUnit Initialization
  593. *****************************************************************************}
  594. {$ifdef I386}
  595. { this should be defined in i386 directory !! PM }
  596. const
  597. fpucw : word = $1332;
  598. FPU_Invalid = 1;
  599. FPU_Denormal = 2;
  600. FPU_DivisionByZero = 4;
  601. FPU_Overflow = 8;
  602. FPU_Underflow = $10;
  603. FPU_StackUnderflow = $20;
  604. FPU_StackOverflow = $40;
  605. {$endif I386}
  606. Procedure ResetFPU;
  607. begin
  608. {$ifdef I386}
  609. {$ifndef CORRECTFLDCW}
  610. {$asmmode direct}
  611. {$endif}
  612. asm
  613. fninit
  614. fldcw fpucw
  615. end;
  616. {$ifndef CORRECTFLDCW}
  617. {$asmmode att}
  618. {$endif}
  619. {$endif I386}
  620. end;
  621. {$ifndef BSD}
  622. {$ifndef newSignal}
  623. Procedure SignalToRunError(Sig:longint);
  624. begin
  625. case sig of
  626. 8 : begin
  627. { this is not allways necessary but I don't know yet
  628. how to tell if it is or not PM }
  629. ResetFPU;
  630. HandleError(200);
  631. end;
  632. 11 : HandleError(216);
  633. end;
  634. end;
  635. Procedure InstallSignals;
  636. var
  637. sr : syscallregs;
  638. begin
  639. sr.reg3:=longint(@SignalToRunError);
  640. { sigsegv }
  641. sr.reg2:=11;
  642. syscall(syscall_nr_signal,sr);
  643. { sigfpe }
  644. sr.reg2:=8;
  645. syscall(syscall_nr_signal,sr);
  646. end;
  647. {$else newSignal}
  648. {$i i386/signal.inc}
  649. procedure SignalToRunerror(Sig: longint; SigContext: SigContextRec); cdecl;
  650. var
  651. res,fpustate : word;
  652. begin
  653. case sig of
  654. 8 : begin
  655. { this is not allways necessary but I don't know yet
  656. how to tell if it is or not PM }
  657. {$ifdef I386}
  658. fpustate:=0;
  659. res:=200;
  660. if assigned(SigContext.fpstate) then
  661. fpuState:=SigContext.fpstate^.sw;
  662. {$ifdef SYSTEMDEBUG}
  663. Writeln(stderr,'FpuState = ',Hexstr(FpuState,4));
  664. {$endif SYSTEMDEBUG}
  665. if (FpuState and $7f) <> 0 then
  666. begin
  667. if (FpuState and FPU_Invalid)<>0 then
  668. res:=216
  669. else if (FpuState and FPU_Denormal)<>0 then
  670. res:=216
  671. else if (FpuState and FPU_DivisionByZero)<>0 then
  672. res:=200
  673. else if (FpuState and FPU_Overflow)<>0 then
  674. res:=205
  675. else if (FpuState and FPU_Underflow)<>0 then
  676. res:=206
  677. else
  678. res:=207; {'Coprocessor Error'}
  679. end;
  680. {$endif I386}
  681. ResetFPU;
  682. HandleError(res);
  683. end;
  684. 11 : HandleError(216);
  685. end;
  686. end;
  687. Procedure InstallSignals;
  688. const
  689. act: SigActionRec = (handler:(Sa:@SignalToRunError);sa_mask:0;sa_flags:0;
  690. Sa_restorer: NIL);
  691. oldact: PSigActionRec = Nil;
  692. begin
  693. ResetFPU;
  694. SigAction(8,@act,oldact);
  695. SigAction(11,@act,oldact);
  696. end;
  697. {$endif newSignal}
  698. {$endif bsd}
  699. procedure SetupCmdLine;
  700. var
  701. bufsize,
  702. len,j,
  703. size,i : longint;
  704. found : boolean;
  705. buf : array[0..1026] of char;
  706. procedure AddBuf;
  707. begin
  708. reallocmem(cmdline,size+bufsize);
  709. move(buf,cmdline[size],bufsize);
  710. inc(size,bufsize);
  711. bufsize:=0;
  712. end;
  713. begin
  714. size:=0;
  715. bufsize:=0;
  716. i:=0;
  717. while (i<argc) do
  718. begin
  719. len:=strlen(argv[i]);
  720. if len>sizeof(buf)-2 then
  721. len:=sizeof(buf)-2;
  722. found:=false;
  723. for j:=1 to len do
  724. if argv[i][j]=' ' then
  725. begin
  726. found:=true;
  727. break;
  728. end;
  729. if bufsize+len>=sizeof(buf)-2 then
  730. AddBuf;
  731. if found then
  732. begin
  733. buf[bufsize]:='"';
  734. inc(bufsize);
  735. end;
  736. move(argv[i]^,buf[bufsize],len);
  737. inc(bufsize,len);
  738. if found then
  739. begin
  740. buf[bufsize]:='"';
  741. inc(bufsize);
  742. end;
  743. if i<argc then
  744. buf[bufsize]:=' '
  745. else
  746. buf[bufsize]:=#0;
  747. inc(bufsize);
  748. inc(i);
  749. end;
  750. AddBuf;
  751. end;
  752. Begin
  753. { Set up signals handlers }
  754. {$ifndef bsd}
  755. InstallSignals;
  756. {$endif}
  757. { Setup heap }
  758. InitHeap;
  759. InitExceptions;
  760. { Arguments }
  761. SetupCmdLine;
  762. { Setup stdin, stdout and stderr }
  763. OpenStdIO(Input,fmInput,StdInputHandle);
  764. OpenStdIO(Output,fmOutput,StdOutputHandle);
  765. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  766. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  767. { Reset IO Error }
  768. InOutRes:=0;
  769. End.
  770. {
  771. $Log$
  772. Revision 1.45 2000-04-16 16:07:58 marco
  773. * BSD fixes
  774. Revision 1.44 2000/04/14 13:04:53 marco
  775. * Merged bsd/syslinux.pp and 1.43 linux/syslinux.pp to this file with ifdefs
  776. Revision 1.43 2000/04/07 14:56:36 peter
  777. * switch to direct asm if not correctfldcw defined
  778. Revision 1.42 2000/03/31 23:26:32 pierre
  779. * FPU needs reset for all SIGFPE even from integer division by zero
  780. Revision 1.41 2000/03/31 23:21:19 pierre
  781. * multiple exception handling works
  782. (for linux only if syslinux is compiled with -dnewsignal)
  783. Revision 1.40 2000/03/31 13:24:28 jonas
  784. * signal handling using sigaction when compiled with -dnewsignal
  785. (allows multiple signals to be received in one run)
  786. Revision 1.39 2000/03/25 12:28:37 peter
  787. * patch for getdir from Pierre
  788. Revision 1.38 2000/03/23 15:24:18 peter
  789. * remove handle check for do_close
  790. Revision 1.37 2000/02/09 16:59:32 peter
  791. * truncated log
  792. Revision 1.36 2000/02/09 12:17:51 peter
  793. * moved halt to system.inc
  794. * syslinux doesn't use direct asm anymore
  795. Revision 1.35 2000/02/08 11:47:09 peter
  796. * paramstr(0) support
  797. Revision 1.34 2000/01/20 23:38:02 peter
  798. * support fm_inout as stdoutput for assign(f,'');rewrite(f,1); becuase
  799. rewrite opens always with filemode 2
  800. Revision 1.33 2000/01/16 22:25:38 peter
  801. * check handle for file closing
  802. Revision 1.32 2000/01/07 16:41:41 daniel
  803. * copyright 2000
  804. Revision 1.31 2000/01/07 16:32:28 daniel
  805. * copyright 2000 added
  806. Revision 1.30 1999/12/01 22:57:31 peter
  807. * cmdline support
  808. Revision 1.29 1999/11/06 14:39:12 peter
  809. * truncated log
  810. Revision 1.28 1999/10/28 09:50:06 peter
  811. * use mmap instead of brk
  812. Revision 1.27 1999/09/10 15:40:35 peter
  813. * fixed do_open flags to be > $100, becuase filemode can be upto 255
  814. Revision 1.26 1999/09/08 16:14:43 peter
  815. * pointer fixes
  816. Revision 1.25 1999/07/28 23:18:36 peter
  817. * closedir fixes, which now disposes the pdir itself
  818. }