syspara.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. { Generates correct argument array on startup }
  13. procedure GenerateArgs;
  14. var
  15. ArgVLen: LongInt;
  16. procedure AllocArg(Idx, Len: LongInt);
  17. var
  18. i, OldArgVLen : LongInt;
  19. begin
  20. if Idx >= ArgVLen then
  21. begin
  22. OldArgVLen := ArgVLen;
  23. ArgVLen := (Idx + 8) and (not 7);
  24. SysReAllocMem(Argv, Argvlen * SizeOf(Pointer));
  25. for i := OldArgVLen to ArgVLen - 1 do
  26. ArgV[i]:=nil;
  27. end;
  28. ArgV[Idx] := SysAllocMem(Succ(Len));
  29. end;
  30. var
  31. Count: Word;
  32. Start: Word;
  33. Ende: Word;
  34. LocalIndex: Word;
  35. i: Integer;
  36. P : PChar;
  37. Temp : AnsiString;
  38. InQuotes: boolean;
  39. begin
  40. P := Args;
  41. ArgVLen := 0;
  42. { Set argv[0] }
  43. Temp := ParamStr(0);
  44. AllocArg(0, Length(Temp));
  45. Move(Temp[1], Argv[0]^, Length(Temp));
  46. Argv[0][Length(Temp)] := #0;
  47. InQuotes := False;
  48. { Handle the other args }
  49. Count := 0;
  50. { first index is one }
  51. LocalIndex := 1;
  52. while (P[Count] <> #0) do
  53. begin
  54. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do
  55. Inc(count);
  56. if p[count] = '"' then
  57. begin
  58. inQuotes := True;
  59. Inc(Count);
  60. end;
  61. start := count;
  62. if inQuotes then
  63. begin
  64. while (p[count]<>#0) and (p[count]<>'"') and (p[count]<>LineEnding) do
  65. begin
  66. Inc(Count)
  67. end;
  68. end else
  69. begin
  70. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do
  71. inc(count);
  72. end;
  73. ende := count;
  74. if not inQuotes then
  75. begin
  76. while (p[start]=' ') and (Start < Ende) do
  77. Inc(Start)
  78. end;
  79. if (ende-start>0) then
  80. begin
  81. allocarg(localindex,ende-start);
  82. move(p[start],argv[localindex]^,ende-start);
  83. argv[localindex][ende-start]:=#0;
  84. if inQuotes and (argv[localindex][(ende-start) - 1] = '"') then
  85. argv[localindex][(ende-start)-1] := #0;
  86. inc(localindex);
  87. end;
  88. if inQuotes and (p[count] = '"') then
  89. Inc(Count);
  90. inQuotes := False;
  91. end;
  92. argc:=localindex;
  93. end;
  94. {*****************************************************************************
  95. ParamStr
  96. *****************************************************************************}
  97. { number of args }
  98. function ParamCount: LongInt;
  99. begin
  100. ParamCount := argc - 1;
  101. end;
  102. { argument number l }
  103. function ParamStr(l: LongInt): string;
  104. var
  105. s1: string;
  106. begin
  107. ParamStr := '';
  108. if (l > 0) and (l + 1 <= argc) then
  109. ParamStr := StrPas(argv[l]);
  110. end;