2015-01-07 Sandra Loosemore <sandra@codesourcery.com>
[official-gcc.git] / libiberty / argv.c
blobf2727e8de95db2d849f57e690e64a9dbc08853ff
1 /* Create and destroy argument vectors (argv's)
2 Copyright (C) 1992, 2001, 2010, 2012 Free Software Foundation, Inc.
3 Written by Fred Fish @ Cygnus Support
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
22 /* Create and destroy argument vectors. An argument vector is simply an
23 array of string pointers, terminated by a NULL pointer. */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include "ansidecl.h"
29 #include "libiberty.h"
30 #include "safe-ctype.h"
32 /* Routines imported from standard C runtime libraries. */
34 #include <stddef.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdio.h>
39 #ifndef NULL
40 #define NULL 0
41 #endif
43 #ifndef EOS
44 #define EOS '\0'
45 #endif
47 #define INITIAL_MAXARGC 8 /* Number of args + NULL in initial argv */
52 @deftypefn Extension char** dupargv (char **@var{vector})
54 Duplicate an argument vector. Simply scans through @var{vector},
55 duplicating each argument until the terminating @code{NULL} is found.
56 Returns a pointer to the argument vector if successful. Returns
57 @code{NULL} if there is insufficient memory to complete building the
58 argument vector.
60 @end deftypefn
64 char **
65 dupargv (char **argv)
67 int argc;
68 char **copy;
70 if (argv == NULL)
71 return NULL;
73 /* the vector */
74 for (argc = 0; argv[argc] != NULL; argc++);
75 copy = (char **) xmalloc ((argc + 1) * sizeof (char *));
77 /* the strings */
78 for (argc = 0; argv[argc] != NULL; argc++)
80 int len = strlen (argv[argc]);
81 copy[argc] = (char *) xmalloc (len + 1);
82 strcpy (copy[argc], argv[argc]);
84 copy[argc] = NULL;
85 return copy;
90 @deftypefn Extension void freeargv (char **@var{vector})
92 Free an argument vector that was built using @code{buildargv}. Simply
93 scans through @var{vector}, freeing the memory for each argument until
94 the terminating @code{NULL} is found, and then frees @var{vector}
95 itself.
97 @end deftypefn
101 void freeargv (char **vector)
103 register char **scan;
105 if (vector != NULL)
107 for (scan = vector; *scan != NULL; scan++)
109 free (*scan);
111 free (vector);
115 static void
116 consume_whitespace (const char **input)
118 while (ISSPACE (**input))
120 (*input)++;
124 static int
125 only_whitespace (const char* input)
127 while (*input != EOS && ISSPACE (*input))
128 input++;
130 return (*input == EOS);
135 @deftypefn Extension char** buildargv (char *@var{sp})
137 Given a pointer to a string, parse the string extracting fields
138 separated by whitespace and optionally enclosed within either single
139 or double quotes (which are stripped off), and build a vector of
140 pointers to copies of the string for each field. The input string
141 remains unchanged. The last element of the vector is followed by a
142 @code{NULL} element.
144 All of the memory for the pointer array and copies of the string
145 is obtained from @code{xmalloc}. All of the memory can be returned to the
146 system with the single function call @code{freeargv}, which takes the
147 returned result of @code{buildargv}, as it's argument.
149 Returns a pointer to the argument vector if successful. Returns
150 @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
151 memory to complete building the argument vector.
153 If the input is a null string (as opposed to a @code{NULL} pointer),
154 then buildarg returns an argument vector that has one arg, a null
155 string.
157 @end deftypefn
159 The memory for the argv array is dynamically expanded as necessary.
161 In order to provide a working buffer for extracting arguments into,
162 with appropriate stripping of quotes and translation of backslash
163 sequences, we allocate a working buffer at least as long as the input
164 string. This ensures that we always have enough space in which to
165 work, since the extracted arg is never larger than the input string.
167 The argument vector is always kept terminated with a @code{NULL} arg
168 pointer, so it can be passed to @code{freeargv} at any time, or
169 returned, as appropriate.
173 char **buildargv (const char *input)
175 char *arg;
176 char *copybuf;
177 int squote = 0;
178 int dquote = 0;
179 int bsquote = 0;
180 int argc = 0;
181 int maxargc = 0;
182 char **argv = NULL;
183 char **nargv;
185 if (input != NULL)
187 copybuf = (char *) xmalloc (strlen (input) + 1);
188 /* Is a do{}while to always execute the loop once. Always return an
189 argv, even for null strings. See NOTES above, test case below. */
192 /* Pick off argv[argc] */
193 consume_whitespace (&input);
195 if ((maxargc == 0) || (argc >= (maxargc - 1)))
197 /* argv needs initialization, or expansion */
198 if (argv == NULL)
200 maxargc = INITIAL_MAXARGC;
201 nargv = (char **) xmalloc (maxargc * sizeof (char *));
203 else
205 maxargc *= 2;
206 nargv = (char **) xrealloc (argv, maxargc * sizeof (char *));
208 argv = nargv;
209 argv[argc] = NULL;
211 /* Begin scanning arg */
212 arg = copybuf;
213 while (*input != EOS)
215 if (ISSPACE (*input) && !squote && !dquote && !bsquote)
217 break;
219 else
221 if (bsquote)
223 bsquote = 0;
224 *arg++ = *input;
226 else if (*input == '\\')
228 bsquote = 1;
230 else if (squote)
232 if (*input == '\'')
234 squote = 0;
236 else
238 *arg++ = *input;
241 else if (dquote)
243 if (*input == '"')
245 dquote = 0;
247 else
249 *arg++ = *input;
252 else
254 if (*input == '\'')
256 squote = 1;
258 else if (*input == '"')
260 dquote = 1;
262 else
264 *arg++ = *input;
267 input++;
270 *arg = EOS;
271 argv[argc] = xstrdup (copybuf);
272 argc++;
273 argv[argc] = NULL;
275 consume_whitespace (&input);
277 while (*input != EOS);
279 free (copybuf);
281 return (argv);
286 @deftypefn Extension int writeargv (const char **@var{argv}, FILE *@var{file})
288 Write each member of ARGV, handling all necessary quoting, to the file
289 named by FILE, separated by whitespace. Return 0 on success, non-zero
290 if an error occurred while writing to FILE.
292 @end deftypefn
297 writeargv (char **argv, FILE *f)
299 int status = 0;
301 if (f == NULL)
302 return 1;
304 while (*argv != NULL)
306 const char *arg = *argv;
308 while (*arg != EOS)
310 char c = *arg;
312 if (ISSPACE(c) || c == '\\' || c == '\'' || c == '"')
313 if (EOF == fputc ('\\', f))
315 status = 1;
316 goto done;
319 if (EOF == fputc (c, f))
321 status = 1;
322 goto done;
324 arg++;
327 if (EOF == fputc ('\n', f))
329 status = 1;
330 goto done;
332 argv++;
335 done:
336 return status;
341 @deftypefn Extension void expandargv (int *@var{argcp}, char ***@var{argvp})
343 The @var{argcp} and @code{argvp} arguments are pointers to the usual
344 @code{argc} and @code{argv} arguments to @code{main}. This function
345 looks for arguments that begin with the character @samp{@@}. Any such
346 arguments are interpreted as ``response files''. The contents of the
347 response file are interpreted as additional command line options. In
348 particular, the file is separated into whitespace-separated strings;
349 each such string is taken as a command-line option. The new options
350 are inserted in place of the option naming the response file, and
351 @code{*argcp} and @code{*argvp} will be updated. If the value of
352 @code{*argvp} is modified by this function, then the new value has
353 been dynamically allocated and can be deallocated by the caller with
354 @code{freeargv}. However, most callers will simply call
355 @code{expandargv} near the beginning of @code{main} and allow the
356 operating system to free the memory when the program exits.
358 @end deftypefn
362 void
363 expandargv (int *argcp, char ***argvp)
365 /* The argument we are currently processing. */
366 int i = 0;
367 /* Non-zero if ***argvp has been dynamically allocated. */
368 int argv_dynamic = 0;
369 /* Limit the number of response files that we parse in order
370 to prevent infinite recursion. */
371 unsigned int iteration_limit = 2000;
372 /* Loop over the arguments, handling response files. We always skip
373 ARGVP[0], as that is the name of the program being run. */
374 while (++i < *argcp)
376 /* The name of the response file. */
377 const char *filename;
378 /* The response file. */
379 FILE *f;
380 /* An upper bound on the number of characters in the response
381 file. */
382 long pos;
383 /* The number of characters in the response file, when actually
384 read. */
385 size_t len;
386 /* A dynamically allocated buffer used to hold options read from a
387 response file. */
388 char *buffer;
389 /* Dynamically allocated storage for the options read from the
390 response file. */
391 char **file_argv;
392 /* The number of options read from the response file, if any. */
393 size_t file_argc;
394 /* We are only interested in options of the form "@file". */
395 filename = (*argvp)[i];
396 if (filename[0] != '@')
397 continue;
398 /* If we have iterated too many times then stop. */
399 if (-- iteration_limit == 0)
401 fprintf (stderr, "%s: error: too many @-files encountered\n", (*argvp)[0]);
402 xexit (1);
404 /* Read the contents of the file. */
405 f = fopen (++filename, "r");
406 if (!f)
407 continue;
408 if (fseek (f, 0L, SEEK_END) == -1)
409 goto error;
410 pos = ftell (f);
411 if (pos == -1)
412 goto error;
413 if (fseek (f, 0L, SEEK_SET) == -1)
414 goto error;
415 buffer = (char *) xmalloc (pos * sizeof (char) + 1);
416 len = fread (buffer, sizeof (char), pos, f);
417 if (len != (size_t) pos
418 /* On Windows, fread may return a value smaller than POS,
419 due to CR/LF->CR translation when reading text files.
420 That does not in-and-of itself indicate failure. */
421 && ferror (f))
422 goto error;
423 /* Add a NUL terminator. */
424 buffer[len] = '\0';
425 /* If the file is empty or contains only whitespace, buildargv would
426 return a single empty argument. In this context we want no arguments,
427 instead. */
428 if (only_whitespace (buffer))
430 file_argv = (char **) xmalloc (sizeof (char *));
431 file_argv[0] = NULL;
433 else
434 /* Parse the string. */
435 file_argv = buildargv (buffer);
436 /* If *ARGVP is not already dynamically allocated, copy it. */
437 if (!argv_dynamic)
438 *argvp = dupargv (*argvp);
439 /* Count the number of arguments. */
440 file_argc = 0;
441 while (file_argv[file_argc])
442 ++file_argc;
443 /* Now, insert FILE_ARGV into ARGV. The "+1" below handles the
444 NULL terminator at the end of ARGV. */
445 *argvp = ((char **)
446 xrealloc (*argvp,
447 (*argcp + file_argc + 1) * sizeof (char *)));
448 memmove (*argvp + i + file_argc, *argvp + i + 1,
449 (*argcp - i) * sizeof (char *));
450 memcpy (*argvp + i, file_argv, file_argc * sizeof (char *));
451 /* The original option has been replaced by all the new
452 options. */
453 *argcp += file_argc - 1;
454 /* Free up memory allocated to process the response file. We do
455 not use freeargv because the individual options in FILE_ARGV
456 are now in the main ARGV. */
457 free (file_argv);
458 free (buffer);
459 /* Rescan all of the arguments just read to support response
460 files that include other response files. */
461 --i;
462 error:
463 /* We're all done with the file now. */
464 fclose (f);
470 @deftypefn Extension int countargv (char **@var{argv})
472 Return the number of elements in @var{argv}.
473 Returns zero if @var{argv} is NULL.
475 @end deftypefn
480 countargv (char **argv)
482 int argc;
484 if (argv == NULL)
485 return 0;
486 for (argc = 0; argv[argc] != NULL; argc++)
487 continue;
488 return argc;
491 #ifdef MAIN
493 /* Simple little test driver. */
495 static const char *const tests[] =
497 "a simple command line",
498 "arg 'foo' is single quoted",
499 "arg \"bar\" is double quoted",
500 "arg \"foo bar\" has embedded whitespace",
501 "arg 'Jack said \\'hi\\'' has single quotes",
502 "arg 'Jack said \\\"hi\\\"' has double quotes",
503 "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",
505 /* This should be expanded into only one argument. */
506 "trailing-whitespace ",
509 NULL
513 main (void)
515 char **argv;
516 const char *const *test;
517 char **targs;
519 for (test = tests; *test != NULL; test++)
521 printf ("buildargv(\"%s\")\n", *test);
522 if ((argv = buildargv (*test)) == NULL)
524 printf ("failed!\n\n");
526 else
528 for (targs = argv; *targs != NULL; targs++)
530 printf ("\t\"%s\"\n", *targs);
532 printf ("\n");
534 freeargv (argv);
537 return 0;
540 #endif /* MAIN */