2002-02-04 Jeff Johnston <jjohnstn@redhat.com>
[binutils.git] / libiberty / argv.c
blob4205579a576530089f432ca737e4fb10b43e85cb
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. */
25 #include "ansidecl.h"
26 #include "libiberty.h"
28 #define ISBLANK(ch) ((ch) == ' ' || (ch) == '\t')
30 /* Routines imported from standard C runtime libraries. */
32 #ifdef __STDC__
34 #include <stddef.h>
35 #include <string.h>
36 #include <stdlib.h>
38 #else /* !__STDC__ */
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 */
47 #endif
49 #endif /* __STDC__ */
52 #ifndef NULL
53 #define NULL 0
54 #endif
56 #ifndef EOS
57 #define EOS '\0'
58 #endif
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
71 argument vector.
73 @end deftypefn
77 char **
78 dupargv (argv)
79 char **argv;
81 int argc;
82 char **copy;
84 if (argv == NULL)
85 return NULL;
87 /* the vector */
88 for (argc = 0; argv[argc] != NULL; argc++);
89 copy = (char **) malloc ((argc + 1) * sizeof (char *));
90 if (copy == NULL)
91 return NULL;
93 /* the strings */
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)
100 freeargv (copy);
101 return NULL;
103 strcpy (copy[argc], argv[argc]);
105 copy[argc] = NULL;
106 return copy;
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}
116 itself.
118 @end deftypefn
122 void freeargv (vector)
123 char **vector;
125 register char **scan;
127 if (vector != NULL)
129 for (scan = vector; *scan != NULL; scan++)
131 free (*scan);
133 free (vector);
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
146 @code{NULL} element.
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
159 string.
161 @end deftypefn
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)
178 const char *input;
180 char *arg;
181 char *copybuf;
182 int squote = 0;
183 int dquote = 0;
184 int bsquote = 0;
185 int argc = 0;
186 int maxargc = 0;
187 char **argv = NULL;
188 char **nargv;
190 if (input != NULL)
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))
200 input++;
202 if ((maxargc == 0) || (argc >= (maxargc - 1)))
204 /* argv needs initialization, or expansion */
205 if (argv == NULL)
207 maxargc = INITIAL_MAXARGC;
208 nargv = (char **) malloc (maxargc * sizeof (char *));
210 else
212 maxargc *= 2;
213 nargv = (char **) realloc (argv, maxargc * sizeof (char *));
215 if (nargv == NULL)
217 if (argv != NULL)
219 freeargv (argv);
220 argv = NULL;
222 break;
224 argv = nargv;
225 argv[argc] = NULL;
227 /* Begin scanning arg */
228 arg = copybuf;
229 while (*input != EOS)
231 if (ISBLANK (*input) && !squote && !dquote && !bsquote)
233 break;
235 else
237 if (bsquote)
239 bsquote = 0;
240 *arg++ = *input;
242 else if (*input == '\\')
244 bsquote = 1;
246 else if (squote)
248 if (*input == '\'')
250 squote = 0;
252 else
254 *arg++ = *input;
257 else if (dquote)
259 if (*input == '"')
261 dquote = 0;
263 else
265 *arg++ = *input;
268 else
270 if (*input == '\'')
272 squote = 1;
274 else if (*input == '"')
276 dquote = 1;
278 else
280 *arg++ = *input;
283 input++;
286 *arg = EOS;
287 argv[argc] = strdup (copybuf);
288 if (argv[argc] == NULL)
290 freeargv (argv);
291 argv = NULL;
292 break;
294 argc++;
295 argv[argc] = NULL;
297 while (ISBLANK (*input))
299 input++;
302 while (*input != EOS);
304 return (argv);
307 #ifdef MAIN
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 ",
325 NULL
328 int main ()
330 char **argv;
331 const char *const *test;
332 char **targs;
334 for (test = tests; *test != NULL; test++)
336 printf ("buildargv(\"%s\")\n", *test);
337 if ((argv = buildargv (*test)) == NULL)
339 printf ("failed!\n\n");
341 else
343 for (targs = argv; *targs != NULL; targs++)
345 printf ("\t\"%s\"\n", *targs);
347 printf ("\n");
349 freeargv (argv);
352 return 0;
355 #endif /* MAIN */