system.pp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004-2006 by Karoly Balogh
  4. System unit for AmigaOS 3.x/4.x
  5. Uses parts of the Free Pascal 1.0.x for Commodore Amiga/68k port
  6. by Carl Eric Codere and Nils Sjoholm
  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 System;
  14. interface
  15. {$define FPC_IS_SYSTEM}
  16. {$I systemh.inc}
  17. const
  18. LineEnding = #10;
  19. LFNSupport = True;
  20. DirectorySeparator = '/';
  21. DriveSeparator = ':';
  22. PathSeparator = ';';
  23. maxExitCode = 255;
  24. MaxPathLen = 256;
  25. const
  26. UnusedHandle : LongInt = -1;
  27. StdInputHandle : LongInt = 0;
  28. StdOutputHandle : LongInt = 0;
  29. StdErrorHandle : LongInt = 0;
  30. FileNameCaseSensitive : Boolean = False;
  31. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  32. sLineBreak : string[1] = LineEnding;
  33. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  34. BreakOn : Boolean = True;
  35. var
  36. AOS_ExecBase : Pointer; external name '_ExecBase';
  37. AOS_DOSBase : Pointer;
  38. AOS_UtilityBase: Pointer;
  39. {$IFDEF CPUPOWERPC}
  40. {$WARNING iExec, iDOS and iUtility should be typed pointer later}
  41. var
  42. IExec : Pointer; external name '_IExec';
  43. IDOS : Pointer;
  44. IUtility : Pointer;
  45. {$ENDIF}
  46. AOS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  47. AOS_origDir : LongInt; { original directory on startup }
  48. AOS_wbMsg : Pointer;
  49. AOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  50. AOS_ConHandle: LongInt;
  51. argc: LongInt;
  52. argv: PPChar;
  53. envp: PPChar;
  54. implementation
  55. {$I system.inc}
  56. {$IFDEF MOSFPC_FILEDEBUG}
  57. {$WARNING Compiling with file debug enabled!}
  58. {$ENDIF}
  59. {$IFDEF MOSFPC_MEMDEBUG}
  60. {$WARNING Compiling with memory debug enabled!}
  61. {$ENDIF}
  62. {*****************************************************************************
  63. Misc. System Dependent Functions
  64. *****************************************************************************}
  65. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  66. procedure System_exit;
  67. begin
  68. { We must remove the CTRL-C FLAG here because halt }
  69. { may call I/O routines, which in turn might call }
  70. { halt, so a recursive stack crash }
  71. if BreakOn then begin
  72. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  73. SetSignal(0,SIGBREAKF_CTRL_C);
  74. end;
  75. { Closing opened files }
  76. CloseList(AOS_fileList);
  77. { Changing back to original directory if changed }
  78. if AOS_origDir<>0 then begin
  79. CurrentDir(AOS_origDir);
  80. end;
  81. {$IFDEF AMIGAOS4}
  82. if iDOS<>nil then DropInterface(iDOS);
  83. if iUtility<>nil then DropInterface(iUtility);
  84. {$ENDIF}
  85. if AOS_UtilityBase<>nil then CloseLibrary(AOS_UtilityBase);
  86. if AOS_DOSBase<>nil then CloseLibrary(AOS_DOSBase);
  87. if AOS_heapPool<>nil then DeletePool(AOS_heapPool);
  88. haltproc(ExitCode);
  89. end;
  90. { Generates correct argument array on startup }
  91. procedure GenerateArgs;
  92. var
  93. argvlen : longint;
  94. procedure allocarg(idx,len:longint);
  95. var
  96. i,oldargvlen : longint;
  97. begin
  98. if idx>=argvlen then
  99. begin
  100. oldargvlen:=argvlen;
  101. argvlen:=(idx+8) and (not 7);
  102. sysreallocmem(argv,argvlen*sizeof(pointer));
  103. for i:=oldargvlen to argvlen-1 do
  104. argv[i]:=nil;
  105. end;
  106. ArgV [Idx] := SysAllocMem (Succ (Len));
  107. end;
  108. var
  109. count: word;
  110. start: word;
  111. localindex: word;
  112. p : pchar;
  113. temp : string;
  114. begin
  115. p:=GetArgStr;
  116. argvlen:=0;
  117. { Set argv[0] }
  118. temp:=paramstr(0);
  119. allocarg(0,length(temp));
  120. move(temp[1],argv[0]^,length(temp));
  121. argv[0][length(temp)]:=#0;
  122. { check if we're started from Ambient }
  123. if AOS_wbMsg<>nil then
  124. begin
  125. argc:=0;
  126. exit;
  127. end;
  128. { Handle the other args }
  129. count:=0;
  130. { first index is one }
  131. localindex:=1;
  132. while (p[count]<>#0) do
  133. begin
  134. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  135. start:=count;
  136. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  137. if (count-start>0) then
  138. begin
  139. allocarg(localindex,count-start);
  140. move(p[start],argv[localindex]^,count-start);
  141. argv[localindex][count-start]:=#0;
  142. inc(localindex);
  143. end;
  144. end;
  145. argc:=localindex;
  146. end;
  147. function GetProgDir: String;
  148. var
  149. s1 : String;
  150. alock : LongInt;
  151. counter: Byte;
  152. begin
  153. GetProgDir:='';
  154. FillChar(s1,255,#0);
  155. { GetLock of program directory }
  156. alock:=GetProgramDir;
  157. if alock<>0 then begin
  158. if NameFromLock(alock,@s1[1],255) then begin
  159. counter:=1;
  160. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  161. s1[0]:=Char(counter-1);
  162. GetProgDir:=s1;
  163. end;
  164. end;
  165. end;
  166. function GetProgramName: String;
  167. { Returns ONLY the program name }
  168. var
  169. s1 : String;
  170. counter: Byte;
  171. begin
  172. GetProgramName:='';
  173. FillChar(s1,255,#0);
  174. if GetProgramName(@s1[1],255) then begin
  175. { now check out and assign the length of the string }
  176. counter := 1;
  177. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  178. s1[0]:=Char(counter-1);
  179. { now remove any component path which should not be there }
  180. for counter:=length(s1) downto 1 do
  181. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  182. { readjust counterv to point to character }
  183. if counter<>1 then Inc(counter);
  184. GetProgramName:=copy(s1,counter,length(s1));
  185. end;
  186. end;
  187. {*****************************************************************************
  188. ParamStr/Randomize
  189. *****************************************************************************}
  190. { number of args }
  191. function paramcount : longint;
  192. begin
  193. if AOS_wbMsg<>nil then
  194. paramcount:=0
  195. else
  196. paramcount:=argc-1;
  197. end;
  198. { argument number l }
  199. function paramstr(l : longint) : string;
  200. var
  201. s1: String;
  202. begin
  203. paramstr:='';
  204. if AOS_wbMsg<>nil then exit;
  205. if l=0 then begin
  206. s1:=GetProgDir;
  207. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  208. else paramstr:=s1+'/'+GetProgramName;
  209. end else begin
  210. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  211. end;
  212. end;
  213. { set randseed to a new pseudo random value }
  214. procedure randomize;
  215. var tmpTime: TDateStamp;
  216. begin
  217. DateStamp(@tmpTime);
  218. randseed:=tmpTime.ds_tick;
  219. end;
  220. { AmigaOS specific startup }
  221. procedure SysInitAmigaOS;
  222. var self: PProcess;
  223. begin
  224. self:=PProcess(FindTask(nil));
  225. if self^.pr_CLI=0 then begin
  226. { if we're running from Ambient/Workbench, we catch its message }
  227. WaitPort(@self^.pr_MsgPort);
  228. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  229. end;
  230. AOS_DOSBase:=OpenLibrary('dos.library',50);
  231. if AOS_DOSBase=nil then Halt(1);
  232. AOS_UtilityBase:=OpenLibrary('utility.library',50);
  233. if AOS_UtilityBase=nil then Halt(1);
  234. {$IFDEF AMIGAOS4}
  235. { Open main interfaces on OS4 }
  236. iDOS := GetInterface(AOS_DOSBase,'main',1,nil);
  237. iUtility := GetInterface(AOS_UtilityBase,'main',1,nil);
  238. {$ENDIF}
  239. { Creating the memory pool for growing heap }
  240. AOS_heapPool:=CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  241. if AOS_heapPool=nil then Halt(1);
  242. if AOS_wbMsg=nil then begin
  243. StdInputHandle:=dosInput;
  244. StdOutputHandle:=dosOutput;
  245. end else begin
  246. AOS_ConHandle:=Open(AOS_ConName,MODE_OLDFILE);
  247. if AOS_ConHandle<>0 then begin
  248. StdInputHandle:=AOS_ConHandle;
  249. StdOutputHandle:=AOS_ConHandle;
  250. end else
  251. Halt(1);
  252. end;
  253. end;
  254. procedure SysInitStdIO;
  255. begin
  256. OpenStdIO(Input,fmInput,StdInputHandle);
  257. OpenStdIO(Output,fmOutput,StdOutputHandle);
  258. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  259. { * AmigaOS doesn't have a separate stderr * }
  260. StdErrorHandle:=StdOutputHandle;
  261. //OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  262. //OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  263. end;
  264. function GetProcessID: SizeUInt;
  265. begin
  266. GetProcessID:=SizeUInt(FindTask(NIL));
  267. end;
  268. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  269. begin
  270. result := stklen;
  271. end;
  272. begin
  273. SysResetFPU;
  274. IsConsole := TRUE;
  275. IsLibrary := FALSE;
  276. StackLength := CheckInitialStkLen(InitialStkLen);
  277. StackBottom := Sptr - StackLength;
  278. { OS specific startup }
  279. AOS_wbMsg:=nil;
  280. AOS_origDir:=0;
  281. AOS_fileList:=nil;
  282. envp:=nil;
  283. SysInitAmigaOS;
  284. { Set up signals handlers }
  285. // InstallSignals;
  286. { Setup heap }
  287. InitHeap;
  288. SysInitExceptions;
  289. { Setup stdin, stdout and stderr }
  290. SysInitStdIO;
  291. { Reset IO Error }
  292. InOutRes:=0;
  293. { Arguments }
  294. GenerateArgs;
  295. InitSystemThreads;
  296. initvariantmanager;
  297. initwidestringmanager;
  298. end.