OSDN Git Service

include/
[pf3gnuchains/gcc-fork.git] / libiberty / argv.c
1 /* Create and destroy argument vectors (argv's)
2    Copyright (C) 1992, 2001 Free Software Foundation, Inc.
3    Written by Fred Fish @ Cygnus Support
4
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.
10
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.
15
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.  */
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
25 #include "ansidecl.h"
26 #include "libiberty.h"
27
28 #define ISBLANK(ch) ((ch) == ' ' || (ch) == '\t')
29
30 /*  Routines imported from standard C runtime libraries. */
31
32 #include <stddef.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #ifndef NULL
37 #define NULL 0
38 #endif
39
40 #ifndef EOS
41 #define EOS '\0'
42 #endif
43
44 #define INITIAL_MAXARGC 8       /* Number of args + NULL in initial argv */
45
46
47 /*
48
49 @deftypefn Extension char** dupargv (char **@var{vector})
50
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.
56
57 @end deftypefn
58
59 */
60
61 char **
62 dupargv (char **argv)
63 {
64   int argc;
65   char **copy;
66   
67   if (argv == NULL)
68     return NULL;
69   
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;
75   
76   /* the strings */
77   for (argc = 0; argv[argc] != NULL; argc++)
78     {
79       int len = strlen (argv[argc]);
80       copy[argc] = malloc (sizeof (char *) * (len + 1));
81       if (copy[argc] == NULL)
82         {
83           freeargv (copy);
84           return NULL;
85         }
86       strcpy (copy[argc], argv[argc]);
87     }
88   copy[argc] = NULL;
89   return copy;
90 }
91
92 /*
93
94 @deftypefn Extension void freeargv (char **@var{vector})
95
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.
100
101 @end deftypefn
102
103 */
104
105 void freeargv (char **vector)
106 {
107   register char **scan;
108
109   if (vector != NULL)
110     {
111       for (scan = vector; *scan != NULL; scan++)
112         {
113           free (*scan);
114         }
115       free (vector);
116     }
117 }
118
119 /*
120
121 @deftypefn Extension char** buildargv (char *@var{sp})
122
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.
129
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.
134
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.
138
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.
142
143 @end deftypefn
144
145 The memory for the argv array is dynamically expanded as necessary.
146
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.
152
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.
156
157 */
158
159 char **buildargv (const char *input)
160 {
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;
170
171   if (input != NULL)
172     {
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. */
176       do
177         {
178           /* Pick off argv[argc] */
179           while (ISBLANK (*input))
180             {
181               input++;
182             }
183           if ((maxargc == 0) || (argc >= (maxargc - 1)))
184             {
185               /* argv needs initialization, or expansion */
186               if (argv == NULL)
187                 {
188                   maxargc = INITIAL_MAXARGC;
189                   nargv = (char **) malloc (maxargc * sizeof (char *));
190                 }
191               else
192                 {
193                   maxargc *= 2;
194                   nargv = (char **) realloc (argv, maxargc * sizeof (char *));
195                 }
196               if (nargv == NULL)
197                 {
198                   if (argv != NULL)
199                     {
200                       freeargv (argv);
201                       argv = NULL;
202                     }
203                   break;
204                 }
205               argv = nargv;
206               argv[argc] = NULL;
207             }
208           /* Begin scanning arg */
209           arg = copybuf;
210           while (*input != EOS)
211             {
212               if (ISBLANK (*input) && !squote && !dquote && !bsquote)
213                 {
214                   break;
215                 }
216               else
217                 {
218                   if (bsquote)
219                     {
220                       bsquote = 0;
221                       *arg++ = *input;
222                     }
223                   else if (*input == '\\')
224                     {
225                       bsquote = 1;
226                     }
227                   else if (squote)
228                     {
229                       if (*input == '\'')
230                         {
231                           squote = 0;
232                         }
233                       else
234                         {
235                           *arg++ = *input;
236                         }
237                     }
238                   else if (dquote)
239                     {
240                       if (*input == '"')
241                         {
242                           dquote = 0;
243                         }
244                       else
245                         {
246                           *arg++ = *input;
247                         }
248                     }
249                   else
250                     {
251                       if (*input == '\'')
252                         {
253                           squote = 1;
254                         }
255                       else if (*input == '"')
256                         {
257                           dquote = 1;
258                         }
259                       else
260                         {
261                           *arg++ = *input;
262                         }
263                     }
264                   input++;
265                 }
266             }
267           *arg = EOS;
268           argv[argc] = strdup (copybuf);
269           if (argv[argc] == NULL)
270             {
271               freeargv (argv);
272               argv = NULL;
273               break;
274             }
275           argc++;
276           argv[argc] = NULL;
277
278           while (ISBLANK (*input))
279             {
280               input++;
281             }
282         }
283       while (*input != EOS);
284     }
285   return (argv);
286 }
287
288 #ifdef MAIN
289
290 /* Simple little test driver. */
291
292 static const char *const tests[] =
293 {
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",
301   
302   /* This should be expanded into only one argument.  */
303   "trailing-whitespace ",
304
305   "",
306   NULL
307 };
308
309 int
310 main (void)
311 {
312   char **argv;
313   const char *const *test;
314   char **targs;
315
316   for (test = tests; *test != NULL; test++)
317     {
318       printf ("buildargv(\"%s\")\n", *test);
319       if ((argv = buildargv (*test)) == NULL)
320         {
321           printf ("failed!\n\n");
322         }
323       else
324         {
325           for (targs = argv; *targs != NULL; targs++)
326             {
327               printf ("\t\"%s\"\n", *targs);
328             }
329           printf ("\n");
330         }
331       freeargv (argv);
332     }
333
334   return 0;
335 }
336
337 #endif  /* MAIN */