syswin32.pp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993-98 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 syswin32;
  14. interface
  15. {$ifdef i386}
  16. {$define Set_i386_Exception_handler}
  17. {$endif i386}
  18. { include system-independent routine headers }
  19. {$I systemh.inc}
  20. { include heap support headers }
  21. {$I heaph.inc}
  22. const
  23. { Default filehandles }
  24. UnusedHandle : longint = -1;
  25. StdInputHandle : longint = 0;
  26. StdOutputHandle : longint = 0;
  27. StdErrorHandle : longint = 0;
  28. type
  29. TStartupInfo=packed record
  30. cb : longint;
  31. lpReserved : Pointer;
  32. lpDesktop : Pointer;
  33. lpTitle : Pointer;
  34. dwX : longint;
  35. dwY : longint;
  36. dwXSize : longint;
  37. dwYSize : longint;
  38. dwXCountChars : longint;
  39. dwYCountChars : longint;
  40. dwFillAttribute : longint;
  41. dwFlags : longint;
  42. wShowWindow : Word;
  43. cbReserved2 : Word;
  44. lpReserved2 : Pointer;
  45. hStdInput : longint;
  46. hStdOutput : longint;
  47. hStdError : longint;
  48. end;
  49. var
  50. { C compatible arguments }
  51. argc : longint;
  52. argv : ppchar;
  53. { Win32 Info }
  54. startupinfo : tstartupinfo;
  55. hprevinst,
  56. HInstance,
  57. MainInstance,
  58. cmdshow : longint;
  59. IsLibrary,IsMultiThreaded,IsConsole : boolean;
  60. DLLreason,DLLparam:longint;
  61. implementation
  62. { include system independent routines }
  63. {$I system.inc}
  64. { some declarations for Win32 API calls }
  65. {$I win32.inc}
  66. CONST
  67. { These constants are used for conversion of error codes }
  68. { from win32 i/o errors to tp i/o errors }
  69. { errors 1 to 18 are the same as in Turbo Pascal }
  70. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING! }
  71. { The media is write protected. }
  72. ERROR_WRITE_PROTECT = 19;
  73. { The system cannot find the device specified. }
  74. ERROR_BAD_UNIT = 20;
  75. { The device is not ready. }
  76. ERROR_NOT_READY = 21;
  77. { The device does not recognize the command. }
  78. ERROR_BAD_COMMAND = 22;
  79. { Data error (cyclic redundancy check) }
  80. ERROR_CRC = 23;
  81. { The program issued a command but the }
  82. { command length is incorrect. }
  83. ERROR_BAD_LENGTH = 24;
  84. { The drive cannot locate a specific }
  85. { area or track on the disk. }
  86. ERROR_SEEK = 25;
  87. { The specified disk or diskette cannot be accessed. }
  88. ERROR_NOT_DOS_DISK = 26;
  89. { The drive cannot find the sector requested. }
  90. ERROR_SECTOR_NOT_FOUND = 27;
  91. { The printer is out of paper. }
  92. ERROR_OUT_OF_PAPER = 28;
  93. { The system cannot write to the specified device. }
  94. ERROR_WRITE_FAULT = 29;
  95. { The system cannot read from the specified device. }
  96. ERROR_READ_FAULT = 30;
  97. { A device attached to the system is not functioning.}
  98. ERROR_GEN_FAILURE = 31;
  99. { The process cannot access the file because }
  100. { it is being used by another process. }
  101. ERROR_SHARING_VIOLATION = 32;
  102. var
  103. errno : longint;
  104. {$ASMMODE ATT}
  105. { misc. functions }
  106. function GetLastError : DWORD;
  107. external 'kernel32' name 'GetLastError';
  108. function MessageBox(w1:longint;l1,l2:pointer;w2:longint):longint;
  109. external 'user32' name 'MessageBoxA';
  110. { time and date functions }
  111. function GetTickCount : longint;
  112. external 'kernel32' name 'GetTickCount';
  113. { process functions }
  114. procedure ExitProcess(uExitCode : UINT);
  115. external 'kernel32' name 'ExitProcess';
  116. Procedure Errno2InOutRes;
  117. Begin
  118. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  119. if (errno >= ERROR_WRITE_PROTECT) and (errno <= ERROR_GEN_FAILURE) THEN
  120. BEGIN
  121. { This is the offset to the Win32 to add to directly map }
  122. { to the DOS/TP compatible error codes when in this range }
  123. InOutRes := word(errno)+131;
  124. END
  125. else
  126. { This case is special }
  127. if errno=ERROR_SHARING_VIOLATION THEN
  128. BEGIN
  129. InOutRes :=5;
  130. END
  131. else
  132. { other error codes can directly be mapped }
  133. InOutRes := Word(errno);
  134. errno:=0;
  135. end;
  136. {$ifdef dummy}
  137. procedure int_stackcheck(stack_size:longint);[public,alias: 'STACKCHECK'];
  138. {
  139. called when trying to get local stack if the compiler directive $S
  140. is set this function must preserve esi !!!! because esi is set by
  141. the calling proc for methods it must preserve all registers !!
  142. With a 2048 byte safe area used to write to StdIo without crossing
  143. the stack boundary
  144. }
  145. begin
  146. asm
  147. pushl %eax
  148. pushl %ebx
  149. movl stack_size,%ebx
  150. addl $2048,%ebx
  151. movl %esp,%eax
  152. subl %ebx,%eax
  153. movl stacklimit,%ebx
  154. cmpl %eax,%ebx
  155. jae .L__short_on_stack
  156. popl %ebx
  157. popl %eax
  158. leave
  159. ret $4
  160. .L__short_on_stack:
  161. { can be usefull for error recovery !! }
  162. popl %ebx
  163. popl %eax
  164. end['EAX','EBX'];
  165. HandleError(202);
  166. end;
  167. {$endif dummy}
  168. procedure halt(errnum : byte);
  169. begin
  170. do_exit;
  171. ExitProcess(errnum);
  172. end;
  173. function paramcount : longint;
  174. begin
  175. paramcount := argc - 1;
  176. end;
  177. function paramstr(l : longint) : string;
  178. begin
  179. if (l>=0) and (l+1<=argc) then
  180. paramstr:=strpas(argv[l])
  181. else
  182. paramstr:='';
  183. end;
  184. procedure randomize;
  185. begin
  186. randseed:=GetTickCount;
  187. end;
  188. {*****************************************************************************
  189. Heap Management
  190. *****************************************************************************}
  191. { memory functions }
  192. function GlobalAlloc(mode,size:longint):longint;
  193. external 'kernel32' name 'GlobalAlloc';
  194. function GlobalLock(handle:longint):pointer;
  195. external 'kernel32' name 'GlobalLock';
  196. {$ifdef SYSTEMDEBUG}
  197. function GlobalSize(h:longint):longint;
  198. external 'kernel32' name 'GlobalSize';
  199. {$endif}
  200. {$ifdef NEWATT}
  201. var heap : longint;external name 'HEAP';
  202. var intern_heapsize : longint;external name 'HEAPSIZE';
  203. {$else NEWATT}
  204. {$asmmode direct}
  205. {$endif def NEWATT}
  206. function getheapstart:pointer;assembler;
  207. asm
  208. leal HEAP,%eax
  209. end ['EAX'];
  210. function getheapsize:longint;assembler;
  211. asm
  212. {$ifdef NEWATT}
  213. movl intern_HEAPSIZE,%eax
  214. {$else}
  215. movl HEAPSIZE,%eax
  216. {$endif}
  217. end ['EAX'];
  218. function Sbrk(size : longint):longint;
  219. var
  220. h,l : longint;
  221. begin
  222. h:=GlobalAlloc(258,size);
  223. l:=longint(GlobalLock(h));
  224. if l=0 then
  225. l:=-1;
  226. {$ifdef SYSTEMDEBUG}
  227. Writeln('new heap part at $',hexstr(l,8), ' size = ',GlobalSize(h));
  228. {$endif}
  229. sbrk:=l;
  230. end;
  231. { include standard heap management }
  232. {$I heap.inc}
  233. {*****************************************************************************
  234. Low Level File Routines
  235. *****************************************************************************}
  236. function WriteFile(fh:longint;buf:pointer;len:longint;var loaded:longint;
  237. overlap:pointer):longint;
  238. external 'kernel32' name 'WriteFile';
  239. function ReadFile(fh:longint;buf:pointer;len:longint;var loaded:longint;
  240. overlap:pointer):longint;
  241. external 'kernel32' name 'ReadFile';
  242. function CloseHandle(h : longint) : longint;
  243. external 'kernel32' name 'CloseHandle';
  244. function DeleteFile(p : pchar) : longint;
  245. external 'kernel32' name 'DeleteFileA';
  246. function MoveFile(old,_new : pchar) : longint;
  247. external 'kernel32' name 'MoveFileA';
  248. function SetFilePointer(l1,l2 : longint;l3 : pointer;l4 : longint) : longint;
  249. external 'kernel32' name 'SetFilePointer';
  250. function GetFileSize(h:longint;p:pointer) : longint;
  251. external 'kernel32' name 'GetFileSize';
  252. function CreateFile(name : pointer;access,sharing : longint;
  253. security : pointer;how,attr,template : longint) : longint;
  254. external 'kernel32' name 'CreateFileA';
  255. function SetEndOfFile(h : longint) : longbool;
  256. external 'kernel32' name 'SetEndOfFile';
  257. function GetFileType(Handle:DWORD):DWord;
  258. external 'kernel32' name 'GetFileType';
  259. procedure AllowSlash(p:pchar);
  260. var
  261. i : longint;
  262. begin
  263. { allow slash as backslash }
  264. for i:=0 to strlen(p) do
  265. if p[i]='/' then p[i]:='\';
  266. end;
  267. procedure do_close(h : longint);
  268. begin
  269. closehandle(h);
  270. end;
  271. procedure do_erase(p : pchar);
  272. begin
  273. AllowSlash(p);
  274. if DeleteFile(p)=0 then
  275. Begin
  276. errno:=GetLastError;
  277. Errno2InoutRes;
  278. end;
  279. end;
  280. procedure do_rename(p1,p2 : pchar);
  281. begin
  282. AllowSlash(p1);
  283. AllowSlash(p2);
  284. if MoveFile(p1,p2)=0 then
  285. Begin
  286. errno:=GetLastError;
  287. Errno2InoutRes;
  288. end;
  289. end;
  290. function do_write(h,addr,len : longint) : longint;
  291. var
  292. size:longint;
  293. begin
  294. if writefile(h,pointer(addr),len,size,nil)=0 then
  295. Begin
  296. errno:=GetLastError;
  297. Errno2InoutRes;
  298. end;
  299. do_write:=size;
  300. end;
  301. function do_read(h,addr,len : longint) : longint;
  302. var
  303. result:longint;
  304. begin
  305. if readfile(h,pointer(addr),len,result,nil)=0 then
  306. Begin
  307. errno:=GetLastError;
  308. Errno2InoutRes;
  309. end;
  310. do_read:=result;
  311. end;
  312. function do_filepos(handle : longint) : longint;
  313. var
  314. l:longint;
  315. begin
  316. l:=SetFilePointer(handle,0,nil,FILE_CURRENT);
  317. if l=-1 then
  318. begin
  319. l:=0;
  320. errno:=GetLastError;
  321. Errno2InoutRes;
  322. end;
  323. do_filepos:=l;
  324. end;
  325. procedure do_seek(handle,pos : longint);
  326. begin
  327. if SetFilePointer(handle,pos,nil,FILE_BEGIN)=-1 then
  328. Begin
  329. errno:=GetLastError;
  330. Errno2InoutRes;
  331. end;
  332. end;
  333. function do_seekend(handle:longint):longint;
  334. begin
  335. do_seekend:=SetFilePointer(handle,0,nil,FILE_END);
  336. if do_seekend=-1 then
  337. begin
  338. errno:=GetLastError;
  339. Errno2InoutRes;
  340. end;
  341. end;
  342. function do_filesize(handle : longint) : longint;
  343. var
  344. aktfilepos : longint;
  345. begin
  346. aktfilepos:=do_filepos(handle);
  347. do_filesize:=do_seekend(handle);
  348. do_seek(handle,aktfilepos);
  349. end;
  350. procedure do_truncate (handle,pos:longint);
  351. begin
  352. do_seek(handle,pos);
  353. if not(SetEndOfFile(handle)) then
  354. begin
  355. errno:=GetLastError;
  356. Errno2InoutRes;
  357. end;
  358. end;
  359. procedure do_open(var f;p : pchar;flags:longint);
  360. {
  361. filerec and textrec have both handle and mode as the first items so
  362. they could use the same routine for opening/creating.
  363. when (flags and $10) the file will be append
  364. when (flags and $100) the file will be truncate/rewritten
  365. when (flags and $1000) there is no check for close (needed for textfiles)
  366. }
  367. Const
  368. file_Share_Read = $00000001;
  369. file_Share_Write = $00000002;
  370. fmShareCompat = $00000000;
  371. fmShareExclusive = $10;
  372. fmShareDenyWrite = $20;
  373. fmShareDenyRead = $30;
  374. fmShareDenyNone = $40;
  375. Var
  376. shflags,
  377. oflags,cd : longint;
  378. begin
  379. AllowSlash(p);
  380. { close first if opened }
  381. if ((flags and $1000)=0) then
  382. begin
  383. case filerec(f).mode of
  384. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  385. fmclosed : ;
  386. else
  387. begin
  388. {not assigned}
  389. inoutres:=102;
  390. exit;
  391. end;
  392. end;
  393. end;
  394. { reset file handle }
  395. filerec(f).handle:=UnusedHandle;
  396. { convert filesharing }
  397. shflags:=0;
  398. if ((filemode and fmshareExclusive) = fmshareExclusive) then
  399. { no sharing }
  400. else
  401. if (filemode = fmShareCompat) or ((filemode and fmshareDenyWrite) = fmshareDenyWrite) then
  402. shflags := file_Share_Read
  403. else
  404. if ((filemode and fmshareDenyRead) = fmshareDenyRead) then
  405. shflags := file_Share_Write
  406. else
  407. if ((filemode and fmshareDenyNone) = fmshareDenyNone) then
  408. shflags := file_Share_Read + file_Share_Write;
  409. { convert filemode to filerec modes }
  410. case (flags and 3) of
  411. 0 : begin
  412. filerec(f).mode:=fminput;
  413. oflags:=GENERIC_READ;
  414. end;
  415. 1 : begin
  416. filerec(f).mode:=fmoutput;
  417. oflags:=GENERIC_WRITE;
  418. end;
  419. 2 : begin
  420. filerec(f).mode:=fminout;
  421. oflags:=GENERIC_WRITE or GENERIC_READ;
  422. end;
  423. end;
  424. { standard is opening and existing file }
  425. cd:=OPEN_EXISTING;
  426. { create it ? }
  427. if (flags and $100)<>0 then
  428. cd:=CREATE_ALWAYS
  429. { or append ? }
  430. else
  431. if (flags and $10)<>0 then
  432. cd:=OPEN_ALWAYS;
  433. { empty name is special }
  434. if p[0]=#0 then
  435. begin
  436. case filerec(f).mode of
  437. fminput : filerec(f).handle:=StdInputHandle;
  438. fmappend,
  439. fmoutput : begin
  440. filerec(f).handle:=StdOutputHandle;
  441. filerec(f).mode:=fmoutput; {fool fmappend}
  442. end;
  443. end;
  444. exit;
  445. end;
  446. filerec(f).handle:=CreateFile(p,oflags,shflags,nil,cd,FILE_ATTRIBUTE_NORMAL,0);
  447. { append mode }
  448. if (flags and $10)<>0 then
  449. begin
  450. do_seekend(filerec(f).handle);
  451. filerec(f).mode:=fmoutput; {fool fmappend}
  452. end;
  453. { get errors }
  454. { handle -1 is returned sometimes !! (PM) }
  455. if (filerec(f).handle=0) or (filerec(f).handle=-1) then
  456. begin
  457. errno:=GetLastError;
  458. Errno2InoutRes;
  459. end;
  460. end;
  461. function do_isdevice(handle:longint):boolean;
  462. begin
  463. do_isdevice:=(getfiletype(handle)=2);
  464. end;
  465. {*****************************************************************************
  466. UnTyped File Handling
  467. *****************************************************************************}
  468. {$i file.inc}
  469. {*****************************************************************************
  470. Typed File Handling
  471. *****************************************************************************}
  472. {$i typefile.inc}
  473. {*****************************************************************************
  474. Text File Handling
  475. *****************************************************************************}
  476. {$DEFINE EOF_CTRLZ}
  477. {$i text.inc}
  478. {*****************************************************************************
  479. Directory Handling
  480. *****************************************************************************}
  481. function CreateDirectory(name : pointer;sec : pointer) : longint;
  482. external 'kernel32' name 'CreateDirectoryA';
  483. function RemoveDirectory(name:pointer):longint;
  484. external 'kernel32' name 'RemoveDirectoryA';
  485. function SetCurrentDirectory(name : pointer) : longint;
  486. external 'kernel32' name 'SetCurrentDirectoryA';
  487. function GetCurrentDirectory(bufsize : longint;name : pchar) : longint;
  488. external 'kernel32' name 'GetCurrentDirectoryA';
  489. type
  490. TDirFnType=function(name:pointer):word;
  491. procedure dirfn(afunc : TDirFnType;const s:string);
  492. var
  493. buffer : array[0..255] of char;
  494. begin
  495. move(s[1],buffer,length(s));
  496. buffer[length(s)]:=#0;
  497. AllowSlash(pchar(@buffer));
  498. if aFunc(@buffer)=0 then
  499. begin
  500. errno:=GetLastError;
  501. Errno2InoutRes;
  502. end;
  503. end;
  504. function CreateDirectoryTrunc(name:pointer):word;
  505. begin
  506. CreateDirectoryTrunc:=CreateDirectory(name,nil);
  507. end;
  508. procedure mkdir(const s:string);[IOCHECK];
  509. begin
  510. If InOutRes <> 0 then exit;
  511. dirfn(TDirFnType(@CreateDirectoryTrunc),s);
  512. end;
  513. procedure rmdir(const s:string);[IOCHECK];
  514. begin
  515. If InOutRes <> 0 then exit;
  516. dirfn(TDirFnType(@RemoveDirectory),s);
  517. end;
  518. procedure chdir(const s:string);[IOCHECK];
  519. begin
  520. If InOutRes <> 0 then exit;
  521. dirfn(TDirFnType(@SetCurrentDirectory),s);
  522. end;
  523. procedure getdir(drivenr:byte;var dir:shortstring);
  524. const
  525. Drive:array[0..3]of char=(#0,':',#0,#0);
  526. var
  527. defaultdrive:boolean;
  528. DirBuf,SaveBuf:array[0..259] of Char;
  529. begin
  530. defaultdrive:=drivenr=0;
  531. if not defaultdrive then
  532. begin
  533. byte(Drive[0]):=Drivenr+64;
  534. GetCurrentDirectory(SizeOf(SaveBuf),SaveBuf);
  535. SetCurrentDirectory(@Drive);
  536. end;
  537. GetCurrentDirectory(SizeOf(DirBuf),DirBuf);
  538. if not defaultdrive then
  539. SetCurrentDirectory(@SaveBuf);
  540. dir:=strpas(DirBuf);
  541. end;
  542. {*****************************************************************************
  543. SystemUnit Initialization
  544. *****************************************************************************}
  545. { Startup }
  546. procedure GetStartupInfo(p : pointer);
  547. external 'kernel32' name 'GetStartupInfoA';
  548. function GetStdHandle(nStdHandle:DWORD):THANDLE;
  549. external 'kernel32' name 'GetStdHandle';
  550. { command line/enviroment functions }
  551. function GetCommandLine : pchar;
  552. external 'kernel32' name 'GetCommandLineA';
  553. { module functions }
  554. function GetModuleFileName(l1:longint;p:pointer;l2:longint):longint;
  555. external 'kernel32' name 'GetModuleFileNameA';
  556. function GetModuleHandle(p : pointer) : longint;
  557. external 'kernel32' name 'GetModuleHandleA';
  558. var
  559. ModuleName : array[0..255] of char;
  560. function GetCommandFile:pchar;
  561. begin
  562. GetModuleFileName(0,@ModuleName,255);
  563. GetCommandFile:=@ModuleName;
  564. end;
  565. procedure setup_arguments;
  566. var
  567. arglen,
  568. count : longint;
  569. argstart,scmdline : pchar;
  570. quote : set of char;
  571. argsbuf : array[0..127] of pchar;
  572. begin
  573. { create commandline, it starts with the executed filename which is argv[0] }
  574. cmdline:=GetCommandLine;
  575. count:=0;
  576. repeat
  577. { skip leading spaces }
  578. while cmdline^ in [' ',#9,#13] do
  579. inc(longint(cmdline));
  580. case cmdline^ of
  581. #0 : break;
  582. '"' : begin
  583. quote:=['"'];
  584. inc(longint(cmdline));
  585. end;
  586. '''' : begin
  587. quote:=[''''];
  588. inc(longint(cmdline));
  589. end;
  590. else
  591. quote:=[' ',#9,#13];
  592. end;
  593. { scan until the end of the argument }
  594. argstart:=cmdline;
  595. while (cmdline^<>#0) and not(cmdline^ in quote) do
  596. inc(longint(cmdline));
  597. { reserve some memory }
  598. arglen:=cmdline-argstart;
  599. getmem(argsbuf[count],arglen+1);
  600. move(argstart^,argsbuf[count]^,arglen);
  601. argsbuf[count][arglen]:=#0;
  602. { skip quote }
  603. if cmdline^ in quote then
  604. inc(longint(cmdline));
  605. if count=0 then
  606. scmdline:=cmdline-1;
  607. inc(count);
  608. until false;
  609. { create argc }
  610. argc:=count;
  611. { create an nil entry }
  612. argsbuf[count]:=nil;
  613. inc(count);
  614. { create the argv }
  615. getmem(argv,count shl 2);
  616. move(argsbuf,argv^,count shl 2);
  617. // finally setup the abused cmdline variable
  618. cmdline:=scmdline;
  619. end;
  620. {*****************************************************************************
  621. System Dependent Exit code
  622. *****************************************************************************}
  623. Procedure system_exit;
  624. begin
  625. end;
  626. {$ifdef dummy}
  627. Function SetUpStack : longint;
  628. { This routine does the following : }
  629. { returns the value of the initial SP - __stklen }
  630. begin
  631. asm
  632. pushl %ebx
  633. pushl %eax
  634. movl __stklen,%ebx
  635. movl %esp,%eax
  636. subl %ebx,%eax
  637. movl %eax,__RESULT
  638. popl %eax
  639. popl %ebx
  640. end;
  641. end;
  642. {$endif}
  643. procedure install_exception_handlers;forward;
  644. {$ifdef NEWATT}
  645. procedure PascalMain;external name 'PASCALMAIN';
  646. procedure fpc_do_exit;external name 'FPC_DO_EXIT';
  647. {$endif def NEWATT}
  648. var
  649. { value of the stack segment
  650. to check if the call stack can be written on exceptions }
  651. _SS : longint;
  652. procedure Exe_entry;[public, alias : '_FPC_EXE_Entry'];
  653. begin
  654. IsLibrary:=false;
  655. { install the handlers for exe only ?
  656. or should we install them for DLL also ? (PM) }
  657. install_exception_handlers;
  658. { This strange construction is needed to solve the _SS problem
  659. with a smartlinked syswin32 (PFV) }
  660. asm
  661. pushl %ebp
  662. xorl %ebp,%ebp
  663. movw %ss,%bp
  664. movl %ebp,_SS
  665. xorl %ebp,%ebp
  666. end;
  667. {$ifndef NEWATT}
  668. {$ASMMODE DIRECT}
  669. {$endif ndef NEWATT}
  670. asm
  671. call PASCALMAIN
  672. popl %ebp
  673. end;
  674. { if we pass here there was no error ! }
  675. ExitProcess(0);
  676. end;
  677. {$ASMMODE ATT}
  678. procedure Dll_entry;[public, alias : '_FPC_DLL_Entry'];
  679. begin
  680. IsLibrary:=true;
  681. case DLLreason of
  682. 1,2 :
  683. begin
  684. asm
  685. xorl %edi,%edi
  686. movw %ss,%di
  687. end;
  688. {$ifndef NEWATT}
  689. {$ASMMODE DIRECT}
  690. {$endif ndef NEWATT}
  691. asm
  692. movl %edi,_SS
  693. call PASCALMAIN
  694. end;
  695. end
  696. else
  697. begin
  698. asm
  699. call FPC_DO_EXIT
  700. end;
  701. end;
  702. end;
  703. end;
  704. {$ASMMODE ATT}
  705. {$ifdef Set_i386_Exception_handler}
  706. const
  707. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  708. EXCEPTION_ACCESS_VIOLATION = $c0000005;
  709. EXCEPTION_BREAKPOINT = $80000003;
  710. EXCEPTION_DATATYPE_MISALIGNMENT = $80000002;
  711. EXCEPTION_SINGLE_STEP = $80000004;
  712. EXCEPTION_ARRAY_BOUNDS_EXCEEDED = $c000008c;
  713. EXCEPTION_FLT_DENORMAL_OPERAND = $c000008d;
  714. EXCEPTION_FLT_DIVIDE_BY_ZERO = $c000008e;
  715. EXCEPTION_FLT_INEXACT_RESULT = $c000008f;
  716. EXCEPTION_FLT_INVALID_OPERATION = $c0000090;
  717. EXCEPTION_FLT_OVERFLOW = $c0000091;
  718. EXCEPTION_FLT_STACK_CHECK = $c0000092;
  719. EXCEPTION_FLT_UNDERFLOW = $c0000093;
  720. EXCEPTION_INT_DIVIDE_BY_ZERO = $c0000094;
  721. EXCEPTION_INT_OVERFLOW = $c0000095;
  722. EXCEPTION_INVALID_HANDLE = $c0000008;
  723. EXCEPTION_PRIV_INSTRUCTION = $c0000096;
  724. EXCEPTION_NONCONTINUABLE_EXCEPTION = $c0000025;
  725. EXCEPTION_NONCONTINUABLE = $1;
  726. EXCEPTION_STACK_OVERFLOW = $c00000fd;
  727. EXCEPTION_INVALID_DISPOSITION = $c0000026;
  728. ExceptionContinueExecution = 0;
  729. ExceptionContinueSearch = 1;
  730. type
  731. FLOATING_SAVE_AREA = record
  732. ControlWord : DWORD;
  733. StatusWord : DWORD;
  734. TagWord : DWORD;
  735. ErrorOffset : DWORD;
  736. ErrorSelector : DWORD;
  737. DataOffset : DWORD;
  738. DataSelector : DWORD;
  739. RegisterArea : array[0..79] of BYTE;
  740. Cr0NpxState : DWORD;
  741. end;
  742. _FLOATING_SAVE_AREA = FLOATING_SAVE_AREA;
  743. TFLOATINGSAVEAREA = FLOATING_SAVE_AREA;
  744. PFLOATINGSAVEAREA = ^FLOATING_SAVE_AREA;
  745. CONTEXT = record
  746. ContextFlags : DWORD;
  747. Dr0 : DWORD;
  748. Dr1 : DWORD;
  749. Dr2 : DWORD;
  750. Dr3 : DWORD;
  751. Dr6 : DWORD;
  752. Dr7 : DWORD;
  753. FloatSave : FLOATING_SAVE_AREA;
  754. SegGs : DWORD;
  755. SegFs : DWORD;
  756. SegEs : DWORD;
  757. SegDs : DWORD;
  758. Edi : DWORD;
  759. Esi : DWORD;
  760. Ebx : DWORD;
  761. Edx : DWORD;
  762. Ecx : DWORD;
  763. Eax : DWORD;
  764. Ebp : DWORD;
  765. Eip : DWORD;
  766. SegCs : DWORD;
  767. EFlags : DWORD;
  768. Esp : DWORD;
  769. SegSs : DWORD;
  770. end;
  771. LPCONTEXT = ^CONTEXT;
  772. _CONTEXT = CONTEXT;
  773. TCONTEXT = CONTEXT;
  774. PCONTEXT = ^CONTEXT;
  775. type pexception_record = ^exception_record;
  776. EXCEPTION_RECORD = record
  777. ExceptionCode : longint;
  778. ExceptionFlags : longint;
  779. ExceptionRecord : pexception_record;
  780. ExceptionAddress : pointer;
  781. NumberParameters : longint;
  782. ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of pointer;
  783. end;
  784. PEXCEPTION_POINTERS = ^EXCEPTION_POINTERS;
  785. EXCEPTION_POINTERS = record
  786. ExceptionRecord : PEXCEPTION_RECORD ;
  787. ContextRecord : PCONTEXT ;
  788. end;
  789. { type of functions that should be used for exception handling }
  790. LPTOP_LEVEL_EXCEPTION_FILTER = function(excep :PEXCEPTION_POINTERS) : longint;
  791. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER)
  792. : LPTOP_LEVEL_EXCEPTION_FILTER;
  793. external 'kernel32' name 'SetUnhandledExceptionFilter';
  794. function syswin32_i386_exception_handler(excep :PEXCEPTION_POINTERS) : longint;
  795. var frame : longint;
  796. begin
  797. { default : unhandled !}
  798. if excep^.ContextRecord^.SegSs=_SS then
  799. frame:=excep^.ContextRecord^.Ebp
  800. else
  801. frame:=0;
  802. syswin32_i386_exception_handler:=ExceptionContinueSearch;
  803. case excep^.ExceptionRecord^.ExceptionCode of
  804. EXCEPTION_ACCESS_VIOLATION :
  805. HandleErrorFrame(216,frame);
  806. { EXCEPTION_BREAKPOINT = $80000003;
  807. EXCEPTION_DATATYPE_MISALIGNMENT = $80000002;
  808. EXCEPTION_SINGLE_STEP = $80000004; }
  809. EXCEPTION_ARRAY_BOUNDS_EXCEEDED :
  810. HandleErrorFrame(201,frame);
  811. { EXCEPTION_FLT_DENORMAL_OPERAND = $c000008d; }
  812. EXCEPTION_FLT_DIVIDE_BY_ZERO :
  813. HandleErrorFrame(200,frame);
  814. {EXCEPTION_FLT_INEXACT_RESULT = $c000008f;
  815. EXCEPTION_FLT_INVALID_OPERATION = $c0000090;}
  816. EXCEPTION_FLT_OVERFLOW :
  817. HandleErrorFrame(205,frame);
  818. EXCEPTION_FLT_STACK_CHECK :
  819. HandleErrorFrame(207,frame);
  820. { EXCEPTION_FLT_UNDERFLOW :
  821. HandleErrorFrame(206,frame); should be accepted as zero !! }
  822. EXCEPTION_INT_DIVIDE_BY_ZERO :
  823. HandleErrorFrame(200,frame);
  824. EXCEPTION_INT_OVERFLOW :
  825. HandleErrorFrame(215,frame);
  826. {EXCEPTION_INVALID_HANDLE = $c0000008;
  827. EXCEPTION_PRIV_INSTRUCTION = $c0000096;
  828. EXCEPTION_NONCONTINUABLE_EXCEPTION = $c0000025;
  829. EXCEPTION_NONCONTINUABLE = $1;}
  830. EXCEPTION_STACK_OVERFLOW :
  831. HandleErrorFrame(202,frame);
  832. {EXCEPTION_INVALID_DISPOSITION = $c0000026;}
  833. end;
  834. end;
  835. var
  836. old_exception : LPTOP_LEVEL_EXCEPTION_FILTER;
  837. procedure install_exception_handlers;
  838. begin
  839. old_exception:=SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  840. end;
  841. {$else not i386 (Processor specific !!)}
  842. procedure install_exception_handlers;
  843. begin
  844. end;
  845. {$endif Set_i386_Exception_handler}
  846. const
  847. Exe_entry_code : pointer = @Exe_entry;
  848. Dll_entry_code : pointer = @Dll_entry;
  849. begin
  850. { get some helpful informations }
  851. GetStartupInfo(@startupinfo);
  852. { some misc Win32 stuff }
  853. hprevinst:=0;
  854. if not IsLibrary then
  855. HInstance:=getmodulehandle(GetCommandFile);
  856. MainInstance:=HInstance;
  857. { No idea how to know this issue !! }
  858. IsMultithreaded:=false;
  859. cmdshow:=startupinfo.wshowwindow;
  860. { to test stack depth }
  861. loweststack:=maxlongint;
  862. { real test stack depth }
  863. { stacklimit := setupstack; }
  864. { Setup heap }
  865. InitHeap;
  866. { Setup stdin, stdout and stderr }
  867. StdInputHandle:=longint(GetStdHandle(STD_INPUT_HANDLE));
  868. StdOutputHandle:=longint(GetStdHandle(STD_OUTPUT_HANDLE));
  869. StdErrorHandle:=longint(GetStdHandle(STD_ERROR_HANDLE));
  870. OpenStdIO(Input,fmInput,StdInputHandle);
  871. OpenStdIO(Output,fmOutput,StdOutputHandle);
  872. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  873. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  874. { Arguments }
  875. setup_arguments;
  876. { Reset IO Error }
  877. InOutRes:=0;
  878. { Reset internal error variable }
  879. errno := 0;
  880. end.
  881. {
  882. $Log$
  883. Revision 1.37 1999-04-08 12:23:11 peter
  884. * removed os.inc
  885. Revision 1.36 1999/03/24 23:25:59 peter
  886. * fixed file sharing
  887. Revision 1.35 1999/03/12 00:07:48 pierre
  888. + code for coff writer
  889. Revision 1.34 1999/03/10 22:15:31 florian
  890. + system.cmdline variable for go32v2 and win32 added
  891. Revision 1.33 1999/01/18 10:05:57 pierre
  892. + system_exit procedure added
  893. Revision 1.32 1998/12/28 23:30:11 peter
  894. * fixes for smartlinking
  895. Revision 1.31 1998/12/28 15:50:51 peter
  896. + stdout, which is needed when you write something in the system unit
  897. to the screen. Like the runtime error
  898. Revision 1.30 1998/12/21 14:28:23 pierre
  899. * HandleError -> HandleErrorFrame to avoid problem in
  900. assembler code in i386.inc
  901. (call to overloaded function in assembler block !)
  902. Revision 1.29 1998/12/15 22:43:14 peter
  903. * removed temp symbols
  904. Revision 1.28 1998/12/09 17:57:33 pierre
  905. + exception handling by default
  906. Revision 1.27 1998/12/01 14:00:08 pierre
  907. + added conversion from exceptions into run time error
  908. (only if syswin32 compiled with -ddebug for now !)
  909. * added HandleErrorFrame(errno,frame)
  910. where you specify the frame
  911. needed for win32 exception handling
  912. Revision 1.26 1998/11/30 13:13:41 pierre
  913. * needs asw to link correctly wprt0 or wdllprt0 file
  914. Revision 1.25 1998/11/30 09:16:58 pierre
  915. + added the changes from Pavel Ozerski after several modifications
  916. to be able to create DLLs
  917. Revision 1.24 1998/11/16 15:48:54 peter
  918. * fixed longbool returns for api calls
  919. Revision 1.23 1998/11/16 14:14:58 pierre
  920. * changed getdir(byte,string) to getdir(byte,shortstring)
  921. Revision 1.22 1998/10/27 15:07:16 florian
  922. + Is* flags added
  923. + IsLibrary works also
  924. Revision 1.21 1998/10/15 16:26:19 peter
  925. + fpuinit
  926. + end of backtrace indicator
  927. Revision 1.20 1998/09/14 10:48:33 peter
  928. * FPC_ names
  929. * Heap manager is now system independent
  930. Revision 1.19 1998/09/02 09:03:46 pierre
  931. * do_open sometimes returns -1 as handle on fail
  932. was not checked correctly
  933. Revision 1.16 1998/08/24 14:45:22 pierre
  934. * sbrk was wrong
  935. heap growing now works for win32
  936. Revision 1.15 1998/08/21 10:10:16 peter
  937. * winheap turned off by default
  938. Revision 1.14 1998/07/30 13:27:19 michael
  939. + Added support for errorproc. Changed runerror to HandleError
  940. Revision 1.13 1998/07/13 21:19:15 florian
  941. * some problems with ansi string support fixed
  942. Revision 1.12 1998/07/07 12:37:28 carl
  943. * correct mapping of error codes for TP compatibility
  944. + implemented stack checking in ifdef dummy
  945. Revision 1.11 1998/07/02 12:33:18 carl
  946. * IOCheck/InOutRes check for mkdir,rmdir and chdir like in TP
  947. Revision 1.10 1998/07/01 15:30:02 peter
  948. * better readln/writeln
  949. Revision 1.9 1998/06/10 10:39:17 peter
  950. * working w32 rtl
  951. Revision 1.8 1998/06/08 23:07:47 peter
  952. * dos interface is now 100% compatible
  953. * fixed call PASCALMAIN which must be direct asm
  954. Revision 1.7 1998/05/06 12:36:51 michael
  955. + Removed log from before restored version.
  956. Revision 1.6 1998/04/27 18:29:09 florian
  957. + do_open implemented, the file-I/O should be now complete
  958. Revision 1.5 1998/04/27 13:58:21 florian
  959. + paramstr/paramcount implemented
  960. Revision 1.4 1998/04/26 22:37:22 florian
  961. * some small extensions
  962. Revision 1.3 1998/04/26 21:49:57 florian
  963. + more stuff added (??dir procedures etc.)
  964. }