argv.c revision 130562
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
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
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, 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#ifdef ANSI_PROTOTYPES
33
34#include <stddef.h>
35#include <string.h>
36#include <stdlib.h>
37
38#else	/* !ANSI_PROTOTYPES */
39
40#if !defined _WIN32 || defined __GNUC__
41extern char *memcpy ();		/* Copy memory region */
42extern int strlen ();		/* Count length of string */
43extern char *malloc ();		/* Standard memory allocater */
44extern char *realloc ();	/* Standard memory reallocator */
45extern void free ();		/* Free malloc'd memory */
46extern char *strdup ();		/* Duplicate a string */
47#endif
48
49#endif	/* ANSI_PROTOTYPES */
50
51
52#ifndef NULL
53#define NULL 0
54#endif
55
56#ifndef EOS
57#define EOS '\0'
58#endif
59
60#define INITIAL_MAXARGC 8	/* Number of args + NULL in initial argv */
61
62
63/*
64
65@deftypefn Extension char** dupargv (char **@var{vector})
66
67Duplicate an argument vector.  Simply scans through @var{vector},
68duplicating each argument until the terminating @code{NULL} is found.
69Returns a pointer to the argument vector if successful.  Returns
70@code{NULL} if there is insufficient memory to complete building the
71argument vector.
72
73@end deftypefn
74
75*/
76
77char **
78dupargv (argv)
79     char **argv;
80{
81  int argc;
82  char **copy;
83
84  if (argv == NULL)
85    return NULL;
86
87  /* the vector */
88  for (argc = 0; argv[argc] != NULL; argc++);
89  copy = (char **) malloc ((argc + 1) * sizeof (char *));
90  if (copy == NULL)
91    return NULL;
92
93  /* the strings */
94  for (argc = 0; argv[argc] != NULL; argc++)
95    {
96      int len = strlen (argv[argc]);
97      copy[argc] = malloc (sizeof (char *) * (len + 1));
98      if (copy[argc] == NULL)
99	{
100	  freeargv (copy);
101	  return NULL;
102	}
103      strcpy (copy[argc], argv[argc]);
104    }
105  copy[argc] = NULL;
106  return copy;
107}
108
109/*
110
111@deftypefn Extension void freeargv (char **@var{vector})
112
113Free an argument vector that was built using @code{buildargv}.  Simply
114scans through @var{vector}, freeing the memory for each argument until
115the terminating @code{NULL} is found, and then frees @var{vector}
116itself.
117
118@end deftypefn
119
120*/
121
122void freeargv (vector)
123char **vector;
124{
125  register char **scan;
126
127  if (vector != NULL)
128    {
129      for (scan = vector; *scan != NULL; scan++)
130	{
131	  free (*scan);
132	}
133      free (vector);
134    }
135}
136
137/*
138
139@deftypefn Extension char** buildargv (char *@var{sp})
140
141Given a pointer to a string, parse the string extracting fields
142separated by whitespace and optionally enclosed within either single
143or double quotes (which are stripped off), and build a vector of
144pointers to copies of the string for each field.  The input string
145remains unchanged.  The last element of the vector is followed by a
146@code{NULL} element.
147
148All of the memory for the pointer array and copies of the string
149is obtained from @code{malloc}.  All of the memory can be returned to the
150system with the single function call @code{freeargv}, which takes the
151returned result of @code{buildargv}, as it's argument.
152
153Returns a pointer to the argument vector if successful.  Returns
154@code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
155memory to complete building the argument vector.
156
157If the input is a null string (as opposed to a @code{NULL} pointer),
158then buildarg returns an argument vector that has one arg, a null
159string.
160
161@end deftypefn
162
163The memory for the argv array is dynamically expanded as necessary.
164
165In order to provide a working buffer for extracting arguments into,
166with appropriate stripping of quotes and translation of backslash
167sequences, we allocate a working buffer at least as long as the input
168string.  This ensures that we always have enough space in which to
169work, since the extracted arg is never larger than the input string.
170
171The argument vector is always kept terminated with a @code{NULL} arg
172pointer, so it can be passed to @code{freeargv} at any time, or
173returned, as appropriate.
174
175*/
176
177char **buildargv (input)
178     const char *input;
179{
180  char *arg;
181  char *copybuf;
182  int squote = 0;
183  int dquote = 0;
184  int bsquote = 0;
185  int argc = 0;
186  int maxargc = 0;
187  char **argv = NULL;
188  char **nargv;
189
190  if (input != NULL)
191    {
192      copybuf = (char *) alloca (strlen (input) + 1);
193      /* Is a do{}while to always execute the loop once.  Always return an
194	 argv, even for null strings.  See NOTES above, test case below. */
195      do
196	{
197	  /* Pick off argv[argc] */
198	  while (ISBLANK (*input))
199	    {
200	      input++;
201	    }
202	  if ((maxargc == 0) || (argc >= (maxargc - 1)))
203	    {
204	      /* argv needs initialization, or expansion */
205	      if (argv == NULL)
206		{
207		  maxargc = INITIAL_MAXARGC;
208		  nargv = (char **) malloc (maxargc * sizeof (char *));
209		}
210	      else
211		{
212		  maxargc *= 2;
213		  nargv = (char **) realloc (argv, maxargc * sizeof (char *));
214		}
215	      if (nargv == NULL)
216		{
217		  if (argv != NULL)
218		    {
219		      freeargv (argv);
220		      argv = NULL;
221		    }
222		  break;
223		}
224	      argv = nargv;
225	      argv[argc] = NULL;
226	    }
227	  /* Begin scanning arg */
228	  arg = copybuf;
229	  while (*input != EOS)
230	    {
231	      if (ISBLANK (*input) && !squote && !dquote && !bsquote)
232		{
233		  break;
234		}
235	      else
236		{
237		  if (bsquote)
238		    {
239		      bsquote = 0;
240		      *arg++ = *input;
241		    }
242		  else if (*input == '\\')
243		    {
244		      bsquote = 1;
245		    }
246		  else if (squote)
247		    {
248		      if (*input == '\'')
249			{
250			  squote = 0;
251			}
252		      else
253			{
254			  *arg++ = *input;
255			}
256		    }
257		  else if (dquote)
258		    {
259		      if (*input == '"')
260			{
261			  dquote = 0;
262			}
263		      else
264			{
265			  *arg++ = *input;
266			}
267		    }
268		  else
269		    {
270		      if (*input == '\'')
271			{
272			  squote = 1;
273			}
274		      else if (*input == '"')
275			{
276			  dquote = 1;
277			}
278		      else
279			{
280			  *arg++ = *input;
281			}
282		    }
283		  input++;
284		}
285	    }
286	  *arg = EOS;
287	  argv[argc] = strdup (copybuf);
288	  if (argv[argc] == NULL)
289	    {
290	      freeargv (argv);
291	      argv = NULL;
292	      break;
293	    }
294	  argc++;
295	  argv[argc] = NULL;
296
297	  while (ISBLANK (*input))
298	    {
299	      input++;
300	    }
301	}
302      while (*input != EOS);
303    }
304  return (argv);
305}
306
307#ifdef MAIN
308
309/* Simple little test driver. */
310
311static const char *const tests[] =
312{
313  "a simple command line",
314  "arg 'foo' is single quoted",
315  "arg \"bar\" is double quoted",
316  "arg \"foo bar\" has embedded whitespace",
317  "arg 'Jack said \\'hi\\'' has single quotes",
318  "arg 'Jack said \\\"hi\\\"' has double quotes",
319  "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",
320
321  /* This should be expanded into only one argument.  */
322  "trailing-whitespace ",
323
324  "",
325  NULL
326};
327
328int main ()
329{
330  char **argv;
331  const char *const *test;
332  char **targs;
333
334  for (test = tests; *test != NULL; test++)
335    {
336      printf ("buildargv(\"%s\")\n", *test);
337      if ((argv = buildargv (*test)) == NULL)
338	{
339	  printf ("failed!\n\n");
340	}
341      else
342	{
343	  for (targs = argv; *targs != NULL; targs++)
344	    {
345	      printf ("\t\"%s\"\n", *targs);
346	    }
347	  printf ("\n");
348	}
349      freeargv (argv);
350    }
351
352  return 0;
353}
354
355#endif	/* MAIN */
356