system.pas 46 KB

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