]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/argv.c
pex-common.c: New file.
[thirdparty/gcc.git] / libiberty / argv.c
CommitLineData
6599da04 1/* Create and destroy argument vectors (argv's)
0be6abca 2 Copyright (C) 1992, 2001 Free Software Foundation, Inc.
6599da04
JM
3 Written by Fred Fish @ Cygnus Support
4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21
22/* Create and destroy argument vectors. An argument vector is simply an
23 array of string pointers, terminated by a NULL pointer. */
24
25#include "ansidecl.h"
26#include "libiberty.h"
27
f6bbde28 28#define ISBLANK(ch) ((ch) == ' ' || (ch) == '\t')
6599da04
JM
29
30/* Routines imported from standard C runtime libraries. */
31
6599da04 32#include <stddef.h>
a81c752a
RH
33#include <string.h>
34#include <stdlib.h>
6599da04 35
6599da04
JM
36#ifndef NULL
37#define NULL 0
38#endif
39
40#ifndef EOS
41#define EOS '\0'
42#endif
43
44#define INITIAL_MAXARGC 8 /* Number of args + NULL in initial argv */
45
46
19ddc834
JM
47/*
48
aac04c15 49@deftypefn Extension char** dupargv (char **@var{vector})
19ddc834 50
aac04c15
DD
51Duplicate an argument vector. Simply scans through @var{vector},
52duplicating each argument until the terminating @code{NULL} is found.
5bed56d9 53Returns a pointer to the argument vector if successful. Returns
aac04c15
DD
54@code{NULL} if there is insufficient memory to complete building the
55argument vector.
19ddc834 56
aac04c15 57@end deftypefn
19ddc834
JM
58
59*/
60
61char **
9486db4f 62dupargv (char **argv)
19ddc834
JM
63{
64 int argc;
65 char **copy;
66
67 if (argv == NULL)
68 return NULL;
69
70 /* the vector */
71 for (argc = 0; argv[argc] != NULL; argc++);
72 copy = (char **) malloc ((argc + 1) * sizeof (char *));
73 if (copy == NULL)
74 return NULL;
75
76 /* the strings */
77 for (argc = 0; argv[argc] != NULL; argc++)
78 {
79 int len = strlen (argv[argc]);
80 copy[argc] = malloc (sizeof (char *) * (len + 1));
81 if (copy[argc] == NULL)
82 {
83 freeargv (copy);
84 return NULL;
85 }
86 strcpy (copy[argc], argv[argc]);
87 }
88 copy[argc] = NULL;
89 return copy;
90}
91
6599da04
JM
92/*
93
aac04c15 94@deftypefn Extension void freeargv (char **@var{vector})
6599da04 95
aac04c15
DD
96Free an argument vector that was built using @code{buildargv}. Simply
97scans through @var{vector}, freeing the memory for each argument until
98the terminating @code{NULL} is found, and then frees @var{vector}
99itself.
6599da04 100
aac04c15 101@end deftypefn
6599da04
JM
102
103*/
104
9486db4f 105void freeargv (char **vector)
6599da04
JM
106{
107 register char **scan;
108
109 if (vector != NULL)
110 {
111 for (scan = vector; *scan != NULL; scan++)
112 {
113 free (*scan);
114 }
115 free (vector);
116 }
117}
118
119/*
120
aac04c15 121@deftypefn Extension char** buildargv (char *@var{sp})
6599da04 122
aac04c15
DD
123Given a pointer to a string, parse the string extracting fields
124separated by whitespace and optionally enclosed within either single
125or double quotes (which are stripped off), and build a vector of
126pointers to copies of the string for each field. The input string
127remains unchanged. The last element of the vector is followed by a
128@code{NULL} element.
6599da04 129
aac04c15
DD
130All of the memory for the pointer array and copies of the string
131is obtained from @code{malloc}. All of the memory can be returned to the
132system with the single function call @code{freeargv}, which takes the
133returned result of @code{buildargv}, as it's argument.
6599da04 134
5bed56d9 135Returns a pointer to the argument vector if successful. Returns
aac04c15
DD
136@code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
137memory to complete building the argument vector.
6599da04 138
aac04c15
DD
139If the input is a null string (as opposed to a @code{NULL} pointer),
140then buildarg returns an argument vector that has one arg, a null
141string.
6599da04 142
aac04c15 143@end deftypefn
6599da04 144
aac04c15 145The memory for the argv array is dynamically expanded as necessary.
6599da04 146
aac04c15
DD
147In order to provide a working buffer for extracting arguments into,
148with appropriate stripping of quotes and translation of backslash
149sequences, we allocate a working buffer at least as long as the input
150string. This ensures that we always have enough space in which to
151work, since the extracted arg is never larger than the input string.
6599da04 152
aac04c15
DD
153The argument vector is always kept terminated with a @code{NULL} arg
154pointer, so it can be passed to @code{freeargv} at any time, or
155returned, as appropriate.
6599da04 156
6599da04
JM
157*/
158
9486db4f 159char **buildargv (const char *input)
6599da04
JM
160{
161 char *arg;
162 char *copybuf;
163 int squote = 0;
164 int dquote = 0;
165 int bsquote = 0;
166 int argc = 0;
167 int maxargc = 0;
168 char **argv = NULL;
169 char **nargv;
170
171 if (input != NULL)
172 {
087aa398 173 copybuf = (char *) alloca (strlen (input) + 1);
6599da04
JM
174 /* Is a do{}while to always execute the loop once. Always return an
175 argv, even for null strings. See NOTES above, test case below. */
176 do
177 {
178 /* Pick off argv[argc] */
f6bbde28 179 while (ISBLANK (*input))
6599da04
JM
180 {
181 input++;
182 }
183 if ((maxargc == 0) || (argc >= (maxargc - 1)))
184 {
185 /* argv needs initialization, or expansion */
186 if (argv == NULL)
187 {
188 maxargc = INITIAL_MAXARGC;
189 nargv = (char **) malloc (maxargc * sizeof (char *));
190 }
191 else
192 {
193 maxargc *= 2;
194 nargv = (char **) realloc (argv, maxargc * sizeof (char *));
195 }
196 if (nargv == NULL)
197 {
198 if (argv != NULL)
199 {
200 freeargv (argv);
201 argv = NULL;
202 }
203 break;
204 }
205 argv = nargv;
206 argv[argc] = NULL;
207 }
208 /* Begin scanning arg */
209 arg = copybuf;
210 while (*input != EOS)
211 {
f6bbde28 212 if (ISBLANK (*input) && !squote && !dquote && !bsquote)
6599da04
JM
213 {
214 break;
215 }
216 else
217 {
218 if (bsquote)
219 {
220 bsquote = 0;
221 *arg++ = *input;
222 }
223 else if (*input == '\\')
224 {
225 bsquote = 1;
226 }
227 else if (squote)
228 {
229 if (*input == '\'')
230 {
231 squote = 0;
232 }
233 else
234 {
235 *arg++ = *input;
236 }
237 }
238 else if (dquote)
239 {
240 if (*input == '"')
241 {
242 dquote = 0;
243 }
244 else
245 {
246 *arg++ = *input;
247 }
248 }
249 else
250 {
251 if (*input == '\'')
252 {
253 squote = 1;
254 }
255 else if (*input == '"')
256 {
257 dquote = 1;
258 }
259 else
260 {
261 *arg++ = *input;
262 }
263 }
264 input++;
265 }
266 }
267 *arg = EOS;
268 argv[argc] = strdup (copybuf);
269 if (argv[argc] == NULL)
270 {
271 freeargv (argv);
272 argv = NULL;
273 break;
274 }
275 argc++;
276 argv[argc] = NULL;
277
f6bbde28 278 while (ISBLANK (*input))
6599da04
JM
279 {
280 input++;
281 }
282 }
283 while (*input != EOS);
284 }
285 return (argv);
286}
287
288#ifdef MAIN
289
290/* Simple little test driver. */
291
0be6abca 292static const char *const tests[] =
6599da04
JM
293{
294 "a simple command line",
295 "arg 'foo' is single quoted",
296 "arg \"bar\" is double quoted",
297 "arg \"foo bar\" has embedded whitespace",
298 "arg 'Jack said \\'hi\\'' has single quotes",
299 "arg 'Jack said \\\"hi\\\"' has double quotes",
300 "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9",
301
302 /* This should be expanded into only one argument. */
303 "trailing-whitespace ",
304
305 "",
306 NULL
307};
308
9486db4f
GDR
309int
310main (void)
6599da04
JM
311{
312 char **argv;
0be6abca 313 const char *const *test;
6599da04
JM
314 char **targs;
315
316 for (test = tests; *test != NULL; test++)
317 {
318 printf ("buildargv(\"%s\")\n", *test);
319 if ((argv = buildargv (*test)) == NULL)
320 {
321 printf ("failed!\n\n");
322 }
323 else
324 {
325 for (targs = argv; *targs != NULL; targs++)
326 {
327 printf ("\t\"%s\"\n", *targs);
328 }
329 printf ("\n");
330 }
331 freeargv (argv);
332 }
333
0be6abca 334 return 0;
6599da04
JM
335}
336
337#endif /* MAIN */