syspara.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2016 by Marcus Sackrow and Karoly Balogh
  4. members of the Free Pascal development team.
  5. Command line parameter handling for Atari
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. (* define this symbol to get ARGV argument passing that's strictly
  13. * compatible with the Atari standard. If it's not defined, then
  14. * the startup code won't validate the ARGV= variable by checking
  15. * the command byte for 127. Note that there are still some
  16. * applications (gulam is a notable example) that implement only
  17. * part of the standard and don't set the command byte to 127.
  18. *)
  19. {$IF 0}
  20. {$DEFINE STRICTLY_COMPATIBLE_WITH_STANDARD }
  21. {$ENDIF}
  22. { Generates correct argument array on startup }
  23. procedure GenerateArgs;
  24. var
  25. ArgVLen: LongInt;
  26. LocalIndex: Word;
  27. len: Integer;
  28. procedure AllocArg(Idx, Len: LongInt);
  29. var
  30. i, OldArgVLen : LongInt;
  31. begin
  32. if Idx >= ArgVLen then
  33. begin
  34. OldArgVLen := ArgVLen;
  35. ArgVLen := (Idx + 8) and (not 7);
  36. SysReAllocMem(Argv, Argvlen * SizeOf(Pointer));
  37. for i := OldArgVLen to ArgVLen - 1 do
  38. ArgV[i]:=nil;
  39. end;
  40. ArgV[Idx] := SysAllocMem(Succ(Len));
  41. end;
  42. function scan_argv : boolean;
  43. var
  44. hp, start : pchar;
  45. len: integer;
  46. begin
  47. hp:=basepage^.p_env;
  48. result:=false;
  49. if (hp=nil) then
  50. exit;
  51. LocalIndex := 0;
  52. while hp^<>#0 do
  53. begin
  54. if (hp[0] = 'A') and (hp[1] = 'R') and (hp[2] = 'G') and (hp[3] = 'V') and (hp[4] = '=') then
  55. begin
  56. { in any case, terminate environment here }
  57. hp[0] := #0;
  58. hp[1] := #0;
  59. { skip ARGV= string }
  60. hp := hp + 5;
  61. if (hp[0] = 'N') and (hp[1] = 'U') and (hp[2] = 'L') and (hp[3] = 'L') and (hp[4] = ':') then
  62. begin
  63. { TODO: handle NULL arguments }
  64. end;
  65. {$ifdef STRICTLY_COMPATIBLE_WITH_STANDARD}
  66. if (len <> 127) then
  67. exit;
  68. {$endif}
  69. { skip ARGV= value }
  70. while hp^<>#0 do
  71. inc(hp);
  72. inc(hp);
  73. { get arguments }
  74. while hp^<>#0 do
  75. begin
  76. start := hp;
  77. while hp^<>#0 do
  78. inc(hp);
  79. len := hp - start;
  80. allocarg(localindex,len);
  81. move(start^,argv[localindex]^,len);
  82. argv[localindex][len]:=#0;
  83. inc(localindex);
  84. inc(hp);
  85. end;
  86. argc:=localindex;
  87. result := true;
  88. exit;
  89. end;
  90. hp := hp + strlen(hp) + 1;
  91. end;
  92. end;
  93. var
  94. Count: Word;
  95. Start: Word;
  96. Ende: Word;
  97. i: Integer;
  98. P : PChar;
  99. begin
  100. P := Args;
  101. ArgVLen := 0;
  102. { check ARGV usage indicator }
  103. len := ord(P[0]);
  104. if scan_argv then
  105. exit;
  106. { Set argv[0] }
  107. AllocArg(0, 0);
  108. Argv[0][0] := #0;
  109. { just in case; commandline cannot be longer }
  110. if len > 127 then
  111. begin
  112. argc := 1;
  113. exit;
  114. end;
  115. { Handle the other args }
  116. p[len + 1] := #0;
  117. Count := 1;
  118. { first index is one }
  119. LocalIndex := 1;
  120. while (P[Count] <> #0) do
  121. begin
  122. while (P[Count] <> #0) and (p[count]<=#32) do
  123. Inc(count);
  124. if p[count] = '"' then
  125. begin
  126. Inc(Count);
  127. start := count;
  128. while (p[count]<>#0) and (p[count]<>'"') and (p[count]>=#32) do
  129. Inc(Count);
  130. ende := count;
  131. if (p[count] = '"') then
  132. Inc(Count);
  133. end else
  134. begin
  135. start := count;
  136. while (p[count]<>#0) and (p[count]>#32) do
  137. inc(count);
  138. ende := count;
  139. end;
  140. if (ende>start) then
  141. begin
  142. allocarg(localindex,ende-start);
  143. move(p[start],argv[localindex]^,ende-start);
  144. argv[localindex][ende-start]:=#0;
  145. inc(localindex);
  146. end;
  147. end;
  148. argc:=localindex;
  149. end;
  150. {*****************************************************************************
  151. ParamStr
  152. *****************************************************************************}
  153. { number of args }
  154. function ParamCount: LongInt;
  155. begin
  156. ParamCount := argc - 1;
  157. end;
  158. { argument number l }
  159. function ParamStr(l: LongInt): string;
  160. var
  161. s1: string;
  162. begin
  163. ParamStr := '';
  164. if (l >= 0) and (l < argc) then
  165. ParamStr := StrPas(argv[l]);
  166. end;