Approved by nickc@redhat.com
[binutils.git] / libiberty / argv.c
blob76502058d3eee64feabebac84967c9d9a2541f2a
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 #include <stddef.h>
33 #include <string.h>
34 #include <stdlib.h>
36 #ifndef NULL
37 #define NULL 0
38 #endif
40 #ifndef EOS
41 #define EOS '\0'
42 #endif
44 #define INITIAL_MAXARGC 8 /* Number of args + NULL in initial argv */
49 @deftypefn Extension char** dupargv (char **@var{vector})
51 Duplicate an argument vector. Simply scans through @var{vector},
52 duplicating each argument until the terminating @code{NULL} is found.
53 Returns a pointer to the argument vector if successful. Returns
54 @code{NULL} if there is insufficient memory to complete building the
55 argument vector.
57 @end deftypefn
61 char **
62 dupargv (char **argv)
64 int argc;
65 char **copy;
67 if (argv == NULL)
68 return NULL;
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;
76 /* the strings */
77 for (argc = 0; argv[argc] != NULL; argc++)
79 int len = strlen (argv[argc]);
80 copy[argc] = malloc (sizeof (char *) * (len + 1));
81 if (copy[argc] == NULL)
83 freeargv (copy);
84 return NULL;
86 strcpy (copy[argc], argv[argc]);
88 copy[argc] = NULL;
89 return copy;
94 @deftypefn Extension void freeargv (char **@var{vector})
96 Free an argument vector that was built using @code{buildargv}. Simply
97 scans through @var{vector}, freeing the memory for each argument until
98 the terminating @code{NULL} is found, and then frees @var{vector}
99 itself.
101 @end deftypefn
105 void freeargv (char **vector)
107 register char **scan;
109 if (vector != NULL)
111 for (scan = vector; *scan != NULL; scan++)
113 free (*scan);
115 free (vector);
121 @deftypefn Extension char** buildargv (char *@var{sp})
123 Given a pointer to a string, parse the string extracting fields
124 separated by whitespace and optionally enclosed within either single
125 or double quotes (which are stripped off), and build a vector of
126 pointers to copies of the string for each field. The input string
127 remains unchanged. The last element of the vector is followed by a
128 @code{NULL} element.
130 All of the memory for the pointer array and copies of the string
131 is obtained from @code{malloc}. All of the memory can be returned to the
132 system with the single function call @code{freeargv}, which takes the
133 returned result of @code{buildargv}, as it's argument.
135 Returns a pointer to the argument vector if successful. Returns
136 @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
137 memory to complete building the argument vector.
139 If the input is a null string (as opposed to a @code{NULL} pointer),
140 then buildarg returns an argument vector that has one arg, a null
141 string.
143 @end deftypefn
145 The memory for the argv array is dynamically expanded as necessary.
147 In order to provide a working buffer for extracting arguments into,
148 with appropriate stripping of quotes and translation of backslash
149 sequences, we allocate a working buffer at least as long as the input
150 string. This ensures that we always have enough space in which to
151 work, since the extracted arg is never larger than the input string.
153 The argument vector is always kept terminated with a @code{NULL} arg
154 pointer, so it can be passed to @code{freeargv} at any time, or
155 returned, as appropriate.
159 char **buildargv (const char *input)
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;
171 if (input != NULL)
173 copybuf = (char *) alloca (strlen (input) + 1);
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. */
178 /* Pick off argv[argc] */
179 while (ISBLANK (*input))
181 input++;
183 if ((maxargc == 0) || (argc >= (maxargc - 1)))
185 /* argv needs initialization, or expansion */
186 if (argv == NULL)
188 maxargc = INITIAL_MAXARGC;
189 nargv = (char **) malloc (maxargc * sizeof (char *));
191 else
193 maxargc *= 2;
194 nargv = (char **) realloc (argv, maxargc * sizeof (char *));
196 if (nargv == NULL)
198 if (argv != NULL)
200 freeargv (argv);
201 argv = NULL;
203 break;
205 argv = nargv;
206 argv[argc] = NULL;
208 /* Begin scanning arg */
209 arg = copybuf;
210 while (*input != EOS)
212 if (ISBLANK (*input) && !squote && !dquote && !bsquote)
214 break;
216 else
218 if (bsquote)
220 bsquote = 0;
221 *arg++ = *input;
223 else if (*input == '\\')
225 bsquote = 1;
227 else if (squote)
229 if (*input == '\'')
231 squote = 0;
233 else
235 *arg++ = *input;
238 else if (dquote)
240 if (*input == '"')
242 dquote = 0;
244 else
246 *arg++ = *input;
249 else
251 if (*input == '\'')
253 squote = 1;
255 else if (*input == '"')
257 dquote = 1;
259 else
261 *arg++ = *input;
264 input++;
267 *arg = EOS;
268 argv[argc] = strdup (copybuf);
269 if (argv[argc] == NULL)
271 freeargv (argv);
272 argv = NULL;
273 break;
275 argc++;
276 argv[argc] = NULL;
278 while (ISBLANK (*input))
280 input++;
283 while (*input != EOS);
285 return (argv);
288 #ifdef MAIN
290 /* Simple little test driver. */
292 static const char *const tests[] =
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",
302 /* This should be expanded into only one argument. */
303 "trailing-whitespace ",
306 NULL
310 main (void)
312 char **argv;
313 const char *const *test;
314 char **targs;
316 for (test = tests; *test != NULL; test++)
318 printf ("buildargv(\"%s\")\n", *test);
319 if ((argv = buildargv (*test)) == NULL)
321 printf ("failed!\n\n");
323 else
325 for (targs = argv; *targs != NULL; targs++)
327 printf ("\t\"%s\"\n", *targs);
329 printf ("\n");
331 freeargv (argv);
334 return 0;
337 #endif /* MAIN */