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. */
29 #include "libiberty.h"
30 #include "safe-ctype.h"
32 /* Routines imported from standard C runtime libraries. */
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
74 for (argc
= 0; argv
[argc
] != NULL
; argc
++);
75 copy
= (char **) xmalloc ((argc
+ 1) * sizeof (char *));
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
]);
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}
101 void freeargv (char **vector
)
103 register char **scan
;
107 for (scan
= vector
; *scan
!= NULL
; scan
++)
116 consume_whitespace (const char **input
)
118 while (ISSPACE (**input
))
125 only_whitespace (const char* input
)
127 while (*input
!= EOS
&& ISSPACE (*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
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
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
)
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 */
200 maxargc
= INITIAL_MAXARGC
;
201 nargv
= (char **) xmalloc (maxargc
* sizeof (char *));
206 nargv
= (char **) xrealloc (argv
, maxargc
* sizeof (char *));
211 /* Begin scanning arg */
213 while (*input
!= EOS
)
215 if (ISSPACE (*input
) && !squote
&& !dquote
&& !bsquote
)
226 else if (*input
== '\\')
258 else if (*input
== '"')
271 argv
[argc
] = xstrdup (copybuf
);
275 consume_whitespace (&input
);
277 while (*input
!= EOS
);
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.
297 writeargv (char **argv
, FILE *f
)
304 while (*argv
!= NULL
)
306 const char *arg
= *argv
;
312 if (ISSPACE(c
) || c
== '\\' || c
== '\'' || c
== '"')
313 if (EOF
== fputc ('\\', f
))
319 if (EOF
== fputc (c
, f
))
327 if (EOF
== fputc ('\n', f
))
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.
363 expandargv (int *argcp
, char ***argvp
)
365 /* The argument we are currently processing. */
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. */
376 /* The name of the response file. */
377 const char *filename
;
378 /* The response file. */
380 /* An upper bound on the number of characters in the response
383 /* The number of characters in the response file, when actually
386 /* A dynamically allocated buffer used to hold options read from a
389 /* Dynamically allocated storage for the options read from the
392 /* The number of options read from the response file, if any. */
394 /* We are only interested in options of the form "@file". */
395 filename
= (*argvp
)[i
];
396 if (filename
[0] != '@')
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]);
404 /* Read the contents of the file. */
405 f
= fopen (++filename
, "r");
408 if (fseek (f
, 0L, SEEK_END
) == -1)
413 if (fseek (f
, 0L, SEEK_SET
) == -1)
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. */
423 /* Add a NUL terminator. */
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,
428 if (only_whitespace (buffer
))
430 file_argv
= (char **) xmalloc (sizeof (char *));
434 /* Parse the string. */
435 file_argv
= buildargv (buffer
);
436 /* If *ARGVP is not already dynamically allocated, copy it. */
438 *argvp
= dupargv (*argvp
);
439 /* Count the number of arguments. */
441 while (file_argv
[file_argc
])
443 /* Now, insert FILE_ARGV into ARGV. The "+1" below handles the
444 NULL terminator at the end of ARGV. */
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
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. */
459 /* Rescan all of the arguments just read to support response
460 files that include other response files. */
463 /* We're all done with the file now. */
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.
480 countargv (char **argv
)
486 for (argc
= 0; argv
[argc
] != NULL
; argc
++)
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 ",
516 const char *const *test
;
519 for (test
= tests
; *test
!= NULL
; test
++)
521 printf ("buildargv(\"%s\")\n", *test
);
522 if ((argv
= buildargv (*test
)) == NULL
)
524 printf ("failed!\n\n");
528 for (targs
= argv
; *targs
!= NULL
; targs
++)
530 printf ("\t\"%s\"\n", *targs
);