Debian Codegraph @

gcc/m2/mc-boot/GCmdArgs.cc

raw
1 /* do not edit automatically generated by mc from CmdArgs. */
2 /* CmdArgs.mod provides procedures to retrieve arguments from strings.
3
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
22
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
27
28 #include "config.h"
29 #include "system.h"
30 #include <stdbool.h>
31 # if !defined (PROC_D)
32 # define PROC_D
33 typedef void (*PROC_t) (void);
34 typedef struct { PROC_t proc; } PROC;
35 # endif
36
37 #define _CmdArgs_C
38
39 #include "GCmdArgs.h"
40 # include "GASCII.h"
41 # include "GStrLib.h"
42
43 # define esc '\\'
44 # define space ' '
45 # define squote '\''
46 # define dquote '"'
47 # define tab ' '
48
49 /*
50 GetArg - takes a command line and attempts to extract argument, n,
51 from CmdLine. The resulting argument is placed into, a.
52 The result of the operation is returned.
53 */
54
55 extern "C" bool CmdArgs_GetArg (const char *CmdLine_, unsigned int _CmdLine_high, unsigned int n, char *Argi, unsigned int _Argi_high);
56
57 /*
58 Narg - returns the number of arguments available from
59 command line, CmdLine.
60 */
61
62 extern "C" unsigned int CmdArgs_Narg (const char *CmdLine_, unsigned int _CmdLine_high);
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 static bool GetNextArg (const char *CmdLine_, unsigned int _CmdLine_high, unsigned int *CmdIndex, char *Arg, unsigned int _Arg_high);
71
72 /*
73 CopyUntilSpace - copies characters until a Space character is found.
74 */
75
76 static void CopyUntilSpace (const char *From_, unsigned int _From_high, unsigned int *FromIndex, unsigned int FromHigh, char *To, unsigned int _To_high, unsigned int *ToIndex, unsigned int ToHigh);
77
78 /*
79 CopyUntil - copies characters until the UntilChar is found.
80 */
81
82 static void CopyUntil (const char *From_, unsigned int _From_high, unsigned int *FromIndex, unsigned int FromHigh, char *To, unsigned int _To_high, unsigned int *ToIndex, unsigned int ToHigh, char UntilChar);
83
84 /*
85 CopyChar - copies a character from string From to string To and
86 takes into consideration escape characters. ie \x
87 Where x is any character.
88 */
89
90 static void CopyChar (const char *From_, unsigned int _From_high, unsigned int *FromIndex, unsigned int FromHigh, char *To, unsigned int _To_high, unsigned int *ToIndex, unsigned int ToHigh);
91 static bool Escape (char ch);
92 static bool Space (char ch);
93 static bool DoubleQuote (char ch);
94 static bool SingleQuote (char ch);
95
96
97 /*
98 GetNextArg - Returns true if another argument may be found.
99 The argument is taken from CmdLine at position Index,
100 Arg is filled with the found argument.
101 */
102
103 static bool GetNextArg (const char *CmdLine_, unsigned int _CmdLine_high, unsigned int *CmdIndex, char *Arg, unsigned int _Arg_high)
104 {
105 unsigned int ArgIndex;
106 unsigned int HighA;
107 unsigned int HighC;
108 char CmdLine[_CmdLine_high+1];
109
110 /* make a local copy of each unbounded array. */
111 memcpy (CmdLine, CmdLine_, _CmdLine_high+1);
112
113 HighA = _Arg_high; /* Index into Arg */
114 HighC = StrLib_StrLen ((const char *) CmdLine, _CmdLine_high);
115 ArgIndex = 0;
116 /* Skip spaces */
117 while (((*CmdIndex) < HighC) && (Space (CmdLine[(*CmdIndex)])))
118 {
119 (*CmdIndex) += 1;
120 }
121 if ((*CmdIndex) < HighC)
122 {
123 /* avoid gcc warning by using compound statement even if not strictly necessary. */
124 if (SingleQuote (CmdLine[(*CmdIndex)]))
125 {
126 /* Skip over the single quote */
127 (*CmdIndex) += 1;
128 CopyUntil ((const char *) CmdLine, _CmdLine_high, CmdIndex, HighC, (char *) Arg, _Arg_high, &ArgIndex, HighA, squote);
129 (*CmdIndex) += 1;
130 }
131 else if (DoubleQuote (CmdLine[(*CmdIndex)]))
132 {
133 /* avoid dangling else. */
134 /* Skip over the double quote */
135 (*CmdIndex) += 1;
136 CopyUntil ((const char *) CmdLine, _CmdLine_high, CmdIndex, HighC, (char *) Arg, _Arg_high, &ArgIndex, HighA, dquote);
137 (*CmdIndex) += 1;
138 }
139 else
140 {
141 /* avoid dangling else. */
142 CopyUntilSpace ((const char *) CmdLine, _CmdLine_high, CmdIndex, HighC, (char *) Arg, _Arg_high, &ArgIndex, HighA);
143 }
144 }
145 /* Skip spaces */
146 while (((*CmdIndex) < HighC) && (Space (CmdLine[(*CmdIndex)])))
147 {
148 (*CmdIndex) += 1;
149 }
150 if (ArgIndex < HighA)
151 {
152 const_cast<char *>(Arg)[ArgIndex] = ASCII_nul;
153 }
154 return (*CmdIndex) < HighC;
155 /* static analysis guarentees a RETURN statement will be used before here. */
156 __builtin_unreachable ();
157 }
158
159
160 /*
161 CopyUntilSpace - copies characters until a Space character is found.
162 */
163
164 static void CopyUntilSpace (const char *From_, unsigned int _From_high, unsigned int *FromIndex, unsigned int FromHigh, char *To, unsigned int _To_high, unsigned int *ToIndex, unsigned int ToHigh)
165 {
166 char From[_From_high+1];
167
168 /* make a local copy of each unbounded array. */
169 memcpy (From, From_, _From_high+1);
170
171 while ((((*FromIndex) < FromHigh) && ((*ToIndex) < ToHigh)) && (! (Space (From[(*FromIndex)]))))
172 {
173 CopyChar ((const char *) From, _From_high, FromIndex, FromHigh, (char *) To, _To_high, ToIndex, ToHigh);
174 }
175 }
176
177
178 /*
179 CopyUntil - copies characters until the UntilChar is found.
180 */
181
182 static void CopyUntil (const char *From_, unsigned int _From_high, unsigned int *FromIndex, unsigned int FromHigh, char *To, unsigned int _To_high, unsigned int *ToIndex, unsigned int ToHigh, char UntilChar)
183 {
184 char From[_From_high+1];
185
186 /* make a local copy of each unbounded array. */
187 memcpy (From, From_, _From_high+1);
188
189 while ((((*FromIndex) < FromHigh) && ((*ToIndex) < ToHigh)) && (From[(*FromIndex)] != UntilChar))
190 {
191 CopyChar ((const char *) From, _From_high, FromIndex, FromHigh, (char *) To, _To_high, ToIndex, ToHigh);
192 }
193 }
194
195
196 /*
197 CopyChar - copies a character from string From to string To and
198 takes into consideration escape characters. ie \x
199 Where x is any character.
200 */
201
202 static void CopyChar (const char *From_, unsigned int _From_high, unsigned int *FromIndex, unsigned int FromHigh, char *To, unsigned int _To_high, unsigned int *ToIndex, unsigned int ToHigh)
203 {
204 char From[_From_high+1];
205
206 /* make a local copy of each unbounded array. */
207 memcpy (From, From_, _From_high+1);
208
209 if (((*FromIndex) < FromHigh) && ((*ToIndex) < ToHigh))
210 {
211 if (Escape (From[(*FromIndex)]))
212 {
213 /* Skip over Escape Character */
214 (*FromIndex) += 1;
215 }
216 if ((*FromIndex) < FromHigh)
217 {
218 /* Copy Normal Character */
219 const_cast<char *>(To)[(*ToIndex)] = From[(*FromIndex)];
220 (*ToIndex) += 1;
221 (*FromIndex) += 1;
222 }
223 }
224 }
225
226 static bool Escape (char ch)
227 {
228 return ch == esc;
229 /* static analysis guarentees a RETURN statement will be used before here. */
230 __builtin_unreachable ();
231 }
232
233 static bool Space (char ch)
234 {
235 return (ch == space) || (ch == tab);
236 /* static analysis guarentees a RETURN statement will be used before here. */
237 __builtin_unreachable ();
238 }
239
240 static bool DoubleQuote (char ch)
241 {
242 return ch == dquote;
243 /* static analysis guarentees a RETURN statement will be used before here. */
244 __builtin_unreachable ();
245 }
246
247 static bool SingleQuote (char ch)
248 {
249 return ch == squote;
250 /* static analysis guarentees a RETURN statement will be used before here. */
251 __builtin_unreachable ();
252 }
253
254
255 /*
256 GetArg - takes a command line and attempts to extract argument, n,
257 from CmdLine. The resulting argument is placed into, a.
258 The result of the operation is returned.
259 */
260
261 extern "C" bool CmdArgs_GetArg (const char *CmdLine_, unsigned int _CmdLine_high, unsigned int n, char *Argi, unsigned int _Argi_high)
262 {
263 unsigned int Index;
264 unsigned int i;
265 bool Another;
266 char CmdLine[_CmdLine_high+1];
267
268 /* make a local copy of each unbounded array. */
269 memcpy (CmdLine, CmdLine_, _CmdLine_high+1);
270
271 Index = 0;
272 /* Continually retrieve an argument until we get the n th argument. */
273 i = 0;
274 do {
275 Another = GetNextArg ((const char *) CmdLine, _CmdLine_high, &Index, (char *) Argi, _Argi_high);
276 i += 1;
277 } while (! ((i > n) || ! Another));
278 return i > n;
279 /* static analysis guarentees a RETURN statement will be used before here. */
280 __builtin_unreachable ();
281 }
282
283
284 /*
285 Narg - returns the number of arguments available from
286 command line, CmdLine.
287 */
288
289 extern "C" unsigned int CmdArgs_Narg (const char *CmdLine_, unsigned int _CmdLine_high)
290 {
291 typedef struct Narg__T1_a Narg__T1;
292
293 struct Narg__T1_a { char array[1000+1]; };
294 Narg__T1 a;
295 unsigned int ArgNo;
296 char CmdLine[_CmdLine_high+1];
297
298 /* make a local copy of each unbounded array. */
299 memcpy (CmdLine, CmdLine_, _CmdLine_high+1);
300
301 ArgNo = 0;
302 while (CmdArgs_GetArg ((const char *) CmdLine, _CmdLine_high, ArgNo, (char *) &a.array[0], 1000))
303 {
304 ArgNo += 1;
305 }
306 /*
307 IF ArgNo>0
308 THEN
309 DEC(ArgNo)
310 END ;
311 */
312 return ArgNo;
313 /* static analysis guarentees a RETURN statement will be used before here. */
314 __builtin_unreachable ();
315 }
316
317 extern "C" void _M2_CmdArgs_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
318 {
319 }
320
321 extern "C" void _M2_CmdArgs_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
322 {
323 }