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. */
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
71 for (argc
= 0; argv
[argc
] != NULL
; argc
++);
72 copy
= (char **) malloc ((argc
+ 1) * sizeof (char *));
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
)
86 strcpy (copy
[argc
], argv
[argc
]);
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}
105 void freeargv (char **vector
)
107 register char **scan
;
111 for (scan
= vector
; *scan
!= NULL
; scan
++)
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
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
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
)
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
))
183 if ((maxargc
== 0) || (argc
>= (maxargc
- 1)))
185 /* argv needs initialization, or expansion */
188 maxargc
= INITIAL_MAXARGC
;
189 nargv
= (char **) malloc (maxargc
* sizeof (char *));
194 nargv
= (char **) realloc (argv
, maxargc
* sizeof (char *));
208 /* Begin scanning arg */
210 while (*input
!= EOS
)
212 if (ISBLANK (*input
) && !squote
&& !dquote
&& !bsquote
)
223 else if (*input
== '\\')
255 else if (*input
== '"')
268 argv
[argc
] = strdup (copybuf
);
269 if (argv
[argc
] == NULL
)
278 while (ISBLANK (*input
))
283 while (*input
!= EOS
);
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 ",
313 const char *const *test
;
316 for (test
= tests
; *test
!= NULL
; test
++)
318 printf ("buildargv(\"%s\")\n", *test
);
319 if ((argv
= buildargv (*test
)) == NULL
)
321 printf ("failed!\n\n");
325 for (targs
= argv
; *targs
!= NULL
; targs
++)
327 printf ("\t\"%s\"\n", *targs
);