xref: /openbsd/gnu/lib/libiberty/src/argv.c (revision 274d7c50)
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., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, 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 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include "ansidecl.h"
29 #include "libiberty.h"
30 #include "safe-ctype.h"
31 
32 /*  Routines imported from standard C runtime libraries. */
33 
34 #include <stddef.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 
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 
52 @deftypefn Extension char** dupargv (char **@var{vector})
53 
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
58 argument vector.
59 
60 @end deftypefn
61 
62 */
63 
64 char **
65 dupargv (char **argv)
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       copy[argc] = strdup (argv[argc]);
83       if (copy[argc] == NULL)
84 	{
85 	  freeargv (copy);
86 	  return NULL;
87 	}
88     }
89   copy[argc] = NULL;
90   return copy;
91 }
92 
93 /*
94 
95 @deftypefn Extension void freeargv (char **@var{vector})
96 
97 Free an argument vector that was built using @code{buildargv}.  Simply
98 scans through @var{vector}, freeing the memory for each argument until
99 the terminating @code{NULL} is found, and then frees @var{vector}
100 itself.
101 
102 @end deftypefn
103 
104 */
105 
106 void freeargv (char **vector)
107 {
108   register char **scan;
109 
110   if (vector != NULL)
111     {
112       for (scan = vector; *scan != NULL; scan++)
113 	{
114 	  free (*scan);
115 	}
116       free (vector);
117     }
118 }
119 
120 /*
121 
122 @deftypefn Extension char** buildargv (char *@var{sp})
123 
124 Given a pointer to a string, parse the string extracting fields
125 separated by whitespace and optionally enclosed within either single
126 or double quotes (which are stripped off), and build a vector of
127 pointers to copies of the string for each field.  The input string
128 remains unchanged.  The last element of the vector is followed by a
129 @code{NULL} element.
130 
131 All of the memory for the pointer array and copies of the string
132 is obtained from @code{malloc}.  All of the memory can be returned to the
133 system with the single function call @code{freeargv}, which takes the
134 returned result of @code{buildargv}, as it's argument.
135 
136 Returns a pointer to the argument vector if successful.  Returns
137 @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
138 memory to complete building the argument vector.
139 
140 If the input is a null string (as opposed to a @code{NULL} pointer),
141 then buildarg returns an argument vector that has one arg, a null
142 string.
143 
144 @end deftypefn
145 
146 The memory for the argv array is dynamically expanded as necessary.
147 
148 In order to provide a working buffer for extracting arguments into,
149 with appropriate stripping of quotes and translation of backslash
150 sequences, we allocate a working buffer at least as long as the input
151 string.  This ensures that we always have enough space in which to
152 work, since the extracted arg is never larger than the input string.
153 
154 The argument vector is always kept terminated with a @code{NULL} arg
155 pointer, so it can be passed to @code{freeargv} at any time, or
156 returned, as appropriate.
157 
158 */
159 
160 char **buildargv (const char *input)
161 {
162   char *arg;
163   char *copybuf;
164   int squote = 0;
165   int dquote = 0;
166   int bsquote = 0;
167   int argc = 0;
168   int maxargc = 0;
169   char **argv = NULL;
170   char **nargv;
171 
172   if (input != NULL)
173     {
174       copybuf = (char *) alloca (strlen (input) + 1);
175       /* Is a do{}while to always execute the loop once.  Always return an
176 	 argv, even for null strings.  See NOTES above, test case below. */
177       do
178 	{
179 	  /* Pick off argv[argc] */
180 	  while (ISBLANK (*input))
181 	    {
182 	      input++;
183 	    }
184 	  if ((maxargc == 0) || (argc >= (maxargc - 1)))
185 	    {
186 	      /* argv needs initialization, or expansion */
187 	      if (argv == NULL)
188 		{
189 		  maxargc = INITIAL_MAXARGC;
190 		  nargv = (char **) malloc (maxargc * sizeof (char *));
191 		}
192 	      else
193 		{
194 		  maxargc *= 2;
195 		  nargv = (char **) realloc (argv, maxargc * sizeof (char *));
196 		}
197 	      if (nargv == NULL)
198 		{
199 		  if (argv != NULL)
200 		    {
201 		      freeargv (argv);
202 		      argv = NULL;
203 		    }
204 		  break;
205 		}
206 	      argv = nargv;
207 	      argv[argc] = NULL;
208 	    }
209 	  /* Begin scanning arg */
210 	  arg = copybuf;
211 	  while (*input != EOS)
212 	    {
213 	      if (ISSPACE (*input) && !squote && !dquote && !bsquote)
214 		{
215 		  break;
216 		}
217 	      else
218 		{
219 		  if (bsquote)
220 		    {
221 		      bsquote = 0;
222 		      *arg++ = *input;
223 		    }
224 		  else if (*input == '\\')
225 		    {
226 		      bsquote = 1;
227 		    }
228 		  else if (squote)
229 		    {
230 		      if (*input == '\'')
231 			{
232 			  squote = 0;
233 			}
234 		      else
235 			{
236 			  *arg++ = *input;
237 			}
238 		    }
239 		  else if (dquote)
240 		    {
241 		      if (*input == '"')
242 			{
243 			  dquote = 0;
244 			}
245 		      else
246 			{
247 			  *arg++ = *input;
248 			}
249 		    }
250 		  else
251 		    {
252 		      if (*input == '\'')
253 			{
254 			  squote = 1;
255 			}
256 		      else if (*input == '"')
257 			{
258 			  dquote = 1;
259 			}
260 		      else
261 			{
262 			  *arg++ = *input;
263 			}
264 		    }
265 		  input++;
266 		}
267 	    }
268 	  *arg = EOS;
269 	  argv[argc] = strdup (copybuf);
270 	  if (argv[argc] == NULL)
271 	    {
272 	      freeargv (argv);
273 	      argv = NULL;
274 	      break;
275 	    }
276 	  argc++;
277 	  argv[argc] = NULL;
278 
279 	  while (ISSPACE (*input))
280 	    {
281 	      input++;
282 	    }
283 	}
284       while (*input != EOS);
285     }
286   return (argv);
287 }
288 
289 /*
290 
291 @deftypefn Extension void expandargv (int *@var{argcp}, char ***@var{argvp})
292 
293 The @var{argcp} and @code{argvp} arguments are pointers to the usual
294 @code{argc} and @code{argv} arguments to @code{main}.  This function
295 looks for arguments that begin with the character @samp{@@}.  Any such
296 arguments are interpreted as ``response files''.  The contents of the
297 response file are interpreted as additional command line options.  In
298 particular, the file is separated into whitespace-separated strings;
299 each such string is taken as a command-line option.  The new options
300 are inserted in place of the option naming the response file, and
301 @code{*argcp} and @code{*argvp} will be updated.  If the value of
302 @code{*argvp} is modified by this function, then the new value has
303 been dynamically allocated and can be deallocated by the caller with
304 @code{freeargv}.  However, most callers will simply call
305 @code{expandargv} near the beginning of @code{main} and allow the
306 operating system to free the memory when the program exits.
307 
308 @end deftypefn
309 
310 */
311 
312 void
313 expandargv (argcp, argvp)
314      int *argcp;
315      char ***argvp;
316 {
317   /* The argument we are currently processing.  */
318   int i = 0;
319   /* Non-zero if ***argvp has been dynamically allocated.  */
320   int argv_dynamic = 0;
321   /* Loop over the arguments, handling response files.  We always skip
322      ARGVP[0], as that is the name of the program being run.  */
323   while (++i < *argcp)
324     {
325       /* The name of the response file.  */
326       const char *filename;
327       /* The response file.  */
328       FILE *f;
329       /* An upper bound on the number of characters in the response
330 	 file.  */
331       off_t pos;
332       /* The number of characters in the response file, when actually
333 	 read.  */
334       size_t len;
335       /* A dynamically allocated buffer used to hold options read from a
336 	 response file.  */
337       char *buffer;
338       /* Dynamically allocated storage for the options read from the
339 	 response file.  */
340       char **file_argv;
341       /* The number of options read from the response file, if any.  */
342       size_t file_argc;
343       /* We are only interested in options of the form "@file".  */
344       filename = (*argvp)[i];
345       if (filename[0] != '@')
346 	continue;
347       /* Read the contents of the file.  */
348       f = fopen (++filename, "r");
349       if (!f)
350 	continue;
351       if (fseek (f, 0L, SEEK_END) == -1)
352 	goto error;
353       pos = ftello (f);
354       if (pos == -1)
355 	goto error;
356       if (fseek (f, 0L, SEEK_SET) == -1)
357 	goto error;
358       buffer = (char *) xmalloc (pos * sizeof (char) + 1);
359       len = fread (buffer, sizeof (char), pos, f);
360       if (len != (size_t) pos
361 	  /* On Windows, fread may return a value smaller than POS,
362 	     due to CR/LF->CR translation when reading text files.
363 	     That does not in-and-of itself indicate failure.  */
364 	  && ferror (f))
365 	goto error;
366       /* Add a NUL terminator.  */
367       buffer[len] = '\0';
368       /* Parse the string.  */
369       file_argv = buildargv (buffer);
370       /* If *ARGVP is not already dynamically allocated, copy it.  */
371       if (!argv_dynamic)
372 	{
373 	  *argvp = dupargv (*argvp);
374 	  if (!*argvp)
375 	    {
376 	      fputs ("\nout of memory\n", stderr);
377 	      xexit (1);
378 	    }
379 	}
380       /* Count the number of arguments.  */
381       file_argc = 0;
382       while (file_argv[file_argc] && *file_argv[file_argc])
383 	++file_argc;
384       /* Now, insert FILE_ARGV into ARGV.  The "+1" below handles the
385 	 NULL terminator at the end of ARGV.  */
386       *argvp = ((char **)
387 		xrealloc (*argvp,
388 			  (*argcp + file_argc + 1) * sizeof (char *)));
389       memmove (*argvp + i + file_argc, *argvp + i + 1,
390 	       (*argcp - i) * sizeof (char *));
391       memcpy (*argvp + i, file_argv, file_argc * sizeof (char *));
392       /* The original option has been replaced by all the new
393 	 options.  */
394       *argcp += file_argc - 1;
395       /* Free up memory allocated to process the response file.  We do
396 	 not use freeargv because the individual options in FILE_ARGV
397 	 are now in the main ARGV.  */
398       free (file_argv);
399       free (buffer);
400       /* Rescan all of the arguments just read to support response
401 	 files that include other response files.  */
402       --i;
403     error:
404       /* We're all done with the file now.  */
405       fclose (f);
406     }
407 }
408 
409 #ifdef MAIN
410 
411 /* Simple little test driver. */
412 
413 static const char *const tests[] =
414 {
415   "a simple command line",
416   "arg 'foo' is single quoted",
417   "arg \"bar\" is double quoted",
418   "arg \"foo bar\" has embedded whitespace",
419   "arg 'Jack said \\'hi\\'' has single quotes",
420   "arg 'Jack said \\\"hi\\\"' has double quotes",
421   "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",
422 
423   /* This should be expanded into only one argument.  */
424   "trailing-whitespace ",
425 
426   "",
427   NULL
428 };
429 
430 int
431 main (void)
432 {
433   char **argv;
434   const char *const *test;
435   char **targs;
436 
437   for (test = tests; *test != NULL; test++)
438     {
439       printf ("buildargv(\"%s\")\n", *test);
440       if ((argv = buildargv (*test)) == NULL)
441 	{
442 	  printf ("failed!\n\n");
443 	}
444       else
445 	{
446 	  for (targs = argv; *targs != NULL; targs++)
447 	    {
448 	      printf ("\t\"%s\"\n", *targs);
449 	    }
450 	  printf ("\n");
451 	}
452       freeargv (argv);
453     }
454 
455   return 0;
456 }
457 
458 #endif	/* MAIN */
459