]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/m2/gm2-libs/SCmdArgs.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs / SCmdArgs.mod
CommitLineData
1eee94d3
GM
1(* SCmdArgs.mod provides procedures to retrieve arguments from strings.
2
83ffe9cd 3Copyright (C) 2001-2023 Free Software Foundation, Inc.
1eee94d3
GM
4Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6This file is part of GNU Modula-2.
7
8GNU Modula-2 is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GNU Modula-2 is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
17
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25<http://www.gnu.org/licenses/>. *)
26
27IMPLEMENTATION MODULE SCmdArgs ;
28
29FROM ASCII IMPORT cr, nul ;
30FROM DynamicStrings IMPORT Length, Slice, char ;
31
32CONST
33 esc = '\' ;
34 space = ' ' ;
35 squote = "'" ;
36 dquote = '"' ;
37 tab = ' ' ;
38
39
40(*
41 isWhite -
42*)
43
44PROCEDURE isWhite (ch: CHAR) : BOOLEAN ;
45BEGIN
46 RETURN (ch=space) OR (ch=tab)
47END isWhite ;
48
49
50(*
51 skipWhite -
52*)
53
54PROCEDURE skipWhite (s: String; i, e: INTEGER) : INTEGER ;
55VAR
56 ch: CHAR ;
57BEGIN
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 )
68END skipWhite ;
69
70
71(*
72 skipOverWhite -
73*)
74
75PROCEDURE skipOverWhite (s: String; start, end: INTEGER) : INTEGER ;
76BEGIN
77 INC(start) ;
78 WHILE (start<end) AND (NOT isWhite(char(s, start))) DO
79 INC(start)
80 END ;
81 RETURN( start )
82END skipOverWhite ;
83
84
85(*
86 skipOver -
87*)
88
89PROCEDURE skipOver (s: String; start, end: INTEGER; ch: CHAR) : INTEGER ;
90BEGIN
91 INC(start) ;
92 WHILE (start<end) AND (char(s, start)#ch) DO
93 INC(start)
94 END ;
95 RETURN( start )
96END skipOver ;
97
98
99(*
100 skipNextArg -
101*)
102
103PROCEDURE skipNextArg (s: String; start, end: INTEGER) : INTEGER ;
104VAR
105 ch: CHAR ;
106BEGIN
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 )
121END 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
130PROCEDURE GetArg (CmdLine: String;
131 n: CARDINAL; VAR Argi: String) : BOOLEAN ;
132VAR
133 i : CARDINAL ;
134 sn,
135 start, end: INTEGER ;
136 ch : CHAR ;
137BEGIN
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 )
156END GetArg ;
157
158
159(*
160 Narg - returns the number of arguments available from
161 command line, CmdLine.
162*)
163
164PROCEDURE Narg (CmdLine: String) : CARDINAL ;
165VAR
166 n : CARDINAL ;
167 s,
168 start, end: INTEGER ;
169BEGIN
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
184END Narg ;
185
186
187PROCEDURE Escape (ch: CHAR) : BOOLEAN ;
188BEGIN
189 RETURN( ch=esc )
190END Escape ;
191
192
193PROCEDURE Space (ch: CHAR) : BOOLEAN ;
194BEGIN
195 RETURN( (ch=space) OR (ch=tab) )
196END Space ;
197
198
199PROCEDURE DoubleQuote (ch: CHAR) : BOOLEAN ;
200BEGIN
201 RETURN( ch=dquote )
202END DoubleQuote ;
203
204
205PROCEDURE SingleQuote (ch: CHAR) : BOOLEAN ;
206BEGIN
207 RETURN( ch=squote )
208END SingleQuote ;
209
210
211END SCmdArgs.