1 (* CmdArgs.mod provides procedures to retrieve arguments from strings.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
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.
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/>. *)
27 IMPLEMENTATION MODULE CmdArgs ;
29 FROM ASCII IMPORT cr, nul ;
30 FROM StrLib IMPORT StrLen ;
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.
46 PROCEDURE GetArg (CmdLine: ARRAY OF CHAR ;
47 n: CARDINAL; VAR Argi: ARRAY OF CHAR) : BOOLEAN ;
54 (* Continually retrieve an argument until we get the n th argument. *)
57 Another := GetNextArg(CmdLine, Index, Argi) ;
59 UNTIL (i>n) OR (NOT Another) ;
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.
70 PROCEDURE GetNextArg (CmdLine: ARRAY OF CHAR; VAR CmdIndex: CARDINAL;
71 VAR Arg: ARRAY OF CHAR) : BOOLEAN ;
73 ArgIndex: CARDINAL ; (* Index into Arg *)
78 HighC := StrLen(CmdLine) ;
81 WHILE (CmdIndex<HighC) AND Space(CmdLine[CmdIndex]) DO
86 IF SingleQuote(CmdLine[CmdIndex])
88 (* Skip over the single quote *)
90 CopyUntil(CmdLine, CmdIndex, HighC, Arg, ArgIndex, HighA, squote) ;
92 ELSIF DoubleQuote(CmdLine[CmdIndex])
94 (* Skip over the double quote *)
96 CopyUntil(CmdLine, CmdIndex, HighC, Arg, ArgIndex, HighA, dquote) ;
99 CopyUntilSpace(CmdLine, CmdIndex, HighC, Arg, ArgIndex, HighA)
103 WHILE (CmdIndex<HighC) AND Space(CmdLine[CmdIndex]) DO
110 RETURN( (CmdIndex<HighC) )
115 CopyUntilSpace - copies characters until a Space character is found.
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) ;
123 WHILE (FromIndex<FromHigh) AND (ToIndex<ToHigh) AND
124 (NOT Space(From[FromIndex])) DO
125 CopyChar(From, FromIndex, FromHigh, To, ToIndex, ToHigh)
131 CopyUntil - copies characters until the UntilChar is found.
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;
140 WHILE (FromIndex<FromHigh) AND (ToIndex<ToHigh) AND
141 (From[FromIndex]#UntilChar) DO
142 CopyChar(From, FromIndex, FromHigh, To, ToIndex, ToHigh)
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.
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) ;
158 IF (FromIndex<FromHigh) AND (ToIndex<ToHigh)
160 IF Escape(From[FromIndex])
162 (* Skip over Escape Character *)
165 IF FromIndex<FromHigh
167 (* Copy Normal Character *)
168 To[ToIndex] := From[FromIndex] ;
177 Narg - returns the number of arguments available from
178 command line, CmdLine.
181 PROCEDURE Narg (CmdLine: ARRAY OF CHAR) : CARDINAL ;
183 a : ARRAY [0..1000] OF CHAR ;
187 WHILE GetArg(CmdLine, ArgNo, a) DO
200 PROCEDURE Escape (ch: CHAR) : BOOLEAN ;
206 PROCEDURE Space (ch: CHAR) : BOOLEAN ;
208 RETURN( (ch=space) OR (ch=tab) )
212 PROCEDURE DoubleQuote (ch: CHAR) : BOOLEAN ;
218 PROCEDURE SingleQuote (ch: CHAR) : BOOLEAN ;