1
/* Create and destroy argument vectors (argv's)
2
Copyright (C) 1992, 2001 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., 59 Temple Place - Suite 330,
19
Boston, MA 02111-1307, USA. */
22
/* Create and destroy argument vectors. An argument vector is simply an
23
array of string pointers, terminated by a NULL pointer. */
26
#include "libiberty.h"
28
#define ISBLANK(ch) ((ch) == ' ' || (ch) == '\t')
30
/* Routines imported from standard C runtime libraries. */
32
#ifdef ANSI_PROTOTYPES
38
#else /* !ANSI_PROTOTYPES */
40
#if !defined _WIN32 || defined __GNUC__
41
extern char *memcpy (); /* Copy memory region */
42
extern int strlen (); /* Count length of string */
43
extern char *malloc (); /* Standard memory allocater */
44
extern char *realloc (); /* Standard memory reallocator */
45
extern void free (); /* Free malloc'd memory */
46
extern char *strdup (); /* Duplicate a string */
49
#endif /* ANSI_PROTOTYPES */
60
#define INITIAL_MAXARGC 8 /* Number of args + NULL in initial argv */
65
@deftypefn Extension char** dupargv (char **@var{vector})
67
Duplicate an argument vector. Simply scans through @var{vector},
68
duplicating each argument until the terminating @code{NULL} is found.
69
Returns a pointer to the argument vector if successful. Returns
70
@code{NULL} if there is insufficient memory to complete building the
88
for (argc = 0; argv[argc] != NULL; argc++);
89
copy = (char **) malloc ((argc + 1) * sizeof (char *));
94
for (argc = 0; argv[argc] != NULL; argc++)
96
int len = strlen (argv[argc]);
97
copy[argc] = malloc (sizeof (char *) * (len + 1));
98
if (copy[argc] == NULL)
103
strcpy (copy[argc], argv[argc]);
111
@deftypefn Extension void freeargv (char **@var{vector})
113
Free an argument vector that was built using @code{buildargv}. Simply
114
scans through @var{vector}, freeing the memory for each argument until
115
the terminating @code{NULL} is found, and then frees @var{vector}
122
void freeargv (vector)
125
register char **scan;
129
for (scan = vector; *scan != NULL; scan++)
139
@deftypefn Extension char** buildargv (char *@var{sp})
141
Given a pointer to a string, parse the string extracting fields
142
separated by whitespace and optionally enclosed within either single
143
or double quotes (which are stripped off), and build a vector of
144
pointers to copies of the string for each field. The input string
145
remains unchanged. The last element of the vector is followed by a
148
All of the memory for the pointer array and copies of the string
149
is obtained from @code{malloc}. All of the memory can be returned to the
150
system with the single function call @code{freeargv}, which takes the
151
returned result of @code{buildargv}, as it's argument.
153
Returns a pointer to the argument vector if successful. Returns
154
@code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
155
memory to complete building the argument vector.
157
If the input is a null string (as opposed to a @code{NULL} pointer),
158
then buildarg returns an argument vector that has one arg, a null
163
The memory for the argv array is dynamically expanded as necessary.
165
In order to provide a working buffer for extracting arguments into,
166
with appropriate stripping of quotes and translation of backslash
167
sequences, we allocate a working buffer at least as long as the input
168
string. This ensures that we always have enough space in which to
169
work, since the extracted arg is never larger than the input string.
171
The argument vector is always kept terminated with a @code{NULL} arg
172
pointer, so it can be passed to @code{freeargv} at any time, or
173
returned, as appropriate.
177
char **buildargv (input)
192
copybuf = (char *) alloca (strlen (input) + 1);
193
/* Is a do{}while to always execute the loop once. Always return an
194
argv, even for null strings. See NOTES above, test case below. */
197
/* Pick off argv[argc] */
198
while (ISBLANK (*input))
202
if ((maxargc == 0) || (argc >= (maxargc - 1)))
204
/* argv needs initialization, or expansion */
207
maxargc = INITIAL_MAXARGC;
208
nargv = (char **) malloc (maxargc * sizeof (char *));
213
nargv = (char **) realloc (argv, maxargc * sizeof (char *));
227
/* Begin scanning arg */
229
while (*input != EOS)
231
if (ISBLANK (*input) && !squote && !dquote && !bsquote)
242
else if (*input == '\\')
274
else if (*input == '"')
287
argv[argc] = strdup (copybuf);
288
if (argv[argc] == NULL)
297
while (ISBLANK (*input))
302
while (*input != EOS);
309
/* Simple little test driver. */
311
static const char *const tests[] =
313
"a simple command line",
314
"arg 'foo' is single quoted",
315
"arg \"bar\" is double quoted",
316
"arg \"foo bar\" has embedded whitespace",
317
"arg 'Jack said \\'hi\\'' has single quotes",
318
"arg 'Jack said \\\"hi\\\"' has double quotes",
319
"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",
321
/* This should be expanded into only one argument. */
322
"trailing-whitespace ",
331
const char *const *test;
334
for (test = tests; *test != NULL; test++)
336
printf ("buildargv(\"%s\")\n", *test);
337
if ((argv = buildargv (*test)) == NULL)
339
printf ("failed!\n\n");
343
for (targs = argv; *targs != NULL; targs++)
345
printf ("\t\"%s\"\n", *targs);