]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/mc-boot/GCmdArgs.c
1 /* do not edit automatically generated by mc from CmdArgs. */
2 /* CmdArgs.mod provides procedures to retrieve arguments from strings.
4 Copyright (C) 2001-2021 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
7 This file is part of GNU Modula-2.
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)
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.
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.
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/>. */
30 # if !defined (PROC_D)
32 typedef void (* PROC_t
) ( void );
33 typedef struct { PROC_t proc
; } PROC
;
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.
54 extern "C" unsigned int CmdArgs_GetArg ( const char * CmdLine_
, unsigned int _CmdLine_high
, unsigned int n
, char * Argi
, unsigned int _Argi_high
);
57 Narg - returns the number of arguments available from
58 command line, CmdLine.
61 extern "C" unsigned int CmdArgs_Narg ( const char * CmdLine_
, unsigned int _CmdLine_high
);
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.
69 static unsigned int GetNextArg ( const char * CmdLine_
, unsigned int _CmdLine_high
, unsigned int * CmdIndex
, char * Arg
, unsigned int _Arg_high
);
72 CopyUntilSpace - copies characters until a Space character is found.
75 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
);
78 CopyUntil - copies characters until the UntilChar is found.
81 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
);
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.
89 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
);
90 static unsigned int Escape ( char ch
);
91 static unsigned int Space ( char ch
);
92 static unsigned int DoubleQuote ( char ch
);
93 static unsigned int SingleQuote ( char ch
);
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.
102 static unsigned int GetNextArg ( const char * CmdLine_
, unsigned int _CmdLine_high
, unsigned int * CmdIndex
, char * Arg
, unsigned int _Arg_high
)
104 unsigned int ArgIndex
;
107 char CmdLine
[ _CmdLine_high
+ 1 ];
109 /* make a local copy of each unbounded array. */
110 memcpy ( CmdLine
, CmdLine_
, _CmdLine_high
+ 1 );
112 HighA
= _Arg_high
; /* Index into Arg */
113 HighC
= StrLib_StrLen (( const char *) CmdLine
, _CmdLine_high
);
116 while (((* CmdIndex
) < HighC
) && ( Space ( CmdLine
[(* CmdIndex
)])))
120 if ((* CmdIndex
) < HighC
)
122 /* avoid gcc warning by using compound statement even if not strictly necessary. */
123 if ( SingleQuote ( CmdLine
[(* CmdIndex
)]))
125 /* Skip over the single quote */
127 CopyUntil (( const char *) CmdLine
, _CmdLine_high
, CmdIndex
, HighC
, ( char *) Arg
, _Arg_high
, & ArgIndex
, HighA
, squote
);
130 else if ( DoubleQuote ( CmdLine
[(* CmdIndex
)]))
132 /* avoid dangling else. */
133 /* Skip over the double quote */
135 CopyUntil (( const char *) CmdLine
, _CmdLine_high
, CmdIndex
, HighC
, ( char *) Arg
, _Arg_high
, & ArgIndex
, HighA
, dquote
);
140 /* avoid dangling else. */
141 CopyUntilSpace (( const char *) CmdLine
, _CmdLine_high
, CmdIndex
, HighC
, ( char *) Arg
, _Arg_high
, & ArgIndex
, HighA
);
145 while (((* CmdIndex
) < HighC
) && ( Space ( CmdLine
[(* CmdIndex
)])))
149 if ( ArgIndex
< HighA
)
151 Arg
[ ArgIndex
] = ASCII_nul
;
153 return (* CmdIndex
) < HighC
;
154 /* static analysis guarentees a RETURN statement will be used before here. */
155 __builtin_unreachable ();
160 CopyUntilSpace - copies characters until a Space character is found.
163 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 char From
[ _From_high
+ 1 ];
167 /* make a local copy of each unbounded array. */
168 memcpy ( From
, From_
, _From_high
+ 1 );
170 while ((((* FromIndex
) < FromHigh
) && ((* ToIndex
) < ToHigh
)) && (! ( Space ( From
[(* FromIndex
)]))))
172 CopyChar (( const char *) From
, _From_high
, FromIndex
, FromHigh
, ( char *) To
, _To_high
, ToIndex
, ToHigh
);
178 CopyUntil - copies characters until the UntilChar is found.
181 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 char From
[ _From_high
+ 1 ];
185 /* make a local copy of each unbounded array. */
186 memcpy ( From
, From_
, _From_high
+ 1 );
188 while ((((* FromIndex
) < FromHigh
) && ((* ToIndex
) < ToHigh
)) && ( From
[(* FromIndex
)] != UntilChar
))
190 CopyChar (( const char *) From
, _From_high
, FromIndex
, FromHigh
, ( char *) To
, _To_high
, ToIndex
, ToHigh
);
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.
201 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 char From
[ _From_high
+ 1 ];
205 /* make a local copy of each unbounded array. */
206 memcpy ( From
, From_
, _From_high
+ 1 );
208 if (((* FromIndex
) < FromHigh
) && ((* ToIndex
) < ToHigh
))
210 if ( Escape ( From
[(* FromIndex
)]))
212 /* Skip over Escape Character */
215 if ((* FromIndex
) < FromHigh
)
217 /* Copy Normal Character */
218 To
[(* ToIndex
)] = From
[(* FromIndex
)];
225 static unsigned int Escape ( char ch
)
228 /* static analysis guarentees a RETURN statement will be used before here. */
229 __builtin_unreachable ();
232 static unsigned int Space ( char ch
)
234 return ( ch
== space
) || ( ch
== tab
);
235 /* static analysis guarentees a RETURN statement will be used before here. */
236 __builtin_unreachable ();
239 static unsigned int DoubleQuote ( char ch
)
242 /* static analysis guarentees a RETURN statement will be used before here. */
243 __builtin_unreachable ();
246 static unsigned int SingleQuote ( char ch
)
249 /* static analysis guarentees a RETURN statement will be used before here. */
250 __builtin_unreachable ();
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.
260 extern "C" unsigned int CmdArgs_GetArg ( const char * CmdLine_
, unsigned int _CmdLine_high
, unsigned int n
, char * Argi
, unsigned int _Argi_high
)
264 unsigned int Another
;
265 char CmdLine
[ _CmdLine_high
+ 1 ];
267 /* make a local copy of each unbounded array. */
268 memcpy ( CmdLine
, CmdLine_
, _CmdLine_high
+ 1 );
271 /* Continually retrieve an argument until we get the n th argument. */
274 Another
= GetNextArg (( const char *) CmdLine
, _CmdLine_high
, & Index
, ( char *) Argi
, _Argi_high
);
276 } while (! (( i
> n
) || ! Another
));
278 /* static analysis guarentees a RETURN statement will be used before here. */
279 __builtin_unreachable ();
284 Narg - returns the number of arguments available from
285 command line, CmdLine.
288 extern "C" unsigned int CmdArgs_Narg ( const char * CmdLine_
, unsigned int _CmdLine_high
)
290 typedef struct Narg__T1_a Narg__T1
;
292 struct Narg__T1_a
{ char array
[ 1000 + 1 ]; };
295 char CmdLine
[ _CmdLine_high
+ 1 ];
297 /* make a local copy of each unbounded array. */
298 memcpy ( CmdLine
, CmdLine_
, _CmdLine_high
+ 1 );
301 while ( CmdArgs_GetArg (( const char *) CmdLine
, _CmdLine_high
, ArgNo
, ( char *) & a
. array
[ 0 ], 1000 ))
312 /* static analysis guarentees a RETURN statement will be used before here. */
313 __builtin_unreachable ();
316 extern "C" void _M2_CmdArgs_init ( __attribute__ (( unused
)) int argc
, __attribute__ (( unused
)) char * argv
[], __attribute__ (( unused
)) char * envp
[])
320 extern "C" void _M2_CmdArgs_finish ( __attribute__ (( unused
)) int argc
, __attribute__ (( unused
)) char * argv
[], __attribute__ (( unused
)) char * envp
[])