syslinux.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. {$I systemh.inc}
  26. {$I heaph.inc}
  27. const
  28. UnusedHandle = -1;
  29. StdInputHandle = 0;
  30. StdOutputHandle = 1;
  31. StdErrorHandle = 2;
  32. var
  33. argc : longint;
  34. argv : ppchar;
  35. envp : ppchar;
  36. Implementation
  37. {$I system.inc}
  38. {$ifdef crtlib}
  39. Procedure _rtl_exit(l: longint); cdecl;
  40. Function _rtl_paramcount: longint; cdecl;
  41. Procedure _rtl_paramstr(st: pchar; l: longint); cdecl;
  42. Function _rtl_open(f: pchar; flags: longint): longint; cdecl;
  43. Procedure _rtl_close(h: longint); cdecl;
  44. Procedure _rtl_write(h: longint; addr: longInt; len : longint); cdecl;
  45. Procedure _rtl_erase(p: pchar); cdecl;
  46. Procedure _rtl_rename(p1: pchar; p2 : pchar); cdecl;
  47. Function _rtl_read(h: longInt; addr: longInt; len : longint) : longint; cdecl;
  48. Function _rtl_filepos(Handle: longint): longint; cdecl;
  49. Procedure _rtl_seek(Handle: longint; pos:longint); cdecl;
  50. Function _rtl_filesize(Handle:longint): longInt; cdecl;
  51. Procedure _rtl_rmdir(buffer: pchar); cdecl;
  52. Procedure _rtl_mkdir(buffer: pchar); cdecl;
  53. Procedure _rtl_chdir(buffer: pchar); cdecl;
  54. {$else}
  55. { used in syscall to report errors.}
  56. var
  57. Errno : longint;
  58. { Include constant and type definitions }
  59. {$i errno.inc } { Error numbers }
  60. {$i sysnr.inc } { System call numbers }
  61. {$i sysconst.inc } { Miscellaneous constants }
  62. {$i systypes.inc } { Types needed for system calls }
  63. { Read actual system call definitions. }
  64. {$i syscalls.inc }
  65. {$endif}
  66. {*****************************************************************************
  67. Misc. System Dependent Functions
  68. *****************************************************************************}
  69. procedure prthaltproc;external name '_haltproc';
  70. procedure System_exit;
  71. begin
  72. {$ifdef i386}
  73. asm
  74. jmp prthaltproc
  75. end;
  76. {$else}
  77. asm
  78. jmp prthaltproc
  79. end;
  80. {$endif}
  81. End;
  82. Function ParamCount: Longint;
  83. Begin
  84. Paramcount:=argc-1
  85. End;
  86. Function ParamStr(l: Longint): String;
  87. var
  88. link,
  89. hs : string;
  90. i : longint;
  91. begin
  92. if l=0 then
  93. begin
  94. str(sys_getpid,hs);
  95. hs:='/proc/'+hs+'/exe'#0;
  96. i:=Sys_readlink(@hs[1],@link[1],high(link));
  97. if i>0 then
  98. begin
  99. link[0]:=chr(i);
  100. paramstr:=link;
  101. end
  102. else
  103. paramstr:=strpas(argv[0]);
  104. end
  105. else
  106. if (l>0) and (l<argc) then
  107. paramstr:=strpas(argv[l])
  108. else
  109. paramstr:='';
  110. end;
  111. Procedure Randomize;
  112. Begin
  113. randseed:=sys_time;
  114. End;
  115. {*****************************************************************************
  116. Heap Management
  117. *****************************************************************************}
  118. var
  119. _HEAP : longint;external name 'HEAP';
  120. _HEAPSIZE : longint;external name 'HEAPSIZE';
  121. function getheapstart:pointer;assembler;
  122. {$ifdef i386}
  123. asm
  124. leal _HEAP,%eax
  125. end ['EAX'];
  126. {$else}
  127. asm
  128. lea.l _HEAP,a0
  129. move.l a0,d0
  130. end;
  131. {$endif}
  132. function getheapsize:longint;assembler;
  133. {$ifdef i386}
  134. asm
  135. movl _HEAPSIZE,%eax
  136. end ['EAX'];
  137. {$else}
  138. asm
  139. move.l _HEAPSIZE,d0
  140. end ['D0'];
  141. {$endif}
  142. Function sbrk(size : longint) : Longint;
  143. begin
  144. Sbrk:=do_syscall(197,0,size,3,$22,-1,0,0);
  145. if ErrNo<>0 then
  146. Sbrk:=0;
  147. end;
  148. { include standard heap management }
  149. {$I heap.inc}
  150. {*****************************************************************************
  151. Low Level File Routines
  152. *****************************************************************************}
  153. {
  154. The lowlevel file functions should take care of setting the InOutRes to the
  155. correct value if an error has occured, else leave it untouched
  156. }
  157. Procedure Errno2Inoutres;
  158. {
  159. Convert ErrNo error to the correct Inoutres value
  160. }
  161. begin
  162. if ErrNo=0 then { Else it will go through all the cases }
  163. exit;
  164. case ErrNo of
  165. Sys_ENFILE,
  166. Sys_EMFILE : Inoutres:=4;
  167. Sys_ENOENT : Inoutres:=2;
  168. Sys_EBADF : Inoutres:=6;
  169. Sys_ENOMEM,
  170. Sys_EFAULT : Inoutres:=217;
  171. Sys_EINVAL : Inoutres:=218;
  172. Sys_EPIPE,
  173. Sys_EINTR,
  174. Sys_EIO,
  175. Sys_EAGAIN,
  176. Sys_ENOSPC : Inoutres:=101;
  177. Sys_ENAMETOOLONG,
  178. Sys_ELOOP,
  179. Sys_ENOTDIR : Inoutres:=3;
  180. Sys_EROFS,
  181. Sys_EEXIST,
  182. Sys_EACCES : Inoutres:=5;
  183. Sys_ETXTBSY : Inoutres:=162;
  184. end;
  185. end;
  186. Procedure Do_Close(Handle:Longint);
  187. Begin
  188. if Handle<=4 then
  189. exit;
  190. {$ifdef crtlib}
  191. _rtl_close(Handle);
  192. {$else}
  193. sys_close(Handle);
  194. {$endif}
  195. End;
  196. Procedure Do_Erase(p:pchar);
  197. Begin
  198. {$ifdef crtlib}
  199. _rtl_erase(p);
  200. {$else}
  201. sys_unlink(p);
  202. Errno2Inoutres;
  203. {$endif}
  204. End;
  205. Procedure Do_Rename(p1,p2:pchar);
  206. Begin
  207. {$ifdef crtlib}
  208. _rtl_rename(p1,p2);
  209. {$else }
  210. sys_rename(p1,p2);
  211. Errno2Inoutres;
  212. {$endif}
  213. End;
  214. Function Do_Write(Handle,Addr,Len:Longint):longint;
  215. Begin
  216. {$ifdef crtlib}
  217. _rtl_write(Handle,addr,len);
  218. Do_Write:=Len;
  219. {$else}
  220. Do_Write:=sys_write(Handle,pchar(addr),len);
  221. Errno2Inoutres;
  222. {$endif}
  223. if Do_Write<0 then
  224. Do_Write:=0;
  225. End;
  226. Function Do_Read(Handle,Addr,Len:Longint):Longint;
  227. Begin
  228. {$ifdef crtlib}
  229. Do_Read:=_rtl_read(Handle,addr,len);
  230. {$else}
  231. Do_Read:=sys_read(Handle,pchar(addr),len);
  232. Errno2Inoutres;
  233. {$endif}
  234. if Do_Read<0 then
  235. Do_Read:=0;
  236. End;
  237. Function Do_FilePos(Handle: Longint): Longint;
  238. Begin
  239. {$ifdef crtlib}
  240. Do_FilePos:=_rtl_filepos(Handle);
  241. {$else}
  242. Do_FilePos:=sys_lseek(Handle, 0, Seek_Cur);
  243. Errno2Inoutres;
  244. {$endif}
  245. End;
  246. Procedure Do_Seek(Handle,Pos:Longint);
  247. Begin
  248. {$ifdef crtlib}
  249. _rtl_seek(Handle, Pos);
  250. {$else}
  251. sys_lseek(Handle, pos, Seek_set);
  252. {$endif}
  253. End;
  254. Function Do_SeekEnd(Handle:Longint): Longint;
  255. begin
  256. {$ifdef crtlib}
  257. Do_SeekEnd:=_rtl_filesize(Handle);
  258. {$else}
  259. Do_SeekEnd:=sys_lseek(Handle,0,Seek_End);
  260. {$endif}
  261. end;
  262. Function Do_FileSize(Handle:Longint): Longint;
  263. {$ifndef crtlib}
  264. var
  265. Info : Stat;
  266. {$endif}
  267. Begin
  268. {$ifdef crtlib}
  269. Do_FileSize:=_rtl_filesize(Handle);
  270. {$else}
  271. if do_SysCall(189,handle,longint(@info))=0 then
  272. Do_FileSize:=Info.Size
  273. else
  274. Do_FileSize:=0;
  275. Errno2Inoutres;
  276. {$endif}
  277. End;
  278. Procedure Do_Truncate(Handle,Pos:longint);
  279. begin
  280. {$ifndef crtlib}
  281. do_syscall(201,handle,pos);
  282. Errno2Inoutres;
  283. {$endif}
  284. end;
  285. Procedure Do_Open(var f;p:pchar;flags:longint);
  286. {
  287. FileRec and textrec have both Handle and mode as the first items so
  288. they could use the same routine for opening/creating.
  289. when (flags and $100) the file will be append
  290. when (flags and $1000) the file will be truncate/rewritten
  291. when (flags and $10000) there is no check for close (needed for textfiles)
  292. }
  293. var
  294. {$ifndef crtlib}
  295. oflags : longint;
  296. {$endif}
  297. Begin
  298. { close first if opened }
  299. if ((flags and $10000)=0) then
  300. begin
  301. case FileRec(f).mode of
  302. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  303. fmclosed : ;
  304. else
  305. begin
  306. inoutres:=102; {not assigned}
  307. exit;
  308. end;
  309. end;
  310. end;
  311. { reset file Handle }
  312. FileRec(f).Handle:=UnusedHandle;
  313. { We do the conversion of filemodes here, concentrated on 1 place }
  314. case (flags and 3) of
  315. 0 : begin
  316. oflags :=Open_RDONLY;
  317. FileRec(f).mode:=fminput;
  318. end;
  319. 1 : begin
  320. oflags :=Open_WRONLY;
  321. FileRec(f).mode:=fmoutput;
  322. end;
  323. 2 : begin
  324. oflags :=Open_RDWR;
  325. FileRec(f).mode:=fminout;
  326. end;
  327. end;
  328. if (flags and $1000)=$1000 then
  329. oflags:=oflags or (Open_CREAT or Open_TRUNC)
  330. else
  331. if (flags and $100)=$100 then
  332. oflags:=oflags or (Open_APPEND);
  333. { empty name is special }
  334. if p[0]=#0 then
  335. begin
  336. case FileRec(f).mode of
  337. fminput :
  338. FileRec(f).Handle:=StdInputHandle;
  339. fminout, { this is set by rewrite }
  340. fmoutput :
  341. FileRec(f).Handle:=StdOutputHandle;
  342. fmappend :
  343. begin
  344. FileRec(f).Handle:=StdOutputHandle;
  345. FileRec(f).mode:=fmoutput; {fool fmappend}
  346. end;
  347. end;
  348. exit;
  349. end;
  350. { real open call }
  351. {$ifdef crtlib}
  352. FileRec(f).Handle:=_rtl_open(p, oflags);
  353. if FileRec(f).Handle<0 then
  354. InOutRes:=2
  355. else
  356. InOutRes:=0;
  357. {$else}
  358. FileRec(f).Handle:=sys_open(p,oflags,438);
  359. if (ErrNo=Sys_EROFS) and ((OFlags and Open_RDWR)<>0) then
  360. begin
  361. Oflags:=Oflags and not(Open_RDWR);
  362. FileRec(f).Handle:=sys_open(p,oflags,438);
  363. end;
  364. Errno2Inoutres;
  365. {$endif}
  366. End;
  367. Function Do_IsDevice(Handle:Longint):boolean;
  368. {
  369. Interface to Unix ioctl call.
  370. Performs various operations on the filedescriptor Handle.
  371. Ndx describes the operation to perform.
  372. Data points to data needed for the Ndx function. The structure of this
  373. data is function-dependent.
  374. }
  375. var
  376. Data : array[0..255] of byte; {Large enough for termios info}
  377. begin
  378. Do_IsDevice:=(do_SysCall(54,handle,$5401,longint(@data))=0);
  379. end;
  380. {*****************************************************************************
  381. UnTyped File Handling
  382. *****************************************************************************}
  383. {$i file.inc}
  384. {*****************************************************************************
  385. Typed File Handling
  386. *****************************************************************************}
  387. {$i typefile.inc}
  388. {*****************************************************************************
  389. Text File Handling
  390. *****************************************************************************}
  391. {$DEFINE SHORT_LINEBREAK}
  392. {$DEFINE EXTENDED_EOF}
  393. {$i text.inc}
  394. {*****************************************************************************
  395. Directory Handling
  396. *****************************************************************************}
  397. Procedure MkDir(Const s: String);[IOCheck];
  398. Var
  399. Buffer: Array[0..255] of Char;
  400. Begin
  401. If InOutRes <> 0 then exit;
  402. Move(s[1], Buffer, Length(s));
  403. Buffer[Length(s)] := #0;
  404. {$ifdef crtlib}
  405. _rtl_mkdir(@buffer);
  406. {$else}
  407. sys_mkdir(@buffer, 511);
  408. Errno2Inoutres;
  409. {$endif}
  410. End;
  411. Procedure RmDir(Const s: String);[IOCheck];
  412. Var
  413. Buffer: Array[0..255] of Char;
  414. Begin
  415. If InOutRes <> 0 then exit;
  416. Move(s[1], Buffer, Length(s));
  417. Buffer[Length(s)] := #0;
  418. {$ifdef crtlib}
  419. _rtl_rmdir(@buffer);
  420. {$else}
  421. sys_rmdir(@buffer);
  422. Errno2Inoutres;
  423. {$endif}
  424. End;
  425. Procedure ChDir(Const s: String);[IOCheck];
  426. Var
  427. Buffer: Array[0..255] of Char;
  428. Begin
  429. If InOutRes <> 0 then exit;
  430. Move(s[1], Buffer, Length(s));
  431. Buffer[Length(s)] := #0;
  432. {$ifdef crtlib}
  433. _rtl_chdir(@buffer);
  434. {$else}
  435. sys_chdir(@buffer);
  436. Errno2Inoutres;
  437. {$endif}
  438. End;
  439. procedure getdir(drivenr : byte;var dir : shortstring);
  440. {$ifndef crtlib}
  441. var
  442. thisdir : stat;
  443. rootino,
  444. thisino,
  445. dotdotino : longint;
  446. rootdev,
  447. thisdev,
  448. dotdotdev : word;
  449. thedir,dummy : string[255];
  450. dirstream : pdir;
  451. d : pdirent;
  452. mountpoint : boolean;
  453. predot : string[255];
  454. {$endif}
  455. begin
  456. drivenr:=0;
  457. dir:='';
  458. {$ifndef crtlib}
  459. thedir:='/'#0;
  460. if sys_stat(@thedir[1],thisdir)<0 then
  461. exit;
  462. rootino:=thisdir.ino;
  463. rootdev:=thisdir.dev;
  464. thedir:='.'#0;
  465. if sys_stat(@thedir[1],thisdir)<0 then
  466. exit;
  467. thisino:=thisdir.ino;
  468. thisdev:=thisdir.dev;
  469. { Now we can uniquely identify the current and root dir }
  470. thedir:='';
  471. predot:='';
  472. while not ((thisino=rootino) and (thisdev=rootdev)) do
  473. begin
  474. { Are we on a mount point ? }
  475. dummy:=predot+'..'#0;
  476. if sys_stat(@dummy[1],thisdir)<0 then
  477. exit;
  478. dotdotino:=thisdir.ino;
  479. dotdotdev:=thisdir.dev;
  480. mountpoint:=(thisdev<>dotdotdev);
  481. { Now, Try to find the name of this dir in the previous one }
  482. dirstream:=opendir (@dummy[1]);
  483. if dirstream=nil then
  484. exit;
  485. repeat
  486. d:=sys_readdir (dirstream);
  487. if (d<>nil) and
  488. (not ((d^.name[0]='.') and ((d^.name[1]=#0) or ((d^.name[1]='.') and (d^.name[2]=#0))))) and
  489. (mountpoint or (d^.ino=thisino)) then
  490. begin
  491. dummy:=predot+'../'+strpas(@(d^.name[0]))+#0;
  492. if sys_stat (@(dummy[1]),thisdir)<0 then
  493. d:=nil;
  494. end;
  495. until (d=nil) or ((thisdir.dev=thisdev) and (thisdir.ino=thisino) );
  496. if (closedir(dirstream)<0) or (d=nil) then
  497. exit;
  498. { At this point, d.name contains the name of the current dir}
  499. thedir:='/'+strpas(@(d^.name[0]))+thedir;
  500. thisdev:=dotdotdev;
  501. thisino:=dotdotino;
  502. predot:=predot+'../';
  503. end;
  504. { Now rootino=thisino and rootdev=thisdev so we've reached / }
  505. dir:=thedir
  506. {$endif}
  507. end;
  508. {*****************************************************************************
  509. SystemUnit Initialization
  510. *****************************************************************************}
  511. Procedure SignalToRunError(Sig:longint);
  512. begin
  513. case sig of
  514. 8 : HandleError(200);
  515. 11 : HandleError(216);
  516. end;
  517. end;
  518. Procedure InstallSignals;
  519. begin
  520. { sr.reg3:=longint(@SignalToRunError);
  521. { sigsegv }
  522. sr.reg2:=11;
  523. syscall(syscall_nr_signal,sr);
  524. { sigfpe }
  525. sr.reg2:=8;
  526. syscall(syscall_nr_signal,sr);
  527. }
  528. end;
  529. procedure SetupCmdLine;
  530. var
  531. bufsize,
  532. len,j,
  533. size,i : longint;
  534. found : boolean;
  535. buf : array[0..1026] of char;
  536. procedure AddBuf;
  537. begin
  538. reallocmem(cmdline,size+bufsize);
  539. move(buf,cmdline[size],bufsize);
  540. inc(size,bufsize);
  541. bufsize:=0;
  542. end;
  543. begin
  544. size:=0;
  545. bufsize:=0;
  546. i:=0;
  547. while (i<argc) do
  548. begin
  549. len:=strlen(argv[i]);
  550. if len>sizeof(buf)-2 then
  551. len:=sizeof(buf)-2;
  552. found:=false;
  553. for j:=1 to len do
  554. if argv[i][j]=' ' then
  555. begin
  556. found:=true;
  557. break;
  558. end;
  559. if bufsize+len>=sizeof(buf)-2 then
  560. AddBuf;
  561. if found then
  562. begin
  563. buf[bufsize]:='"';
  564. inc(bufsize);
  565. end;
  566. move(argv[i]^,buf[bufsize],len);
  567. inc(bufsize,len);
  568. if found then
  569. begin
  570. buf[bufsize]:='"';
  571. inc(bufsize);
  572. end;
  573. if i<argc then
  574. buf[bufsize]:=' '
  575. else
  576. buf[bufsize]:=#0;
  577. inc(bufsize);
  578. inc(i);
  579. end;
  580. AddBuf;
  581. end;
  582. Begin
  583. { Set up signals handlers }
  584. InstallSignals;
  585. { Setup heap }
  586. InitHeap;
  587. InitExceptions;
  588. { Arguments }
  589. SetupCmdLine;
  590. { Setup stdin, stdout and stderr }
  591. OpenStdIO(Input,fmInput,StdInputHandle);
  592. OpenStdIO(Output,fmOutput,StdOutputHandle);
  593. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  594. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  595. { Reset IO Error }
  596. InOutRes:=0;
  597. End.
  598. {
  599. $Log$
  600. Revision 1.1 2000-03-01 20:23:07 marco
  601. * Initial version. Compiles (syscall.inc inclusion prob has to be solved
  602. Revision 1.37 2000/02/09 16:59:32 peter
  603. * truncated log
  604. Revision 1.36 2000/02/09 12:17:51 peter
  605. * moved halt to system.inc
  606. * syslinux doesn't use direct asm anymore
  607. Revision 1.35 2000/02/08 11:47:09 peter
  608. * paramstr(0) support
  609. Revision 1.34 2000/01/20 23:38:02 peter
  610. * support fm_inout as stdoutput for assign(f,'');rewrite(f,1); becuase
  611. rewrite opens always with filemode 2
  612. Revision 1.33 2000/01/16 22:25:38 peter
  613. * check handle for file closing
  614. Revision 1.32 2000/01/07 16:41:41 daniel
  615. * copyright 2000
  616. Revision 1.31 2000/01/07 16:32:28 daniel
  617. * copyright 2000 added
  618. Revision 1.30 1999/12/01 22:57:31 peter
  619. * cmdline support
  620. Revision 1.29 1999/11/06 14:39:12 peter
  621. * truncated log
  622. Revision 1.28 1999/10/28 09:50:06 peter
  623. * use mmap instead of brk
  624. Revision 1.27 1999/09/10 15:40:35 peter
  625. * fixed do_open flags to be > $100, becuase filemode can be upto 255
  626. Revision 1.26 1999/09/08 16:14:43 peter
  627. * pointer fixes
  628. Revision 1.25 1999/07/28 23:18:36 peter
  629. * closedir fixes, which now disposes the pdir itself
  630. }