paramhandling.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2016 by Marcus Sackrow,
  4. member of the Free Pascal development team.
  5. Parameter handling for Amiga-like systems
  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. function GetWBArgsNum: Integer;
  13. var
  14. Startup: PWBStartup;
  15. begin
  16. GetWBArgsNum := 0;
  17. Startup := nil;
  18. Startup := PWBStartup(AOS_wbMsg);
  19. if Startup <> nil then
  20. begin
  21. Result := Startup^.sm_NumArgs - 1;
  22. end;
  23. end;
  24. function GetWBArg(Idx: Integer): string;
  25. var
  26. startup: PWBStartup;
  27. wbarg: PWBArgList;
  28. Path: array[0..254] of Char;
  29. strPath: string;
  30. Len: Integer;
  31. begin
  32. GetWBArg := '';
  33. FillChar(Path[0],255,#0);
  34. Startup := PWBStartup(AOS_wbMsg);
  35. if Startup <> nil then
  36. begin
  37. //if (Idx >= 0) and (Idx < Startup^.sm_NumArgs) then
  38. begin
  39. wbarg := Startup^.sm_ArgList;
  40. if NameFromLock(wbarg^[Idx + 1].wa_Lock,@Path[0],255) then
  41. begin
  42. Len := 0;
  43. while (Path[Len] <> #0) and (Len < 254) do
  44. Inc(Len);
  45. if Len > 0 then
  46. if (Path[Len - 1] <> ':') and (Path[Len - 1] <> '/') then
  47. Path[Len] := '/';
  48. strPath := Path;
  49. end;
  50. Result := strPath + wbarg^[Idx + 1].wa_Name;
  51. end;
  52. end;
  53. end;
  54. { Generates correct argument array on startup }
  55. procedure GenerateArgs;
  56. var
  57. ArgVLen: LongInt;
  58. procedure AllocArg(Idx, Len: LongInt);
  59. var
  60. i, OldArgVLen : LongInt;
  61. begin
  62. if Idx >= ArgVLen then
  63. begin
  64. OldArgVLen := ArgVLen;
  65. ArgVLen := (Idx + 8) and (not 7);
  66. SysReAllocMem(Argv, Argvlen * SizeOf(Pointer));
  67. for i := OldArgVLen to ArgVLen - 1 do
  68. ArgV[i]:=nil;
  69. end;
  70. ArgV[Idx] := SysAllocMem(Succ(Len));
  71. end;
  72. var
  73. Count: Word;
  74. Start: Word;
  75. Ende: Word;
  76. LocalIndex: Word;
  77. i: Integer;
  78. P : PChar;
  79. Temp : AnsiString;
  80. InQuotes: boolean;
  81. begin
  82. P := GetArgStr;
  83. ArgVLen := 0;
  84. { Set argv[0] }
  85. Temp := ParamStr(0);
  86. AllocArg(0, Length(Temp));
  87. Move(Temp[1], Argv[0]^, Length(Temp));
  88. Argv[0][Length(Temp)] := #0;
  89. { check if we're started from Workbench }
  90. if AOS_wbMsg <> nil then
  91. begin
  92. ArgC := GetWBArgsNum + 1;
  93. for i := 1 to ArgC - 1 do
  94. begin
  95. Temp := GetWBArg(i);
  96. AllocArg(i, Length(Temp));
  97. Move(Temp[1], Argv[i]^, Length(Temp));
  98. Argv[i][Length(Temp)] := #0;
  99. end;
  100. Exit;
  101. end;
  102. InQuotes := False;
  103. { Handle the other args }
  104. Count := 0;
  105. { first index is one }
  106. LocalIndex := 1;
  107. while (P[Count] <> #0) do
  108. begin
  109. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do
  110. Inc(count);
  111. if p[count] = '"' then
  112. begin
  113. inQuotes := True;
  114. Inc(Count);
  115. end;
  116. start := count;
  117. if inQuotes then
  118. begin
  119. while (p[count]<>#0) and (p[count]<>'"') and (p[count]<>LineEnding) do
  120. begin
  121. Inc(Count)
  122. end;
  123. end else
  124. begin
  125. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do
  126. inc(count);
  127. end;
  128. ende := count;
  129. if not inQuotes then
  130. begin
  131. while (p[start]=' ') and (Start < Ende) do
  132. Inc(Start)
  133. end;
  134. if (ende-start>0) then
  135. begin
  136. allocarg(localindex,ende-start);
  137. move(p[start],argv[localindex]^,ende-start);
  138. argv[localindex][ende-start]:=#0;
  139. if inQuotes and (argv[localindex][(ende-start) - 1] = '"') then
  140. argv[localindex][(ende-start)-1] := #0;
  141. inc(localindex);
  142. end;
  143. if inQuotes and (p[count] = '"') then
  144. Inc(Count);
  145. inQuotes := False;
  146. end;
  147. argc:=localindex;
  148. end;
  149. function GetProgDir: string;
  150. var
  151. s1: string;
  152. alock: LongInt;
  153. counter: Byte;
  154. begin
  155. GetProgDir := '';
  156. FillChar(s1, 255, #0);
  157. { GetLock of program directory }
  158. alock := GetProgramDir;
  159. if alock <> 0 then
  160. begin
  161. if NameFromLock(alock, @s1[1], 255) then
  162. begin
  163. Counter := 1;
  164. while (s1[Counter] <> #0) and (Counter <> 0) do
  165. Inc(Counter);
  166. s1[0] := char(Counter - 1);
  167. GetProgDir := s1;
  168. end;
  169. end;
  170. end;
  171. function GetProgramName: string;
  172. { Returns ONLY the program name }
  173. var
  174. s1: string;
  175. Counter: Byte;
  176. begin
  177. GetProgramName := '';
  178. FillChar(s1, 255, #0);
  179. if GetProgramName(@s1[1], 255) then
  180. begin
  181. { now check out and assign the length of the string }
  182. Counter := 1;
  183. while (s1[Counter] <> #0) and (Counter <> 0) do
  184. Inc(Counter);
  185. s1[0] := char(Counter - 1);
  186. { now remove any component path which should not be there }
  187. for Counter := Length(s1) downto 1 do
  188. if (s1[Counter] = '/') or (s1[Counter] = ':') then
  189. break;
  190. { readjust counterv to point to character }
  191. if Counter <> 1 then
  192. Inc(Counter);
  193. GetProgramName := Copy(s1, Counter, Length(s1));
  194. end;
  195. end;
  196. {*****************************************************************************
  197. ParamStr
  198. *****************************************************************************}
  199. { number of args }
  200. function ParamCount: LongInt;
  201. begin
  202. if AOS_wbMsg <> nil then
  203. ParamCount := GetWBArgsNum
  204. else
  205. ParamCount := argc - 1;
  206. end;
  207. { argument number l }
  208. function ParamStr(l: LongInt): string;
  209. var
  210. s1: string;
  211. begin
  212. ParamStr := '';
  213. if AOS_wbMsg <> nil then
  214. begin
  215. ParamStr := GetWBArg(l);
  216. end
  217. else
  218. begin
  219. if l = 0 then
  220. begin
  221. s1 := GetProgDir;
  222. if length(s1) > 0 then
  223. begin
  224. if s1[Length(s1)] = ':' then
  225. paramstr := s1 + GetProgramName
  226. else
  227. paramstr:=s1+'/'+GetProgramName;
  228. end
  229. else
  230. paramstr:=GetProgramName;
  231. end
  232. else
  233. begin
  234. if (l > 0) and (l + 1 <= argc) then
  235. ParamStr := StrPas(argv[l]);
  236. end;
  237. end;
  238. end;