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., 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"
31 #define ISBLANK(ch) ((ch) == ' ' || (ch) == '\t')
33 /* 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 **) malloc ((argc
+ 1) * sizeof (char *));
80 for (argc
= 0; argv
[argc
] != NULL
; argc
++)
82 int len
= strlen (argv
[argc
]);
83 copy
[argc
] = (char *) malloc (len
+ 1);
84 if (copy
[argc
] == NULL
)
89 strcpy (copy
[argc
], argv
[argc
]);
97 @deftypefn Extension void freeargv (char **@var{vector})
99 Free an argument vector that was built using @code{buildargv}. Simply
100 scans through @var{vector}, freeing the memory for each argument until
101 the terminating @code{NULL} is found, and then frees @var{vector}
108 void freeargv (char **vector
)
110 register char **scan
;
114 for (scan
= vector
; *scan
!= NULL
; scan
++)
124 @deftypefn Extension char** buildargv (char *@var{sp})
126 Given a pointer to a string, parse the string extracting fields
127 separated by whitespace and optionally enclosed within either single
128 or double quotes (which are stripped off), and build a vector of
129 pointers to copies of the string for each field. The input string
130 remains unchanged. The last element of the vector is followed by a
133 All of the memory for the pointer array and copies of the string
134 is obtained from @code{malloc}. All of the memory can be returned to the
135 system with the single function call @code{freeargv}, which takes the
136 returned result of @code{buildargv}, as it's argument.
138 Returns a pointer to the argument vector if successful. Returns
139 @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
140 memory to complete building the argument vector.
142 If the input is a null string (as opposed to a @code{NULL} pointer),
143 then buildarg returns an argument vector that has one arg, a null
148 The memory for the argv array is dynamically expanded as necessary.
150 In order to provide a working buffer for extracting arguments into,
151 with appropriate stripping of quotes and translation of backslash
152 sequences, we allocate a working buffer at least as long as the input
153 string. This ensures that we always have enough space in which to
154 work, since the extracted arg is never larger than the input string.
156 The argument vector is always kept terminated with a @code{NULL} arg
157 pointer, so it can be passed to @code{freeargv} at any time, or
158 returned, as appropriate.
162 char **buildargv (const char *input
)
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. */
181 /* Pick off argv[argc] */
182 while (ISBLANK (*input
))
186 if ((maxargc
== 0) || (argc
>= (maxargc
- 1)))
188 /* argv needs initialization, or expansion */
191 maxargc
= INITIAL_MAXARGC
;
192 nargv
= (char **) malloc (maxargc
* sizeof (char *));
197 nargv
= (char **) realloc (argv
, maxargc
* sizeof (char *));
211 /* Begin scanning arg */
213 while (*input
!= EOS
)
215 if (ISBLANK (*input
) && !squote
&& !dquote
&& !bsquote
)
226 else if (*input
== '\\')
258 else if (*input
== '"')
271 argv
[argc
] = strdup (copybuf
);
272 if (argv
[argc
] == NULL
)
281 while (ISBLANK (*input
))
286 while (*input
!= EOS
);
293 /* Simple little test driver. */
295 static const char *const tests
[] =
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",
305 /* This should be expanded into only one argument. */
306 "trailing-whitespace ",
316 const char *const *test
;
319 for (test
= tests
; *test
!= NULL
; test
++)
321 printf ("buildargv(\"%s\")\n", *test
);
322 if ((argv
= buildargv (*test
)) == NULL
)
324 printf ("failed!\n\n");
328 for (targs
= argv
; *targs
!= NULL
; targs
++)
330 printf ("\t\"%s\"\n", *targs
);