2005-09-08 Paul Brook <paul@codesourcery.com>
[deliverable/binutils-gdb.git] / libiberty / argv.c
CommitLineData
252b5132 1/* Create and destroy argument vectors (argv's)
e6450fe5 2 Copyright (C) 1992, 2001 Free Software Foundation, Inc.
252b5132
RH
3 Written by Fred Fish @ Cygnus Support
4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If
979c05d3
NC
18not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19Boston, MA 02110-1301, USA. */
252b5132
RH
20
21
22/* Create and destroy argument vectors. An argument vector is simply an
23 array of string pointers, terminated by a NULL pointer. */
24
fa99459d
DD
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
252b5132
RH
28#include "ansidecl.h"
29#include "libiberty.h"
30
ac424eb3 31#define ISBLANK(ch) ((ch) == ' ' || (ch) == '\t')
252b5132
RH
32
33/* Routines imported from standard C runtime libraries. */
34
252b5132 35#include <stddef.h>
35ca97ea
RH
36#include <string.h>
37#include <stdlib.h>
252b5132 38
252b5132
RH
39#ifndef NULL
40#define NULL 0
41#endif
42
43#ifndef EOS
44#define EOS '\0'
45#endif
46
47#define INITIAL_MAXARGC 8 /* Number of args + NULL in initial argv */
48
49
50/*
51
ba19b94f 52@deftypefn Extension char** dupargv (char **@var{vector})
252b5132 53
ba19b94f
DD
54Duplicate an argument vector. Simply scans through @var{vector},
55duplicating each argument until the terminating @code{NULL} is found.
5d852400 56Returns a pointer to the argument vector if successful. Returns
ba19b94f
DD
57@code{NULL} if there is insufficient memory to complete building the
58argument vector.
252b5132 59
ba19b94f 60@end deftypefn
252b5132
RH
61
62*/
63
64char **
9334f9c6 65dupargv (char **argv)
252b5132
RH
66{
67 int argc;
68 char **copy;
69
70 if (argv == NULL)
71 return NULL;
72
73 /* the vector */
74 for (argc = 0; argv[argc] != NULL; argc++);
75 copy = (char **) malloc ((argc + 1) * sizeof (char *));
76 if (copy == NULL)
77 return NULL;
78
79 /* the strings */
80 for (argc = 0; argv[argc] != NULL; argc++)
81 {
82 int len = strlen (argv[argc]);
ab70e2a5 83 copy[argc] = (char *) malloc (len + 1);
252b5132
RH
84 if (copy[argc] == NULL)
85 {
86 freeargv (copy);
87 return NULL;
88 }
89 strcpy (copy[argc], argv[argc]);
90 }
91 copy[argc] = NULL;
92 return copy;
93}
94
95/*
96
ba19b94f 97@deftypefn Extension void freeargv (char **@var{vector})
252b5132 98
ba19b94f
DD
99Free an argument vector that was built using @code{buildargv}. Simply
100scans through @var{vector}, freeing the memory for each argument until
101the terminating @code{NULL} is found, and then frees @var{vector}
102itself.
252b5132 103
ba19b94f 104@end deftypefn
252b5132
RH
105
106*/
107
9334f9c6 108void freeargv (char **vector)
252b5132
RH
109{
110 register char **scan;
111
112 if (vector != NULL)
113 {
114 for (scan = vector; *scan != NULL; scan++)
115 {
116 free (*scan);
117 }
118 free (vector);
119 }
120}
121
122/*
123
ba19b94f 124@deftypefn Extension char** buildargv (char *@var{sp})
252b5132 125
ba19b94f
DD
126Given a pointer to a string, parse the string extracting fields
127separated by whitespace and optionally enclosed within either single
128or double quotes (which are stripped off), and build a vector of
129pointers to copies of the string for each field. The input string
130remains unchanged. The last element of the vector is followed by a
131@code{NULL} element.
252b5132 132
ba19b94f
DD
133All of the memory for the pointer array and copies of the string
134is obtained from @code{malloc}. All of the memory can be returned to the
135system with the single function call @code{freeargv}, which takes the
136returned result of @code{buildargv}, as it's argument.
252b5132 137
5d852400 138Returns a pointer to the argument vector if successful. Returns
ba19b94f
DD
139@code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
140memory to complete building the argument vector.
252b5132 141
ba19b94f
DD
142If the input is a null string (as opposed to a @code{NULL} pointer),
143then buildarg returns an argument vector that has one arg, a null
144string.
252b5132 145
ba19b94f 146@end deftypefn
252b5132 147
ba19b94f 148The memory for the argv array is dynamically expanded as necessary.
252b5132 149
ba19b94f
DD
150In order to provide a working buffer for extracting arguments into,
151with appropriate stripping of quotes and translation of backslash
152sequences, we allocate a working buffer at least as long as the input
153string. This ensures that we always have enough space in which to
154work, since the extracted arg is never larger than the input string.
252b5132 155
ba19b94f
DD
156The argument vector is always kept terminated with a @code{NULL} arg
157pointer, so it can be passed to @code{freeargv} at any time, or
158returned, as appropriate.
252b5132 159
252b5132
RH
160*/
161
9334f9c6 162char **buildargv (const char *input)
252b5132
RH
163{
164 char *arg;
165 char *copybuf;
166 int squote = 0;
167 int dquote = 0;
168 int bsquote = 0;
169 int argc = 0;
170 int maxargc = 0;
171 char **argv = NULL;
172 char **nargv;
173
174 if (input != NULL)
175 {
176 copybuf = (char *) alloca (strlen (input) + 1);
177 /* Is a do{}while to always execute the loop once. Always return an
178 argv, even for null strings. See NOTES above, test case below. */
179 do
180 {
181 /* Pick off argv[argc] */
ac424eb3 182 while (ISBLANK (*input))
252b5132
RH
183 {
184 input++;
185 }
186 if ((maxargc == 0) || (argc >= (maxargc - 1)))
187 {
188 /* argv needs initialization, or expansion */
189 if (argv == NULL)
190 {
191 maxargc = INITIAL_MAXARGC;
192 nargv = (char **) malloc (maxargc * sizeof (char *));
193 }
194 else
195 {
196 maxargc *= 2;
197 nargv = (char **) realloc (argv, maxargc * sizeof (char *));
198 }
199 if (nargv == NULL)
200 {
201 if (argv != NULL)
202 {
203 freeargv (argv);
204 argv = NULL;
205 }
206 break;
207 }
208 argv = nargv;
209 argv[argc] = NULL;
210 }
211 /* Begin scanning arg */
212 arg = copybuf;
213 while (*input != EOS)
214 {
ac424eb3 215 if (ISBLANK (*input) && !squote && !dquote && !bsquote)
252b5132
RH
216 {
217 break;
218 }
219 else
220 {
221 if (bsquote)
222 {
223 bsquote = 0;
224 *arg++ = *input;
225 }
226 else if (*input == '\\')
227 {
228 bsquote = 1;
229 }
230 else if (squote)
231 {
232 if (*input == '\'')
233 {
234 squote = 0;
235 }
236 else
237 {
238 *arg++ = *input;
239 }
240 }
241 else if (dquote)
242 {
243 if (*input == '"')
244 {
245 dquote = 0;
246 }
247 else
248 {
249 *arg++ = *input;
250 }
251 }
252 else
253 {
254 if (*input == '\'')
255 {
256 squote = 1;
257 }
258 else if (*input == '"')
259 {
260 dquote = 1;
261 }
262 else
263 {
264 *arg++ = *input;
265 }
266 }
267 input++;
268 }
269 }
270 *arg = EOS;
271 argv[argc] = strdup (copybuf);
272 if (argv[argc] == NULL)
273 {
274 freeargv (argv);
275 argv = NULL;
276 break;
277 }
278 argc++;
279 argv[argc] = NULL;
280
ac424eb3 281 while (ISBLANK (*input))
252b5132
RH
282 {
283 input++;
284 }
285 }
286 while (*input != EOS);
287 }
288 return (argv);
289}
290
291#ifdef MAIN
292
293/* Simple little test driver. */
294
e6450fe5 295static const char *const tests[] =
252b5132
RH
296{
297 "a simple command line",
298 "arg 'foo' is single quoted",
299 "arg \"bar\" is double quoted",
300 "arg \"foo bar\" has embedded whitespace",
301 "arg 'Jack said \\'hi\\'' has single quotes",
302 "arg 'Jack said \\\"hi\\\"' has double quotes",
303 "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",
304
305 /* This should be expanded into only one argument. */
306 "trailing-whitespace ",
307
308 "",
309 NULL
310};
311
9334f9c6
DD
312int
313main (void)
252b5132
RH
314{
315 char **argv;
e6450fe5 316 const char *const *test;
252b5132
RH
317 char **targs;
318
319 for (test = tests; *test != NULL; test++)
320 {
321 printf ("buildargv(\"%s\")\n", *test);
322 if ((argv = buildargv (*test)) == NULL)
323 {
324 printf ("failed!\n\n");
325 }
326 else
327 {
328 for (targs = argv; *targs != NULL; targs++)
329 {
330 printf ("\t\"%s\"\n", *targs);
331 }
332 printf ("\n");
333 }
334 freeargv (argv);
335 }
336
e6450fe5 337 return 0;
252b5132
RH
338}
339
340#endif /* MAIN */
This page took 0.286812 seconds and 4 git commands to generate.