system.pas 35 KB

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