system.pp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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. {$ifndef VER1_0}
  14. { $define MT}
  15. {$endif VER1_0}
  16. unit {$ifdef VER1_0}SysWin32{$else}System{$endif};
  17. interface
  18. {$ifdef SYSTEMDEBUG}
  19. {$define SYSTEMEXCEPTIONDEBUG}
  20. {$endif SYSTEMDEBUG}
  21. {$ifdef cpui386}
  22. {$define Set_i386_Exception_handler}
  23. {$endif cpui386}
  24. { include system-independent routine headers }
  25. {$I systemh.inc}
  26. {Platform specific information}
  27. type
  28. {$ifdef CPU64}
  29. THandle = QWord;
  30. {$else CPU64}
  31. THandle = DWord;
  32. {$endif CPU64}
  33. const
  34. LineEnding = #13#10;
  35. LFNSupport = true;
  36. DirectorySeparator = '\';
  37. DriveSeparator = ':';
  38. PathSeparator = ';';
  39. { FileNameCaseSensitive is defined separately below!!! }
  40. maxExitCode = 65535;
  41. type
  42. PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  43. TEXCEPTION_FRAME = record
  44. next : PEXCEPTION_FRAME;
  45. handler : pointer;
  46. end;
  47. { include heap support headers }
  48. {$I heaph.inc}
  49. const
  50. { Default filehandles }
  51. UnusedHandle : THandle = -1;
  52. StdInputHandle : THandle = 0;
  53. StdOutputHandle : THandle = 0;
  54. StdErrorHandle : THandle = 0;
  55. FileNameCaseSensitive : boolean = true;
  56. sLineBreak = LineEnding;
  57. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  58. { Thread count for DLL }
  59. Thread_count : longint = 0;
  60. System_exception_frame : PEXCEPTION_FRAME =nil;
  61. type
  62. TStartupInfo=packed record
  63. cb : longint;
  64. lpReserved : Pointer;
  65. lpDesktop : Pointer;
  66. lpTitle : Pointer;
  67. dwX : longint;
  68. dwY : longint;
  69. dwXSize : longint;
  70. dwYSize : longint;
  71. dwXCountChars : longint;
  72. dwYCountChars : longint;
  73. dwFillAttribute : longint;
  74. dwFlags : longint;
  75. wShowWindow : Word;
  76. cbReserved2 : Word;
  77. lpReserved2 : Pointer;
  78. hStdInput : longint;
  79. hStdOutput : longint;
  80. hStdError : longint;
  81. end;
  82. var
  83. { C compatible arguments }
  84. argc : longint;
  85. argv : ppchar;
  86. { Win32 Info }
  87. startupinfo : tstartupinfo;
  88. hprevinst,
  89. HInstance,
  90. MainInstance,
  91. cmdshow : longint;
  92. DLLreason,DLLparam:longint;
  93. Win32StackTop : Dword;
  94. type
  95. TDLL_Process_Entry_Hook = function (dllparam : longint) : longbool;
  96. TDLL_Entry_Hook = procedure (dllparam : longint);
  97. const
  98. Dll_Process_Attach_Hook : TDLL_Process_Entry_Hook = nil;
  99. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  100. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  101. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  102. type
  103. HMODULE = THandle;
  104. implementation
  105. { include system independent routines }
  106. {$I system.inc}
  107. { some declarations for Win32 API calls }
  108. {$I win32.inc}
  109. CONST
  110. { These constants are used for conversion of error codes }
  111. { from win32 i/o errors to tp i/o errors }
  112. { errors 1 to 18 are the same as in Turbo Pascal }
  113. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING! }
  114. { The media is write protected. }
  115. ERROR_WRITE_PROTECT = 19;
  116. { The system cannot find the device specified. }
  117. ERROR_BAD_UNIT = 20;
  118. { The device is not ready. }
  119. ERROR_NOT_READY = 21;
  120. { The device does not recognize the command. }
  121. ERROR_BAD_COMMAND = 22;
  122. { Data error (cyclic redundancy check) }
  123. ERROR_CRC = 23;
  124. { The program issued a command but the }
  125. { command length is incorrect. }
  126. ERROR_BAD_LENGTH = 24;
  127. { The drive cannot locate a specific }
  128. { area or track on the disk. }
  129. ERROR_SEEK = 25;
  130. { The specified disk or diskette cannot be accessed. }
  131. ERROR_NOT_DOS_DISK = 26;
  132. { The drive cannot find the sector requested. }
  133. ERROR_SECTOR_NOT_FOUND = 27;
  134. { The printer is out of paper. }
  135. ERROR_OUT_OF_PAPER = 28;
  136. { The system cannot write to the specified device. }
  137. ERROR_WRITE_FAULT = 29;
  138. { The system cannot read from the specified device. }
  139. ERROR_READ_FAULT = 30;
  140. { A device attached to the system is not functioning.}
  141. ERROR_GEN_FAILURE = 31;
  142. { The process cannot access the file because }
  143. { it is being used by another process. }
  144. ERROR_SHARING_VIOLATION = 32;
  145. { A pipe has been closed on the other end }
  146. { Removing that error allows eof to works as on other OSes }
  147. ERROR_BROKEN_PIPE = 109;
  148. ERROR_DIR_NOT_EMPTY = 145;
  149. ERROR_ALREADY_EXISTS = 183;
  150. {$IFDEF SUPPORT_THREADVAR}
  151. threadvar
  152. {$ELSE SUPPORT_THREADVAR}
  153. var
  154. {$ENDIF SUPPORT_THREADVAR}
  155. errno : longint;
  156. {$ASMMODE ATT}
  157. { misc. functions }
  158. function GetLastError : DWORD;
  159. stdcall;external 'kernel32' name 'GetLastError';
  160. { time and date functions }
  161. function GetTickCount : longint;
  162. stdcall;external 'kernel32' name 'GetTickCount';
  163. { process functions }
  164. procedure ExitProcess(uExitCode : UINT);
  165. stdcall;external 'kernel32' name 'ExitProcess';
  166. Procedure Errno2InOutRes;
  167. Begin
  168. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  169. case Errno of
  170. ERROR_WRITE_PROTECT..ERROR_GEN_FAILURE :
  171. begin
  172. { This is the offset to the Win32 to add to directly map }
  173. { to the DOS/TP compatible error codes when in this range }
  174. InOutRes := word(errno)+131;
  175. end;
  176. ERROR_DIR_NOT_EMPTY,
  177. ERROR_ALREADY_EXISTS,
  178. ERROR_SHARING_VIOLATION :
  179. begin
  180. InOutRes :=5;
  181. end;
  182. else
  183. begin
  184. { other error codes can directly be mapped }
  185. InOutRes := Word(errno);
  186. end;
  187. end;
  188. errno:=0;
  189. end;
  190. function paramcount : longint;
  191. begin
  192. paramcount := argc - 1;
  193. end;
  194. { module functions }
  195. function GetModuleFileName(l1:longint;p:pointer;l2:longint):longint;
  196. stdcall;external 'kernel32' name 'GetModuleFileNameA';
  197. function GetModuleHandle(p : pointer) : longint;
  198. stdcall;external 'kernel32' name 'GetModuleHandleA';
  199. function GetCommandFile:pchar;forward;
  200. function paramstr(l : longint) : string;
  201. begin
  202. if (l>=0) and (l<argc) then
  203. paramstr:=strpas(argv[l])
  204. else
  205. paramstr:='';
  206. end;
  207. procedure randomize;
  208. begin
  209. randseed:=GetTickCount;
  210. end;
  211. {*****************************************************************************
  212. Heap Management
  213. *****************************************************************************}
  214. { memory functions }
  215. function GetProcessHeap : DWord;
  216. stdcall;external 'kernel32' name 'GetProcessHeap';
  217. function HeapAlloc(hHeap : DWord; dwFlags : DWord; dwBytes : DWord) : Longint;
  218. stdcall;external 'kernel32' name 'HeapAlloc';
  219. function HeapFree(hHeap : dword; dwFlags : dword; lpMem: pointer) : boolean;
  220. stdcall;external 'kernel32' name 'HeapFree';
  221. {$IFDEF SYSTEMDEBUG}
  222. function WinAPIHeapSize(hHeap : DWord; dwFlags : DWord; ptr : Pointer) : DWord;
  223. stdcall;external 'kernel32' name 'HeapSize';
  224. {$ENDIF}
  225. {*****************************************************************************
  226. OS Memory allocation / deallocation
  227. ****************************************************************************}
  228. function SysOSAlloc(size: ptrint): pointer;
  229. var
  230. l : longword;
  231. begin
  232. l := HeapAlloc(GetProcessHeap, 0, size);
  233. {$ifdef DUMPGROW}
  234. Writeln('new heap part at $',hexstr(l,8), ' size = ',WinAPIHeapSize(GetProcessHeap()));
  235. {$endif}
  236. SysOSAlloc := pointer(l);
  237. end;
  238. {$define HAS_SYSOSFREE}
  239. procedure SysOSFree(p: pointer; size: ptrint);
  240. begin
  241. HeapFree(GetProcessHeap, 0, p);
  242. end;
  243. { include standard heap management }
  244. {$I heap.inc}
  245. {*****************************************************************************
  246. Low Level File Routines
  247. *****************************************************************************}
  248. function WriteFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  249. overlap:pointer):longint;
  250. stdcall;external 'kernel32' name 'WriteFile';
  251. function ReadFile(fh:thandle;buf:pointer;len:longint;var loaded:longint;
  252. overlap:pointer):longint;
  253. stdcall;external 'kernel32' name 'ReadFile';
  254. function CloseHandle(h : thandle) : longint;
  255. stdcall;external 'kernel32' name 'CloseHandle';
  256. function DeleteFile(p : pchar) : longint;
  257. stdcall;external 'kernel32' name 'DeleteFileA';
  258. function MoveFile(old,_new : pchar) : longint;
  259. stdcall;external 'kernel32' name 'MoveFileA';
  260. function SetFilePointer(l1,l2 : thandle;l3 : pointer;l4 : longint) : longint;
  261. stdcall;external 'kernel32' name 'SetFilePointer';
  262. function GetFileSize(h:thandle;p:pointer) : longint;
  263. stdcall;external 'kernel32' name 'GetFileSize';
  264. function CreateFile(lpFileName:pchar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  265. lpSecurityAttributes:PSECURITYATTRIBUTES; dwCreationDisposition:DWORD;
  266. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):longint;
  267. stdcall;external 'kernel32' name 'CreateFileA';
  268. function SetEndOfFile(h : thandle) : longbool;
  269. stdcall;external 'kernel32' name 'SetEndOfFile';
  270. function GetFileType(Handle:thandle):DWord;
  271. stdcall;external 'kernel32' name 'GetFileType';
  272. function GetFileAttributes(p : pchar) : dword;
  273. stdcall;external 'kernel32' name 'GetFileAttributesA';
  274. procedure AllowSlash(p:pchar);
  275. var
  276. i : longint;
  277. begin
  278. { allow slash as backslash }
  279. for i:=0 to strlen(p) do
  280. if p[i]='/' then p[i]:='\';
  281. end;
  282. function do_isdevice(handle:thandle):boolean;
  283. begin
  284. do_isdevice:=(getfiletype(handle)=2);
  285. end;
  286. procedure do_close(h : thandle);
  287. begin
  288. if do_isdevice(h) then
  289. exit;
  290. CloseHandle(h);
  291. end;
  292. procedure do_erase(p : pchar);
  293. begin
  294. AllowSlash(p);
  295. if DeleteFile(p)=0 then
  296. Begin
  297. errno:=GetLastError;
  298. if errno=5 then
  299. begin
  300. if (GetFileAttributes(p)=FILE_ATTRIBUTE_DIRECTORY) then
  301. errno:=2;
  302. end;
  303. Errno2InoutRes;
  304. end;
  305. end;
  306. procedure do_rename(p1,p2 : pchar);
  307. begin
  308. AllowSlash(p1);
  309. AllowSlash(p2);
  310. if MoveFile(p1,p2)=0 then
  311. Begin
  312. errno:=GetLastError;
  313. Errno2InoutRes;
  314. end;
  315. end;
  316. function do_write(h:thandle;addr:pointer;len : longint) : longint;
  317. var
  318. size:longint;
  319. begin
  320. if writefile(h,addr,len,size,nil)=0 then
  321. Begin
  322. errno:=GetLastError;
  323. Errno2InoutRes;
  324. end;
  325. do_write:=size;
  326. end;
  327. function do_read(h:thandle;addr:pointer;len : longint) : longint;
  328. var
  329. _result:longint;
  330. begin
  331. if readfile(h,addr,len,_result,nil)=0 then
  332. Begin
  333. errno:=GetLastError;
  334. if errno=ERROR_BROKEN_PIPE then
  335. errno:=0
  336. else
  337. Errno2InoutRes;
  338. end;
  339. do_read:=_result;
  340. end;
  341. function do_filepos(handle : thandle) : longint;
  342. var
  343. l:longint;
  344. begin
  345. l:=SetFilePointer(handle,0,nil,FILE_CURRENT);
  346. if l=-1 then
  347. begin
  348. l:=0;
  349. errno:=GetLastError;
  350. Errno2InoutRes;
  351. end;
  352. do_filepos:=l;
  353. end;
  354. procedure do_seek(handle:thandle;pos : longint);
  355. begin
  356. if SetFilePointer(handle,pos,nil,FILE_BEGIN)=-1 then
  357. Begin
  358. errno:=GetLastError;
  359. Errno2InoutRes;
  360. end;
  361. end;
  362. function do_seekend(handle:thandle):longint;
  363. begin
  364. do_seekend:=SetFilePointer(handle,0,nil,FILE_END);
  365. if do_seekend=-1 then
  366. begin
  367. errno:=GetLastError;
  368. Errno2InoutRes;
  369. end;
  370. end;
  371. function do_filesize(handle : thandle) : longint;
  372. var
  373. aktfilepos : longint;
  374. begin
  375. aktfilepos:=do_filepos(handle);
  376. do_filesize:=do_seekend(handle);
  377. do_seek(handle,aktfilepos);
  378. end;
  379. procedure do_truncate (handle:thandle;pos:longint);
  380. begin
  381. do_seek(handle,pos);
  382. if not(SetEndOfFile(handle)) then
  383. begin
  384. errno:=GetLastError;
  385. Errno2InoutRes;
  386. end;
  387. end;
  388. procedure do_open(var f;p:pchar;flags:longint);
  389. {
  390. filerec and textrec have both handle and mode as the first items so
  391. they could use the same routine for opening/creating.
  392. when (flags and $100) the file will be append
  393. when (flags and $1000) the file will be truncate/rewritten
  394. when (flags and $10000) there is no check for close (needed for textfiles)
  395. }
  396. Const
  397. file_Share_Read = $00000001;
  398. file_Share_Write = $00000002;
  399. Var
  400. shflags,
  401. oflags,cd : longint;
  402. security : TSecurityAttributes;
  403. begin
  404. AllowSlash(p);
  405. { close first if opened }
  406. if ((flags and $10000)=0) then
  407. begin
  408. case filerec(f).mode of
  409. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  410. fmclosed : ;
  411. else
  412. begin
  413. {not assigned}
  414. inoutres:=102;
  415. exit;
  416. end;
  417. end;
  418. end;
  419. { reset file handle }
  420. filerec(f).handle:=UnusedHandle;
  421. { convert filesharing }
  422. shflags:=0;
  423. if ((filemode and fmshareExclusive) = fmshareExclusive) then
  424. { no sharing }
  425. else
  426. if (filemode = fmShareCompat) or ((filemode and fmshareDenyWrite) = fmshareDenyWrite) then
  427. shflags := file_Share_Read
  428. else
  429. if ((filemode and fmshareDenyRead) = fmshareDenyRead) then
  430. shflags := file_Share_Write
  431. else
  432. if ((filemode and fmshareDenyNone) = fmshareDenyNone) then
  433. shflags := file_Share_Read + file_Share_Write;
  434. { convert filemode to filerec modes }
  435. case (flags and 3) of
  436. 0 : begin
  437. filerec(f).mode:=fminput;
  438. oflags:=longint(GENERIC_READ);
  439. end;
  440. 1 : begin
  441. filerec(f).mode:=fmoutput;
  442. oflags:=longint(GENERIC_WRITE);
  443. end;
  444. 2 : begin
  445. filerec(f).mode:=fminout;
  446. oflags:=longint(GENERIC_WRITE or GENERIC_READ);
  447. end;
  448. end;
  449. { create it ? }
  450. if (flags and $1000)<>0 then
  451. cd:=CREATE_ALWAYS
  452. { or Append/Open ? }
  453. else
  454. cd:=OPEN_EXISTING;
  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. security.nLength := Sizeof(TSecurityAttributes);
  473. security.bInheritHandle:=true;
  474. security.lpSecurityDescriptor:=nil;
  475. filerec(f).handle:=CreateFile(p,oflags,shflags,@security,cd,FILE_ATTRIBUTE_NORMAL,0);
  476. { append mode }
  477. if ((flags and $100)<>0) and
  478. (filerec(f).handle<>0) and
  479. (filerec(f).handle<>UnusedHandle) then
  480. begin
  481. do_seekend(filerec(f).handle);
  482. filerec(f).mode:=fmoutput; {fool fmappend}
  483. end;
  484. { get errors }
  485. { handle -1 is returned sometimes !! (PM) }
  486. if (filerec(f).handle=0) or (filerec(f).handle=UnusedHandle) then
  487. begin
  488. errno:=GetLastError;
  489. Errno2InoutRes;
  490. end;
  491. end;
  492. {*****************************************************************************
  493. UnTyped File Handling
  494. *****************************************************************************}
  495. {$i file.inc}
  496. {*****************************************************************************
  497. Typed File Handling
  498. *****************************************************************************}
  499. {$i typefile.inc}
  500. {*****************************************************************************
  501. Text File Handling
  502. *****************************************************************************}
  503. {$DEFINE EOF_CTRLZ}
  504. {$i text.inc}
  505. {*****************************************************************************
  506. Directory Handling
  507. *****************************************************************************}
  508. function CreateDirectory(name : pointer;sec : pointer) : longbool;
  509. stdcall;external 'kernel32' name 'CreateDirectoryA';
  510. function RemoveDirectory(name:pointer):longbool;
  511. stdcall;external 'kernel32' name 'RemoveDirectoryA';
  512. function SetCurrentDirectory(name : pointer) : longbool;
  513. stdcall;external 'kernel32' name 'SetCurrentDirectoryA';
  514. function GetCurrentDirectory(bufsize : longint;name : pchar) : longbool;
  515. stdcall;external 'kernel32' name 'GetCurrentDirectoryA';
  516. type
  517. TDirFnType=function(name:pointer):longbool;stdcall;
  518. procedure dirfn(afunc : TDirFnType;const s:string);
  519. var
  520. buffer : array[0..255] of char;
  521. begin
  522. move(s[1],buffer,length(s));
  523. buffer[length(s)]:=#0;
  524. AllowSlash(pchar(@buffer));
  525. if not aFunc(@buffer) then
  526. begin
  527. errno:=GetLastError;
  528. Errno2InoutRes;
  529. end;
  530. end;
  531. function CreateDirectoryTrunc(name:pointer):longbool;stdcall;
  532. begin
  533. CreateDirectoryTrunc:=CreateDirectory(name,nil);
  534. end;
  535. procedure mkdir(const s:string);[IOCHECK];
  536. begin
  537. If (s='') or (InOutRes <> 0) then
  538. exit;
  539. dirfn(TDirFnType(@CreateDirectoryTrunc),s);
  540. end;
  541. procedure rmdir(const s:string);[IOCHECK];
  542. begin
  543. if (s ='.') then
  544. InOutRes := 16;
  545. If (s='') or (InOutRes <> 0) then
  546. exit;
  547. dirfn(TDirFnType(@RemoveDirectory),s);
  548. end;
  549. procedure chdir(const s:string);[IOCHECK];
  550. begin
  551. If (s='') or (InOutRes <> 0) then
  552. exit;
  553. dirfn(TDirFnType(@SetCurrentDirectory),s);
  554. if Inoutres=2 then
  555. Inoutres:=3;
  556. end;
  557. procedure GetDir (DriveNr: byte; var Dir: ShortString);
  558. const
  559. Drive:array[0..3]of char=(#0,':',#0,#0);
  560. var
  561. defaultdrive:boolean;
  562. DirBuf,SaveBuf:array[0..259] of Char;
  563. begin
  564. defaultdrive:=drivenr=0;
  565. if not defaultdrive then
  566. begin
  567. byte(Drive[0]):=Drivenr+64;
  568. GetCurrentDirectory(SizeOf(SaveBuf),SaveBuf);
  569. if not SetCurrentDirectory(@Drive) then
  570. begin
  571. errno := word (GetLastError);
  572. Errno2InoutRes;
  573. Dir := char (DriveNr + 64) + ':\';
  574. SetCurrentDirectory(@SaveBuf);
  575. Exit;
  576. end;
  577. end;
  578. GetCurrentDirectory(SizeOf(DirBuf),DirBuf);
  579. if not defaultdrive then
  580. SetCurrentDirectory(@SaveBuf);
  581. dir:=strpas(DirBuf);
  582. if not FileNameCaseSensitive then
  583. dir:=upcase(dir);
  584. end;
  585. {*****************************************************************************
  586. SystemUnit Initialization
  587. *****************************************************************************}
  588. { Startup }
  589. procedure GetStartupInfo(p : pointer);
  590. stdcall;external 'kernel32' name 'GetStartupInfoA';
  591. function GetStdHandle(nStdHandle:DWORD):THANDLE;
  592. stdcall;external 'kernel32' name 'GetStdHandle';
  593. { command line/enviroment functions }
  594. function GetCommandLine : pchar;
  595. stdcall;external 'kernel32' name 'GetCommandLineA';
  596. function GetCurrentThread : dword;
  597. stdcall; external 'kernel32' name 'GetCurrentThread';
  598. var
  599. ModuleName : array[0..255] of char;
  600. function GetCommandFile:pchar;
  601. begin
  602. GetModuleFileName(0,@ModuleName,255);
  603. GetCommandFile:=@ModuleName;
  604. end;
  605. procedure setup_arguments;
  606. var
  607. arglen,
  608. count : longint;
  609. argstart,
  610. pc,arg : pchar;
  611. quote : char;
  612. argvlen : longint;
  613. procedure allocarg(idx,len:longint);
  614. begin
  615. if idx>=argvlen then
  616. begin
  617. argvlen:=(idx+8) and (not 7);
  618. sysreallocmem(argv,argvlen*sizeof(pointer));
  619. end;
  620. { use realloc to reuse already existing memory }
  621. { always allocate, even if length is zero, since }
  622. { the arg. is still present! }
  623. sysreallocmem(argv[idx],len+1);
  624. end;
  625. begin
  626. { create commandline, it starts with the executed filename which is argv[0] }
  627. { Win32 passes the command NOT via the args, but via getmodulefilename}
  628. count:=0;
  629. argv:=nil;
  630. argvlen:=0;
  631. pc:=getcommandfile;
  632. Arglen:=0;
  633. repeat
  634. Inc(Arglen);
  635. until (pc[Arglen]=#0);
  636. allocarg(count,arglen);
  637. move(pc^,argv[count]^,arglen);
  638. { Setup cmdline variable }
  639. cmdline:=GetCommandLine;
  640. { process arguments }
  641. pc:=cmdline;
  642. {$IfDef SYSTEM_DEBUG_STARTUP}
  643. Writeln(stderr,'Win32 GetCommandLine is #',pc,'#');
  644. {$EndIf }
  645. while pc^<>#0 do
  646. begin
  647. { skip leading spaces }
  648. while pc^ in [#1..#32] do
  649. inc(pc);
  650. if pc^=#0 then
  651. break;
  652. { calc argument length }
  653. quote:=' ';
  654. argstart:=pc;
  655. arglen:=0;
  656. while (pc^<>#0) do
  657. begin
  658. case pc^ of
  659. #1..#32 :
  660. begin
  661. if quote<>' ' then
  662. inc(arglen)
  663. else
  664. break;
  665. end;
  666. '"' :
  667. begin
  668. if quote<>'''' then
  669. begin
  670. if pchar(pc+1)^<>'"' then
  671. begin
  672. if quote='"' then
  673. quote:=' '
  674. else
  675. quote:='"';
  676. end
  677. else
  678. inc(pc);
  679. end
  680. else
  681. inc(arglen);
  682. end;
  683. '''' :
  684. begin
  685. if quote<>'"' then
  686. begin
  687. if pchar(pc+1)^<>'''' then
  688. begin
  689. if quote='''' then
  690. quote:=' '
  691. else
  692. quote:='''';
  693. end
  694. else
  695. inc(pc);
  696. end
  697. else
  698. inc(arglen);
  699. end;
  700. else
  701. inc(arglen);
  702. end;
  703. inc(pc);
  704. end;
  705. { copy argument }
  706. { Don't copy the first one, it is already there.}
  707. If Count<>0 then
  708. begin
  709. allocarg(count,arglen);
  710. quote:=' ';
  711. pc:=argstart;
  712. arg:=argv[count];
  713. while (pc^<>#0) do
  714. begin
  715. case pc^ of
  716. #1..#32 :
  717. begin
  718. if quote<>' ' then
  719. begin
  720. arg^:=pc^;
  721. inc(arg);
  722. end
  723. else
  724. break;
  725. end;
  726. '"' :
  727. begin
  728. if quote<>'''' then
  729. begin
  730. if pchar(pc+1)^<>'"' then
  731. begin
  732. if quote='"' then
  733. quote:=' '
  734. else
  735. quote:='"';
  736. end
  737. else
  738. inc(pc);
  739. end
  740. else
  741. begin
  742. arg^:=pc^;
  743. inc(arg);
  744. end;
  745. end;
  746. '''' :
  747. begin
  748. if quote<>'"' then
  749. begin
  750. if pchar(pc+1)^<>'''' then
  751. begin
  752. if quote='''' then
  753. quote:=' '
  754. else
  755. quote:='''';
  756. end
  757. else
  758. inc(pc);
  759. end
  760. else
  761. begin
  762. arg^:=pc^;
  763. inc(arg);
  764. end;
  765. end;
  766. else
  767. begin
  768. arg^:=pc^;
  769. inc(arg);
  770. end;
  771. end;
  772. inc(pc);
  773. end;
  774. arg^:=#0;
  775. end;
  776. {$IfDef SYSTEM_DEBUG_STARTUP}
  777. Writeln(stderr,'dos arg ',count,' #',arglen,'#',argv[count],'#');
  778. {$EndIf SYSTEM_DEBUG_STARTUP}
  779. inc(count);
  780. end;
  781. { get argc and create an nil entry }
  782. argc:=count;
  783. allocarg(argc,0);
  784. { free unused memory }
  785. sysreallocmem(argv,(argc+1)*sizeof(pointer));
  786. end;
  787. {*****************************************************************************
  788. System Dependent Exit code
  789. *****************************************************************************}
  790. procedure install_exception_handlers;forward;
  791. procedure remove_exception_handlers;forward;
  792. procedure PascalMain;stdcall;external name 'PASCALMAIN';
  793. procedure fpc_do_exit;stdcall;external name 'FPC_DO_EXIT';
  794. Procedure ExitDLL(Exitcode : longint); forward;
  795. procedure asm_exit; stdcall;external name 'asm_exit';
  796. Procedure system_exit;
  797. begin
  798. { don't call ExitProcess inside
  799. the DLL exit code !!
  800. This crashes Win95 at least PM }
  801. if IsLibrary then
  802. ExitDLL(ExitCode);
  803. if not IsConsole then
  804. begin
  805. Close(stderr);
  806. Close(stdout);
  807. { what about Input and Output ?? PM }
  808. end;
  809. remove_exception_handlers;
  810. { call exitprocess, with cleanup as required }
  811. asm
  812. xorl %eax, %eax
  813. movw exitcode,%ax
  814. call asm_exit
  815. end;
  816. end;
  817. var
  818. { value of the stack segment
  819. to check if the call stack can be written on exceptions }
  820. _SS : Cardinal;
  821. procedure Exe_entry;[public, alias : '_FPC_EXE_Entry'];
  822. begin
  823. IsLibrary:=false;
  824. { install the handlers for exe only ?
  825. or should we install them for DLL also ? (PM) }
  826. install_exception_handlers;
  827. { This strange construction is needed to solve the _SS problem
  828. with a smartlinked syswin32 (PFV) }
  829. asm
  830. { allocate space for an exception frame }
  831. pushl $0
  832. pushl %fs:(0)
  833. { movl %esp,%fs:(0)
  834. but don't insert it as it doesn't
  835. point to anything yet
  836. this will be used in signals unit }
  837. movl %esp,%eax
  838. movl %eax,System_exception_frame
  839. pushl %ebp
  840. xorl %ebp,%ebp
  841. movl %esp,%eax
  842. movl %eax,Win32StackTop
  843. movw %ss,%bp
  844. movl %ebp,_SS
  845. call SysResetFPU
  846. xorl %ebp,%ebp
  847. call PASCALMAIN
  848. popl %ebp
  849. end;
  850. { if we pass here there was no error ! }
  851. system_exit;
  852. end;
  853. Const
  854. { DllEntryPoint }
  855. DLL_PROCESS_ATTACH = 1;
  856. DLL_THREAD_ATTACH = 2;
  857. DLL_PROCESS_DETACH = 0;
  858. DLL_THREAD_DETACH = 3;
  859. Var
  860. DLLBuf : Jmp_buf;
  861. Const
  862. DLLExitOK : boolean = true;
  863. function Dll_entry : longbool;[public, alias : '_FPC_DLL_Entry'];
  864. var
  865. res : longbool;
  866. begin
  867. IsLibrary:=true;
  868. Dll_entry:=false;
  869. case DLLreason of
  870. DLL_PROCESS_ATTACH :
  871. begin
  872. If SetJmp(DLLBuf) = 0 then
  873. begin
  874. if assigned(Dll_Process_Attach_Hook) then
  875. begin
  876. res:=Dll_Process_Attach_Hook(DllParam);
  877. if not res then
  878. exit(false);
  879. end;
  880. PASCALMAIN;
  881. Dll_entry:=true;
  882. end
  883. else
  884. Dll_entry:=DLLExitOK;
  885. end;
  886. DLL_THREAD_ATTACH :
  887. begin
  888. inc(Thread_count);
  889. {$warning Allocate Threadvars !}
  890. if assigned(Dll_Thread_Attach_Hook) then
  891. Dll_Thread_Attach_Hook(DllParam);
  892. Dll_entry:=true; { return value is ignored }
  893. end;
  894. DLL_THREAD_DETACH :
  895. begin
  896. dec(Thread_count);
  897. if assigned(Dll_Thread_Detach_Hook) then
  898. Dll_Thread_Detach_Hook(DllParam);
  899. {$warning Release Threadvars !}
  900. Dll_entry:=true; { return value is ignored }
  901. end;
  902. DLL_PROCESS_DETACH :
  903. begin
  904. Dll_entry:=true; { return value is ignored }
  905. If SetJmp(DLLBuf) = 0 then
  906. begin
  907. FPC_DO_EXIT;
  908. end;
  909. if assigned(Dll_Process_Detach_Hook) then
  910. Dll_Process_Detach_Hook(DllParam);
  911. end;
  912. end;
  913. end;
  914. Procedure ExitDLL(Exitcode : longint);
  915. begin
  916. DLLExitOK:=ExitCode=0;
  917. LongJmp(DLLBuf,1);
  918. end;
  919. function GetCurrentProcess : dword;
  920. stdcall;external 'kernel32' name 'GetCurrentProcess';
  921. function ReadProcessMemory(process : dword;address : pointer;dest : pointer;size : dword;bytesread : pdword) : longbool;
  922. stdcall;external 'kernel32' name 'ReadProcessMemory';
  923. function is_prefetch(p : pointer) : boolean;
  924. var
  925. a : array[0..15] of byte;
  926. doagain : boolean;
  927. instrlo,instrhi,opcode : byte;
  928. i : longint;
  929. begin
  930. result:=false;
  931. { read memory savely without causing another exeception }
  932. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  933. exit;
  934. i:=0;
  935. doagain:=true;
  936. while doagain and (i<15) do
  937. begin
  938. opcode:=a[i];
  939. instrlo:=opcode and $f;
  940. instrhi:=opcode and $f0;
  941. case instrhi of
  942. { prefix? }
  943. $20,$30:
  944. doagain:=(instrlo and 7)=6;
  945. $60:
  946. doagain:=(instrlo and $c)=4;
  947. $f0:
  948. doagain:=instrlo in [0,2,3];
  949. $0:
  950. begin
  951. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  952. exit;
  953. end;
  954. else
  955. doagain:=false;
  956. end;
  957. inc(i);
  958. end;
  959. end;
  960. //
  961. // Hardware exception handling
  962. //
  963. {$ifdef Set_i386_Exception_handler}
  964. {
  965. Error code definitions for the Win32 API functions
  966. Values are 32 bit values layed out as follows:
  967. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  968. 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
  969. +---+-+-+-----------------------+-------------------------------+
  970. |Sev|C|R| Facility | Code |
  971. +---+-+-+-----------------------+-------------------------------+
  972. where
  973. Sev - is the severity code
  974. 00 - Success
  975. 01 - Informational
  976. 10 - Warning
  977. 11 - Error
  978. C - is the Customer code flag
  979. R - is a reserved bit
  980. Facility - is the facility code
  981. Code - is the facility's status code
  982. }
  983. const
  984. SEVERITY_SUCCESS = $00000000;
  985. SEVERITY_INFORMATIONAL = $40000000;
  986. SEVERITY_WARNING = $80000000;
  987. SEVERITY_ERROR = $C0000000;
  988. const
  989. STATUS_SEGMENT_NOTIFICATION = $40000005;
  990. DBG_TERMINATE_THREAD = $40010003;
  991. DBG_TERMINATE_PROCESS = $40010004;
  992. DBG_CONTROL_C = $40010005;
  993. DBG_CONTROL_BREAK = $40010008;
  994. STATUS_GUARD_PAGE_VIOLATION = $80000001;
  995. STATUS_DATATYPE_MISALIGNMENT = $80000002;
  996. STATUS_BREAKPOINT = $80000003;
  997. STATUS_SINGLE_STEP = $80000004;
  998. DBG_EXCEPTION_NOT_HANDLED = $80010001;
  999. STATUS_ACCESS_VIOLATION = $C0000005;
  1000. STATUS_IN_PAGE_ERROR = $C0000006;
  1001. STATUS_INVALID_HANDLE = $C0000008;
  1002. STATUS_NO_MEMORY = $C0000017;
  1003. STATUS_ILLEGAL_INSTRUCTION = $C000001D;
  1004. STATUS_NONCONTINUABLE_EXCEPTION = $C0000025;
  1005. STATUS_INVALID_DISPOSITION = $C0000026;
  1006. STATUS_ARRAY_BOUNDS_EXCEEDED = $C000008C;
  1007. STATUS_FLOAT_DENORMAL_OPERAND = $C000008D;
  1008. STATUS_FLOAT_DIVIDE_BY_ZERO = $C000008E;
  1009. STATUS_FLOAT_INEXACT_RESULT = $C000008F;
  1010. STATUS_FLOAT_INVALID_OPERATION = $C0000090;
  1011. STATUS_FLOAT_OVERFLOW = $C0000091;
  1012. STATUS_FLOAT_STACK_CHECK = $C0000092;
  1013. STATUS_FLOAT_UNDERFLOW = $C0000093;
  1014. STATUS_INTEGER_DIVIDE_BY_ZERO = $C0000094;
  1015. STATUS_INTEGER_OVERFLOW = $C0000095;
  1016. STATUS_PRIVILEGED_INSTRUCTION = $C0000096;
  1017. STATUS_STACK_OVERFLOW = $C00000FD;
  1018. STATUS_CONTROL_C_EXIT = $C000013A;
  1019. STATUS_FLOAT_MULTIPLE_FAULTS = $C00002B4;
  1020. STATUS_FLOAT_MULTIPLE_TRAPS = $C00002B5;
  1021. STATUS_REG_NAT_CONSUMPTION = $C00002C9;
  1022. EXCEPTION_EXECUTE_HANDLER = 1;
  1023. EXCEPTION_CONTINUE_EXECUTION = -1;
  1024. EXCEPTION_CONTINUE_SEARCH = 0;
  1025. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  1026. CONTEXT_X86 = $00010000;
  1027. CONTEXT_CONTROL = CONTEXT_X86 or $00000001;
  1028. CONTEXT_INTEGER = CONTEXT_X86 or $00000002;
  1029. CONTEXT_SEGMENTS = CONTEXT_X86 or $00000004;
  1030. CONTEXT_FLOATING_POINT = CONTEXT_X86 or $00000008;
  1031. CONTEXT_DEBUG_REGISTERS = CONTEXT_X86 or $00000010;
  1032. CONTEXT_EXTENDED_REGISTERS = CONTEXT_X86 or $00000020;
  1033. CONTEXT_FULL = CONTEXT_CONTROL or CONTEXT_INTEGER or CONTEXT_SEGMENTS;
  1034. MAXIMUM_SUPPORTED_EXTENSION = 512;
  1035. type
  1036. PFloatingSaveArea = ^TFloatingSaveArea;
  1037. TFloatingSaveArea = packed record
  1038. ControlWord : Cardinal;
  1039. StatusWord : Cardinal;
  1040. TagWord : Cardinal;
  1041. ErrorOffset : Cardinal;
  1042. ErrorSelector : Cardinal;
  1043. DataOffset : Cardinal;
  1044. DataSelector : Cardinal;
  1045. RegisterArea : array[0..79] of Byte;
  1046. Cr0NpxState : Cardinal;
  1047. end;
  1048. PContext = ^TContext;
  1049. TContext = packed record
  1050. //
  1051. // The flags values within this flag control the contents of
  1052. // a CONTEXT record.
  1053. //
  1054. ContextFlags : Cardinal;
  1055. //
  1056. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  1057. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  1058. // included in CONTEXT_FULL.
  1059. //
  1060. Dr0, Dr1, Dr2,
  1061. Dr3, Dr6, Dr7 : Cardinal;
  1062. //
  1063. // This section is specified/returned if the
  1064. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  1065. //
  1066. FloatSave : TFloatingSaveArea;
  1067. //
  1068. // This section is specified/returned if the
  1069. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  1070. //
  1071. SegGs, SegFs,
  1072. SegEs, SegDs : Cardinal;
  1073. //
  1074. // This section is specified/returned if the
  1075. // ContextFlags word contains the flag CONTEXT_INTEGER.
  1076. //
  1077. Edi, Esi, Ebx,
  1078. Edx, Ecx, Eax : Cardinal;
  1079. //
  1080. // This section is specified/returned if the
  1081. // ContextFlags word contains the flag CONTEXT_CONTROL.
  1082. //
  1083. Ebp : Cardinal;
  1084. Eip : Cardinal;
  1085. SegCs : Cardinal;
  1086. EFlags, Esp, SegSs : Cardinal;
  1087. //
  1088. // This section is specified/returned if the ContextFlags word
  1089. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  1090. // The format and contexts are processor specific
  1091. //
  1092. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  1093. end;
  1094. type
  1095. PExceptionRecord = ^TExceptionRecord;
  1096. TExceptionRecord = packed record
  1097. ExceptionCode : Longint;
  1098. ExceptionFlags : Longint;
  1099. ExceptionRecord : PExceptionRecord;
  1100. ExceptionAddress : Pointer;
  1101. NumberParameters : Longint;
  1102. ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of Pointer;
  1103. end;
  1104. PExceptionPointers = ^TExceptionPointers;
  1105. TExceptionPointers = packed record
  1106. ExceptionRecord : PExceptionRecord;
  1107. ContextRecord : PContext;
  1108. end;
  1109. { type of functions that should be used for exception handling }
  1110. TTopLevelExceptionFilter = function (excep : PExceptionPointers) : Longint;stdcall;
  1111. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : TTopLevelExceptionFilter) : TTopLevelExceptionFilter;
  1112. stdcall;external 'kernel32' name 'SetUnhandledExceptionFilter';
  1113. const
  1114. MaxExceptionLevel = 16;
  1115. exceptLevel : Byte = 0;
  1116. var
  1117. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  1118. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  1119. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  1120. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1121. procedure DebugHandleErrorAddrFrame(error, addr, frame : longint);
  1122. begin
  1123. if IsConsole then begin
  1124. write(stderr,'HandleErrorAddrFrame(error=',error);
  1125. write(stderr,',addr=',hexstr(addr,8));
  1126. writeln(stderr,',frame=',hexstr(frame,8),')');
  1127. end;
  1128. HandleErrorAddrFrame(error,addr,frame);
  1129. end;
  1130. {$endif SYSTEMEXCEPTIONDEBUG}
  1131. procedure JumpToHandleErrorFrame;
  1132. var
  1133. eip, ebp, error : Longint;
  1134. begin
  1135. // save ebp
  1136. asm
  1137. movl (%ebp),%eax
  1138. movl %eax,ebp
  1139. end;
  1140. if (exceptLevel > 0) then
  1141. dec(exceptLevel);
  1142. eip:=exceptEip[exceptLevel];
  1143. error:=exceptError[exceptLevel];
  1144. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1145. if IsConsole then
  1146. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  1147. {$endif SYSTEMEXCEPTIONDEBUG}
  1148. if resetFPU[exceptLevel] then asm
  1149. fninit
  1150. fldcw fpucw
  1151. end;
  1152. { build a fake stack }
  1153. asm
  1154. {$ifdef REGCALL}
  1155. movl ebp,%ecx
  1156. movl eip,%edx
  1157. movl error,%eax
  1158. pushl eip
  1159. movl ebp,%ebp // Change frame pointer
  1160. {$else}
  1161. movl ebp,%eax
  1162. pushl %eax
  1163. movl eip,%eax
  1164. pushl %eax
  1165. movl error,%eax
  1166. pushl %eax
  1167. movl eip,%eax
  1168. pushl %eax
  1169. movl ebp,%ebp // Change frame pointer
  1170. {$endif}
  1171. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1172. jmpl DebugHandleErrorAddrFrame
  1173. {$else not SYSTEMEXCEPTIONDEBUG}
  1174. jmpl HandleErrorAddrFrame
  1175. {$endif SYSTEMEXCEPTIONDEBUG}
  1176. end;
  1177. end;
  1178. function syswin32_i386_exception_handler(excep : PExceptionPointers) : Longint;stdcall;
  1179. var
  1180. frame,
  1181. res : longint;
  1182. function SysHandleErrorFrame(error, frame : Longint; must_reset_fpu : Boolean) : Longint;
  1183. begin
  1184. if (frame = 0) then
  1185. SysHandleErrorFrame:=EXCEPTION_CONTINUE_SEARCH
  1186. else begin
  1187. if (exceptLevel >= MaxExceptionLevel) then exit;
  1188. exceptEip[exceptLevel] := excep^.ContextRecord^.Eip;
  1189. exceptError[exceptLevel] := error;
  1190. resetFPU[exceptLevel] := must_reset_fpu;
  1191. inc(exceptLevel);
  1192. excep^.ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  1193. excep^.ExceptionRecord^.ExceptionCode := 0;
  1194. SysHandleErrorFrame := EXCEPTION_CONTINUE_EXECUTION;
  1195. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1196. if IsConsole then begin
  1197. writeln(stderr,'Exception Continue Exception set at ',
  1198. hexstr(exceptEip[exceptLevel],8));
  1199. writeln(stderr,'Eip changed to ',
  1200. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', error);
  1201. end;
  1202. {$endif SYSTEMEXCEPTIONDEBUG}
  1203. end;
  1204. end;
  1205. begin
  1206. if excep^.ContextRecord^.SegSs=_SS then
  1207. frame := excep^.ContextRecord^.Ebp
  1208. else
  1209. frame := 0;
  1210. res := EXCEPTION_CONTINUE_SEARCH;
  1211. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1212. if IsConsole then Writeln(stderr,'Exception ',
  1213. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  1214. {$endif SYSTEMEXCEPTIONDEBUG}
  1215. case cardinal(excep^.ExceptionRecord^.ExceptionCode) of
  1216. STATUS_INTEGER_DIVIDE_BY_ZERO,
  1217. STATUS_FLOAT_DIVIDE_BY_ZERO :
  1218. res := SysHandleErrorFrame(200, frame, true);
  1219. STATUS_ARRAY_BOUNDS_EXCEEDED :
  1220. res := SysHandleErrorFrame(201, frame, false);
  1221. STATUS_STACK_OVERFLOW :
  1222. res := SysHandleErrorFrame(202, frame, false);
  1223. STATUS_FLOAT_OVERFLOW :
  1224. res := SysHandleErrorFrame(205, frame, true);
  1225. STATUS_FLOAT_DENORMAL_OPERAND,
  1226. STATUS_FLOAT_UNDERFLOW :
  1227. res := SysHandleErrorFrame(206, frame, true);
  1228. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  1229. STATUS_FLOAT_INEXACT_RESULT,
  1230. STATUS_FLOAT_INVALID_OPERATION,
  1231. STATUS_FLOAT_STACK_CHECK :
  1232. res := SysHandleErrorFrame(207, frame, true);
  1233. STATUS_INTEGER_OVERFLOW :
  1234. res := SysHandleErrorFrame(215, frame, false);
  1235. STATUS_ILLEGAL_INSTRUCTION:
  1236. res := SysHandleErrorFrame(216, frame, true);
  1237. STATUS_ACCESS_VIOLATION:
  1238. { Athlon prefetch bug? }
  1239. if is_prefetch(pointer(excep^.ContextRecord^.Eip)) then
  1240. begin
  1241. { if yes, then retry }
  1242. excep^.ExceptionRecord^.ExceptionCode := 0;
  1243. res:=EXCEPTION_CONTINUE_EXECUTION;
  1244. end
  1245. else
  1246. res := SysHandleErrorFrame(216, frame, true);
  1247. STATUS_CONTROL_C_EXIT:
  1248. res := SysHandleErrorFrame(217, frame, true);
  1249. STATUS_PRIVILEGED_INSTRUCTION:
  1250. res := SysHandleErrorFrame(218, frame, false);
  1251. else
  1252. begin
  1253. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  1254. res := SysHandleErrorFrame(217, frame, true)
  1255. else
  1256. res := SysHandleErrorFrame(255, frame, true);
  1257. end;
  1258. end;
  1259. syswin32_i386_exception_handler := res;
  1260. end;
  1261. procedure install_exception_handlers;
  1262. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1263. var
  1264. oldexceptaddr,
  1265. newexceptaddr : Longint;
  1266. {$endif SYSTEMEXCEPTIONDEBUG}
  1267. begin
  1268. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1269. asm
  1270. movl $0,%eax
  1271. movl %fs:(%eax),%eax
  1272. movl %eax,oldexceptaddr
  1273. end;
  1274. {$endif SYSTEMEXCEPTIONDEBUG}
  1275. SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  1276. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1277. asm
  1278. movl $0,%eax
  1279. movl %fs:(%eax),%eax
  1280. movl %eax,newexceptaddr
  1281. end;
  1282. if IsConsole then
  1283. writeln(stderr,'Old exception ',hexstr(oldexceptaddr,8),
  1284. ' new exception ',hexstr(newexceptaddr,8));
  1285. {$endif SYSTEMEXCEPTIONDEBUG}
  1286. end;
  1287. procedure remove_exception_handlers;
  1288. begin
  1289. SetUnhandledExceptionFilter(nil);
  1290. end;
  1291. {$else not cpui386 (Processor specific !!)}
  1292. procedure install_exception_handlers;
  1293. begin
  1294. end;
  1295. procedure remove_exception_handlers;
  1296. begin
  1297. end;
  1298. {$endif Set_i386_Exception_handler}
  1299. {****************************************************************************
  1300. Error Message writing using messageboxes
  1301. ****************************************************************************}
  1302. function MessageBox(w1:longint;l1,l2:pointer;w2:longint):longint;
  1303. stdcall;external 'user32' name 'MessageBoxA';
  1304. const
  1305. ErrorBufferLength = 1024;
  1306. var
  1307. ErrorBuf : array[0..ErrorBufferLength] of char;
  1308. ErrorLen : longint;
  1309. Function ErrorWrite(Var F: TextRec): Integer;
  1310. {
  1311. An error message should always end with #13#10#13#10
  1312. }
  1313. var
  1314. p : pchar;
  1315. i : longint;
  1316. Begin
  1317. if F.BufPos>0 then
  1318. begin
  1319. if F.BufPos+ErrorLen>ErrorBufferLength then
  1320. i:=ErrorBufferLength-ErrorLen
  1321. else
  1322. i:=F.BufPos;
  1323. Move(F.BufPtr^,ErrorBuf[ErrorLen],i);
  1324. inc(ErrorLen,i);
  1325. ErrorBuf[ErrorLen]:=#0;
  1326. end;
  1327. if ErrorLen>3 then
  1328. begin
  1329. p:=@ErrorBuf[ErrorLen];
  1330. for i:=1 to 4 do
  1331. begin
  1332. dec(p);
  1333. if not(p^ in [#10,#13]) then
  1334. break;
  1335. end;
  1336. end;
  1337. if ErrorLen=ErrorBufferLength then
  1338. i:=4;
  1339. if (i=4) then
  1340. begin
  1341. MessageBox(0,@ErrorBuf,pchar('Error'),0);
  1342. ErrorLen:=0;
  1343. end;
  1344. F.BufPos:=0;
  1345. ErrorWrite:=0;
  1346. End;
  1347. Function ErrorClose(Var F: TextRec): Integer;
  1348. begin
  1349. if ErrorLen>0 then
  1350. begin
  1351. MessageBox(0,@ErrorBuf,pchar('Error'),0);
  1352. ErrorLen:=0;
  1353. end;
  1354. ErrorLen:=0;
  1355. ErrorClose:=0;
  1356. end;
  1357. Function ErrorOpen(Var F: TextRec): Integer;
  1358. Begin
  1359. TextRec(F).InOutFunc:=@ErrorWrite;
  1360. TextRec(F).FlushFunc:=@ErrorWrite;
  1361. TextRec(F).CloseFunc:=@ErrorClose;
  1362. ErrorOpen:=0;
  1363. End;
  1364. procedure AssignError(Var T: Text);
  1365. begin
  1366. Assign(T,'');
  1367. TextRec(T).OpenFunc:=@ErrorOpen;
  1368. Rewrite(T);
  1369. end;
  1370. procedure SysInitStdIO;
  1371. begin
  1372. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  1373. displayed in and messagebox }
  1374. StdInputHandle:=longint(GetStdHandle(cardinal(STD_INPUT_HANDLE)));
  1375. StdOutputHandle:=longint(GetStdHandle(cardinal(STD_OUTPUT_HANDLE)));
  1376. StdErrorHandle:=longint(GetStdHandle(cardinal(STD_ERROR_HANDLE)));
  1377. if not IsConsole then
  1378. begin
  1379. AssignError(stderr);
  1380. AssignError(stdout);
  1381. Assign(Output,'');
  1382. Assign(Input,'');
  1383. Assign(ErrOutput,'');
  1384. end
  1385. else
  1386. begin
  1387. OpenStdIO(Input,fmInput,StdInputHandle);
  1388. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1389. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  1390. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  1391. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1392. end;
  1393. end;
  1394. const
  1395. Exe_entry_code : pointer = @Exe_entry;
  1396. Dll_entry_code : pointer = @Dll_entry;
  1397. begin
  1398. StackLength := InitialStkLen;
  1399. StackBottom := Sptr - StackLength;
  1400. { get some helpful informations }
  1401. GetStartupInfo(@startupinfo);
  1402. { some misc Win32 stuff }
  1403. hprevinst:=0;
  1404. if not IsLibrary then
  1405. HInstance:=getmodulehandle(GetCommandFile);
  1406. MainInstance:=HInstance;
  1407. cmdshow:=startupinfo.wshowwindow;
  1408. { Setup heap }
  1409. InitHeap;
  1410. SysInitExceptions;
  1411. SysInitStdIO;
  1412. { Arguments }
  1413. setup_arguments;
  1414. { Reset IO Error }
  1415. InOutRes:=0;
  1416. ProcessID := GetCurrentProcess;
  1417. ThreadID := GetCurrentThread;
  1418. { Reset internal error variable }
  1419. errno:=0;
  1420. {$ifdef HASVARIANT}
  1421. initvariantmanager;
  1422. {$endif HASVARIANT}
  1423. end.
  1424. {
  1425. $Log$
  1426. Revision 1.63 2004-11-04 09:32:31 peter
  1427. ErrOutput added
  1428. Revision 1.62 2004/10/25 15:38:59 peter
  1429. * compiler defined HEAP and HEAPSIZE removed
  1430. Revision 1.61 2004/09/03 19:27:25 olle
  1431. + added maxExitCode to all System.pp
  1432. * constrained error code to be below maxExitCode in RunError et. al.
  1433. Revision 1.60 2004/06/27 11:57:18 florian
  1434. * finally (hopefully) fixed sysalloc trouble
  1435. Revision 1.59 2004/06/26 15:05:14 florian
  1436. * fixed argument copying
  1437. Revision 1.58 2004/06/20 09:24:40 peter
  1438. fixed go32v2 compile
  1439. Revision 1.57 2004/06/17 16:16:14 peter
  1440. * New heapmanager that releases memory back to the OS, donated
  1441. by Micha Nelissen
  1442. Revision 1.56 2004/05/16 18:51:20 peter
  1443. * use thandle in do_*
  1444. Revision 1.55 2004/04/22 21:10:56 peter
  1445. * do_read/do_write addr argument changed to pointer
  1446. Revision 1.54 2004/02/15 21:37:18 hajny
  1447. * ProcessID initialization added
  1448. Revision 1.53 2004/02/02 17:01:47 florian
  1449. * workaround for AMD prefetch bug
  1450. Revision 1.52 2004/01/20 23:12:49 hajny
  1451. * ExecuteProcess fixes, ProcessID and ThreadID added
  1452. Revision 1.51 2003/12/17 21:56:33 peter
  1453. * win32 regcall patches
  1454. Revision 1.50 2003/12/04 20:52:41 peter
  1455. * stdcall for CreateFile
  1456. Revision 1.49 2003/11/24 23:08:37 michael
  1457. + Redefined Fileopen so it corresponds to ascdef.inc definition
  1458. Revision 1.48 2003/11/03 09:42:28 marco
  1459. * Peter's Cardinal<->Longint fixes patch
  1460. Revision 1.47 2003/10/17 22:15:10 olle
  1461. * changed i386 to cpui386
  1462. Revision 1.46 2003/10/16 15:43:13 peter
  1463. * THandle is platform dependent
  1464. Revision 1.45 2003/10/06 23:52:53 florian
  1465. * some data types cleaned up
  1466. Revision 1.44 2003/09/27 11:52:36 peter
  1467. * sbrk returns pointer
  1468. Revision 1.43 2003/09/26 07:30:34 michael
  1469. + Win32 Do_open crahs on append
  1470. Revision 1.42 2003/09/17 15:06:36 peter
  1471. * stdcall patch
  1472. Revision 1.41 2003/09/12 12:33:43 olle
  1473. * nice-ified
  1474. Revision 1.40 2003/01/01 20:56:57 florian
  1475. + added invalid instruction exception
  1476. Revision 1.39 2002/12/24 15:35:15 peter
  1477. * error code fixes
  1478. Revision 1.38 2002/12/07 13:58:45 carl
  1479. * fix warnings
  1480. Revision 1.37 2002/11/30 18:17:35 carl
  1481. + profiling support
  1482. Revision 1.36 2002/10/31 15:17:58 carl
  1483. * always allocate argument even if its empty (bugfix web bug 2202)
  1484. Revision 1.35 2002/10/14 20:40:22 florian
  1485. * InitFPU renamed to SysResetFPU
  1486. Revision 1.34 2002/10/14 19:39:17 peter
  1487. * threads unit added for thread support
  1488. Revision 1.33 2002/10/13 09:28:45 florian
  1489. + call to initvariantmanager inserted
  1490. Revision 1.32 2002/09/07 21:28:10 carl
  1491. - removed os_types
  1492. * fix range check errors
  1493. Revision 1.31 2002/09/07 16:01:29 peter
  1494. * old logs removed and tabs fixed
  1495. Revision 1.30 2002/08/26 13:49:18 pierre
  1496. * fix bug report 2086
  1497. Revision 1.29 2002/07/28 20:43:49 florian
  1498. * several fixes for linux/powerpc
  1499. * several fixes to MT
  1500. Revision 1.28 2002/07/01 16:29:05 peter
  1501. * sLineBreak changed to normal constant like Kylix
  1502. Revision 1.27 2002/06/04 09:25:14 pierre
  1503. * Rename HeapSize to WinAPIHeapSize to avoid conflict with general function
  1504. Revision 1.26 2002/04/12 17:45:13 carl
  1505. + generic stack checking
  1506. Revision 1.25 2002/03/11 19:10:33 peter
  1507. * Regenerated with updated fpcmake
  1508. Revision 1.24 2002/01/30 14:57:11 pierre
  1509. * fix compilation failure
  1510. Revision 1.23 2002/01/25 16:23:03 peter
  1511. * merged filesearch() fix
  1512. }