system.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. {
  2. $Id$
  3. ****************************************************************************
  4. This file is part of the Free Pascal run time library.
  5. Copyright (c) 1999-2002 by Free Pascal development team
  6. Free Pascal - OS/2 runtime library
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. ****************************************************************************}
  13. unit {$ifdef VER1_0}sysos2{$else}System{$endif};
  14. interface
  15. {Link the startup code.}
  16. {$ifdef VER1_0}
  17. {$l prt1.oo2}
  18. {$else}
  19. {$l prt1.o}
  20. {$endif}
  21. {$ifdef SYSTEMDEBUG}
  22. {$define SYSTEMEXCEPTIONDEBUG}
  23. {$endif SYSTEMDEBUG}
  24. { $DEFINE OS2EXCEPTIONS}
  25. {$I systemh.inc}
  26. {$IFDEF OS2EXCEPTIONS}
  27. (* Types and constants for exception handler support *)
  28. type
  29. {x} PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  30. {x} TEXCEPTION_FRAME = record
  31. {x} next : PEXCEPTION_FRAME;
  32. {x} handler : pointer;
  33. {x} end;
  34. {$ENDIF OS2EXCEPTIONS}
  35. {$I heaph.inc}
  36. {Platform specific information}
  37. type
  38. THandle = Longint;
  39. const
  40. LineEnding = #13#10;
  41. { LFNSupport is defined separately below!!! }
  42. DirectorySeparator = '\';
  43. DriveSeparator = ':';
  44. PathSeparator = ';';
  45. { FileNameCaseSensitive is defined separately below!!! }
  46. {$IFDEF OS2EXCEPTIONS}
  47. {x} System_exception_frame : PEXCEPTION_FRAME =nil;
  48. {$ENDIF OS2EXCEPTIONS}
  49. type Tos=(osDOS,osOS2,osDPMI);
  50. var os_mode:Tos;
  51. type TByteArray = array [0..$ffff] of byte;
  52. PByteArray = ^TByteArray;
  53. TSysThreadIB = record
  54. TID,
  55. Priority,
  56. Version: cardinal;
  57. MCCount,
  58. MCForceFlag: word;
  59. end;
  60. PSysThreadIB = ^TSysThreadIB;
  61. TThreadInfoBlock = record
  62. PExChain,
  63. Stack,
  64. StackLimit: pointer;
  65. TIB2: PSysThreadIB;
  66. Version,
  67. Ordinal: cardinal;
  68. end;
  69. PThreadInfoBlock = ^TThreadInfoBlock;
  70. PPThreadInfoBlock = ^PThreadInfoBlock;
  71. TProcessInfoBlock = record
  72. PID,
  73. ParentPid,
  74. Handle: cardinal;
  75. Cmd,
  76. Env: PByteArray;
  77. Status,
  78. ProcType: cardinal;
  79. end;
  80. PProcessInfoBlock = ^TProcessInfoBlock;
  81. PPProcessInfoBlock = ^PProcessInfoBlock;
  82. const UnusedHandle=$ffff;
  83. StdInputHandle=0;
  84. StdOutputHandle=1;
  85. StdErrorHandle=2;
  86. LFNSupport: boolean = true;
  87. FileNameCaseSensitive: boolean = false;
  88. sLineBreak = LineEnding;
  89. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  90. var
  91. { C-compatible arguments and environment }
  92. argc : longint;external name '_argc';
  93. argv : ppchar;external name '_argv';
  94. envp : ppchar;external name '_environ';
  95. EnvC: cardinal; external name '_envc';
  96. (* Pointer to the block of environment variables - used e.g. in unit Dos. *)
  97. Environment: PChar;
  98. var
  99. (* Type / run mode of the current process: *)
  100. (* 0 .. full screen OS/2 session *)
  101. (* 1 .. DOS session *)
  102. (* 2 .. VIO windowable OS/2 session *)
  103. (* 3 .. Presentation Manager OS/2 session *)
  104. (* 4 .. detached (background) OS/2 process *)
  105. ApplicationType: cardinal;
  106. implementation
  107. {$I system.inc}
  108. var
  109. heap_base: pointer; external name '__heap_base';
  110. heap_brk: pointer; external name '__heap_brk';
  111. heap_end: pointer; external name '__heap_end';
  112. (* Maximum heap size - only used if heap is allocated as continuous block. *)
  113. {$IFDEF CONTHEAP}
  114. BrkLimit: cardinal;
  115. {$ENDIF CONTHEAP}
  116. procedure DosGetInfoBlocks (PATIB: PPThreadInfoBlock;
  117. PAPIB: PPProcessInfoBlock); cdecl;
  118. external 'DOSCALLS' index 312;
  119. function DosLoadModule (ObjName: PChar; ObjLen: cardinal; DLLName: PChar;
  120. var Handle: cardinal): cardinal; cdecl;
  121. external 'DOSCALLS' index 318;
  122. function DosQueryProcAddr (Handle, Ordinal: cardinal; ProcName: PChar;
  123. var Address: pointer): cardinal; cdecl;
  124. external 'DOSCALLS' index 321;
  125. function DosSetRelMaxFH (var ReqCount: longint; var CurMaxFH: cardinal):
  126. cardinal; cdecl;
  127. external 'DOSCALLS' index 382;
  128. function DosSetCurrentDir (Name:PChar): cardinal; cdecl;
  129. external 'DOSCALLS' index 255;
  130. procedure DosQueryCurrentDisk(var DiskNum:cardinal;var Logical:cardinal); cdecl;
  131. external 'DOSCALLS' index 275;
  132. function DosSetDefaultDisk (DiskNum:cardinal): cardinal; cdecl;
  133. external 'DOSCALLS' index 220;
  134. { This is not real prototype, but is close enough }
  135. { for us (the 2nd parameter is actually a pointer }
  136. { to a structure). }
  137. function DosCreateDir (Name: PChar; P: pointer): cardinal; cdecl;
  138. external 'DOSCALLS' index 270;
  139. function DosDeleteDir (Name: PChar): cardinal; cdecl;
  140. external 'DOSCALLS' index 226;
  141. function DosQueryCurrentDir(DiskNum:cardinal;var Buffer;
  142. var BufLen:cardinal): cardinal; cdecl;
  143. external 'DOSCALLS' index 274;
  144. function DosMove(OldFile,NewFile:PChar):cardinal; cdecl;
  145. external 'DOSCALLS' index 271;
  146. function DosDelete(FileName:PChar):cardinal; cdecl;
  147. external 'DOSCALLS' index 259;
  148. procedure DosExit(Action, Result: cardinal); cdecl;
  149. external 'DOSCALLS' index 234;
  150. {This is the correct way to call external assembler procedures.}
  151. procedure syscall; external name '___SYSCALL';
  152. { converts an OS/2 error code to a TP compatible error }
  153. { code. Same thing exists under most other supported }
  154. { systems. }
  155. { Only call for OS/2 DLL imported routines }
  156. Procedure Errno2InOutRes;
  157. Begin
  158. { errors 1..18 are the same as in DOS }
  159. case InOutRes of
  160. { simple offset to convert these error codes }
  161. { exactly like the error codes in Win32 }
  162. 19..31 : InOutRes := InOutRes + 131;
  163. { gets a bit more complicated ... }
  164. 32..33 : InOutRes := 5;
  165. 38 : InOutRes := 100;
  166. 39 : InOutRes := 101;
  167. 112 : InOutRes := 101;
  168. 110 : InOutRes := 5;
  169. 114 : InOutRes := 6;
  170. 290 : InOutRes := 290;
  171. end;
  172. { all other cases ... we keep the same error code }
  173. end;
  174. {$IFDEF OS2EXCEPTIONS}
  175. (*
  176. The operating system defines a class of error conditions called exceptions, and specifies the default actions that are taken when these exceptions occur. The system default action in most cases is to terminate the thread that caused the exception.
  177. Exception values have the following 32-bit format:
  178. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  179. 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
  180. ÚÄÄÄÂÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  181. ³Sev³C³ Facility ³ Code ³
  182. ÀÄÄÄÁÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  183. Sev Severity code. Possible values are described in the following list:
  184. 00 Success
  185. 01 Informational
  186. 10 Warning
  187. 11 Error
  188. C Customer code flag.
  189. Facility Facility code.
  190. Code Facility's status code.
  191. Exceptions that are specific to OS/2 Version 2.X (for example, XCPT_SIGNAL) have a facility code of 1.
  192. System exceptions include both synchronous and asynchronous exceptions. Synchronous exceptions are caused by events that are internal to a thread's execution. For example, synchronous exceptions could be caused by invalid parameters, or by a thread's request to end its own execution.
  193. Asynchronous exceptions are caused by events that are external to a thread's execution. For example, an asynchronous exception can be caused by a user's entering a Ctrl+C or Ctrl+Break key sequence, or by a process' issuing DosKillProcess to end the execution of another process.
  194. The Ctrl+Break and Ctrl+C exceptions are also known as signals, or as signal exceptions.
  195. The following tables show the symbolic names of system exceptions, their numerical values, and related information fields.
  196. Portable, Non-Fatal, Software-Generated Exceptions
  197. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ¿
  198. ³Exception Name ³Value ³
  199. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  200. ³XCPT_GUARD_PAGE_VIOLATION ³0x80000001³
  201. ³ ExceptionInfo[0] - R/W flag ³ ³
  202. ³ ExceptionInfo[1] - FaultAddr ³ ³
  203. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  204. ³XCPT_UNABLE_TO_GROW_STACK ³0x80010001³
  205. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÙ
  206. Portable, Fatal, Hardware-Generated Exceptions
  207. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  208. ³Exception Name ³Value ³Related Trap ³
  209. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  210. ³XCPT_ACCESS_VIOLATION ³0xC0000005³0x09, 0x0B, ³
  211. ³ ExceptionInfo[0] - Flags ³ ³0x0C, 0x0D, ³
  212. ³ XCPT_UNKNOWN_ACCESS 0x0 ³ ³0x0E ³
  213. ³ XCPT_READ_ACCESS 0x1 ³ ³ ³
  214. ³ XCPT_WRITE_ACCESS 0x2 ³ ³ ³
  215. ³ XCPT_EXECUTE_ACCESS 0x4 ³ ³ ³
  216. ³ XCPT_SPACE_ACCESS 0x8 ³ ³ ³
  217. ³ XCPT_LIMIT_ACCESS 0x10 ³ ³ ³
  218. ³ ExceptionInfo[1] - FaultAddr ³ ³ ³
  219. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  220. ³XCPT_INTEGER_DIVIDE_BY_ZERO ³0xC000009B³0 ³
  221. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  222. ³XCPT_FLOAT_DIVIDE_BY_ZERO ³0xC0000095³0x10 ³
  223. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  224. ³XCPT_FLOAT_INVALID_OPERATION ³0xC0000097³0x10 ³
  225. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  226. ³XCPT_ILLEGAL_INSTRUCTION ³0xC000001C³0x06 ³
  227. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  228. ³XCPT_PRIVILEGED_INSTRUCTION ³0xC000009D³0x0D ³
  229. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  230. ³XCPT_INTEGER_OVERFLOW ³0xC000009C³0x04 ³
  231. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  232. ³XCPT_FLOAT_OVERFLOW ³0xC0000098³0x10 ³
  233. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  234. ³XCPT_FLOAT_UNDERFLOW ³0xC000009A³0x10 ³
  235. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  236. ³XCPT_FLOAT_DENORMAL_OPERAND ³0xC0000094³0x10 ³
  237. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  238. ³XCPT_FLOAT_INEXACT_RESULT ³0xC0000096³0x10 ³
  239. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  240. ³XCPT_FLOAT_STACK_CHECK ³0xC0000099³0x10 ³
  241. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  242. ³XCPT_DATATYPE_MISALIGNMENT ³0xC000009E³0x11 ³
  243. ³ ExceptionInfo[0] - R/W flag ³ ³ ³
  244. ³ ExceptionInfo[1] - Alignment ³ ³ ³
  245. ³ ExceptionInfo[2] - FaultAddr ³ ³ ³
  246. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  247. ³XCPT_BREAKPOINT ³0xC000009F³0x03 ³
  248. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  249. ³XCPT_SINGLE_STEP ³0xC00000A0³0x01 ³
  250. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  251. Portable, Fatal, Software-Generated Exceptions
  252. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  253. ³Exception Name ³Value ³Related Trap ³
  254. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  255. ³XCPT_IN_PAGE_ERROR ³0xC0000006³0x0E ³
  256. ³ ExceptionInfo[0] - FaultAddr ³ ³ ³
  257. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  258. ³XCPT_PROCESS_TERMINATE ³0xC0010001³ ³
  259. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  260. ³XCPT_ASYNC_PROCESS_TERMINATE ³0xC0010002³ ³
  261. ³ ExceptionInfo[0] - TID of ³ ³ ³
  262. ³ terminating thread ³ ³ ³
  263. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  264. ³XCPT_NONCONTINUABLE_EXCEPTION ³0xC0000024³ ³
  265. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  266. ³XCPT_INVALID_DISPOSITION ³0xC0000025³ ³
  267. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  268. Non-Portable, Fatal Exceptions
  269. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  270. ³Exception Name ³Value ³Related Trap ³
  271. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  272. ³XCPT_INVALID_LOCK_SEQUENCE ³0xC000001D³ ³
  273. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  274. ³XCPT_ARRAY_BOUNDS_EXCEEDED ³0xC0000093³0x05 ³
  275. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  276. Unwind Operation Exceptions
  277. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ¿
  278. ³Exception Name ³Value ³
  279. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  280. ³XCPT_UNWIND ³0xC0000026³
  281. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  282. ³XCPT_BAD_STACK ³0xC0000027³
  283. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  284. ³XCPT_INVALID_UNWIND_TARGET ³0xC0000028³
  285. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÙ
  286. Fatal Signal Exceptions
  287. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ¿
  288. ³Exception Name ³Value ³
  289. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  290. ³XCPT_SIGNAL ³0xC0010003³
  291. ³ ExceptionInfo[ 0 ] - Signal ³ ³
  292. ³ Number ³ ³
  293. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÙ
  294. *)
  295. {$ENDIF OS2EXCEPTIONS}
  296. {****************************************************************************
  297. Miscellaneous related routines.
  298. ****************************************************************************}
  299. procedure system_exit;
  300. begin
  301. DosExit(1{process}, exitcode);
  302. end;
  303. {$ASMMODE ATT}
  304. function paramcount:longint;assembler;
  305. asm
  306. movl argc,%eax
  307. decl %eax
  308. end ['EAX'];
  309. function args:pointer;assembler;
  310. asm
  311. movl argv,%eax
  312. end ['EAX'];
  313. function paramstr(l:longint):string;
  314. var p:^Pchar;
  315. begin
  316. if L = 0 then
  317. begin
  318. GetMem (P, 260);
  319. p[0] := #0; { in case of error, initialize to empty string }
  320. {$ASMMODE INTEL}
  321. asm
  322. mov edx, P
  323. mov ecx, 260
  324. mov eax, 7F33h
  325. call syscall { error handle already with empty string }
  326. end ['eax', 'ecx', 'edx'];
  327. ParamStr := StrPas (PChar (P));
  328. FreeMem (P, 260);
  329. end
  330. else
  331. if (l>0) and (l<=paramcount) then
  332. begin
  333. p:=args;
  334. paramstr:=strpas(p[l]);
  335. end
  336. else paramstr:='';
  337. end;
  338. procedure randomize; assembler;
  339. asm
  340. mov ah, 2Ch
  341. call syscall
  342. mov word ptr [randseed], cx
  343. mov word ptr [randseed + 2], dx
  344. end ['eax', 'ecx', 'edx'];
  345. {$ASMMODE ATT}
  346. {****************************************************************************
  347. Heap management releated routines.
  348. ****************************************************************************}
  349. { this function allows to extend the heap by calling
  350. syscall $7f00 resizes the brk area}
  351. function sbrk(size:longint):pointer;
  352. {$IFDEF DUMPGROW}
  353. var
  354. L: longword;
  355. begin
  356. WriteLn ('Trying to grow heap by ', Size, ' to ', HeapSize + Size);
  357. {$IFDEF CONTHEAP}
  358. WriteLn ('BrkLimit is ', BrkLimit);
  359. {$ENDIF CONTHEAP}
  360. asm
  361. movl size,%edx
  362. movw $0x7f00,%ax
  363. call syscall { result directly in EAX }
  364. inc eax { Result in EAX, -1 = error (has to be transformed to 0) }
  365. jz .LSbrk_End
  366. dec eax { No error - back to previous value }
  367. .LSbrk_End:
  368. mov %eax,L
  369. end ['eax', 'edx'];
  370. WriteLn ('New heap at ', L);
  371. Sbrk := pointer (L);
  372. end;
  373. {$ELSE DUMPGROW}
  374. assembler;
  375. asm
  376. movl size,%edx
  377. movw $0x7f00,%ax
  378. call syscall
  379. inc %eax { Result in EAX, -1 = error (has to be transformed to 0) }
  380. jz .LSbrk_End
  381. dec %eax { No error - back to previous value }
  382. .LSbrk_End:
  383. end ['eax', 'edx'];
  384. {$ENDIF DUMPGROW}
  385. function getheapstart:pointer;assembler;
  386. asm
  387. movl heap_base,%eax
  388. end ['EAX'];
  389. function getheapsize:longint;assembler;
  390. asm
  391. movl heap_brk,%eax
  392. end ['EAX'];
  393. {$i heap.inc}
  394. {****************************************************************************
  395. Low Level File Routines
  396. ****************************************************************************}
  397. procedure allowslash(p:Pchar);
  398. {Allow slash as backslash.}
  399. var i:longint;
  400. begin
  401. for i:=0 to strlen(p) do
  402. if p[i]='/' then p[i]:='\';
  403. end;
  404. procedure do_close(h:longint);
  405. begin
  406. { Only three standard handles under real OS/2 }
  407. if h>2 then
  408. begin
  409. asm
  410. pushl %ebx
  411. movb $0x3e,%ah
  412. movl h,%ebx
  413. call syscall
  414. jnc .Lnoerror { error code? }
  415. movw %ax, InOutRes { yes, then set InOutRes }
  416. .Lnoerror:
  417. popl %ebx
  418. end ['eax'];
  419. end;
  420. end;
  421. procedure do_erase(p:Pchar);
  422. begin
  423. allowslash(p);
  424. inoutres:=DosDelete(p);
  425. end;
  426. procedure do_rename(p1,p2:Pchar);
  427. begin
  428. allowslash(p1);
  429. allowslash(p2);
  430. inoutres:=DosMove(p1, p2);
  431. end;
  432. function do_read(h,addr,len:longint):longint; assembler;
  433. asm
  434. movl len,%ecx
  435. movl addr,%edx
  436. movl h,%ebx
  437. movb $0x3f,%ah
  438. call syscall
  439. jnc .LDOSREAD1
  440. movw %ax,inoutres;
  441. xorl %eax,%eax
  442. .LDOSREAD1:
  443. end ['eax', 'ebx', 'ecx', 'edx'];
  444. function do_write(h,addr,len:longint) : longint; assembler;
  445. asm
  446. xorl %eax,%eax
  447. cmpl $0,len { 0 bytes to write is undefined behavior }
  448. jz .LDOSWRITE1
  449. movl len,%ecx
  450. movl addr,%edx
  451. movl h,%ebx
  452. movb $0x40,%ah
  453. call syscall
  454. jnc .LDOSWRITE1
  455. movw %ax,inoutres;
  456. .LDOSWRITE1:
  457. end ['eax', 'ebx', 'ecx', 'edx'];
  458. function do_filepos(handle:longint): longint; assembler;
  459. asm
  460. movw $0x4201,%ax
  461. movl handle,%ebx
  462. xorl %edx,%edx
  463. call syscall
  464. jnc .LDOSFILEPOS
  465. movw %ax,inoutres;
  466. xorl %eax,%eax
  467. .LDOSFILEPOS:
  468. end ['eax', 'ebx', 'ecx', 'edx'];
  469. procedure do_seek(handle,pos:longint); assembler;
  470. asm
  471. movw $0x4200,%ax
  472. movl handle,%ebx
  473. movl pos,%edx
  474. call syscall
  475. jnc .LDOSSEEK1
  476. movw %ax,inoutres;
  477. .LDOSSEEK1:
  478. end ['eax', 'ebx', 'ecx', 'edx'];
  479. function do_seekend(handle:longint):longint; assembler;
  480. asm
  481. movw $0x4202,%ax
  482. movl handle,%ebx
  483. xorl %edx,%edx
  484. call syscall
  485. jnc .Lset_at_end1
  486. movw %ax,inoutres;
  487. xorl %eax,%eax
  488. .Lset_at_end1:
  489. end ['eax', 'ebx', 'ecx', 'edx'];
  490. function do_filesize(handle:longint):longint;
  491. var aktfilepos:longint;
  492. begin
  493. aktfilepos:=do_filepos(handle);
  494. do_filesize:=do_seekend(handle);
  495. do_seek(handle,aktfilepos);
  496. end;
  497. procedure do_truncate(handle,pos:longint); assembler;
  498. asm
  499. (* DOS function 40h isn't safe for this according to EMX documentation *)
  500. movl $0x7F25,%eax
  501. movl Handle,%ebx
  502. movl Pos,%edx
  503. call syscall
  504. incl %eax
  505. movl %ecx, %eax
  506. jnz .LTruncate1 { compare the value of EAX to verify error }
  507. (* File position is undefined after truncation, move to the end. *)
  508. movl $0x4202,%eax
  509. movl Handle,%ebx
  510. movl $0,%edx
  511. call syscall
  512. jnc .LTruncate2
  513. .LTruncate1:
  514. movw %ax,inoutres;
  515. .LTruncate2:
  516. end ['eax', 'ebx', 'ecx', 'edx'];
  517. const
  518. FileHandleCount: cardinal = 20;
  519. function Increase_File_Handle_Count: boolean;
  520. var Err: word;
  521. L1: longint;
  522. L2: cardinal;
  523. begin
  524. L1 := 10;
  525. if DosSetRelMaxFH (L1, L2) <> 0 then
  526. Increase_File_Handle_Count := false
  527. else
  528. if L2 > FileHandleCount then
  529. begin
  530. FileHandleCount := L2;
  531. Increase_File_Handle_Count := true;
  532. end
  533. else
  534. Increase_File_Handle_Count := false;
  535. end;
  536. procedure do_open(var f;p:pchar;flags:longint);
  537. {
  538. filerec and textrec have both handle and mode as the first items so
  539. they could use the same routine for opening/creating.
  540. when (flags and $100) the file will be append
  541. when (flags and $1000) the file will be truncate/rewritten
  542. when (flags and $10000) there is no check for close (needed for textfiles)
  543. }
  544. var Action: cardinal;
  545. begin
  546. allowslash(p);
  547. { close first if opened }
  548. if ((flags and $10000)=0) then
  549. begin
  550. case filerec(f).mode of
  551. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  552. fmclosed:;
  553. else
  554. begin
  555. inoutres:=102; {not assigned}
  556. exit;
  557. end;
  558. end;
  559. end;
  560. { reset file handle }
  561. filerec(f).handle := UnusedHandle;
  562. Action := 0;
  563. { convert filemode to filerec modes }
  564. case (flags and 3) of
  565. 0 : filerec(f).mode:=fminput;
  566. 1 : filerec(f).mode:=fmoutput;
  567. 2 : filerec(f).mode:=fminout;
  568. end;
  569. if (flags and $1000)<>0 then
  570. Action := $50000; (* Create / replace *)
  571. { empty name is special }
  572. if p[0]=#0 then
  573. begin
  574. case FileRec(f).mode of
  575. fminput :
  576. FileRec(f).Handle:=StdInputHandle;
  577. fminout, { this is set by rewrite }
  578. fmoutput :
  579. FileRec(f).Handle:=StdOutputHandle;
  580. fmappend :
  581. begin
  582. FileRec(f).Handle:=StdOutputHandle;
  583. FileRec(f).mode:=fmoutput; {fool fmappend}
  584. end;
  585. end;
  586. exit;
  587. end;
  588. Action := Action or (Flags and $FF);
  589. (* DenyNone if sharing not specified. *)
  590. if Flags and 112 = 0 then
  591. Action := Action or 64;
  592. asm
  593. pushl %ebx
  594. movl $0x7f2b, %eax
  595. movl Action, %ecx
  596. movl p, %edx
  597. call syscall
  598. cmpl $0xffffffff, %eax
  599. jnz .LOPEN1
  600. movw %cx, InOutRes
  601. movw UnusedHandle, %ax
  602. .LOPEN1:
  603. movl f,%edx { Warning : This assumes Handle is first }
  604. movw %ax,(%edx) { field of FileRec }
  605. popl %ebx
  606. end ['eax', 'ecx', 'edx'];
  607. if (InOutRes = 4) and Increase_File_Handle_Count then
  608. (* Trying again after increasing amount of file handles *)
  609. asm
  610. movl $0x7f2b, %eax
  611. movl Action, %ecx
  612. movl p, %edx
  613. call syscall
  614. cmpl $0xffffffff, %eax
  615. jnz .LOPEN2
  616. movw %cx, InOutRes
  617. movw UnusedHandle, %ax
  618. .LOPEN2:
  619. movl f,%edx
  620. movw %ax,(%edx)
  621. end ['eax', 'ecx', 'edx'];
  622. { for systems that have more handles }
  623. if FileRec (F).Handle > FileHandleCount then
  624. FileHandleCount := FileRec (F).Handle;
  625. if ((Flags and $100) <> 0) and
  626. (FileRec (F).Handle <> UnusedHandle) then
  627. begin
  628. do_seekend (FileRec (F).Handle);
  629. FileRec (F).Mode := fmOutput; {fool fmappend}
  630. end;
  631. end;
  632. {$ASMMODE INTEL}
  633. function do_isdevice (Handle: longint): boolean; assembler;
  634. (*
  635. var HT, Attr: longint;
  636. begin
  637. if os_mode = osOS2 then
  638. begin
  639. if DosQueryHType (Handle, HT, Attr) <> 0 then HT := 1;
  640. end
  641. else
  642. *)
  643. asm
  644. mov ebx, Handle
  645. mov eax, 4400h
  646. call syscall
  647. mov eax, 1
  648. jc @IsDevEnd
  649. test edx, 80h { verify if it is a file }
  650. jnz @IsDevEnd
  651. dec eax { nope, so result is zero }
  652. @IsDevEnd:
  653. end ['eax', 'ebx', 'edx'];
  654. {$ASMMODE ATT}
  655. {*****************************************************************************
  656. UnTyped File Handling
  657. *****************************************************************************}
  658. {$i file.inc}
  659. {*****************************************************************************
  660. Typed File Handling
  661. *****************************************************************************}
  662. {$i typefile.inc}
  663. {*****************************************************************************
  664. Text File Handling
  665. *****************************************************************************}
  666. {$DEFINE EOF_CTRLZ}
  667. {$i text.inc}
  668. {****************************************************************************
  669. Directory related routines.
  670. ****************************************************************************}
  671. {*****************************************************************************
  672. Directory Handling
  673. *****************************************************************************}
  674. procedure MkDir (const S: string);[IOCHECK];
  675. var buffer:array[0..255] of char;
  676. Rc : word;
  677. begin
  678. If (s='') or (InOutRes <> 0) then
  679. exit;
  680. move(s[1],buffer,length(s));
  681. buffer[length(s)]:=#0;
  682. allowslash(Pchar(@buffer));
  683. Rc := DosCreateDir(buffer,nil);
  684. if Rc <> 0 then
  685. begin
  686. InOutRes := Rc;
  687. Errno2Inoutres;
  688. end;
  689. end;
  690. procedure rmdir(const s : string);[IOCHECK];
  691. var buffer:array[0..255] of char;
  692. Rc : word;
  693. begin
  694. if (s = '.' ) then
  695. InOutRes := 16;
  696. If (s='') or (InOutRes <> 0) then
  697. exit;
  698. move(s[1],buffer,length(s));
  699. buffer[length(s)]:=#0;
  700. allowslash(Pchar(@buffer));
  701. Rc := DosDeleteDir(buffer);
  702. if Rc <> 0 then
  703. begin
  704. InOutRes := Rc;
  705. Errno2Inoutres;
  706. end;
  707. end;
  708. {$ASMMODE INTEL}
  709. procedure ChDir (const S: string);[IOCheck];
  710. var RC: cardinal;
  711. Buffer: array [0..255] of char;
  712. begin
  713. If (s='') or (InOutRes <> 0) then exit;
  714. if (Length (S) >= 2) and (S [2] = ':') then
  715. begin
  716. RC := DosSetDefaultDisk ((Ord (S [1]) and not ($20)) - $40);
  717. if RC <> 0 then
  718. InOutRes := RC
  719. else
  720. if Length (S) > 2 then
  721. begin
  722. Move (S [1], Buffer, Length (S));
  723. Buffer [Length (S)] := #0;
  724. AllowSlash (PChar (@Buffer));
  725. RC := DosSetCurrentDir (@Buffer);
  726. if RC <> 0 then
  727. begin
  728. InOutRes := RC;
  729. Errno2InOutRes;
  730. end;
  731. end;
  732. end else begin
  733. Move (S [1], Buffer, Length (S));
  734. Buffer [Length (S)] := #0;
  735. AllowSlash (PChar (@Buffer));
  736. RC := DosSetCurrentDir (@Buffer);
  737. if RC <> 0 then
  738. begin
  739. InOutRes:= RC;
  740. Errno2InOutRes;
  741. end;
  742. end;
  743. end;
  744. {$ASMMODE ATT}
  745. procedure GetDir (DriveNr: byte; var Dir: ShortString);
  746. {Written by Michael Van Canneyt.}
  747. var sof: Pchar;
  748. i:byte;
  749. l,l2:cardinal;
  750. begin
  751. Dir [4] := #0;
  752. { Used in case the specified drive isn't available }
  753. sof:=pchar(@dir[4]);
  754. { dir[1..3] will contain '[drivenr]:\', but is not }
  755. { supplied by DOS, so we let dos string start at }
  756. { dir[4] }
  757. { Get dir from drivenr : 0=default, 1=A etc... }
  758. l:=255-3;
  759. InOutRes:=longint (DosQueryCurrentDir(DriveNr, sof^, l));
  760. {$WARNING Result code should be translated in some cases!}
  761. { Now Dir should be filled with directory in ASCIIZ, }
  762. { starting from dir[4] }
  763. dir[0]:=#3;
  764. dir[2]:=':';
  765. dir[3]:='\';
  766. i:=4;
  767. {Conversion to Pascal string }
  768. while (dir[i]<>#0) do
  769. begin
  770. { convert path name to DOS }
  771. if dir[i]='/' then
  772. dir[i]:='\';
  773. dir[0]:=char(i);
  774. inc(i);
  775. end;
  776. { upcase the string (FPC function) }
  777. if drivenr<>0 then { Drive was supplied. We know it }
  778. dir[1]:=chr(64+drivenr)
  779. else
  780. begin
  781. { We need to get the current drive from DOS function 19H }
  782. { because the drive was the default, which can be unknown }
  783. DosQueryCurrentDisk(l, l2);
  784. dir[1]:=chr(64+l);
  785. end;
  786. if not (FileNameCaseSensitive) then dir:=upcase(dir);
  787. end;
  788. {*****************************************************************************
  789. System unit initialization.
  790. ****************************************************************************}
  791. {****************************************************************************
  792. Error Message writing using messageboxes
  793. ****************************************************************************}
  794. type
  795. TWinMessageBox = function (Parent, Owner: cardinal;
  796. BoxText, BoxTitle: PChar; Identity, Style: cardinal): cardinal; cdecl;
  797. TWinInitialize = function (Options: cardinal): cardinal; cdecl;
  798. TWinCreateMsgQueue = function (Handle: cardinal; cmsg: longint): cardinal;
  799. cdecl;
  800. const
  801. ErrorBufferLength = 1024;
  802. mb_OK = $0000;
  803. mb_Error = $0040;
  804. mb_Moveable = $4000;
  805. MBStyle = mb_OK or mb_Error or mb_Moveable;
  806. WinInitialize: TWinInitialize = nil;
  807. WinCreateMsgQueue: TWinCreateMsgQueue = nil;
  808. WinMessageBox: TWinMessageBox = nil;
  809. EnvSize: cardinal = 0;
  810. var
  811. ErrorBuf: array [0..ErrorBufferLength] of char;
  812. ErrorLen: longint;
  813. PMWinHandle: cardinal;
  814. function ErrorWrite (var F: TextRec): integer;
  815. {
  816. An error message should always end with #13#10#13#10
  817. }
  818. var
  819. P: PChar;
  820. I: longint;
  821. begin
  822. if F.BufPos > 0 then
  823. begin
  824. if F.BufPos + ErrorLen > ErrorBufferLength then
  825. I := ErrorBufferLength - ErrorLen
  826. else
  827. I := F.BufPos;
  828. Move (F.BufPtr^, ErrorBuf [ErrorLen], I);
  829. Inc (ErrorLen, I);
  830. ErrorBuf [ErrorLen] := #0;
  831. end;
  832. if ErrorLen > 3 then
  833. begin
  834. P := @ErrorBuf [ErrorLen];
  835. for I := 1 to 4 do
  836. begin
  837. Dec (P);
  838. if not (P^ in [#10, #13]) then
  839. break;
  840. end;
  841. end;
  842. if ErrorLen = ErrorBufferLength then
  843. I := 4;
  844. if (I = 4) then
  845. begin
  846. WinMessageBox (0, 0, @ErrorBuf, PChar ('Error'), 0, MBStyle);
  847. ErrorLen := 0;
  848. end;
  849. F.BufPos := 0;
  850. ErrorWrite := 0;
  851. end;
  852. function ErrorClose (var F: TextRec): integer;
  853. begin
  854. if ErrorLen > 0 then
  855. begin
  856. WinMessageBox (0, 0, @ErrorBuf, PChar ('Error'), 0, MBStyle);
  857. ErrorLen := 0;
  858. end;
  859. ErrorLen := 0;
  860. ErrorClose := 0;
  861. end;
  862. function ErrorOpen (var F: TextRec): integer;
  863. begin
  864. TextRec(F).InOutFunc := @ErrorWrite;
  865. TextRec(F).FlushFunc := @ErrorWrite;
  866. TextRec(F).CloseFunc := @ErrorClose;
  867. ErrorOpen := 0;
  868. end;
  869. procedure AssignError (var T: Text);
  870. begin
  871. Assign (T, '');
  872. TextRec (T).OpenFunc := @ErrorOpen;
  873. Rewrite (T);
  874. end;
  875. procedure SysInitStdIO;
  876. begin
  877. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  878. displayed in a messagebox }
  879. (*
  880. StdInputHandle := longint(GetStdHandle(cardinal(STD_INPUT_HANDLE)));
  881. StdOutputHandle := longint(GetStdHandle(cardinal(STD_OUTPUT_HANDLE)));
  882. StdErrorHandle := longint(GetStdHandle(cardinal(STD_ERROR_HANDLE)));
  883. if not IsConsole then
  884. begin
  885. if (DosLoadModule (nil, 0, 'PMWIN', PMWinHandle) = 0) and
  886. (DosQueryProcAddr (PMWinHandle, 789, nil, pointer (WinMessageBox)) = 0)
  887. and
  888. (DosQueryProcAddr (PMWinHandle, 763, nil, pointer (WinInitialize)) = 0)
  889. and
  890. (DosQueryProcAddr (PMWinHandle, 716, nil, pointer (WinCreateMsgQueue))
  891. = 0)
  892. then
  893. begin
  894. WinInitialize (0);
  895. WinCreateMsgQueue (0, 0);
  896. end
  897. else
  898. HandleError (2);
  899. AssignError (StdErr);
  900. AssignError (StdOut);
  901. Assign (Output, '');
  902. Assign (Input, '');
  903. end
  904. else
  905. begin
  906. *)
  907. OpenStdIO (Input, fmInput, StdInputHandle);
  908. OpenStdIO (Output, fmOutput, StdOutputHandle);
  909. OpenStdIO (StdOut, fmOutput, StdOutputHandle);
  910. OpenStdIO (StdErr, fmOutput, StdErrorHandle);
  911. (*
  912. end;
  913. *)
  914. end;
  915. function GetFileHandleCount: longint;
  916. var L1: longint;
  917. L2: cardinal;
  918. begin
  919. L1 := 0; (* Don't change the amount, just check. *)
  920. if DosSetRelMaxFH (L1, L2) <> 0 then GetFileHandleCount := 50
  921. else GetFileHandleCount := L2;
  922. end;
  923. var TIB: PThreadInfoBlock;
  924. PIB: PProcessInfoBlock;
  925. begin
  926. IsLibrary := FALSE;
  927. os_mode:=OsOs2;
  928. {$ASMMODE INTEL}
  929. asm
  930. {Enable the brk area by initializing it with the initial heap size.}
  931. mov eax, 7F01h
  932. mov edx, heap_brk
  933. add edx, heap_base
  934. call syscall
  935. cmp eax, -1
  936. jnz @heapok
  937. push dword 204
  938. call HandleError
  939. @heapok:
  940. {$IFDEF CONTHEAP}
  941. { Find out brk limit }
  942. mov eax, 7F02h
  943. mov ecx, 3
  944. call syscall
  945. jcxz @heaplimitknown
  946. mov eax, 0
  947. @heaplimitknown:
  948. mov BrkLimit, eax
  949. {$ELSE CONTHEAP}
  950. { Change sbrk behaviour to allocate arbitrary (non-contiguous) memory blocks }
  951. mov eax, 7F0Fh
  952. mov ecx, 0Ch
  953. mov edx, 8
  954. call syscall
  955. {$ENDIF CONTHEAP}
  956. end;
  957. {Now request, if we are running under DOS,
  958. read-access to the first meg. of memory.}
  959. (* Initialize the amount of file handles *)
  960. FileHandleCount := GetFileHandleCount;
  961. DosGetInfoBlocks (@TIB, @PIB);
  962. StackBottom := cardinal (TIB^.Stack);
  963. Environment := pointer (PIB^.Env);
  964. ApplicationType := PIB^.ProcType;
  965. IsConsole := ApplicationType <> 3;
  966. exitproc:=nil;
  967. {Initialize the heap.}
  968. initheap;
  969. { ... and exceptions }
  970. SysInitExceptions;
  971. { ... and I/O }
  972. SysInitStdIO;
  973. { no I/O-Error }
  974. inoutres:=0;
  975. {$ifdef HASVARIANT}
  976. initvariantmanager;
  977. {$endif HASVARIANT}
  978. {$IFDEF DUMPGROW}
  979. {$IFDEF CONTHEAP}
  980. WriteLn ('Initial brk size is ', GetHeapSize);
  981. WriteLn ('Brk limit is ', BrkLimit);
  982. {$ENDIF CONTHEAP}
  983. {$ENDIF DUMPGROW}
  984. end.
  985. {
  986. $Log$
  987. Revision 1.47 2003-10-16 15:43:13 peter
  988. * THandle is platform dependent
  989. Revision 1.46 2003/10/14 21:10:06 hajny
  990. * another longint2cardinal fix
  991. Revision 1.45 2003/10/13 21:17:31 hajny
  992. * longint to cardinal corrections
  993. Revision 1.44 2003/10/12 18:07:30 hajny
  994. * wrong use of Intel syntax
  995. Revision 1.43 2003/10/12 17:59:40 hajny
  996. * wrong use of Intel syntax
  997. Revision 1.42 2003/10/12 17:52:28 hajny
  998. * wrong use of Intel syntax
  999. Revision 1.41 2003/10/12 10:45:36 hajny
  1000. * sbrk error handling corrected
  1001. Revision 1.40 2003/10/07 21:26:35 hajny
  1002. * stdcall fixes and asm routines cleanup
  1003. Revision 1.39 2003/10/06 16:58:27 yuri
  1004. * Another set of native functions.
  1005. Revision 1.38 2003/10/06 14:22:40 yuri
  1006. * Some emx code removed. Now withous so stupid error as with dos ;)
  1007. Revision 1.37 2003/10/04 08:30:59 yuri
  1008. * at&t syntax instead of intel syntax was used
  1009. Revision 1.36 2003/10/03 21:46:41 peter
  1010. * stdcall fixes
  1011. Revision 1.35 2003/10/01 18:42:49 yuri
  1012. * Unclosed comment
  1013. Revision 1.34 2003/09/29 18:39:59 hajny
  1014. * append fix applied to GO32v2, OS/2 and EMX
  1015. Revision 1.33 2003/09/27 11:52:36 peter
  1016. * sbrk returns pointer
  1017. Revision 1.32 2003/03/30 09:20:30 hajny
  1018. * platform extension unification
  1019. Revision 1.31 2003/01/15 22:16:12 hajny
  1020. * default sharing mode changed to DenyNone
  1021. Revision 1.30 2002/12/15 22:41:41 hajny
  1022. * First_Meg fixed + Environment initialization under Dos
  1023. Revision 1.29 2002/12/08 16:39:58 hajny
  1024. - WriteLn in GUI mode support commented out until fixed
  1025. Revision 1.28 2002/12/07 19:17:14 hajny
  1026. * GetEnv correction, better PM support, ...
  1027. Revision 1.27 2002/11/17 22:31:02 hajny
  1028. * type corrections (longint x cardinal)
  1029. Revision 1.26 2002/10/27 14:29:00 hajny
  1030. * heap management (hopefully) fixed
  1031. Revision 1.25 2002/10/14 19:39:17 peter
  1032. * threads unit added for thread support
  1033. Revision 1.24 2002/10/13 09:28:45 florian
  1034. + call to initvariantmanager inserted
  1035. Revision 1.23 2002/09/07 16:01:25 peter
  1036. * old logs removed and tabs fixed
  1037. Revision 1.22 2002/07/01 16:29:05 peter
  1038. * sLineBreak changed to normal constant like Kylix
  1039. Revision 1.21 2002/04/21 15:54:20 carl
  1040. + initialize some global variables
  1041. Revision 1.20 2002/04/12 17:42:16 carl
  1042. + generic stack checking
  1043. Revision 1.19 2002/03/11 19:10:33 peter
  1044. * Regenerated with updated fpcmake
  1045. Revision 1.18 2002/02/10 13:46:20 hajny
  1046. * heap management corrected (heap_brk)
  1047. }