system.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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. {.$define DEBUGENVIRONMENT}
  25. {.$define DEBUGARGUMENTS}
  26. {$endif SYSTEMDEBUG}
  27. { $DEFINE OS2EXCEPTIONS}
  28. {$I systemh.inc}
  29. {$IFDEF OS2EXCEPTIONS}
  30. (* Types and constants for exception handler support *)
  31. type
  32. {x} PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  33. {x} TEXCEPTION_FRAME = record
  34. {x} next : PEXCEPTION_FRAME;
  35. {x} handler : pointer;
  36. {x} end;
  37. {$ENDIF OS2EXCEPTIONS}
  38. {$I heaph.inc}
  39. {Platform specific information}
  40. type
  41. THandle = Longint;
  42. const
  43. LineEnding = #13#10;
  44. { LFNSupport is defined separately below!!! }
  45. DirectorySeparator = '\';
  46. DriveSeparator = ':';
  47. PathSeparator = ';';
  48. { FileNameCaseSensitive is defined separately below!!! }
  49. type Tos=(osDOS,osOS2,osDPMI);
  50. const os_mode: Tos = osOS2;
  51. first_meg: pointer = nil;
  52. {$IFDEF OS2EXCEPTIONS}
  53. {x} System_exception_frame : PEXCEPTION_FRAME =nil;
  54. {$ENDIF OS2EXCEPTIONS}
  55. type TByteArray = array [0..$ffff] of byte;
  56. PByteArray = ^TByteArray;
  57. TSysThreadIB = record
  58. TID,
  59. Priority,
  60. Version: cardinal;
  61. MCCount,
  62. MCForceFlag: word;
  63. end;
  64. PSysThreadIB = ^TSysThreadIB;
  65. TThreadInfoBlock = record
  66. PExChain,
  67. Stack,
  68. StackLimit: pointer;
  69. TIB2: PSysThreadIB;
  70. Version,
  71. Ordinal: cardinal;
  72. end;
  73. PThreadInfoBlock = ^TThreadInfoBlock;
  74. PPThreadInfoBlock = ^PThreadInfoBlock;
  75. TProcessInfoBlock = record
  76. PID,
  77. ParentPid,
  78. Handle: cardinal;
  79. Cmd,
  80. Env: PByteArray;
  81. Status,
  82. ProcType: cardinal;
  83. end;
  84. PProcessInfoBlock = ^TProcessInfoBlock;
  85. PPProcessInfoBlock = ^PProcessInfoBlock;
  86. const UnusedHandle=-1;
  87. StdInputHandle=0;
  88. StdOutputHandle=1;
  89. StdErrorHandle=2;
  90. LFNSupport: boolean = true;
  91. FileNameCaseSensitive: boolean = false;
  92. sLineBreak = LineEnding;
  93. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  94. var
  95. { C-compatible arguments and environment }
  96. argc : longint; //external name '_argc';
  97. argv : ppchar; //external name '_argv';
  98. envp : ppchar; //external name '_environ';
  99. EnvC: cardinal; //external name '_envc';
  100. (* Pointer to the block of environment variables - used e.g. in unit Dos. *)
  101. Environment: PChar;
  102. var
  103. (* Type / run mode of the current process: *)
  104. (* 0 .. full screen OS/2 session *)
  105. (* 1 .. DOS session *)
  106. (* 2 .. VIO windowable OS/2 session *)
  107. (* 3 .. Presentation Manager OS/2 session *)
  108. (* 4 .. detached (background) OS/2 process *)
  109. ApplicationType: cardinal;
  110. implementation
  111. {$I system.inc}
  112. //var
  113. // heap_base: pointer; external name '__heap_base';
  114. // heap_brk: pointer; external name '__heap_brk';
  115. // heap_end: pointer; external name '__heap_end';
  116. (* Maximum heap size - only used if heap is allocated as continuous block. *)
  117. {$IFDEF CONTHEAP}
  118. // BrkLimit: cardinal;
  119. {$ENDIF CONTHEAP}
  120. procedure DosGetInfoBlocks (PATIB: PPThreadInfoBlock;
  121. PAPIB: PPProcessInfoBlock); cdecl;
  122. external 'DOSCALLS' index 312;
  123. function DosLoadModule (ObjName: PChar; ObjLen: cardinal; DLLName: PChar;
  124. var Handle: cardinal): cardinal; cdecl;
  125. external 'DOSCALLS' index 318;
  126. function DosQueryProcAddr (Handle, Ordinal: cardinal; ProcName: PChar;
  127. var Address: pointer): cardinal; cdecl;
  128. external 'DOSCALLS' index 321;
  129. function DosSetRelMaxFH (var ReqCount: longint; var CurMaxFH: cardinal):
  130. cardinal; cdecl;
  131. external 'DOSCALLS' index 382;
  132. function DosSetCurrentDir (Name:PChar): cardinal; cdecl;
  133. external 'DOSCALLS' index 255;
  134. procedure DosQueryCurrentDisk(var DiskNum:cardinal;var Logical:cardinal); cdecl;
  135. external 'DOSCALLS' index 275;
  136. function DosSetDefaultDisk (DiskNum:cardinal): cardinal; cdecl;
  137. external 'DOSCALLS' index 220;
  138. { This is not real prototype, but is close enough }
  139. { for us (the 2nd parameter is actually a pointer }
  140. { to a structure). }
  141. function DosCreateDir (Name: PChar; P: pointer): cardinal; cdecl;
  142. external 'DOSCALLS' index 270;
  143. function DosDeleteDir (Name: PChar): cardinal; cdecl;
  144. external 'DOSCALLS' index 226;
  145. function DosQueryCurrentDir(DiskNum:cardinal;var Buffer;
  146. var BufLen:cardinal): cardinal; cdecl;
  147. external 'DOSCALLS' index 274;
  148. function DosMove(OldFile,NewFile:PChar):cardinal; cdecl;
  149. external 'DOSCALLS' index 271;
  150. function DosDelete(FileName:PChar):cardinal; cdecl;
  151. external 'DOSCALLS' index 259;
  152. procedure DosExit(Action, Result: cardinal); cdecl;
  153. external 'DOSCALLS' index 234;
  154. // EAs not used in System unit
  155. function DosOpen(FileName:PChar;var Handle:longint;var Action:cardinal;
  156. InitSize,Attrib,OpenFlags,FileMode:cardinal;
  157. EA:Pointer):longint; cdecl;
  158. external 'DOSCALLS' index 273;
  159. function DosClose(Handle:longint): longint; cdecl;
  160. external 'DOSCALLS' index 257;
  161. function DosRead(Handle:longint; Buffer: Pointer;Count:longint;
  162. var ActCount:longint):longint; cdecl;
  163. external 'DOSCALLS' index 281;
  164. function DosWrite(Handle:longint; Buffer: Pointer;Count:longint;
  165. var ActCount:longint):longint; cdecl;
  166. external 'DOSCALLS' index 282;
  167. function DosSetFilePtr(Handle:longint;Pos:longint;Method:cardinal;
  168. var PosActual:longint):longint; cdecl;
  169. external 'DOSCALLS' index 256;
  170. function DosSetFileSize(Handle:longint;Size:cardinal):longint; cdecl;
  171. external 'DOSCALLS' index 272;
  172. function DosQueryHType(Handle:longint;var HandType:longint;
  173. var Attr:longint):longint; cdecl;
  174. external 'DOSCALLS' index 224;
  175. type
  176. TSysDateTime=packed record
  177. Hour,
  178. Minute,
  179. Second,
  180. Sec100,
  181. Day,
  182. Month: byte;
  183. Year: word;
  184. TimeZone: smallint;
  185. WeekDay: byte;
  186. end;
  187. function DosGetDateTime(var Buf:TSysDateTime):longint; cdecl;
  188. external 'DOSCALLS' index 230;
  189. { converts an OS/2 error code to a TP compatible error }
  190. { code. Same thing exists under most other supported }
  191. { systems. }
  192. { Only call for OS/2 DLL imported routines }
  193. Procedure Errno2InOutRes;
  194. Begin
  195. { errors 1..18 are the same as in DOS }
  196. case InOutRes of
  197. { simple offset to convert these error codes }
  198. { exactly like the error codes in Win32 }
  199. 19..31 : InOutRes := InOutRes + 131;
  200. { gets a bit more complicated ... }
  201. 32..33 : InOutRes := 5;
  202. 38 : InOutRes := 100;
  203. 39 : InOutRes := 101;
  204. 112 : InOutRes := 101;
  205. 110 : InOutRes := 5;
  206. 114 : InOutRes := 6;
  207. 290 : InOutRes := 290;
  208. end;
  209. { all other cases ... we keep the same error code }
  210. end;
  211. {$IFDEF OS2EXCEPTIONS}
  212. (*
  213. 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.
  214. Exception values have the following 32-bit format:
  215. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  216. 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
  217. ÚÄÄÄÂÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  218. ³Sev³C³ Facility ³ Code ³
  219. ÀÄÄÄÁÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  220. Sev Severity code. Possible values are described in the following list:
  221. 00 Success
  222. 01 Informational
  223. 10 Warning
  224. 11 Error
  225. C Customer code flag.
  226. Facility Facility code.
  227. Code Facility's status code.
  228. Exceptions that are specific to OS/2 Version 2.X (for example, XCPT_SIGNAL) have a facility code of 1.
  229. 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.
  230. 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.
  231. The Ctrl+Break and Ctrl+C exceptions are also known as signals, or as signal exceptions.
  232. The following tables show the symbolic names of system exceptions, their numerical values, and related information fields.
  233. Portable, Non-Fatal, Software-Generated Exceptions
  234. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ¿
  235. ³Exception Name ³Value ³
  236. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  237. ³XCPT_GUARD_PAGE_VIOLATION ³0x80000001³
  238. ³ ExceptionInfo[0] - R/W flag ³ ³
  239. ³ ExceptionInfo[1] - FaultAddr ³ ³
  240. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  241. ³XCPT_UNABLE_TO_GROW_STACK ³0x80010001³
  242. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÙ
  243. Portable, Fatal, Hardware-Generated Exceptions
  244. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  245. ³Exception Name ³Value ³Related Trap ³
  246. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  247. ³XCPT_ACCESS_VIOLATION ³0xC0000005³0x09, 0x0B, ³
  248. ³ ExceptionInfo[0] - Flags ³ ³0x0C, 0x0D, ³
  249. ³ XCPT_UNKNOWN_ACCESS 0x0 ³ ³0x0E ³
  250. ³ XCPT_READ_ACCESS 0x1 ³ ³ ³
  251. ³ XCPT_WRITE_ACCESS 0x2 ³ ³ ³
  252. ³ XCPT_EXECUTE_ACCESS 0x4 ³ ³ ³
  253. ³ XCPT_SPACE_ACCESS 0x8 ³ ³ ³
  254. ³ XCPT_LIMIT_ACCESS 0x10 ³ ³ ³
  255. ³ ExceptionInfo[1] - FaultAddr ³ ³ ³
  256. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  257. ³XCPT_INTEGER_DIVIDE_BY_ZERO ³0xC000009B³0 ³
  258. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  259. ³XCPT_FLOAT_DIVIDE_BY_ZERO ³0xC0000095³0x10 ³
  260. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  261. ³XCPT_FLOAT_INVALID_OPERATION ³0xC0000097³0x10 ³
  262. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  263. ³XCPT_ILLEGAL_INSTRUCTION ³0xC000001C³0x06 ³
  264. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  265. ³XCPT_PRIVILEGED_INSTRUCTION ³0xC000009D³0x0D ³
  266. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  267. ³XCPT_INTEGER_OVERFLOW ³0xC000009C³0x04 ³
  268. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  269. ³XCPT_FLOAT_OVERFLOW ³0xC0000098³0x10 ³
  270. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  271. ³XCPT_FLOAT_UNDERFLOW ³0xC000009A³0x10 ³
  272. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  273. ³XCPT_FLOAT_DENORMAL_OPERAND ³0xC0000094³0x10 ³
  274. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  275. ³XCPT_FLOAT_INEXACT_RESULT ³0xC0000096³0x10 ³
  276. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  277. ³XCPT_FLOAT_STACK_CHECK ³0xC0000099³0x10 ³
  278. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  279. ³XCPT_DATATYPE_MISALIGNMENT ³0xC000009E³0x11 ³
  280. ³ ExceptionInfo[0] - R/W flag ³ ³ ³
  281. ³ ExceptionInfo[1] - Alignment ³ ³ ³
  282. ³ ExceptionInfo[2] - FaultAddr ³ ³ ³
  283. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  284. ³XCPT_BREAKPOINT ³0xC000009F³0x03 ³
  285. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  286. ³XCPT_SINGLE_STEP ³0xC00000A0³0x01 ³
  287. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  288. Portable, Fatal, Software-Generated Exceptions
  289. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  290. ³Exception Name ³Value ³Related Trap ³
  291. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  292. ³XCPT_IN_PAGE_ERROR ³0xC0000006³0x0E ³
  293. ³ ExceptionInfo[0] - FaultAddr ³ ³ ³
  294. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  295. ³XCPT_PROCESS_TERMINATE ³0xC0010001³ ³
  296. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  297. ³XCPT_ASYNC_PROCESS_TERMINATE ³0xC0010002³ ³
  298. ³ ExceptionInfo[0] - TID of ³ ³ ³
  299. ³ terminating thread ³ ³ ³
  300. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  301. ³XCPT_NONCONTINUABLE_EXCEPTION ³0xC0000024³ ³
  302. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  303. ³XCPT_INVALID_DISPOSITION ³0xC0000025³ ³
  304. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  305. Non-Portable, Fatal Exceptions
  306. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  307. ³Exception Name ³Value ³Related Trap ³
  308. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  309. ³XCPT_INVALID_LOCK_SEQUENCE ³0xC000001D³ ³
  310. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´
  311. ³XCPT_ARRAY_BOUNDS_EXCEEDED ³0xC0000093³0x05 ³
  312. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  313. Unwind Operation Exceptions
  314. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ¿
  315. ³Exception Name ³Value ³
  316. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  317. ³XCPT_UNWIND ³0xC0000026³
  318. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  319. ³XCPT_BAD_STACK ³0xC0000027³
  320. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  321. ³XCPT_INVALID_UNWIND_TARGET ³0xC0000028³
  322. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÙ
  323. Fatal Signal Exceptions
  324. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ¿
  325. ³Exception Name ³Value ³
  326. ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´
  327. ³XCPT_SIGNAL ³0xC0010003³
  328. ³ ExceptionInfo[ 0 ] - Signal ³ ³
  329. ³ Number ³ ³
  330. ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÙ
  331. *)
  332. {$ENDIF OS2EXCEPTIONS}
  333. {****************************************************************************
  334. Miscellaneous related routines.
  335. ****************************************************************************}
  336. procedure system_exit;
  337. begin
  338. DosExit(1{process}, exitcode);
  339. end;
  340. {$ASMMODE ATT}
  341. function paramcount:longint;assembler;
  342. asm
  343. movl argc,%eax
  344. decl %eax
  345. end {['EAX']};
  346. function args:pointer;assembler;
  347. asm
  348. movl argv,%eax
  349. end {['EAX']};
  350. function paramstr(l:longint):string;
  351. var p:^Pchar;
  352. begin
  353. if (l>=0) and (l<=paramcount) then
  354. begin
  355. p:=args;
  356. paramstr:=strpas(p[l]);
  357. end
  358. else paramstr:='';
  359. end;
  360. procedure randomize;
  361. var
  362. dt: TSysDateTime;
  363. begin
  364. // Hmm... Lets use timer
  365. DosGetDateTime(dt);
  366. randseed:=dt.hour+(dt.minute shl 8)+(dt.second shl 16)+(dt.sec100 shl 32);
  367. end;
  368. {$ASMMODE ATT}
  369. {****************************************************************************
  370. Heap management releated routines.
  371. ****************************************************************************}
  372. {Get some memory.
  373. P = Pointer to memory will be returned here.
  374. Size = Number of bytes to get. The size is rounded up to a multiple
  375. of 4096. This is probably not the case on non-intel 386
  376. versions of OS/2.
  377. Flags = One or more of the mfXXXX constants.}
  378. function DosAllocMem(var P:pointer;Size,Flag:cardinal):longint; cdecl;
  379. external 'DOSCALLS' index 299;
  380. function DosSetMem(P:pointer;Size,Flag:cardinal):longint; cdecl;
  381. external 'DOSCALLS' index 305;
  382. var
  383. int_heap: pointer;
  384. int_heap_end: pointer;
  385. function sbrk(size:longint):pointer;
  386. var
  387. p: pointer;
  388. rc: longint;
  389. begin
  390. {$IFDEF DUMPGROW}
  391. WriteLn ('Trying to grow heap by ', Size, ' to ', HeapSize + Size);
  392. {$ENDIF}
  393. p:=int_heap_end;
  394. // commit memory
  395. rc:=DosSetMem(p, size, $10+3);
  396. if rc<>0 then p:=nil;
  397. {$IFDEF DUMPGROW}
  398. WriteLn ('New heap at ', Cardinal(p));
  399. {$ENDIF}
  400. sbrk:=int_heap_end;
  401. inc(int_heap_end, size);
  402. end;
  403. function getheapstart:pointer;
  404. begin
  405. getheapstart:=int_heap;
  406. end;
  407. function getheapsize:longint;
  408. begin
  409. getheapsize:=longint(int_heap_end)-longint(int_heap);
  410. end;
  411. {$i heap.inc}
  412. {****************************************************************************
  413. Low Level File Routines
  414. ****************************************************************************}
  415. procedure allowslash(p:Pchar);
  416. {Allow slash as backslash.}
  417. var i:longint;
  418. begin
  419. for i:=0 to strlen(p) do
  420. if p[i]='/' then p[i]:='\';
  421. end;
  422. procedure do_close(h:longint);
  423. begin
  424. { Only three standard handles under real OS/2 }
  425. if h>2 then
  426. begin
  427. InOutRes:=DosClose(h);
  428. end;
  429. {$ifdef IODEBUG}
  430. writeln('do_close: handle=', H, ', InOutRes=', InOutRes);
  431. {$endif}
  432. end;
  433. procedure do_erase(p:Pchar);
  434. begin
  435. allowslash(p);
  436. inoutres:=DosDelete(p);
  437. end;
  438. procedure do_rename(p1,p2:Pchar);
  439. begin
  440. allowslash(p1);
  441. allowslash(p2);
  442. inoutres:=DosMove(p1, p2);
  443. end;
  444. function do_read(h,addr,len:longint):longint;
  445. Var
  446. T: Longint;
  447. begin
  448. {$ifdef IODEBUG}
  449. write('do_read: handle=', h, ', addr=', addr, ', length=', len);
  450. {$endif}
  451. InOutRes:=DosRead(H, Pointer(Addr), Len, T);
  452. do_read:=T;
  453. {$ifdef IODEBUG}
  454. writeln(', actual_len=', t, ', InOutRes=', InOutRes);
  455. {$endif}
  456. end;
  457. function do_write(h,addr,len:longint) : longint;
  458. Var
  459. T: Longint;
  460. begin
  461. {$ifdef IODEBUG}
  462. write('do_write: handle=', h, ', addr=', addr, ', length=', len);
  463. {$endif}
  464. InOutRes:=DosWrite(H, Pointer(Addr), Len, T);
  465. do_write:=T;
  466. {$ifdef IODEBUG}
  467. writeln(', actual_len=', t, ', InOutRes=', InOutRes);
  468. {$endif}
  469. end;
  470. function do_filepos(handle:longint): longint;
  471. var
  472. PosActual: Longint;
  473. begin
  474. InOutRes:=DosSetFilePtr(Handle, 0, 1, PosActual);
  475. do_filepos:=PosActual;
  476. {$ifdef IODEBUG}
  477. writeln('do_filepos: handle=', Handle, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
  478. {$endif}
  479. end;
  480. procedure do_seek(handle,pos:longint);
  481. var
  482. PosActual: Longint;
  483. begin
  484. InOutRes:=DosSetFilePtr(Handle, Pos, 0 {ZeroBased}, PosActual);
  485. {$ifdef IODEBUG}
  486. writeln('do_seek: handle=', Handle, ', pos=', pos, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
  487. {$endif}
  488. end;
  489. function do_seekend(handle:longint):longint;
  490. var
  491. PosActual: Longint;
  492. begin
  493. InOutRes:=DosSetFilePtr(Handle, 0, 2 {EndBased}, PosActual);
  494. do_seekend:=PosActual;
  495. {$ifdef IODEBUG}
  496. writeln('do_seekend: handle=', Handle, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
  497. {$endif}
  498. end;
  499. function do_filesize(handle:longint):longint;
  500. var aktfilepos:longint;
  501. begin
  502. aktfilepos:=do_filepos(handle);
  503. do_filesize:=do_seekend(handle);
  504. do_seek(handle,aktfilepos);
  505. end;
  506. procedure do_truncate(handle,pos:longint);
  507. begin
  508. InOutRes:=DosSetFileSize(Handle, Pos);
  509. do_seekend(handle);
  510. end;
  511. const
  512. FileHandleCount: cardinal = 20;
  513. function Increase_File_Handle_Count: boolean;
  514. var Err: word;
  515. L1: longint;
  516. L2: cardinal;
  517. begin
  518. L1 := 10;
  519. if DosSetRelMaxFH (L1, L2) <> 0 then
  520. Increase_File_Handle_Count := false
  521. else
  522. if L2 > FileHandleCount then
  523. begin
  524. FileHandleCount := L2;
  525. Increase_File_Handle_Count := true;
  526. end
  527. else
  528. Increase_File_Handle_Count := false;
  529. end;
  530. procedure do_open(var f;p:pchar;flags:longint);
  531. {
  532. filerec and textrec have both handle and mode as the first items so
  533. they could use the same routine for opening/creating.
  534. when (flags and $100) the file will be append
  535. when (flags and $1000) the file will be truncate/rewritten
  536. when (flags and $10000) there is no check for close (needed for textfiles)
  537. }
  538. var
  539. Action, Attrib, OpenFlags, FM: Cardinal;
  540. begin
  541. // convert unix slashes to normal slashes
  542. allowslash(p);
  543. // close first if opened
  544. if ((flags and $10000)=0) then
  545. begin
  546. case filerec(f).mode of
  547. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  548. fmclosed:;
  549. else
  550. begin
  551. inoutres:=102; {not assigned}
  552. exit;
  553. end;
  554. end;
  555. end;
  556. // reset file handle
  557. filerec(f).handle := UnusedHandle;
  558. Attrib:=0;
  559. OpenFlags:=0;
  560. // convert filesharing
  561. FM := Flags and $FF and not (8);
  562. (* DenyNone if sharing not specified. *)
  563. if FM and 112 = 0 then
  564. FM := FM or 64;
  565. // convert filemode to filerec modes and access mode
  566. case (FM and 3) of
  567. 0: filerec(f).mode:=fminput;
  568. 1: filerec(f).mode:=fmoutput;
  569. 2: filerec(f).mode:=fminout;
  570. end;
  571. if (flags and $1000)<>0 then
  572. OpenFlags:=OpenFlags or 2 {doOverwrite} or 16 {doCreate} // Create/overwrite
  573. else
  574. OpenFlags:=OpenFlags or 1 {doOpen}; // Open existing
  575. // Handle Std I/O
  576. if p[0]=#0 then
  577. begin
  578. case FileRec(f).mode of
  579. fminput :
  580. FileRec(f).Handle:=StdInputHandle;
  581. fminout, // this is set by rewrite
  582. fmoutput :
  583. FileRec(f).Handle:=StdOutputHandle;
  584. fmappend :
  585. begin
  586. FileRec(f).Handle:=StdOutputHandle;
  587. FileRec(f).mode:=fmoutput; // fool fmappend
  588. end;
  589. end;
  590. exit;
  591. end;
  592. Attrib:=32 {faArchive};
  593. InOutRes:=DosOpen(p, FileRec(F).Handle, Action, 0, Attrib, OpenFlags, FM, nil);
  594. // If too many open files try to set more file handles and open again
  595. if (InOutRes = 4) then
  596. if Increase_File_Handle_Count then
  597. InOutRes:=DosOpen(p, FileRec(F).Handle, Action, 0, Attrib, OpenFlags, FM, nil);
  598. If InOutRes<>0 then FileRec(F).Handle:=UnusedHandle;
  599. // If Handle created -> make some things
  600. if (FileRec(F).Handle <> UnusedHandle) then
  601. begin
  602. // Move to end of file for Append command
  603. if ((Flags and $100) <> 0) then
  604. begin
  605. do_seekend(FileRec(F).Handle);
  606. FileRec(F).Mode := fmOutput;
  607. end;
  608. end;
  609. {$ifdef IODEBUG}
  610. writeln('do_open,', filerec(f).handle, ',', filerec(f).name, ',', filerec(f).mode, ', InOutRes=', InOutRes);
  611. {$endif}
  612. end;
  613. function do_isdevice (Handle: longint): boolean;
  614. var
  615. HT, Attr: longint;
  616. begin
  617. do_isdevice:=false;
  618. If DosQueryHType(Handle, HT, Attr)<>0 then exit;
  619. if ht=1 then do_isdevice:=true;
  620. end;
  621. {$ASMMODE ATT}
  622. {*****************************************************************************
  623. UnTyped File Handling
  624. *****************************************************************************}
  625. {$i file.inc}
  626. {*****************************************************************************
  627. Typed File Handling
  628. *****************************************************************************}
  629. {$i typefile.inc}
  630. {*****************************************************************************
  631. Text File Handling
  632. *****************************************************************************}
  633. {$DEFINE EOF_CTRLZ}
  634. {$i text.inc}
  635. {****************************************************************************
  636. Directory related routines.
  637. ****************************************************************************}
  638. {*****************************************************************************
  639. Directory Handling
  640. *****************************************************************************}
  641. procedure MkDir (const S: string);[IOCHECK];
  642. var buffer:array[0..255] of char;
  643. Rc : word;
  644. begin
  645. If (s='') or (InOutRes <> 0) then
  646. exit;
  647. move(s[1],buffer,length(s));
  648. buffer[length(s)]:=#0;
  649. allowslash(Pchar(@buffer));
  650. Rc := DosCreateDir(buffer,nil);
  651. if Rc <> 0 then
  652. begin
  653. InOutRes := Rc;
  654. Errno2Inoutres;
  655. end;
  656. end;
  657. procedure rmdir(const s : string);[IOCHECK];
  658. var buffer:array[0..255] of char;
  659. Rc : word;
  660. begin
  661. if (s = '.' ) then
  662. InOutRes := 16;
  663. If (s='') or (InOutRes <> 0) then
  664. exit;
  665. move(s[1],buffer,length(s));
  666. buffer[length(s)]:=#0;
  667. allowslash(Pchar(@buffer));
  668. Rc := DosDeleteDir(buffer);
  669. if Rc <> 0 then
  670. begin
  671. InOutRes := Rc;
  672. Errno2Inoutres;
  673. end;
  674. end;
  675. {$ASMMODE INTEL}
  676. procedure ChDir (const S: string);[IOCheck];
  677. var RC: cardinal;
  678. Buffer: array [0..255] of char;
  679. begin
  680. If (s='') or (InOutRes <> 0) then exit;
  681. if (Length (S) >= 2) and (S [2] = ':') then
  682. begin
  683. RC := DosSetDefaultDisk ((Ord (S [1]) and not ($20)) - $40);
  684. if RC <> 0 then
  685. InOutRes := RC
  686. else
  687. if Length (S) > 2 then
  688. begin
  689. Move (S [1], Buffer, Length (S));
  690. Buffer [Length (S)] := #0;
  691. AllowSlash (PChar (@Buffer));
  692. RC := DosSetCurrentDir (@Buffer);
  693. if RC <> 0 then
  694. begin
  695. InOutRes := RC;
  696. Errno2InOutRes;
  697. end;
  698. end;
  699. end else begin
  700. Move (S [1], Buffer, Length (S));
  701. Buffer [Length (S)] := #0;
  702. AllowSlash (PChar (@Buffer));
  703. RC := DosSetCurrentDir (@Buffer);
  704. if RC <> 0 then
  705. begin
  706. InOutRes:= RC;
  707. Errno2InOutRes;
  708. end;
  709. end;
  710. end;
  711. {$ASMMODE ATT}
  712. procedure GetDir (DriveNr: byte; var Dir: ShortString);
  713. {Written by Michael Van Canneyt.}
  714. var sof: Pchar;
  715. i:byte;
  716. l,l2:cardinal;
  717. begin
  718. Dir [4] := #0;
  719. { Used in case the specified drive isn't available }
  720. sof:=pchar(@dir[4]);
  721. { dir[1..3] will contain '[drivenr]:\', but is not }
  722. { supplied by DOS, so we let dos string start at }
  723. { dir[4] }
  724. { Get dir from drivenr : 0=default, 1=A etc... }
  725. l:=255-3;
  726. InOutRes:=longint (DosQueryCurrentDir(DriveNr, sof^, l));
  727. {$WARNING Result code should be translated in some cases!}
  728. { Now Dir should be filled with directory in ASCIIZ, }
  729. { starting from dir[4] }
  730. dir[0]:=#3;
  731. dir[2]:=':';
  732. dir[3]:='\';
  733. i:=4;
  734. {Conversion to Pascal string }
  735. while (dir[i]<>#0) do
  736. begin
  737. { convert path name to DOS }
  738. if dir[i]='/' then
  739. dir[i]:='\';
  740. dir[0]:=char(i);
  741. inc(i);
  742. end;
  743. { upcase the string (FPC function) }
  744. if drivenr<>0 then { Drive was supplied. We know it }
  745. dir[1]:=chr(64+drivenr)
  746. else
  747. begin
  748. { We need to get the current drive from DOS function 19H }
  749. { because the drive was the default, which can be unknown }
  750. DosQueryCurrentDisk(l, l2);
  751. dir[1]:=chr(64+l);
  752. end;
  753. if not (FileNameCaseSensitive) then dir:=upcase(dir);
  754. end;
  755. {*****************************************************************************
  756. System unit initialization.
  757. ****************************************************************************}
  758. {****************************************************************************
  759. Error Message writing using messageboxes
  760. ****************************************************************************}
  761. type
  762. TWinMessageBox = function (Parent, Owner: cardinal;
  763. BoxText, BoxTitle: PChar; Identity, Style: cardinal): cardinal; cdecl;
  764. TWinInitialize = function (Options: cardinal): cardinal; cdecl;
  765. TWinCreateMsgQueue = function (Handle: cardinal; cmsg: longint): cardinal;
  766. cdecl;
  767. const
  768. ErrorBufferLength = 1024;
  769. mb_OK = $0000;
  770. mb_Error = $0040;
  771. mb_Moveable = $4000;
  772. MBStyle = mb_OK or mb_Error or mb_Moveable;
  773. WinInitialize: TWinInitialize = nil;
  774. WinCreateMsgQueue: TWinCreateMsgQueue = nil;
  775. WinMessageBox: TWinMessageBox = nil;
  776. EnvSize: cardinal = 0;
  777. var
  778. ErrorBuf: array [0..ErrorBufferLength] of char;
  779. ErrorLen: longint;
  780. PMWinHandle: cardinal;
  781. function ErrorWrite (var F: TextRec): integer;
  782. {
  783. An error message should always end with #13#10#13#10
  784. }
  785. var
  786. P: PChar;
  787. I: longint;
  788. begin
  789. if F.BufPos > 0 then
  790. begin
  791. if F.BufPos + ErrorLen > ErrorBufferLength then
  792. I := ErrorBufferLength - ErrorLen
  793. else
  794. I := F.BufPos;
  795. Move (F.BufPtr^, ErrorBuf [ErrorLen], I);
  796. Inc (ErrorLen, I);
  797. ErrorBuf [ErrorLen] := #0;
  798. end;
  799. if ErrorLen > 3 then
  800. begin
  801. P := @ErrorBuf [ErrorLen];
  802. for I := 1 to 4 do
  803. begin
  804. Dec (P);
  805. if not (P^ in [#10, #13]) then
  806. break;
  807. end;
  808. end;
  809. if ErrorLen = ErrorBufferLength then
  810. I := 4;
  811. if (I = 4) then
  812. begin
  813. WinMessageBox (0, 0, @ErrorBuf, PChar ('Error'), 0, MBStyle);
  814. ErrorLen := 0;
  815. end;
  816. F.BufPos := 0;
  817. ErrorWrite := 0;
  818. end;
  819. function ErrorClose (var F: TextRec): integer;
  820. begin
  821. if ErrorLen > 0 then
  822. begin
  823. WinMessageBox (0, 0, @ErrorBuf, PChar ('Error'), 0, MBStyle);
  824. ErrorLen := 0;
  825. end;
  826. ErrorLen := 0;
  827. ErrorClose := 0;
  828. end;
  829. function ErrorOpen (var F: TextRec): integer;
  830. begin
  831. TextRec(F).InOutFunc := @ErrorWrite;
  832. TextRec(F).FlushFunc := @ErrorWrite;
  833. TextRec(F).CloseFunc := @ErrorClose;
  834. ErrorOpen := 0;
  835. end;
  836. procedure AssignError (var T: Text);
  837. begin
  838. Assign (T, '');
  839. TextRec (T).OpenFunc := @ErrorOpen;
  840. Rewrite (T);
  841. end;
  842. procedure SysInitStdIO;
  843. begin
  844. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  845. displayed in a messagebox }
  846. (*
  847. StdInputHandle := longint(GetStdHandle(cardinal(STD_INPUT_HANDLE)));
  848. StdOutputHandle := longint(GetStdHandle(cardinal(STD_OUTPUT_HANDLE)));
  849. StdErrorHandle := longint(GetStdHandle(cardinal(STD_ERROR_HANDLE)));
  850. if not IsConsole then
  851. begin
  852. if (DosLoadModule (nil, 0, 'PMWIN', PMWinHandle) = 0) and
  853. (DosQueryProcAddr (PMWinHandle, 789, nil, pointer (WinMessageBox)) = 0)
  854. and
  855. (DosQueryProcAddr (PMWinHandle, 763, nil, pointer (WinInitialize)) = 0)
  856. and
  857. (DosQueryProcAddr (PMWinHandle, 716, nil, pointer (WinCreateMsgQueue))
  858. = 0)
  859. then
  860. begin
  861. WinInitialize (0);
  862. WinCreateMsgQueue (0, 0);
  863. end
  864. else
  865. HandleError (2);
  866. AssignError (StdErr);
  867. AssignError (StdOut);
  868. Assign (Output, '');
  869. Assign (Input, '');
  870. end
  871. else
  872. begin
  873. *)
  874. OpenStdIO (Input, fmInput, StdInputHandle);
  875. OpenStdIO (Output, fmOutput, StdOutputHandle);
  876. OpenStdIO (StdOut, fmOutput, StdOutputHandle);
  877. OpenStdIO (StdErr, fmOutput, StdErrorHandle);
  878. (*
  879. end;
  880. *)
  881. end;
  882. function strcopy(dest,source : pchar) : pchar;assembler;
  883. var
  884. saveeax,saveesi,saveedi : longint;
  885. asm
  886. movl %edi,saveedi
  887. movl %esi,saveesi
  888. {$ifdef REGCALL}
  889. movl %eax,saveeax
  890. movl %edx,%edi
  891. {$else}
  892. movl source,%edi
  893. {$endif}
  894. testl %edi,%edi
  895. jz .LStrCopyDone
  896. leal 3(%edi),%ecx
  897. andl $-4,%ecx
  898. movl %edi,%esi
  899. subl %edi,%ecx
  900. {$ifdef REGCALL}
  901. movl %eax,%edi
  902. {$else}
  903. movl dest,%edi
  904. {$endif}
  905. jz .LStrCopyAligned
  906. .LStrCopyAlignLoop:
  907. movb (%esi),%al
  908. incl %edi
  909. incl %esi
  910. testb %al,%al
  911. movb %al,-1(%edi)
  912. jz .LStrCopyDone
  913. decl %ecx
  914. jnz .LStrCopyAlignLoop
  915. .balign 16
  916. .LStrCopyAligned:
  917. movl (%esi),%eax
  918. movl %eax,%edx
  919. leal 0x0fefefeff(%eax),%ecx
  920. notl %edx
  921. addl $4,%esi
  922. andl %edx,%ecx
  923. andl $0x080808080,%ecx
  924. jnz .LStrCopyEndFound
  925. movl %eax,(%edi)
  926. addl $4,%edi
  927. jmp .LStrCopyAligned
  928. .LStrCopyEndFound:
  929. testl $0x0ff,%eax
  930. jz .LStrCopyByte
  931. testl $0x0ff00,%eax
  932. jz .LStrCopyWord
  933. testl $0x0ff0000,%eax
  934. jz .LStrCopy3Bytes
  935. movl %eax,(%edi)
  936. jmp .LStrCopyDone
  937. .LStrCopy3Bytes:
  938. xorb %dl,%dl
  939. movw %ax,(%edi)
  940. movb %dl,2(%edi)
  941. jmp .LStrCopyDone
  942. .LStrCopyWord:
  943. movw %ax,(%edi)
  944. jmp .LStrCopyDone
  945. .LStrCopyByte:
  946. movb %al,(%edi)
  947. .LStrCopyDone:
  948. {$ifdef REGCALL}
  949. movl saveeax,%eax
  950. {$else}
  951. movl dest,%eax
  952. {$endif}
  953. movl saveedi,%edi
  954. movl saveesi,%esi
  955. end;
  956. procedure InitEnvironment;
  957. var env_count : longint;
  958. dos_env,cp : pchar;
  959. begin
  960. env_count:=0;
  961. cp:=environment;
  962. while cp ^ <> #0 do
  963. begin
  964. inc(env_count);
  965. while (cp^ <> #0) do inc(longint(cp)); { skip to NUL }
  966. inc(longint(cp)); { skip to next character }
  967. end;
  968. envp := sysgetmem((env_count+1) * sizeof(pchar));
  969. envc := env_count;
  970. if (envp = nil) then exit;
  971. cp:=environment;
  972. env_count:=0;
  973. while cp^ <> #0 do
  974. begin
  975. envp[env_count] := sysgetmem(strlen(cp)+1);
  976. strcopy(envp[env_count], cp);
  977. {$IfDef DEBUGENVIRONMENT}
  978. Writeln(stderr,'env ',env_count,' = "',envp[env_count],'"');
  979. {$EndIf}
  980. inc(env_count);
  981. while (cp^ <> #0) do
  982. inc(longint(cp)); { skip to NUL }
  983. inc(longint(cp)); { skip to next character }
  984. end;
  985. envp[env_count]:=nil;
  986. end;
  987. procedure InitArguments;
  988. var
  989. arglen,
  990. count : longint;
  991. argstart,
  992. pc,arg : pchar;
  993. quote : char;
  994. argvlen : longint;
  995. procedure allocarg(idx,len:longint);
  996. begin
  997. if idx>=argvlen then
  998. begin
  999. argvlen:=(idx+8) and (not 7);
  1000. sysreallocmem(argv,argvlen*sizeof(pointer));
  1001. end;
  1002. { use realloc to reuse already existing memory }
  1003. { always allocate, even if length is zero, since }
  1004. { the arg. is still present! }
  1005. sysreallocmem(argv[idx],len+1);
  1006. end;
  1007. begin
  1008. count:=0;
  1009. argv:=nil;
  1010. argvlen:=0;
  1011. // Get argv[0]
  1012. pc:=cmdline;
  1013. Arglen:=0;
  1014. repeat
  1015. Inc(Arglen);
  1016. until (pc[Arglen]=#0);
  1017. allocarg(count,arglen);
  1018. move(pc^,argv[count]^,arglen);
  1019. { ReSetup cmdline variable }
  1020. repeat
  1021. Inc(Arglen);
  1022. until (pc[Arglen]=#0);
  1023. Inc(Arglen);
  1024. pc:=GetMem(ArgLen);
  1025. move(cmdline^, pc^, arglen);
  1026. Arglen:=0;
  1027. repeat
  1028. Inc(Arglen);
  1029. until (pc[Arglen]=#0);
  1030. pc[Arglen]:=' '; // combine argv[0] and command line
  1031. CmdLine:=pc;
  1032. { process arguments }
  1033. pc:=cmdline;
  1034. {$IfDef DEBUGARGUMENTS}
  1035. Writeln(stderr,'GetCommandLine is #',pc,'#');
  1036. {$EndIf }
  1037. while pc^<>#0 do
  1038. begin
  1039. { skip leading spaces }
  1040. while pc^ in [#1..#32] do
  1041. inc(pc);
  1042. if pc^=#0 then
  1043. break;
  1044. { calc argument length }
  1045. quote:=' ';
  1046. argstart:=pc;
  1047. arglen:=0;
  1048. while (pc^<>#0) do
  1049. begin
  1050. case pc^ of
  1051. #1..#32 :
  1052. begin
  1053. if quote<>' ' then
  1054. inc(arglen)
  1055. else
  1056. break;
  1057. end;
  1058. '"' :
  1059. begin
  1060. if quote<>'''' then
  1061. begin
  1062. if pchar(pc+1)^<>'"' then
  1063. begin
  1064. if quote='"' then
  1065. quote:=' '
  1066. else
  1067. quote:='"';
  1068. end
  1069. else
  1070. inc(pc);
  1071. end
  1072. else
  1073. inc(arglen);
  1074. end;
  1075. '''' :
  1076. begin
  1077. if quote<>'"' then
  1078. begin
  1079. if pchar(pc+1)^<>'''' then
  1080. begin
  1081. if quote='''' then
  1082. quote:=' '
  1083. else
  1084. quote:='''';
  1085. end
  1086. else
  1087. inc(pc);
  1088. end
  1089. else
  1090. inc(arglen);
  1091. end;
  1092. else
  1093. inc(arglen);
  1094. end;
  1095. inc(pc);
  1096. end;
  1097. { copy argument }
  1098. { Don't copy the first one, it is already there.}
  1099. If Count<>0 then
  1100. begin
  1101. allocarg(count,arglen);
  1102. quote:=' ';
  1103. pc:=argstart;
  1104. arg:=argv[count];
  1105. while (pc^<>#0) do
  1106. begin
  1107. case pc^ of
  1108. #1..#32 :
  1109. begin
  1110. if quote<>' ' then
  1111. begin
  1112. arg^:=pc^;
  1113. inc(arg);
  1114. end
  1115. else
  1116. break;
  1117. end;
  1118. '"' :
  1119. begin
  1120. if quote<>'''' then
  1121. begin
  1122. if pchar(pc+1)^<>'"' then
  1123. begin
  1124. if quote='"' then
  1125. quote:=' '
  1126. else
  1127. quote:='"';
  1128. end
  1129. else
  1130. inc(pc);
  1131. end
  1132. else
  1133. begin
  1134. arg^:=pc^;
  1135. inc(arg);
  1136. end;
  1137. end;
  1138. '''' :
  1139. begin
  1140. if quote<>'"' then
  1141. begin
  1142. if pchar(pc+1)^<>'''' then
  1143. begin
  1144. if quote='''' then
  1145. quote:=' '
  1146. else
  1147. quote:='''';
  1148. end
  1149. else
  1150. inc(pc);
  1151. end
  1152. else
  1153. begin
  1154. arg^:=pc^;
  1155. inc(arg);
  1156. end;
  1157. end;
  1158. else
  1159. begin
  1160. arg^:=pc^;
  1161. inc(arg);
  1162. end;
  1163. end;
  1164. inc(pc);
  1165. end;
  1166. arg^:=#0;
  1167. end;
  1168. {$IfDef DEBUGARGUMENTS}
  1169. Writeln(stderr,'dos arg ',count,' #',arglen,'#',argv[count],'#');
  1170. {$EndIf}
  1171. inc(count);
  1172. end;
  1173. { get argc and create an nil entry }
  1174. argc:=count;
  1175. allocarg(argc,0);
  1176. { free unused memory }
  1177. sysreallocmem(argv,(argc+1)*sizeof(pointer));
  1178. end;
  1179. function GetFileHandleCount: longint;
  1180. var L1: longint;
  1181. L2: cardinal;
  1182. begin
  1183. L1 := 0; (* Don't change the amount, just check. *)
  1184. if DosSetRelMaxFH (L1, L2) <> 0 then GetFileHandleCount := 50
  1185. else GetFileHandleCount := L2;
  1186. end;
  1187. var TIB: PThreadInfoBlock;
  1188. PIB: PProcessInfoBlock;
  1189. begin
  1190. IsLibrary := FALSE;
  1191. (* Initialize the amount of file handles *)
  1192. FileHandleCount := GetFileHandleCount;
  1193. DosGetInfoBlocks (@TIB, @PIB);
  1194. StackBottom := TIB^.Stack;
  1195. {Set type of application}
  1196. ApplicationType := PIB^.ProcType;
  1197. IsConsole := ApplicationType <> 3;
  1198. exitproc:=nil;
  1199. {Initialize the heap.}
  1200. // Logic is following:
  1201. // Application allocates maximum possible memory array (~512Mb),
  1202. // but without commiting. On heap growing required amount of
  1203. // memory commited. So heap can be grown up to 512Mb.
  1204. // For newer systems maximum ammount of memory block can be
  1205. // 2 Gb, but here used 512 for campatability reasons.
  1206. // Note: Check for higher limit of heap not implemented yet.
  1207. // Note: Check for amount of memory for process not implemented yet.
  1208. // While used hard-coded value of max heapsize (256Mb)
  1209. DosAllocMem(Int_Heap, 256*1024*1024, 3);
  1210. Int_Heap_End:=Int_Heap;
  1211. InitHeap;
  1212. { ... and exceptions }
  1213. SysInitExceptions;
  1214. { ... and I/O }
  1215. SysInitStdIO;
  1216. { no I/O-Error }
  1217. inoutres:=0;
  1218. {Initialize environment (must be after InitHeap because allocates memory)}
  1219. Environment := pointer (PIB^.Env);
  1220. InitEnvironment;
  1221. CmdLine := pointer (PIB^.Cmd);
  1222. InitArguments;
  1223. {$ifdef HASVARIANT}
  1224. initvariantmanager;
  1225. {$endif HASVARIANT}
  1226. {$IFDEF DUMPGROW}
  1227. {$IFDEF CONTHEAP}
  1228. WriteLn ('Initial brk size is ', GetHeapSize);
  1229. // WriteLn ('Brk limit is ', BrkLimit);
  1230. {$ENDIF CONTHEAP}
  1231. {$ENDIF DUMPGROW}
  1232. end.
  1233. {
  1234. $Log$
  1235. Revision 1.61 2003-12-04 21:22:38 peter
  1236. * regcall updates (untested)
  1237. Revision 1.60 2003/11/23 07:21:16 yuri
  1238. * native heap
  1239. Revision 1.59 2003/11/19 18:21:11 yuri
  1240. * Memory allocation bug fixed
  1241. Revision 1.58 2003/11/19 16:50:21 yuri
  1242. * Environment and arguments initialization now native
  1243. Revision 1.57 2003/11/06 17:20:44 yuri
  1244. * Unused constants removed
  1245. Revision 1.56 2003/11/03 09:42:28 marco
  1246. * Peter's Cardinal<->Longint fixes patch
  1247. Revision 1.55 2003/11/02 00:51:17 hajny
  1248. * corrections for do_open and os_mode back
  1249. Revision 1.54 2003/10/28 14:57:31 yuri
  1250. * do_* functions now native
  1251. Revision 1.53 2003/10/27 04:33:58 yuri
  1252. * os_mode removed (not required anymore)
  1253. Revision 1.52 2003/10/25 22:45:37 hajny
  1254. * file handling related fixes
  1255. Revision 1.51 2003/10/19 12:13:41 hajny
  1256. * UnusedHandle value made the same as with other targets
  1257. Revision 1.50 2003/10/19 09:37:00 hajny
  1258. * minor fix in non-default sbrk code
  1259. Revision 1.49 2003/10/19 09:06:28 hajny
  1260. * fix for terrible long-time bug in do_open
  1261. Revision 1.48 2003/10/18 16:58:39 hajny
  1262. * stdcall fixes again
  1263. Revision 1.47 2003/10/16 15:43:13 peter
  1264. * THandle is platform dependent
  1265. Revision 1.46 2003/10/14 21:10:06 hajny
  1266. * another longint2cardinal fix
  1267. Revision 1.45 2003/10/13 21:17:31 hajny
  1268. * longint to cardinal corrections
  1269. Revision 1.44 2003/10/12 18:07:30 hajny
  1270. * wrong use of Intel syntax
  1271. Revision 1.43 2003/10/12 17:59:40 hajny
  1272. * wrong use of Intel syntax
  1273. Revision 1.42 2003/10/12 17:52:28 hajny
  1274. * wrong use of Intel syntax
  1275. Revision 1.41 2003/10/12 10:45:36 hajny
  1276. * sbrk error handling corrected
  1277. Revision 1.40 2003/10/07 21:26:35 hajny
  1278. * stdcall fixes and asm routines cleanup
  1279. Revision 1.39 2003/10/06 16:58:27 yuri
  1280. * Another set of native functions.
  1281. Revision 1.38 2003/10/06 14:22:40 yuri
  1282. * Some emx code removed. Now withous so stupid error as with dos ;)
  1283. Revision 1.37 2003/10/04 08:30:59 yuri
  1284. * at&t syntax instead of intel syntax was used
  1285. Revision 1.36 2003/10/03 21:46:41 peter
  1286. * stdcall fixes
  1287. Revision 1.35 2003/10/01 18:42:49 yuri
  1288. * Unclosed comment
  1289. Revision 1.34 2003/09/29 18:39:59 hajny
  1290. * append fix applied to GO32v2, OS/2 and EMX
  1291. Revision 1.33 2003/09/27 11:52:36 peter
  1292. * sbrk returns pointer
  1293. Revision 1.32 2003/03/30 09:20:30 hajny
  1294. * platform extension unification
  1295. Revision 1.31 2003/01/15 22:16:12 hajny
  1296. * default sharing mode changed to DenyNone
  1297. Revision 1.30 2002/12/15 22:41:41 hajny
  1298. * First_Meg fixed + Environment initialization under Dos
  1299. Revision 1.29 2002/12/08 16:39:58 hajny
  1300. - WriteLn in GUI mode support commented out until fixed
  1301. Revision 1.28 2002/12/07 19:17:14 hajny
  1302. * GetEnv correction, better PM support, ...
  1303. Revision 1.27 2002/11/17 22:31:02 hajny
  1304. * type corrections (longint x cardinal)
  1305. Revision 1.26 2002/10/27 14:29:00 hajny
  1306. * heap management (hopefully) fixed
  1307. Revision 1.25 2002/10/14 19:39:17 peter
  1308. * threads unit added for thread support
  1309. Revision 1.24 2002/10/13 09:28:45 florian
  1310. + call to initvariantmanager inserted
  1311. Revision 1.23 2002/09/07 16:01:25 peter
  1312. * old logs removed and tabs fixed
  1313. Revision 1.22 2002/07/01 16:29:05 peter
  1314. * sLineBreak changed to normal constant like Kylix
  1315. Revision 1.21 2002/04/21 15:54:20 carl
  1316. + initialize some global variables
  1317. Revision 1.20 2002/04/12 17:42:16 carl
  1318. + generic stack checking
  1319. Revision 1.19 2002/03/11 19:10:33 peter
  1320. * Regenerated with updated fpcmake
  1321. Revision 1.18 2002/02/10 13:46:20 hajny
  1322. * heap management corrected (heap_brk)
  1323. }