system.pp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl and Pavel Ozerski
  5. member of the Free Pascal development team.
  6. FPC Pascal system unit for the Win32 API.
  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. unit {$ifdef VER1_0}SysWin32{$else}System{$endif};
  14. interface
  15. {$ifdef SYSTEMDEBUG}
  16. {$define SYSTEMEXCEPTIONDEBUG}
  17. {$endif SYSTEMDEBUG}
  18. {$ifdef i386}
  19. {$define Set_i386_Exception_handler}
  20. {$endif i386}
  21. { include system-independent routine headers }
  22. {$I systemh.inc}
  23. type
  24. { the fields of this record are os dependent }
  25. { and they shouldn't be used in a program }
  26. { only the type TCriticalSection is important }
  27. TCriticalSection = packed record
  28. DebugInfo : pointer;
  29. LockCount : longint;
  30. RecursionCount : longint;
  31. OwningThread : DWord;
  32. LockSemaphore : DWord;
  33. Reserved : DWord;
  34. end;
  35. { include threading stuff }
  36. {$i threadh.inc}
  37. { include heap support headers }
  38. {$I heaph.inc}
  39. const
  40. { Default filehandles }
  41. UnusedHandle : longint = -1;
  42. StdInputHandle : longint = 0;
  43. StdOutputHandle : longint = 0;
  44. StdErrorHandle : longint = 0;
  45. FileNameCaseSensitive : boolean = true;
  46. sLineBreak : string[2] = #13#10;
  47. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  48. { Thread count for DLL }
  49. Thread_count : longint = 0;
  50. type
  51. TStartupInfo=packed record
  52. cb : longint;
  53. lpReserved : Pointer;
  54. lpDesktop : Pointer;
  55. lpTitle : Pointer;
  56. dwX : longint;
  57. dwY : longint;
  58. dwXSize : longint;
  59. dwYSize : longint;
  60. dwXCountChars : longint;
  61. dwYCountChars : longint;
  62. dwFillAttribute : longint;
  63. dwFlags : longint;
  64. wShowWindow : Word;
  65. cbReserved2 : Word;
  66. lpReserved2 : Pointer;
  67. hStdInput : longint;
  68. hStdOutput : longint;
  69. hStdError : longint;
  70. end;
  71. var
  72. { C compatible arguments }
  73. argc : longint;
  74. argv : ppchar;
  75. { Win32 Info }
  76. startupinfo : tstartupinfo;
  77. hprevinst,
  78. HInstance,
  79. MainInstance,
  80. cmdshow : longint;
  81. DLLreason,DLLparam:longint;
  82. Win32StackTop : Dword;
  83. type
  84. TDLL_Process_Entry_Hook = function (dllparam : longint) : longbool;
  85. TDLL_Entry_Hook = procedure (dllparam : longint);
  86. const
  87. Dll_Process_Attach_Hook : TDLL_Process_Entry_Hook = nil;
  88. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  89. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  90. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  91. implementation
  92. { include system independent routines }
  93. {$I system.inc}
  94. { some declarations for Win32 API calls }
  95. {$I win32.inc}
  96. CONST
  97. { These constants are used for conversion of error codes }
  98. { from win32 i/o errors to tp i/o errors }
  99. { errors 1 to 18 are the same as in Turbo Pascal }
  100. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING! }
  101. { The media is write protected. }
  102. ERROR_WRITE_PROTECT = 19;
  103. { The system cannot find the device specified. }
  104. ERROR_BAD_UNIT = 20;
  105. { The device is not ready. }
  106. ERROR_NOT_READY = 21;
  107. { The device does not recognize the command. }
  108. ERROR_BAD_COMMAND = 22;
  109. { Data error (cyclic redundancy check) }
  110. ERROR_CRC = 23;
  111. { The program issued a command but the }
  112. { command length is incorrect. }
  113. ERROR_BAD_LENGTH = 24;
  114. { The drive cannot locate a specific }
  115. { area or track on the disk. }
  116. ERROR_SEEK = 25;
  117. { The specified disk or diskette cannot be accessed. }
  118. ERROR_NOT_DOS_DISK = 26;
  119. { The drive cannot find the sector requested. }
  120. ERROR_SECTOR_NOT_FOUND = 27;
  121. { The printer is out of paper. }
  122. ERROR_OUT_OF_PAPER = 28;
  123. { The system cannot write to the specified device. }
  124. ERROR_WRITE_FAULT = 29;
  125. { The system cannot read from the specified device. }
  126. ERROR_READ_FAULT = 30;
  127. { A device attached to the system is not functioning.}
  128. ERROR_GEN_FAILURE = 31;
  129. { The process cannot access the file because }
  130. { it is being used by another process. }
  131. ERROR_SHARING_VIOLATION = 32;
  132. var
  133. errno : longint;
  134. {$ASMMODE ATT}
  135. { misc. functions }
  136. function GetLastError : DWORD;
  137. external 'kernel32' name 'GetLastError';
  138. { time and date functions }
  139. function GetTickCount : longint;
  140. external 'kernel32' name 'GetTickCount';
  141. { process functions }
  142. procedure ExitProcess(uExitCode : UINT);
  143. external 'kernel32' name 'ExitProcess';
  144. Procedure Errno2InOutRes;
  145. Begin
  146. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  147. if (errno >= ERROR_WRITE_PROTECT) and (errno <= ERROR_GEN_FAILURE) THEN
  148. BEGIN
  149. { This is the offset to the Win32 to add to directly map }
  150. { to the DOS/TP compatible error codes when in this range }
  151. InOutRes := word(errno)+131;
  152. END
  153. else
  154. { This case is special }
  155. if errno=ERROR_SHARING_VIOLATION THEN
  156. BEGIN
  157. InOutRes :=5;
  158. END
  159. else
  160. { other error codes can directly be mapped }
  161. InOutRes := Word(errno);
  162. errno:=0;
  163. end;
  164. {$ifdef dummy}
  165. procedure int_stackcheck(stack_size:longint);[public,alias: 'STACKCHECK'];
  166. {
  167. called when trying to get local stack if the compiler directive $S
  168. is set this function must preserve esi !!!! because esi is set by
  169. the calling proc for methods it must preserve all registers !!
  170. With a 2048 byte safe area used to write to StdIo without crossing
  171. the stack boundary
  172. }
  173. begin
  174. asm
  175. pushl %eax
  176. pushl %ebx
  177. movl stack_size,%ebx
  178. addl $2048,%ebx
  179. movl %esp,%eax
  180. subl %ebx,%eax
  181. movl stacklimit,%ebx
  182. cmpl %eax,%ebx
  183. jae .L__short_on_stack
  184. popl %ebx
  185. popl %eax
  186. leave
  187. ret $4
  188. .L__short_on_stack:
  189. { can be usefull for error recovery !! }
  190. popl %ebx
  191. popl %eax
  192. end['EAX','EBX'];
  193. HandleError(202);
  194. end;
  195. {$endif dummy}
  196. function paramcount : longint;
  197. begin
  198. paramcount := argc - 1;
  199. end;
  200. { module functions }
  201. function GetModuleFileName(l1:longint;p:pointer;l2:longint):longint;
  202. external 'kernel32' name 'GetModuleFileNameA';
  203. function GetModuleHandle(p : pointer) : longint;
  204. external 'kernel32' name 'GetModuleHandleA';
  205. function GetCommandFile:pchar;forward;
  206. function paramstr(l : longint) : string;
  207. begin
  208. if (l>=0) and (l<argc) then
  209. paramstr:=strpas(argv[l])
  210. else
  211. paramstr:='';
  212. end;
  213. procedure randomize;
  214. begin
  215. randseed:=GetTickCount;
  216. end;
  217. {*****************************************************************************
  218. Heap Management
  219. *****************************************************************************}
  220. { memory functions }
  221. function GetProcessHeap : DWord;
  222. external 'kernel32' name 'GetProcessHeap';
  223. function HeapAlloc(hHeap : DWord; dwFlags : DWord; dwBytes : DWord) : Longint;
  224. external 'kernel32' name 'HeapAlloc';
  225. {$IFDEF SYSTEMDEBUG}
  226. function HeapSize(hHeap : DWord; dwFlags : DWord; ptr : Pointer) : DWord;
  227. external 'kernel32' name 'HeapSize';
  228. {$ENDIF}
  229. var
  230. heap : longint;external name 'HEAP';
  231. intern_heapsize : longint;external name 'HEAPSIZE';
  232. function getheapstart:pointer;assembler;
  233. asm
  234. leal HEAP,%eax
  235. end ['EAX'];
  236. function getheapsize:longint;assembler;
  237. asm
  238. movl intern_HEAPSIZE,%eax
  239. end ['EAX'];
  240. function Sbrk(size : longint):longint;
  241. var
  242. l : longint;
  243. begin
  244. l := HeapAlloc(GetProcessHeap(), 0, size);
  245. if (l = 0) then
  246. l := -1;
  247. {$ifdef DUMPGROW}
  248. Writeln('new heap part at $',hexstr(l,8), ' size = ',HeapSize(GetProcessHeap()));
  249. {$endif}
  250. sbrk:=l;
  251. end;
  252. { include standard heap management }
  253. {$I heap.inc}
  254. {*****************************************************************************
  255. Low Level File Routines
  256. *****************************************************************************}
  257. function WriteFile(fh:longint;buf:pointer;len:longint;var loaded:longint;
  258. overlap:pointer):longint;
  259. external 'kernel32' name 'WriteFile';
  260. function ReadFile(fh:longint;buf:pointer;len:longint;var loaded:longint;
  261. overlap:pointer):longint;
  262. external 'kernel32' name 'ReadFile';
  263. function CloseHandle(h : longint) : longint;
  264. external 'kernel32' name 'CloseHandle';
  265. function DeleteFile(p : pchar) : longint;
  266. external 'kernel32' name 'DeleteFileA';
  267. function MoveFile(old,_new : pchar) : longint;
  268. external 'kernel32' name 'MoveFileA';
  269. function SetFilePointer(l1,l2 : longint;l3 : pointer;l4 : longint) : longint;
  270. external 'kernel32' name 'SetFilePointer';
  271. function GetFileSize(h:longint;p:pointer) : longint;
  272. external 'kernel32' name 'GetFileSize';
  273. function CreateFile(name : pointer;access,sharing : longint;
  274. security : pointer;how,attr,template : longint) : longint;
  275. external 'kernel32' name 'CreateFileA';
  276. function SetEndOfFile(h : longint) : longbool;
  277. external 'kernel32' name 'SetEndOfFile';
  278. function GetFileType(Handle:DWORD):DWord;
  279. external 'kernel32' name 'GetFileType';
  280. procedure AllowSlash(p:pchar);
  281. var
  282. i : longint;
  283. begin
  284. { allow slash as backslash }
  285. for i:=0 to strlen(p) do
  286. if p[i]='/' then p[i]:='\';
  287. end;
  288. function do_isdevice(handle:longint):boolean;
  289. begin
  290. do_isdevice:=(getfiletype(handle)=2);
  291. end;
  292. procedure do_close(h : longint);
  293. begin
  294. if do_isdevice(h) then
  295. exit;
  296. CloseHandle(h);
  297. end;
  298. procedure do_erase(p : pchar);
  299. begin
  300. AllowSlash(p);
  301. if DeleteFile(p)=0 then
  302. Begin
  303. errno:=GetLastError;
  304. Errno2InoutRes;
  305. end;
  306. end;
  307. procedure do_rename(p1,p2 : pchar);
  308. begin
  309. AllowSlash(p1);
  310. AllowSlash(p2);
  311. if MoveFile(p1,p2)=0 then
  312. Begin
  313. errno:=GetLastError;
  314. Errno2InoutRes;
  315. end;
  316. end;
  317. function do_write(h,addr,len : longint) : longint;
  318. var
  319. size:longint;
  320. begin
  321. if writefile(h,pointer(addr),len,size,nil)=0 then
  322. Begin
  323. errno:=GetLastError;
  324. Errno2InoutRes;
  325. end;
  326. do_write:=size;
  327. end;
  328. function do_read(h,addr,len : longint) : longint;
  329. var
  330. _result:longint;
  331. begin
  332. if readfile(h,pointer(addr),len,_result,nil)=0 then
  333. Begin
  334. errno:=GetLastError;
  335. Errno2InoutRes;
  336. end;
  337. do_read:=_result;
  338. end;
  339. function do_filepos(handle : longint) : longint;
  340. var
  341. l:longint;
  342. begin
  343. l:=SetFilePointer(handle,0,nil,FILE_CURRENT);
  344. if l=-1 then
  345. begin
  346. l:=0;
  347. errno:=GetLastError;
  348. Errno2InoutRes;
  349. end;
  350. do_filepos:=l;
  351. end;
  352. procedure do_seek(handle,pos : longint);
  353. begin
  354. if SetFilePointer(handle,pos,nil,FILE_BEGIN)=-1 then
  355. Begin
  356. errno:=GetLastError;
  357. Errno2InoutRes;
  358. end;
  359. end;
  360. function do_seekend(handle:longint):longint;
  361. begin
  362. do_seekend:=SetFilePointer(handle,0,nil,FILE_END);
  363. if do_seekend=-1 then
  364. begin
  365. errno:=GetLastError;
  366. Errno2InoutRes;
  367. end;
  368. end;
  369. function do_filesize(handle : longint) : longint;
  370. var
  371. aktfilepos : longint;
  372. begin
  373. aktfilepos:=do_filepos(handle);
  374. do_filesize:=do_seekend(handle);
  375. do_seek(handle,aktfilepos);
  376. end;
  377. procedure do_truncate (handle,pos:longint);
  378. begin
  379. do_seek(handle,pos);
  380. if not(SetEndOfFile(handle)) then
  381. begin
  382. errno:=GetLastError;
  383. Errno2InoutRes;
  384. end;
  385. end;
  386. procedure do_open(var f;p : pchar;flags:longint);
  387. {
  388. filerec and textrec have both handle and mode as the first items so
  389. they could use the same routine for opening/creating.
  390. when (flags and $100) the file will be append
  391. when (flags and $1000) the file will be truncate/rewritten
  392. when (flags and $10000) there is no check for close (needed for textfiles)
  393. }
  394. Const
  395. file_Share_Read = $00000001;
  396. file_Share_Write = $00000002;
  397. Var
  398. shflags,
  399. oflags,cd : longint;
  400. begin
  401. AllowSlash(p);
  402. { close first if opened }
  403. if ((flags and $10000)=0) then
  404. begin
  405. case filerec(f).mode of
  406. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  407. fmclosed : ;
  408. else
  409. begin
  410. {not assigned}
  411. inoutres:=102;
  412. exit;
  413. end;
  414. end;
  415. end;
  416. { reset file handle }
  417. filerec(f).handle:=UnusedHandle;
  418. { convert filesharing }
  419. shflags:=0;
  420. if ((filemode and fmshareExclusive) = fmshareExclusive) then
  421. { no sharing }
  422. else
  423. if (filemode = fmShareCompat) or ((filemode and fmshareDenyWrite) = fmshareDenyWrite) then
  424. shflags := file_Share_Read
  425. else
  426. if ((filemode and fmshareDenyRead) = fmshareDenyRead) then
  427. shflags := file_Share_Write
  428. else
  429. if ((filemode and fmshareDenyNone) = fmshareDenyNone) then
  430. shflags := file_Share_Read + file_Share_Write;
  431. { convert filemode to filerec modes }
  432. case (flags and 3) of
  433. 0 : begin
  434. filerec(f).mode:=fminput;
  435. oflags:=GENERIC_READ;
  436. end;
  437. 1 : begin
  438. filerec(f).mode:=fmoutput;
  439. oflags:=GENERIC_WRITE;
  440. end;
  441. 2 : begin
  442. filerec(f).mode:=fminout;
  443. oflags:=GENERIC_WRITE or GENERIC_READ;
  444. end;
  445. end;
  446. { standard is opening and existing file }
  447. cd:=OPEN_EXISTING;
  448. { create it ? }
  449. if (flags and $1000)<>0 then
  450. cd:=CREATE_ALWAYS
  451. { or append ? }
  452. else
  453. if (flags and $100)<>0 then
  454. cd:=OPEN_ALWAYS;
  455. { empty name is special }
  456. if p[0]=#0 then
  457. begin
  458. case FileRec(f).mode of
  459. fminput :
  460. FileRec(f).Handle:=StdInputHandle;
  461. fminout, { this is set by rewrite }
  462. fmoutput :
  463. FileRec(f).Handle:=StdOutputHandle;
  464. fmappend :
  465. begin
  466. FileRec(f).Handle:=StdOutputHandle;
  467. FileRec(f).mode:=fmoutput; {fool fmappend}
  468. end;
  469. end;
  470. exit;
  471. end;
  472. filerec(f).handle:=CreateFile(p,oflags,shflags,nil,cd,FILE_ATTRIBUTE_NORMAL,0);
  473. { append mode }
  474. if (flags and $100)<>0 then
  475. begin
  476. do_seekend(filerec(f).handle);
  477. filerec(f).mode:=fmoutput; {fool fmappend}
  478. end;
  479. { get errors }
  480. { handle -1 is returned sometimes !! (PM) }
  481. if (filerec(f).handle=0) or (filerec(f).handle=-1) then
  482. begin
  483. errno:=GetLastError;
  484. Errno2InoutRes;
  485. end;
  486. end;
  487. {*****************************************************************************
  488. UnTyped File Handling
  489. *****************************************************************************}
  490. {$i file.inc}
  491. {*****************************************************************************
  492. Typed File Handling
  493. *****************************************************************************}
  494. {$i typefile.inc}
  495. {*****************************************************************************
  496. Text File Handling
  497. *****************************************************************************}
  498. {$DEFINE EOF_CTRLZ}
  499. {$i text.inc}
  500. {*****************************************************************************
  501. Directory Handling
  502. *****************************************************************************}
  503. function CreateDirectory(name : pointer;sec : pointer) : longint;
  504. external 'kernel32' name 'CreateDirectoryA';
  505. function RemoveDirectory(name:pointer):longint;
  506. external 'kernel32' name 'RemoveDirectoryA';
  507. function SetCurrentDirectory(name : pointer) : longint;
  508. external 'kernel32' name 'SetCurrentDirectoryA';
  509. function GetCurrentDirectory(bufsize : longint;name : pchar) : longint;
  510. external 'kernel32' name 'GetCurrentDirectoryA';
  511. type
  512. TDirFnType=function(name:pointer):word;
  513. procedure dirfn(afunc : TDirFnType;const s:string);
  514. var
  515. buffer : array[0..255] of char;
  516. begin
  517. move(s[1],buffer,length(s));
  518. buffer[length(s)]:=#0;
  519. AllowSlash(pchar(@buffer));
  520. if aFunc(@buffer)=0 then
  521. begin
  522. errno:=GetLastError;
  523. Errno2InoutRes;
  524. end;
  525. end;
  526. function CreateDirectoryTrunc(name:pointer):word;
  527. begin
  528. CreateDirectoryTrunc:=CreateDirectory(name,nil);
  529. end;
  530. procedure mkdir(const s:string);[IOCHECK];
  531. begin
  532. If (s='') or (InOutRes <> 0) then
  533. exit;
  534. dirfn(TDirFnType(@CreateDirectoryTrunc),s);
  535. end;
  536. procedure rmdir(const s:string);[IOCHECK];
  537. begin
  538. If (s='') or (InOutRes <> 0) then
  539. exit;
  540. dirfn(TDirFnType(@RemoveDirectory),s);
  541. end;
  542. procedure chdir(const s:string);[IOCHECK];
  543. begin
  544. If (s='') or (InOutRes <> 0) then
  545. exit;
  546. dirfn(TDirFnType(@SetCurrentDirectory),s);
  547. end;
  548. procedure GetDir (DriveNr: byte; var Dir: ShortString);
  549. const
  550. Drive:array[0..3]of char=(#0,':',#0,#0);
  551. var
  552. defaultdrive:boolean;
  553. DirBuf,SaveBuf:array[0..259] of Char;
  554. begin
  555. defaultdrive:=drivenr=0;
  556. if not defaultdrive then
  557. begin
  558. byte(Drive[0]):=Drivenr+64;
  559. GetCurrentDirectory(SizeOf(SaveBuf),SaveBuf);
  560. if SetCurrentDirectory(@Drive) <> 0 then
  561. begin
  562. errno := word (GetLastError);
  563. Errno2InoutRes;
  564. end;
  565. end;
  566. GetCurrentDirectory(SizeOf(DirBuf),DirBuf);
  567. if not defaultdrive then
  568. SetCurrentDirectory(@SaveBuf);
  569. dir:=strpas(DirBuf);
  570. if not FileNameCaseSensitive then
  571. dir:=upcase(dir);
  572. end;
  573. {*****************************************************************************
  574. Thread Handling
  575. *****************************************************************************}
  576. const
  577. fpucw : word = $1332;
  578. procedure InitFPU;assembler;
  579. asm
  580. fninit
  581. fldcw fpucw
  582. end;
  583. { include threading stuff, this is os independend part }
  584. {$I thread.inc}
  585. {*****************************************************************************
  586. SystemUnit Initialization
  587. *****************************************************************************}
  588. { Startup }
  589. procedure GetStartupInfo(p : pointer);
  590. external 'kernel32' name 'GetStartupInfoA';
  591. function GetStdHandle(nStdHandle:DWORD):THANDLE;
  592. external 'kernel32' name 'GetStdHandle';
  593. { command line/enviroment functions }
  594. function GetCommandLine : pchar;
  595. external 'kernel32' name 'GetCommandLineA';
  596. var
  597. ModuleName : array[0..255] of char;
  598. function GetCommandFile:pchar;
  599. begin
  600. GetModuleFileName(0,@ModuleName,255);
  601. GetCommandFile:=@ModuleName;
  602. end;
  603. procedure setup_arguments;
  604. var
  605. arglen,
  606. count : longint;
  607. argstart,
  608. pc : pchar;
  609. quote : set of char;
  610. argsbuf : array[0..127] of pchar;
  611. begin
  612. { create commandline, it starts with the executed filename which is argv[0] }
  613. { Win32 passes the command NOT via the args, but via getmodulefilename}
  614. count:=0;
  615. pc:=getcommandfile;
  616. Arglen:=0;
  617. repeat
  618. Inc(Arglen);
  619. until (pc[Arglen]=#0);
  620. getmem(argsbuf[count],arglen+1);
  621. move(pc^,argsbuf[count]^,arglen);
  622. { Now skip the first one }
  623. pc:=GetCommandLine;
  624. repeat
  625. { skip leading spaces }
  626. while pc^ in [' ',#9,#13] do
  627. inc(pc);
  628. case pc^ of
  629. #0 : break;
  630. '"' : begin
  631. quote:=['"'];
  632. inc(pc);
  633. end;
  634. '''' : begin
  635. quote:=[''''];
  636. inc(pc);
  637. end;
  638. else
  639. quote:=[' ',#9,#13];
  640. end;
  641. { scan until the end of the argument }
  642. argstart:=pc;
  643. while (pc^<>#0) and not(pc^ in quote) do
  644. inc(pc);
  645. { Don't copy the first one, it is already there.}
  646. If Count<>0 then
  647. begin
  648. { reserve some memory }
  649. arglen:=pc-argstart;
  650. getmem(argsbuf[count],arglen+1);
  651. move(argstart^,argsbuf[count]^,arglen);
  652. argsbuf[count][arglen]:=#0;
  653. end;
  654. { skip quote }
  655. if pc^ in quote then
  656. inc(pc);
  657. inc(count);
  658. until false;
  659. { create argc }
  660. argc:=count;
  661. { create an nil entry }
  662. argsbuf[count]:=nil;
  663. inc(count);
  664. { create the argv }
  665. getmem(argv,count shl 2);
  666. move(argsbuf,argv^,count shl 2);
  667. { Setup cmdline variable }
  668. cmdline:=GetCommandLine;
  669. end;
  670. {*****************************************************************************
  671. System Dependent Exit code
  672. *****************************************************************************}
  673. procedure install_exception_handlers;forward;
  674. procedure remove_exception_handlers;forward;
  675. procedure PascalMain;external name 'PASCALMAIN';
  676. procedure fpc_do_exit;external name 'FPC_DO_EXIT';
  677. Procedure ExitDLL(Exitcode : longint); forward;
  678. Procedure system_exit;
  679. begin
  680. { don't call ExitProcess inside
  681. the DLL exit code !!
  682. This crashes Win95 at least PM }
  683. if IsLibrary then
  684. ExitDLL(ExitCode);
  685. if not IsConsole then
  686. begin
  687. Close(stderr);
  688. Close(stdout);
  689. { what about Input and Output ?? PM }
  690. end;
  691. remove_exception_handlers;
  692. ExitProcess(ExitCode);
  693. end;
  694. {$ifdef dummy}
  695. Function SetUpStack : longint;
  696. { This routine does the following : }
  697. { returns the value of the initial SP - __stklen }
  698. begin
  699. asm
  700. pushl %ebx
  701. pushl %eax
  702. movl __stklen,%ebx
  703. movl %esp,%eax
  704. subl %ebx,%eax
  705. movl %eax,__RESULT
  706. popl %eax
  707. popl %ebx
  708. end;
  709. end;
  710. {$endif}
  711. var
  712. { value of the stack segment
  713. to check if the call stack can be written on exceptions }
  714. _SS : longint;
  715. procedure Exe_entry;[public, alias : '_FPC_EXE_Entry'];
  716. begin
  717. IsLibrary:=false;
  718. { install the handlers for exe only ?
  719. or should we install them for DLL also ? (PM) }
  720. install_exception_handlers;
  721. { This strange construction is needed to solve the _SS problem
  722. with a smartlinked syswin32 (PFV) }
  723. asm
  724. pushl %ebp
  725. xorl %ebp,%ebp
  726. movl %esp,%eax
  727. movl %eax,Win32StackTop
  728. movw %ss,%bp
  729. movl %ebp,_SS
  730. call InitFPU
  731. xorl %ebp,%ebp
  732. call PASCALMAIN
  733. popl %ebp
  734. end;
  735. { if we pass here there was no error ! }
  736. system_exit;
  737. end;
  738. Const
  739. { DllEntryPoint }
  740. DLL_PROCESS_ATTACH = 1;
  741. DLL_THREAD_ATTACH = 2;
  742. DLL_PROCESS_DETACH = 0;
  743. DLL_THREAD_DETACH = 3;
  744. Var
  745. DLLBuf : Jmp_buf;
  746. Const
  747. DLLExitOK : boolean = true;
  748. function Dll_entry : longbool;[public, alias : '_FPC_DLL_Entry'];
  749. var
  750. res : longbool;
  751. begin
  752. IsLibrary:=true;
  753. Dll_entry:=false;
  754. case DLLreason of
  755. DLL_PROCESS_ATTACH :
  756. begin
  757. If SetJmp(DLLBuf) = 0 then
  758. begin
  759. if assigned(Dll_Process_Attach_Hook) then
  760. begin
  761. res:=Dll_Process_Attach_Hook(DllParam);
  762. if not res then
  763. exit(false);
  764. end;
  765. PASCALMAIN;
  766. Dll_entry:=true;
  767. end
  768. else
  769. Dll_entry:=DLLExitOK;
  770. end;
  771. DLL_THREAD_ATTACH :
  772. begin
  773. inc(Thread_count);
  774. {$ifdef MT}
  775. AllocateThreadVars;
  776. {$endif MT}
  777. if assigned(Dll_Thread_Attach_Hook) then
  778. Dll_Thread_Attach_Hook(DllParam);
  779. Dll_entry:=true; { return value is ignored }
  780. end;
  781. DLL_THREAD_DETACH :
  782. begin
  783. dec(Thread_count);
  784. if assigned(Dll_Thread_Detach_Hook) then
  785. Dll_Thread_Detach_Hook(DllParam);
  786. {$ifdef MT}
  787. ReleaseThreadVars;
  788. {$endif MT}
  789. Dll_entry:=true; { return value is ignored }
  790. end;
  791. DLL_PROCESS_DETACH :
  792. begin
  793. Dll_entry:=true; { return value is ignored }
  794. If SetJmp(DLLBuf) = 0 then
  795. begin
  796. FPC_DO_EXIT;
  797. end;
  798. if assigned(Dll_Process_Detach_Hook) then
  799. Dll_Process_Detach_Hook(DllParam);
  800. end;
  801. end;
  802. end;
  803. Procedure ExitDLL(Exitcode : longint);
  804. begin
  805. DLLExitOK:=ExitCode=0;
  806. LongJmp(DLLBuf,1);
  807. end;
  808. //
  809. // Hardware exception handling
  810. //
  811. {$ifdef Set_i386_Exception_handler}
  812. (*
  813. Error code definitions for the Win32 API functions
  814. Values are 32 bit values layed out as follows:
  815. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  816. 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  817. +---+-+-+-----------------------+-------------------------------+
  818. |Sev|C|R| Facility | Code |
  819. +---+-+-+-----------------------+-------------------------------+
  820. where
  821. Sev - is the severity code
  822. 00 - Success
  823. 01 - Informational
  824. 10 - Warning
  825. 11 - Error
  826. C - is the Customer code flag
  827. R - is a reserved bit
  828. Facility - is the facility code
  829. Code - is the facility's status code
  830. *)
  831. const
  832. SEVERITY_SUCCESS = $00000000;
  833. SEVERITY_INFORMATIONAL = $40000000;
  834. SEVERITY_WARNING = $80000000;
  835. SEVERITY_ERROR = $C0000000;
  836. const
  837. STATUS_SEGMENT_NOTIFICATION = $40000005;
  838. DBG_TERMINATE_THREAD = $40010003;
  839. DBG_TERMINATE_PROCESS = $40010004;
  840. DBG_CONTROL_C = $40010005;
  841. DBG_CONTROL_BREAK = $40010008;
  842. STATUS_GUARD_PAGE_VIOLATION = $80000001;
  843. STATUS_DATATYPE_MISALIGNMENT = $80000002;
  844. STATUS_BREAKPOINT = $80000003;
  845. STATUS_SINGLE_STEP = $80000004;
  846. DBG_EXCEPTION_NOT_HANDLED = $80010001;
  847. STATUS_ACCESS_VIOLATION = $C0000005;
  848. STATUS_IN_PAGE_ERROR = $C0000006;
  849. STATUS_INVALID_HANDLE = $C0000008;
  850. STATUS_NO_MEMORY = $C0000017;
  851. STATUS_ILLEGAL_INSTRUCTION = $C000001D;
  852. STATUS_NONCONTINUABLE_EXCEPTION = $C0000025;
  853. STATUS_INVALID_DISPOSITION = $C0000026;
  854. STATUS_ARRAY_BOUNDS_EXCEEDED = $C000008C;
  855. STATUS_FLOAT_DENORMAL_OPERAND = $C000008D;
  856. STATUS_FLOAT_DIVIDE_BY_ZERO = $C000008E;
  857. STATUS_FLOAT_INEXACT_RESULT = $C000008F;
  858. STATUS_FLOAT_INVALID_OPERATION = $C0000090;
  859. STATUS_FLOAT_OVERFLOW = $C0000091;
  860. STATUS_FLOAT_STACK_CHECK = $C0000092;
  861. STATUS_FLOAT_UNDERFLOW = $C0000093;
  862. STATUS_INTEGER_DIVIDE_BY_ZERO = $C0000094;
  863. STATUS_INTEGER_OVERFLOW = $C0000095;
  864. STATUS_PRIVILEGED_INSTRUCTION = $C0000096;
  865. STATUS_STACK_OVERFLOW = $C00000FD;
  866. STATUS_CONTROL_C_EXIT = $C000013A;
  867. STATUS_FLOAT_MULTIPLE_FAULTS = $C00002B4;
  868. STATUS_FLOAT_MULTIPLE_TRAPS = $C00002B5;
  869. STATUS_REG_NAT_CONSUMPTION = $C00002C9;
  870. EXCEPTION_EXECUTE_HANDLER = 1;
  871. EXCEPTION_CONTINUE_EXECUTION = -1;
  872. EXCEPTION_CONTINUE_SEARCH = 0;
  873. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  874. CONTEXT_X86 = $00010000;
  875. CONTEXT_CONTROL = CONTEXT_X86 or $00000001;
  876. CONTEXT_INTEGER = CONTEXT_X86 or $00000002;
  877. CONTEXT_SEGMENTS = CONTEXT_X86 or $00000004;
  878. CONTEXT_FLOATING_POINT = CONTEXT_X86 or $00000008;
  879. CONTEXT_DEBUG_REGISTERS = CONTEXT_X86 or $00000010;
  880. CONTEXT_EXTENDED_REGISTERS = CONTEXT_X86 or $00000020;
  881. CONTEXT_FULL = CONTEXT_CONTROL or CONTEXT_INTEGER or CONTEXT_SEGMENTS;
  882. MAXIMUM_SUPPORTED_EXTENSION = 512;
  883. type
  884. PFloatingSaveArea = ^TFloatingSaveArea;
  885. TFloatingSaveArea = packed record
  886. ControlWord : Cardinal;
  887. StatusWord : Cardinal;
  888. TagWord : Cardinal;
  889. ErrorOffset : Cardinal;
  890. ErrorSelector : Cardinal;
  891. DataOffset : Cardinal;
  892. DataSelector : Cardinal;
  893. RegisterArea : array[0..79] of Byte;
  894. Cr0NpxState : Cardinal;
  895. end;
  896. PContext = ^TContext;
  897. TContext = packed record
  898. //
  899. // The flags values within this flag control the contents of
  900. // a CONTEXT record.
  901. //
  902. ContextFlags : Cardinal;
  903. //
  904. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  905. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  906. // included in CONTEXT_FULL.
  907. //
  908. Dr0, Dr1, Dr2,
  909. Dr3, Dr6, Dr7 : Cardinal;
  910. //
  911. // This section is specified/returned if the
  912. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  913. //
  914. FloatSave : TFloatingSaveArea;
  915. //
  916. // This section is specified/returned if the
  917. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  918. //
  919. SegGs, SegFs,
  920. SegEs, SegDs : Cardinal;
  921. //
  922. // This section is specified/returned if the
  923. // ContextFlags word contains the flag CONTEXT_INTEGER.
  924. //
  925. Edi, Esi, Ebx,
  926. Edx, Ecx, Eax : Cardinal;
  927. //
  928. // This section is specified/returned if the
  929. // ContextFlags word contains the flag CONTEXT_CONTROL.
  930. //
  931. Ebp : Cardinal;
  932. Eip : Cardinal;
  933. SegCs : Cardinal;
  934. EFlags, Esp, SegSs : Cardinal;
  935. //
  936. // This section is specified/returned if the ContextFlags word
  937. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  938. // The format and contexts are processor specific
  939. //
  940. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  941. end;
  942. type
  943. PExceptionRecord = ^TExceptionRecord;
  944. TExceptionRecord = packed record
  945. ExceptionCode : Longint;
  946. ExceptionFlags : Longint;
  947. ExceptionRecord : PExceptionRecord;
  948. ExceptionAddress : Pointer;
  949. NumberParameters : Longint;
  950. ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of Pointer;
  951. end;
  952. PExceptionPointers = ^TExceptionPointers;
  953. TExceptionPointers = packed record
  954. ExceptionRecord : PExceptionRecord;
  955. ContextRecord : PContext;
  956. end;
  957. { type of functions that should be used for exception handling }
  958. TTopLevelExceptionFilter = function (excep : PExceptionPointers) : Longint;stdcall;
  959. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : TTopLevelExceptionFilter) : TTopLevelExceptionFilter;
  960. external 'kernel32' name 'SetUnhandledExceptionFilter';
  961. const
  962. MaxExceptionLevel = 16;
  963. exceptLevel : Byte = 0;
  964. var
  965. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  966. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  967. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  968. {$ifdef SYSTEMEXCEPTIONDEBUG}
  969. procedure DebugHandleErrorAddrFrame(error, addr, frame : longint);
  970. begin
  971. if IsConsole then begin
  972. write(stderr,'HandleErrorAddrFrame(error=',error);
  973. write(stderr,',addr=',hexstr(addr,8));
  974. writeln(stderr,',frame=',hexstr(frame,8),')');
  975. end;
  976. HandleErrorAddrFrame(error,addr,frame);
  977. end;
  978. {$endif SYSTEMEXCEPTIONDEBUG}
  979. procedure JumpToHandleErrorFrame;
  980. var
  981. eip, ebp, error : Longint;
  982. begin
  983. // save ebp
  984. asm
  985. movl (%ebp),%eax
  986. movl %eax,ebp
  987. end;
  988. if (exceptLevel > 0) then
  989. dec(exceptLevel);
  990. eip:=exceptEip[exceptLevel];
  991. error:=exceptError[exceptLevel];
  992. {$ifdef SYSTEMEXCEPTIONDEBUG}
  993. if IsConsole then
  994. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  995. end;
  996. {$endif SYSTEMEXCEPTIONDEBUG}
  997. if resetFPU[exceptLevel] then asm
  998. fninit
  999. fldcw fpucw
  1000. end;
  1001. { build a fake stack }
  1002. asm
  1003. movl ebp,%eax
  1004. pushl %eax
  1005. movl eip,%eax
  1006. pushl %eax
  1007. movl error,%eax
  1008. pushl %eax
  1009. movl eip,%eax
  1010. pushl %eax
  1011. movl ebp,%ebp // Change frame pointer
  1012. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1013. jmpl DebugHandleErrorAddrFrame
  1014. {$else not SYSTEMEXCEPTIONDEBUG}
  1015. jmpl HandleErrorAddrFrame
  1016. {$endif SYSTEMEXCEPTIONDEBUG}
  1017. end;
  1018. end;
  1019. function syswin32_i386_exception_handler(excep : PExceptionPointers) : Longint;stdcall;
  1020. var
  1021. frame,
  1022. res : longint;
  1023. function SysHandleErrorFrame(error, frame : Longint; must_reset_fpu : Boolean) : Longint;
  1024. begin
  1025. if (frame = 0) then
  1026. SysHandleErrorFrame:=EXCEPTION_CONTINUE_SEARCH
  1027. else begin
  1028. if (exceptLevel >= MaxExceptionLevel) then exit;
  1029. exceptEip[exceptLevel] := excep^.ContextRecord^.Eip;
  1030. exceptError[exceptLevel] := error;
  1031. resetFPU[exceptLevel] := must_reset_fpu;
  1032. inc(exceptLevel);
  1033. excep^.ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  1034. excep^.ExceptionRecord^.ExceptionCode := 0;
  1035. SysHandleErrorFrame := EXCEPTION_CONTINUE_EXECUTION;
  1036. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1037. if IsConsole then begin
  1038. writeln(stderr,'Exception Continue Exception set at ',
  1039. hexstr(exceptEip[exceptLevel],8));
  1040. writeln(stderr,'Eip changed to ',
  1041. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', error);
  1042. end;
  1043. {$endif SYSTEMEXCEPTIONDEBUG}
  1044. end;
  1045. end;
  1046. begin
  1047. if excep^.ContextRecord^.SegSs=_SS then
  1048. frame := excep^.ContextRecord^.Ebp
  1049. else
  1050. frame := 0;
  1051. res := EXCEPTION_CONTINUE_SEARCH;
  1052. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1053. if IsConsole then Writeln(stderr,'Exception ',
  1054. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  1055. {$endif SYSTEMEXCEPTIONDEBUG}
  1056. case cardinal(excep^.ExceptionRecord^.ExceptionCode) of
  1057. STATUS_INTEGER_DIVIDE_BY_ZERO,
  1058. STATUS_FLOAT_DIVIDE_BY_ZERO :
  1059. res := SysHandleErrorFrame(200, frame, true);
  1060. STATUS_ARRAY_BOUNDS_EXCEEDED :
  1061. res := SysHandleErrorFrame(201, frame, false);
  1062. STATUS_STACK_OVERFLOW :
  1063. res := SysHandleErrorFrame(202, frame, false);
  1064. STATUS_FLOAT_OVERFLOW :
  1065. res := SysHandleErrorFrame(205, frame, true);
  1066. STATUS_FLOAT_UNDERFLOW :
  1067. res := SysHandleErrorFrame(206, frame, true);
  1068. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  1069. STATUS_FLOAT_INVALID_OPERATION,
  1070. STATUS_FLOAT_STACK_CHECK :
  1071. res := SysHandleErrorFrame(207, frame, true);
  1072. STATUS_INTEGER_OVERFLOW :
  1073. res := SysHandleErrorFrame(215, frame, false);
  1074. STATUS_ACCESS_VIOLATION,
  1075. STATUS_FLOAT_DENORMAL_OPERAND :
  1076. res := SysHandleErrorFrame(216, frame, true);
  1077. else begin
  1078. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  1079. res := SysHandleErrorFrame(217, frame, true);
  1080. end;
  1081. end;
  1082. syswin32_i386_exception_handler := res;
  1083. end;
  1084. procedure install_exception_handlers;
  1085. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1086. var
  1087. oldexceptaddr,
  1088. newexceptaddr : Longint;
  1089. {$endif SYSTEMEXCEPTIONDEBUG}
  1090. begin
  1091. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1092. asm
  1093. movl $0,%eax
  1094. movl %fs:(%eax),%eax
  1095. movl %eax,oldexceptaddr
  1096. end;
  1097. {$endif SYSTEMEXCEPTIONDEBUG}
  1098. SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  1099. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1100. asm
  1101. movl $0,%eax
  1102. movl %fs:(%eax),%eax
  1103. movl %eax,newexceptaddr
  1104. end;
  1105. if IsConsole then
  1106. writeln(stderr,'Old exception ',hexstr(oldexceptaddr,8),
  1107. ' new exception ',hexstr(newexceptaddr,8));
  1108. {$endif SYSTEMEXCEPTIONDEBUG}
  1109. end;
  1110. procedure remove_exception_handlers;
  1111. begin
  1112. SetUnhandledExceptionFilter(nil);
  1113. end;
  1114. {$else not i386 (Processor specific !!)}
  1115. procedure install_exception_handlers;
  1116. begin
  1117. end;
  1118. procedure remove_exception_handlers;
  1119. begin
  1120. end;
  1121. {$endif Set_i386_Exception_handler}
  1122. {****************************************************************************
  1123. Error Message writing using messageboxes
  1124. ****************************************************************************}
  1125. function MessageBox(w1:longint;l1,l2:pointer;w2:longint):longint;
  1126. external 'user32' name 'MessageBoxA';
  1127. const
  1128. ErrorBufferLength = 1024;
  1129. var
  1130. ErrorBuf : array[0..ErrorBufferLength] of char;
  1131. ErrorLen : longint;
  1132. Function ErrorWrite(Var F: TextRec): Integer;
  1133. {
  1134. An error message should always end with #13#10#13#10
  1135. }
  1136. var
  1137. p : pchar;
  1138. i : longint;
  1139. Begin
  1140. if F.BufPos>0 then
  1141. begin
  1142. if F.BufPos+ErrorLen>ErrorBufferLength then
  1143. i:=ErrorBufferLength-ErrorLen
  1144. else
  1145. i:=F.BufPos;
  1146. Move(F.BufPtr^,ErrorBuf[ErrorLen],i);
  1147. inc(ErrorLen,i);
  1148. ErrorBuf[ErrorLen]:=#0;
  1149. end;
  1150. if ErrorLen>3 then
  1151. begin
  1152. p:=@ErrorBuf[ErrorLen];
  1153. for i:=1 to 4 do
  1154. begin
  1155. dec(p);
  1156. if not(p^ in [#10,#13]) then
  1157. break;
  1158. end;
  1159. end;
  1160. if ErrorLen=ErrorBufferLength then
  1161. i:=4;
  1162. if (i=4) then
  1163. begin
  1164. MessageBox(0,@ErrorBuf,pchar('Error'),0);
  1165. ErrorLen:=0;
  1166. end;
  1167. F.BufPos:=0;
  1168. ErrorWrite:=0;
  1169. End;
  1170. Function ErrorClose(Var F: TextRec): Integer;
  1171. begin
  1172. if ErrorLen>0 then
  1173. begin
  1174. MessageBox(0,@ErrorBuf,pchar('Error'),0);
  1175. ErrorLen:=0;
  1176. end;
  1177. ErrorLen:=0;
  1178. ErrorClose:=0;
  1179. end;
  1180. Function ErrorOpen(Var F: TextRec): Integer;
  1181. Begin
  1182. TextRec(F).InOutFunc:=@ErrorWrite;
  1183. TextRec(F).FlushFunc:=@ErrorWrite;
  1184. TextRec(F).CloseFunc:=@ErrorClose;
  1185. ErrorOpen:=0;
  1186. End;
  1187. procedure AssignError(Var T: Text);
  1188. begin
  1189. Assign(T,'');
  1190. TextRec(T).OpenFunc:=@ErrorOpen;
  1191. Rewrite(T);
  1192. end;
  1193. const
  1194. Exe_entry_code : pointer = @Exe_entry;
  1195. Dll_entry_code : pointer = @Dll_entry;
  1196. begin
  1197. { get some helpful informations }
  1198. GetStartupInfo(@startupinfo);
  1199. { some misc Win32 stuff }
  1200. hprevinst:=0;
  1201. if not IsLibrary then
  1202. HInstance:=getmodulehandle(GetCommandFile);
  1203. MainInstance:=HInstance;
  1204. { No idea how to know this issue !! }
  1205. IsMultithreaded:=false;
  1206. cmdshow:=startupinfo.wshowwindow;
  1207. { to test stack depth }
  1208. loweststack:=maxlongint;
  1209. { real test stack depth }
  1210. { stacklimit := setupstack; }
  1211. {$ifdef MT}
  1212. { allocate one threadvar entry from windows, we use this entry }
  1213. { for a pointer to our threadvars }
  1214. dataindex:=TlsAlloc;
  1215. { the exceptions use threadvars so do this _before_ initexceptions }
  1216. AllocateThreadVars;
  1217. {$endif MT}
  1218. { Setup heap }
  1219. InitHeap;
  1220. InitExceptions;
  1221. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  1222. displayed in and messagebox }
  1223. StdInputHandle:=longint(GetStdHandle(STD_INPUT_HANDLE));
  1224. StdOutputHandle:=longint(GetStdHandle(STD_OUTPUT_HANDLE));
  1225. StdErrorHandle:=longint(GetStdHandle(STD_ERROR_HANDLE));
  1226. if not IsConsole then
  1227. begin
  1228. AssignError(stderr);
  1229. AssignError(stdout);
  1230. Assign(Output,'');
  1231. Assign(Input,'');
  1232. end
  1233. else
  1234. begin
  1235. OpenStdIO(Input,fmInput,StdInputHandle);
  1236. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1237. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  1238. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1239. end;
  1240. { Arguments }
  1241. setup_arguments;
  1242. { Reset IO Error }
  1243. InOutRes:=0;
  1244. { Reset internal error variable }
  1245. errno:=0;
  1246. end.
  1247. {
  1248. $Log$
  1249. Revision 1.9 2001-03-21 23:29:40 florian
  1250. + sLineBreak and misc. stuff for Kylix compatiblity
  1251. Revision 1.8 2001/03/21 21:08:20 hajny
  1252. * GetDir fixed
  1253. Revision 1.7 2001/03/16 20:09:58 hajny
  1254. * universal FExpand
  1255. Revision 1.6 2001/02/20 21:31:12 peter
  1256. * chdir,mkdir,rmdir with empty string fixed
  1257. Revision 1.5 2001/01/26 16:38:03 florian
  1258. *** empty log message ***
  1259. Revision 1.4 2001/01/24 21:47:38 florian
  1260. + more MT stuff added
  1261. Revision 1.3 2001/01/05 15:44:35 florian
  1262. * some stuff for MT
  1263. Revision 1.2 2000/12/18 17:28:58 jonas
  1264. * fixed range check errors
  1265. Revision 1.1 2000/10/15 08:19:49 peter
  1266. * system unit rename for 1.1 branch
  1267. Revision 1.6 2000/10/13 12:01:52 peter
  1268. * fixed exception callback
  1269. Revision 1.5 2000/10/11 16:05:55 peter
  1270. * stdcall for callbacks (merged)
  1271. Revision 1.4 2000/09/11 20:19:28 florian
  1272. * complete exception handling provided by Thomas Schatzl
  1273. Revision 1.3 2000/09/04 19:36:59 peter
  1274. * new heapalloc calls, patch from Thomas Schatzl
  1275. Revision 1.2 2000/07/13 11:33:58 michael
  1276. + removed logs
  1277. }