]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/gm2-libs/SCmdArgs.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs / SCmdArgs.mod
1 (* SCmdArgs.mod provides procedures to retrieve arguments from strings.
2
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
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 SCmdArgs ;
28
29 FROM ASCII IMPORT cr, nul ;
30 FROM DynamicStrings IMPORT Length, Slice, char ;
31
32 CONST
33 esc = '\' ;
34 space = ' ' ;
35 squote = "'" ;
36 dquote = '"' ;
37 tab = ' ' ;
38
39
40 (*
41 isWhite -
42 *)
43
44 PROCEDURE isWhite (ch: CHAR) : BOOLEAN ;
45 BEGIN
46 RETURN (ch=space) OR (ch=tab)
47 END isWhite ;
48
49
50 (*
51 skipWhite -
52 *)
53
54 PROCEDURE skipWhite (s: String; i, e: INTEGER) : INTEGER ;
55 VAR
56 ch: CHAR ;
57 BEGIN
58 WHILE i<e DO
59 ch := char(s, i) ;
60 IF isWhite(ch)
61 THEN
62 INC(i)
63 ELSE
64 RETURN( i )
65 END
66 END ;
67 RETURN( i )
68 END skipWhite ;
69
70
71 (*
72 skipOverWhite -
73 *)
74
75 PROCEDURE skipOverWhite (s: String; start, end: INTEGER) : INTEGER ;
76 BEGIN
77 INC(start) ;
78 WHILE (start<end) AND (NOT isWhite(char(s, start))) DO
79 INC(start)
80 END ;
81 RETURN( start )
82 END skipOverWhite ;
83
84
85 (*
86 skipOver -
87 *)
88
89 PROCEDURE skipOver (s: String; start, end: INTEGER; ch: CHAR) : INTEGER ;
90 BEGIN
91 INC(start) ;
92 WHILE (start<end) AND (char(s, start)#ch) DO
93 INC(start)
94 END ;
95 RETURN( start )
96 END skipOver ;
97
98
99 (*
100 skipNextArg -
101 *)
102
103 PROCEDURE skipNextArg (s: String; start, end: INTEGER) : INTEGER ;
104 VAR
105 ch: CHAR ;
106 BEGIN
107 IF start<end
108 THEN
109 ch := char(s, start) ;
110 IF ch=dquote
111 THEN
112 end := skipOver(s, start, end, dquote)
113 ELSIF ch=squote
114 THEN
115 end := skipOver(s, start, end, squote)
116 ELSE
117 end := skipOverWhite(s, start, end)
118 END
119 END ;
120 RETURN( end )
121 END skipNextArg ;
122
123
124 (*
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.
128 *)
129
130 PROCEDURE GetArg (CmdLine: String;
131 n: CARDINAL; VAR Argi: String) : BOOLEAN ;
132 VAR
133 i : CARDINAL ;
134 sn,
135 start, end: INTEGER ;
136 ch : CHAR ;
137 BEGIN
138 i := 0 ;
139 start := 0 ;
140 end := Length(CmdLine) ;
141 WHILE i<n DO
142 start := skipWhite(CmdLine, start, end) ;
143 sn := skipNextArg(CmdLine, start, end) ;
144 IF sn<end
145 THEN
146 start := sn ;
147 INC(i)
148 ELSE
149 RETURN( FALSE )
150 END
151 END ;
152 start := skipWhite(CmdLine, start, end) ;
153 sn := skipNextArg(CmdLine, start, end) ;
154 Argi := Slice(CmdLine, start, sn) ;
155 RETURN( TRUE )
156 END GetArg ;
157
158
159 (*
160 Narg - returns the number of arguments available from
161 command line, CmdLine.
162 *)
163
164 PROCEDURE Narg (CmdLine: String) : CARDINAL ;
165 VAR
166 n : CARDINAL ;
167 s,
168 start, end: INTEGER ;
169 BEGIN
170 n := 0 ;
171 start := 0 ;
172 end := Length(CmdLine) ;
173 LOOP
174 start := skipWhite(CmdLine, start, end) ;
175 s := skipNextArg(CmdLine, start, end) ;
176 IF s<end
177 THEN
178 start := s ;
179 INC(n)
180 ELSE
181 RETURN( n )
182 END
183 END
184 END Narg ;
185
186
187 PROCEDURE Escape (ch: CHAR) : BOOLEAN ;
188 BEGIN
189 RETURN( ch=esc )
190 END Escape ;
191
192
193 PROCEDURE Space (ch: CHAR) : BOOLEAN ;
194 BEGIN
195 RETURN( (ch=space) OR (ch=tab) )
196 END Space ;
197
198
199 PROCEDURE DoubleQuote (ch: CHAR) : BOOLEAN ;
200 BEGIN
201 RETURN( ch=dquote )
202 END DoubleQuote ;
203
204
205 PROCEDURE SingleQuote (ch: CHAR) : BOOLEAN ;
206 BEGIN
207 RETURN( ch=squote )
208 END SingleQuote ;
209
210
211 END SCmdArgs.