syswin32.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. FPC Pascal system unit for the Win32 API.
  5. Copyright (c) 1993-98 by Florian Klaempfl and Pavel Ozerski
  6. member of the Free Pascal development team.
  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. {$I os.inc}
  15. interface
  16. {$I systemh.inc}
  17. var
  18. hprevinst,hinstance,cmdshow : longint;
  19. heaperror : pointer;
  20. { $I heaph.inc}
  21. const
  22. UnusedHandle : longint = -1;
  23. StdInputHandle : longint = 0;
  24. StdOutputHandle : longint = 0;
  25. StdErrorHandle : longint = 0;
  26. implementation
  27. { some declarations for Win32 API calls }
  28. {$I Win32.inc}
  29. {$I system.inc}
  30. type
  31. plongint = ^longint;
  32. {$ifdef dummy}
  33. {$S-}
  34. procedure st1(stack_size : longint);[public,alias: 'STACKCHECK'];
  35. begin
  36. { called when trying to get local stack }
  37. { if the compiler directive $S is set }
  38. { this function must preserve esi !!!! }
  39. { because esi is set by the calling }
  40. { proc for methods }
  41. { it must preserve all registers !! }
  42. asm
  43. pushl %eax
  44. pushl %ebx
  45. movl stack_size,%ebx
  46. movl %esp,%eax
  47. subl %ebx,%eax
  48. {$ifdef SYSTEMDEBUG}
  49. movl U_SYSTEM_LOWESTSTACK,%ebx
  50. cmpl %eax,%ebx
  51. jb _is_not_lowest
  52. movl %eax,U_SYSTEM_LOWESTSTACK
  53. _is_not_lowest:
  54. {$endif SYSTEMDEBUG}
  55. movl __stkbottom,%ebx
  56. cmpl %eax,%ebx
  57. jae __short_on_stack
  58. popl %ebx
  59. popl %eax
  60. leave
  61. ret $4
  62. __short_on_stack:
  63. { can be usefull for error recovery !! }
  64. popl %ebx
  65. popl %eax
  66. end['EAX','EBX'];
  67. RunError(202);
  68. { this needs a local variable }
  69. { so the function called itself !! }
  70. { Writeln('low in stack ');
  71. RunError(202); }
  72. end;
  73. {$endif dummy}
  74. var
  75. argc : longint;
  76. args : pointer;
  77. arg_buffer : pointer;
  78. procedure halt(errnum : byte);
  79. begin
  80. do_exit;
  81. flush(stderr);
  82. LocalFree(arg_buffer);
  83. ExitProcess(errnum);
  84. end;
  85. function paramcount : longint;
  86. begin
  87. paramcount:=argc-1;
  88. end;
  89. function paramstr(l : longint) : string;
  90. var
  91. p : ^pchar;
  92. begin
  93. if (l>=0) and (l<=paramcount) then
  94. begin
  95. p:=args;
  96. paramstr:=strpas(p[l]);
  97. end
  98. else paramstr:='';
  99. end;
  100. procedure randomize;
  101. begin
  102. randseed:=GetTickCount;
  103. end;
  104. {$i winheap.inc}
  105. { $I heap.inc}
  106. {****************************************************************************
  107. Low Level File Routines
  108. ****************************************************************************}
  109. procedure AllowSlash(p:pchar);
  110. var
  111. i : longint;
  112. begin
  113. { allow slash as backslash }
  114. for i:=0 to strlen(p) do
  115. if p[i]='/' then p[i]:='\';
  116. end;
  117. procedure do_close(h : longint);
  118. begin
  119. closehandle(h);
  120. end;
  121. procedure do_erase(p : pchar);
  122. begin
  123. AllowSlash(p);
  124. if DeleteFile(p)=0 then
  125. inoutres:=GetLastError;
  126. end;
  127. procedure do_rename(p1,p2 : pchar);
  128. begin
  129. AllowSlash(p1);
  130. AllowSlash(p2);
  131. if MoveFile(p1,p2)=0 then
  132. inoutres:=GetLastError;
  133. end;
  134. function do_write(h,addr,len : longint) : longint;
  135. var
  136. size:longint;
  137. begin
  138. if writefile(h,pointer(addr),len,size,nil)=0 then
  139. inoutres:=GetLastError;
  140. do_write:=size;
  141. end;
  142. function do_read(h,addr,len : longint) : longint;
  143. var
  144. result:longint;
  145. begin
  146. if readfile(h,pointer(addr),len,result,nil)=0 then
  147. inoutres:=GetLastError;
  148. do_read:=result;
  149. end;
  150. function do_filepos(handle : longint) : longint;
  151. var
  152. l:longint;
  153. begin
  154. l:=SetFilePointer(handle,0,nil,1);
  155. if l=-1 then
  156. begin
  157. l:=0;
  158. inoutres:=GetLastError;
  159. end;
  160. do_filepos:=l;
  161. end;
  162. procedure do_seek(handle,pos : longint);
  163. begin
  164. if SetFilePointer(handle,pos,nil,0)=-1 then
  165. inoutres:=GetLastError;
  166. end;
  167. function do_seekend(handle:longint):longint;
  168. begin
  169. {!!!!!!!!!!!!}
  170. end;
  171. function do_filesize(handle : longint) : longint;
  172. var
  173. aktfilepos : longint;
  174. begin
  175. aktfilepos:=do_filepos(handle);
  176. do_filesize:=do_seekend(handle);
  177. do_seek(handle,aktfilepos);
  178. end;
  179. procedure do_truncate (handle,pos:longint);
  180. begin
  181. {!!!!!!!!!!!!}
  182. end;
  183. procedure do_open(var f;p:pchar;flags:longint);
  184. {
  185. filerec and textrec have both handle and mode as the first items so
  186. they could use the same routine for opening/creating.
  187. when (flags and $10) the file will be append
  188. when (flags and $100) the file will be truncate/rewritten
  189. when (flags and $1000) there is no check for close (needed for textfiles)
  190. }
  191. begin
  192. AllowSlash(p);
  193. {!!!!!!!!!!!!}
  194. end;
  195. {*****************************************************************************
  196. UnTyped File Handling
  197. *****************************************************************************}
  198. {$i file.inc}
  199. {*****************************************************************************
  200. Typed File Handling
  201. *****************************************************************************}
  202. {$i typefile.inc}
  203. {*****************************************************************************
  204. Text File Handling
  205. *****************************************************************************}
  206. {$DEFINE EOF_CTRLZ}
  207. {$i text.inc}
  208. {*****************************************************************************
  209. Directory Handling
  210. *****************************************************************************}
  211. type
  212. TDirFnType=function(name:pointer):word;
  213. procedure dirfn(afunc : TDirFnType;const s:string);
  214. var
  215. buffer : array[0..255] of char;
  216. begin
  217. move(s[1],buffer,length(s));
  218. buffer[length(s)]:=#0;
  219. AllowSlash(pchar(@buffer));
  220. if aFunc(@buffer)=0 then
  221. inoutres:=GetLastError;
  222. end;
  223. function CreateDirectoryTrunc(name:pointer):word;
  224. begin
  225. CreateDirectoryTrunc:=CreateDirectory(name,nil);
  226. end;
  227. procedure mkdir(const s:string);[IOCHECK];
  228. begin
  229. dirfn(TDirFnType(@CreateDirectoryTrunc),s);
  230. end;
  231. procedure rmdir(const s:string);[IOCHECK];
  232. begin
  233. dirfn(TDirFnType(@RemoveDirectory),s);
  234. end;
  235. procedure chdir(const s:string);[IOCHECK];
  236. begin
  237. dirfn(TDirFnType(@SetCurrentDirectory),s);
  238. end;
  239. procedure getdir(drivenr:byte;var dir:string);
  240. const
  241. Drive:array[0..3]of char=(#0,':',#0,#0);
  242. var
  243. defaultdrive:boolean;
  244. DirBuf,SaveBuf:array[0..259] of Char;
  245. begin
  246. defaultdrive:=drivenr=0;
  247. if not defaultdrive then
  248. begin
  249. byte(Drive[0]):=Drivenr+64;
  250. GetCurrentDirectory(SizeOf(SaveBuf),SaveBuf);
  251. SetCurrentDirectory(@Drive);
  252. end;
  253. GetCurrentDirectory(SizeOf(DirBuf),DirBuf);
  254. if not defaultdrive then
  255. SetCurrentDirectory(@SaveBuf);
  256. dir:=strpas(DirBuf);
  257. end;
  258. {*****************************************************************************
  259. SystemUnit Initialization
  260. *****************************************************************************}
  261. procedure Entry;[public,alias: '_mainCRTStartup'];
  262. {
  263. the following procedure is written with the help of an article of
  264. the german computer magazine c't (3/97 p. 372)
  265. }
  266. var
  267. cmdline : pchar;
  268. begin
  269. cmdline:=GetCommandLine;
  270. argc:=0;
  271. while true do
  272. begin
  273. break;
  274. end;
  275. arg_buffer:=LocalAlloc(LMEM_FIXED,8);
  276. { call to the pascal main }
  277. asm
  278. call PASCALMAIN
  279. end;
  280. { that's all folks }
  281. LocalFree(arg_buffer);
  282. ExitProcess(0);
  283. end;
  284. procedure OpenStdIO(var f:text;mode:word;hdl:longint);
  285. begin
  286. Assign(f,'');
  287. TextRec(f).Handle:=hdl;
  288. TextRec(f).Mode:=mode;
  289. TextRec(f).InOutFunc:=@FileInOutFunc;
  290. TextRec(f).FlushFunc:=@FileInOutFunc;
  291. TextRec(f).Closefunc:=@fileclosefunc;
  292. end;
  293. {$PACKRECORDS 1}
  294. var
  295. s : string;
  296. StartupInfo : record
  297. cb : longint;
  298. lpReserved : Pointer;
  299. lpDesktop : Pointer;
  300. lpTitle : Pointer;
  301. dwX : longint;
  302. dwY : longint;
  303. dwXSize : longint;
  304. dwYSize : longint;
  305. dwXCountChars : longint;
  306. dwYCountChars : longint;
  307. dwFillAttribute : longint;
  308. dwFlags : longint;
  309. wShowWindow : Word;
  310. cbReserved2 : Word;
  311. lpReserved2 : Pointer;
  312. hStdInput : longint;
  313. hStdOutput : longint;
  314. hStdError : longint;
  315. end;
  316. {$PACKRECORDS NORMAL}
  317. begin
  318. { get some helpful informations }
  319. GetStartupInfo(@startupinfo);
  320. { Initialize ExitProc }
  321. ExitProc:=Nil;
  322. { to test stack depth }
  323. loweststack:=maxlongint;
  324. { Setup heap }
  325. {!!! InitHeap; }
  326. { Setup stdin, stdout and stderr }
  327. StdInputHandle:=longint(GetStdHandle(STD_INPUT_HANDLE));
  328. StdOutputHandle:=longint(GetStdHandle(STD_OUTPUT_HANDLE));
  329. StdErrorHandle:=longint(GetStdHandle(STD_ERROR_HANDLE));
  330. OpenStdIO(Input,fmInput,StdInputHandle);
  331. OpenStdIO(Output,fmOutput,StdOutputHandle);
  332. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  333. { Reset IO Error }
  334. InOutRes:=0;
  335. { some misc Win32 stuff }
  336. hprevinst:=0;
  337. getmodulefilename(0,@s,256);
  338. hinstance:=getmodulehandle(@s);
  339. cmdshow:=startupinfo.wshowwindow;
  340. end.
  341. {
  342. $Log$
  343. Revision 1.4 1998-04-26 22:37:22 florian
  344. * some small extensions
  345. Revision 1.3 1998/04/26 21:49:57 florian
  346. + more stuff added (??dir procedures etc.)
  347. Revision 1.2 1998/03/27 00:50:22 peter
  348. * small fixes so it compiles
  349. Revision 1.1.1.1 1998/03/25 11:18:47 root
  350. * Restored version
  351. Revision 1.13 1998/03/05 22:37:36 florian
  352. * some stuff added
  353. Revision 1.12 1998/01/26 12:02:28 michael
  354. + Added log at the end
  355. Working file: rtl/win32/syswin32.pp
  356. description:
  357. ----------------------------
  358. revision 1.11
  359. date: 1998/01/25 21:53:37; author: peter; state: Exp; lines: +415 -408
  360. + Universal Handles support for StdIn/StdOut/StdErr
  361. * Updated layout of sysamiga.pas
  362. ----------------------------
  363. revision 1.10
  364. date: 1998/01/16 22:22:59; author: michael; state: Exp; lines: +408 -544
  365. * Synchronised with other system files (Peter Vreman)
  366. ----------------------------
  367. revision 1.9
  368. date: 1998/01/07 00:04:55; author: michael; state: Exp; lines: +84 -124
  369. + Final adjustments for a uniform file handling interface.
  370. (From Peter Vreman)
  371. ----------------------------
  372. revision 1.8
  373. date: 1998/01/05 16:51:26; author: michael; state: Exp; lines: +18 -52
  374. + Moved init of heap to heap.inc: INITheap() (From Peter Vreman)
  375. ----------------------------
  376. revision 1.7
  377. date: 1997/12/19 11:47:08; author: florian; state: Exp; lines: +2 -2
  378. *** empty log message ***
  379. ----------------------------
  380. revision 1.6
  381. date: 1997/12/01 12:42:52; author: michael; state: Exp; lines: +12 -5
  382. + added copyright reference in header.
  383. ----------------------------
  384. revision 1.5
  385. date: 1997/11/27 23:04:10; author: florian; state: Exp; lines: +1 -13
  386. Old log entries to log-file added
  387. ----------------------------
  388. revision 1.4
  389. date: 1997/11/27 23:01:09; author: florian; state: Exp; lines: +8 -3
  390. This was a test
  391. ----------------------------
  392. revision 1.3
  393. date: 1997/11/27 22:49:06; author: florian; state: Exp; lines: +12 -9
  394. - CPU.PP added
  395. - some bugs in DOS fixed (espsecially for go32v1)
  396. - the win32 system unit is now compilable
  397. ----------------------------
  398. revision 1.2
  399. date: 1997/11/27 17:40:12; author: florian; state: Exp; lines: +8 -1
  400. Added log and id to syswin32.pp for test purposes
  401. ----------------------------
  402. revision 1.1
  403. date: 1997/11/27 10:15:33; author: florian; state: Exp;
  404. Win32 files added (they are untested)
  405. =============================================================================
  406. }