system.pas 37 KB

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