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