| 1 |
(* CmdArgs.mod provides procedures to retrieve arguments from strings. |
| 2 |
|
| 3 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. *) |
| 26 |
|
| 27 |
IMPLEMENTATION MODULE CmdArgs ; |
| 28 |
|
| 29 |
FROM ASCII IMPORT cr, nul ; |
| 30 |
FROM StrLib IMPORT StrLen ; |
| 31 |
|
| 32 |
CONST |
| 33 |
esc = '\' ; |
| 34 |
space = ' ' ; |
| 35 |
squote = "'" ; |
| 36 |
dquote = '"' ; |
| 37 |
tab = ' ' ; |
| 38 |
|
| 39 |
|
| 40 |
(* |
| 41 |
GetArg - takes a command line and attempts to extract argument, n, |
| 42 |
from CmdLine. The resulting argument is placed into, a. |
| 43 |
The result of the operation is returned. |
| 44 |
*) |
| 45 |
|
| 46 |
PROCEDURE GetArg (CmdLine: ARRAY OF CHAR ; |
| 47 |
n: CARDINAL; VAR Argi: ARRAY OF CHAR) : BOOLEAN ; |
| 48 |
VAR |
| 49 |
Index, |
| 50 |
i : CARDINAL ; |
| 51 |
Another: BOOLEAN ; |
| 52 |
BEGIN |
| 53 |
Index := 0 ; |
| 54 |
(* Continually retrieve an argument until we get the n th argument. *) |
| 55 |
i := 0 ; |
| 56 |
REPEAT |
| 57 |
Another := GetNextArg(CmdLine, Index, Argi) ; |
| 58 |
INC(i) ; |
| 59 |
UNTIL (i>n) OR (NOT Another) ; |
| 60 |
RETURN( i>n ) |
| 61 |
END GetArg ; |
| 62 |
|
| 63 |
|
| 64 |
(* |
| 65 |
GetNextArg - Returns true if another argument may be found. |
| 66 |
The argument is taken from CmdLine at position Index, |
| 67 |
Arg is filled with the found argument. |
| 68 |
*) |
| 69 |
|
| 70 |
PROCEDURE GetNextArg (CmdLine: ARRAY OF CHAR; VAR CmdIndex: CARDINAL; |
| 71 |
VAR Arg: ARRAY OF CHAR) : BOOLEAN ; |
| 72 |
VAR |
| 73 |
ArgIndex: CARDINAL ; (* Index into Arg *) |
| 74 |
HighA, |
| 75 |
HighC: CARDINAL ; |
| 76 |
BEGIN |
| 77 |
HighA := HIGH(Arg) ; |
| 78 |
HighC := StrLen(CmdLine) ; |
| 79 |
ArgIndex := 0 ; |
| 80 |
(* Skip spaces *) |
| 81 |
WHILE (CmdIndex<HighC) AND Space(CmdLine[CmdIndex]) DO |
| 82 |
INC(CmdIndex) |
| 83 |
END ; |
| 84 |
IF CmdIndex<HighC |
| 85 |
THEN |
| 86 |
IF SingleQuote(CmdLine[CmdIndex]) |
| 87 |
THEN |
| 88 |
(* Skip over the single quote *) |
| 89 |
INC(CmdIndex) ; |
| 90 |
CopyUntil(CmdLine, CmdIndex, HighC, Arg, ArgIndex, HighA, squote) ; |
| 91 |
INC(CmdIndex) |
| 92 |
ELSIF DoubleQuote(CmdLine[CmdIndex]) |
| 93 |
THEN |
| 94 |
(* Skip over the double quote *) |
| 95 |
INC(CmdIndex) ; |
| 96 |
CopyUntil(CmdLine, CmdIndex, HighC, Arg, ArgIndex, HighA, dquote) ; |
| 97 |
INC(CmdIndex) |
| 98 |
ELSE |
| 99 |
CopyUntilSpace(CmdLine, CmdIndex, HighC, Arg, ArgIndex, HighA) |
| 100 |
END |
| 101 |
END ; |
| 102 |
(* Skip spaces *) |
| 103 |
WHILE (CmdIndex<HighC) AND Space(CmdLine[CmdIndex]) DO |
| 104 |
INC(CmdIndex) |
| 105 |
END ; |
| 106 |
IF ArgIndex<HighA |
| 107 |
THEN |
| 108 |
Arg[ArgIndex] := nul |
| 109 |
END ; |
| 110 |
RETURN( (CmdIndex<HighC) ) |
| 111 |
END GetNextArg ; |
| 112 |
|
| 113 |
|
| 114 |
(* |
| 115 |
CopyUntilSpace - copies characters until a Space character is found. |
| 116 |
*) |
| 117 |
|
| 118 |
PROCEDURE CopyUntilSpace (From: ARRAY OF CHAR; |
| 119 |
VAR FromIndex: CARDINAL; FromHigh: CARDINAL; |
| 120 |
VAR To: ARRAY OF CHAR; |
| 121 |
VAR ToIndex: CARDINAL; ToHigh: CARDINAL) ; |
| 122 |
BEGIN |
| 123 |
WHILE (FromIndex<FromHigh) AND (ToIndex<ToHigh) AND |
| 124 |
(NOT Space(From[FromIndex])) DO |
| 125 |
CopyChar(From, FromIndex, FromHigh, To, ToIndex, ToHigh) |
| 126 |
END |
| 127 |
END CopyUntilSpace ; |
| 128 |
|
| 129 |
|
| 130 |
(* |
| 131 |
CopyUntil - copies characters until the UntilChar is found. |
| 132 |
*) |
| 133 |
|
| 134 |
PROCEDURE CopyUntil (From: ARRAY OF CHAR; |
| 135 |
VAR FromIndex: CARDINAL; FromHigh: CARDINAL; |
| 136 |
VAR To: ARRAY OF CHAR; |
| 137 |
VAR ToIndex: CARDINAL; ToHigh: CARDINAL; |
| 138 |
UntilChar: CHAR) ; |
| 139 |
BEGIN |
| 140 |
WHILE (FromIndex<FromHigh) AND (ToIndex<ToHigh) AND |
| 141 |
(From[FromIndex]#UntilChar) DO |
| 142 |
CopyChar(From, FromIndex, FromHigh, To, ToIndex, ToHigh) |
| 143 |
END |
| 144 |
END CopyUntil ; |
| 145 |
|
| 146 |
|
| 147 |
(* |
| 148 |
CopyChar - copies a character from string From to string To and |
| 149 |
takes into consideration escape characters. ie \x |
| 150 |
Where x is any character. |
| 151 |
*) |
| 152 |
|
| 153 |
PROCEDURE CopyChar (From: ARRAY OF CHAR; |
| 154 |
VAR FromIndex: CARDINAL; FromHigh: CARDINAL; |
| 155 |
VAR To: ARRAY OF CHAR; |
| 156 |
VAR ToIndex: CARDINAL; ToHigh: CARDINAL) ; |
| 157 |
BEGIN |
| 158 |
IF (FromIndex<FromHigh) AND (ToIndex<ToHigh) |
| 159 |
THEN |
| 160 |
IF Escape(From[FromIndex]) |
| 161 |
THEN |
| 162 |
(* Skip over Escape Character *) |
| 163 |
INC(FromIndex) |
| 164 |
END ; |
| 165 |
IF FromIndex<FromHigh |
| 166 |
THEN |
| 167 |
(* Copy Normal Character *) |
| 168 |
To[ToIndex] := From[FromIndex] ; |
| 169 |
INC(ToIndex) ; |
| 170 |
INC(FromIndex) |
| 171 |
END |
| 172 |
END |
| 173 |
END CopyChar ; |
| 174 |
|
| 175 |
|
| 176 |
(* |
| 177 |
Narg - returns the number of arguments available from |
| 178 |
command line, CmdLine. |
| 179 |
*) |
| 180 |
|
| 181 |
PROCEDURE Narg (CmdLine: ARRAY OF CHAR) : CARDINAL ; |
| 182 |
VAR |
| 183 |
a : ARRAY [0..1000] OF CHAR ; |
| 184 |
ArgNo: CARDINAL ; |
| 185 |
BEGIN |
| 186 |
ArgNo := 0 ; |
| 187 |
WHILE GetArg(CmdLine, ArgNo, a) DO |
| 188 |
INC( ArgNo ) |
| 189 |
END ; |
| 190 |
(* |
| 191 |
IF ArgNo>0 |
| 192 |
THEN |
| 193 |
DEC(ArgNo) |
| 194 |
END ; |
| 195 |
*) |
| 196 |
RETURN( ArgNo ) |
| 197 |
END Narg ; |
| 198 |
|
| 199 |
|
| 200 |
PROCEDURE Escape (ch: CHAR) : BOOLEAN ; |
| 201 |
BEGIN |
| 202 |
RETURN( ch=esc ) |
| 203 |
END Escape ; |
| 204 |
|
| 205 |
|
| 206 |
PROCEDURE Space (ch: CHAR) : BOOLEAN ; |
| 207 |
BEGIN |
| 208 |
RETURN( (ch=space) OR (ch=tab) ) |
| 209 |
END Space ; |
| 210 |
|
| 211 |
|
| 212 |
PROCEDURE DoubleQuote (ch: CHAR) : BOOLEAN ; |
| 213 |
BEGIN |
| 214 |
RETURN( ch=dquote ) |
| 215 |
END DoubleQuote ; |
| 216 |
|
| 217 |
|
| 218 |
PROCEDURE SingleQuote (ch: CHAR) : BOOLEAN ; |
| 219 |
BEGIN |
| 220 |
RETURN( ch=squote ) |
| 221 |
END SingleQuote ; |
| 222 |
|
| 223 |
|
| 224 |
END CmdArgs. |