1 (* SCmdArgs.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 SCmdArgs ;
29 FROM ASCII IMPORT cr, nul ;
30 FROM DynamicStrings IMPORT Length, Slice, char ;
44 PROCEDURE isWhite (ch: CHAR) : BOOLEAN ;
46 RETURN (ch=space) OR (ch=tab)
54 PROCEDURE skipWhite (s: String; i, e: INTEGER) : INTEGER ;
75 PROCEDURE skipOverWhite (s: String; start, end: INTEGER) : INTEGER ;
78 WHILE (start<end) AND (NOT isWhite(char(s, start))) DO
89 PROCEDURE skipOver (s: String; start, end: INTEGER; ch: CHAR) : INTEGER ;
92 WHILE (start<end) AND (char(s, start)#ch) DO
103 PROCEDURE skipNextArg (s: String; start, end: INTEGER) : INTEGER ;
109 ch := char(s, start) ;
112 end := skipOver(s, start, end, dquote)
115 end := skipOver(s, start, end, squote)
117 end := skipOverWhite(s, start, end)
125 GetArg - takes a command line and attempts to extract argument, n,
126 from CmdLine. The resulting argument is placed into, a.
127 The result of the operation is returned.
130 PROCEDURE GetArg (CmdLine: String;
131 n: CARDINAL; VAR Argi: String) : BOOLEAN ;
135 start, end: INTEGER ;
140 end := Length(CmdLine) ;
142 start := skipWhite(CmdLine, start, end) ;
143 sn := skipNextArg(CmdLine, start, end) ;
152 start := skipWhite(CmdLine, start, end) ;
153 sn := skipNextArg(CmdLine, start, end) ;
154 Argi := Slice(CmdLine, start, sn) ;
160 Narg - returns the number of arguments available from
161 command line, CmdLine.
164 PROCEDURE Narg (CmdLine: String) : CARDINAL ;
168 start, end: INTEGER ;
172 end := Length(CmdLine) ;
174 start := skipWhite(CmdLine, start, end) ;
175 s := skipNextArg(CmdLine, start, end) ;
187 PROCEDURE Escape (ch: CHAR) : BOOLEAN ;
193 PROCEDURE Space (ch: CHAR) : BOOLEAN ;
195 RETURN( (ch=space) OR (ch=tab) )
199 PROCEDURE DoubleQuote (ch: CHAR) : BOOLEAN ;
205 PROCEDURE SingleQuote (ch: CHAR) : BOOLEAN ;