/[gnats]/gnats/libiberty/argv.c
ViewVC logotype

Diff of /gnats/libiberty/argv.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by pdm, Mon Dec 10 23:03:27 2001 UTC revision 1.4 by chewie, Sat Nov 13 05:14:17 2004 UTC
# Line 1  Line 1 
1  /* Create and destroy argument vectors (argv's)  /* Create and destroy argument vectors (argv's)
2     Copyright (C) 1992 Free Software Foundation, Inc.     Copyright (C) 1992, 2001 Free Software Foundation, Inc.
3     Written by Fred Fish @ Cygnus Support     Written by Fred Fish @ Cygnus Support
4    
5  This file is part of the libiberty library.  This file is part of the libiberty library.
# Line 29  Boston, MA 02111-1307, USA.  */ Line 29  Boston, MA 02111-1307, USA.  */
29    
30  /*  Routines imported from standard C runtime libraries. */  /*  Routines imported from standard C runtime libraries. */
31    
32  #ifdef __STDC__  #ifdef ANSI_PROTOTYPES
33    
34  #include <stddef.h>  #include <stddef.h>
35  #include <string.h>  #include <string.h>
36  #include <stdlib.h>  #include <stdlib.h>
37    
38  #else   /* !__STDC__ */  #else   /* !ANSI_PROTOTYPES */
39    
40  #if !defined _WIN32 || defined __GNUC__  #if !defined _WIN32 || defined __GNUC__
41  extern char *memcpy ();         /* Copy memory region */  extern char *memcpy ();         /* Copy memory region */
# Line 46  extern void free ();           /* Free malloc'd m Line 46  extern void free ();           /* Free malloc'd m
46  extern char *strdup ();         /* Duplicate a string */  extern char *strdup ();         /* Duplicate a string */
47  #endif  #endif
48    
49  #endif  /* __STDC__ */  #endif  /* ANSI_PROTOTYPES */
50    
 #include "alloca-conf.h"  
51    
52  #ifndef NULL  #ifndef NULL
53  #define NULL 0  #define NULL 0
# Line 63  extern char *strdup ();                /* Duplicate a Line 62  extern char *strdup ();                /* Duplicate a
62    
63  /*  /*
64    
65  NAME  @deftypefn Extension char** dupargv (char **@var{vector})
66    
67          dupargv -- duplicate an argument vector  Duplicate an argument vector.  Simply scans through @var{vector},
68    duplicating each argument until the terminating @code{NULL} is found.
69    Returns a pointer to the argument vector if successful.  Returns
70    @code{NULL} if there is insufficient memory to complete building the
71    argument vector.
72    
73  SYNOPSIS  @end deftypefn
   
         char **dupargv (vector)  
         char **vector;  
   
 DESCRIPTION  
   
         Duplicate an argument vector.  Simply scans through the  
         vector, duplicating each argument until the  
         terminating NULL is found.  
   
 RETURNS  
   
         Returns a pointer to the argument vector if  
         successful. Returns NULL if there is insufficient memory to  
         complete building the argument vector.  
74    
75  */  */
76    
# Line 120  dupargv (argv) Line 108  dupargv (argv)
108    
109  /*  /*
110    
111  NAME  @deftypefn Extension void freeargv (char **@var{vector})
   
         freeargv -- free an argument vector  
112    
113  SYNOPSIS  Free an argument vector that was built using @code{buildargv}.  Simply
114    scans through @var{vector}, freeing the memory for each argument until
115    the terminating @code{NULL} is found, and then frees @var{vector}
116    itself.
117    
118          void freeargv (vector)  @end deftypefn
         char **vector;  
   
 DESCRIPTION  
   
         Free an argument vector that was built using buildargv.  Simply scans  
         through the vector, freeing the memory for each argument until the  
         terminating NULL is found, and then frees the vector itself.  
   
 RETURNS  
   
         No value.  
119    
120  */  */
121    
# Line 158  char **vector; Line 136  char **vector;
136    
137  /*  /*
138    
139  NAME  @deftypefn Extension char** buildargv (char *@var{sp})
   
         buildargv -- build an argument vector from a string  
   
 SYNOPSIS  
   
         char **buildargv (sp)  
         char *sp;  
   
 DESCRIPTION  
   
         Given a pointer to a string, parse the string extracting fields  
         separated by whitespace and optionally enclosed within either single  
         or double quotes (which are stripped off), and build a vector of  
         pointers to copies of the string for each field.  The input string  
         remains unchanged.  
   
         All of the memory for the pointer array and copies of the string  
         is obtained from malloc.  All of the memory can be returned to the  
         system with the single function call freeargv, which takes the  
         returned result of buildargv, as it's argument.  
   
         The memory for the argv array is dynamically expanded as necessary.  
   
 RETURNS  
   
         Returns a pointer to the argument vector if successful. Returns NULL  
         if the input string pointer is NULL or if there is insufficient  
         memory to complete building the argument vector.  
   
 NOTES  
   
         In order to provide a working buffer for extracting arguments into,  
         with appropriate stripping of quotes and translation of backslash  
         sequences, we allocate a working buffer at least as long as the input  
         string.  This ensures that we always have enough space in which to  
         work, since the extracted arg is never larger than the input string.  
140    
141          If the input is a null string (as opposed to a NULL pointer), then  Given a pointer to a string, parse the string extracting fields
142          buildarg returns an argv that has one arg, a null string.  separated by whitespace and optionally enclosed within either single
143    or double quotes (which are stripped off), and build a vector of
144    pointers to copies of the string for each field.  The input string
145    remains unchanged.  The last element of the vector is followed by a
146    @code{NULL} element.
147    
148    All of the memory for the pointer array and copies of the string
149    is obtained from @code{malloc}.  All of the memory can be returned to the
150    system with the single function call @code{freeargv}, which takes the
151    returned result of @code{buildargv}, as it's argument.
152    
153    Returns a pointer to the argument vector if successful.  Returns
154    @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
155    memory to complete building the argument vector.
156    
157    If the input is a null string (as opposed to a @code{NULL} pointer),
158    then buildarg returns an argument vector that has one arg, a null
159    string.
160    
161    @end deftypefn
162    
163    The memory for the argv array is dynamically expanded as necessary.
164    
165    In order to provide a working buffer for extracting arguments into,
166    with appropriate stripping of quotes and translation of backslash
167    sequences, we allocate a working buffer at least as long as the input
168    string.  This ensures that we always have enough space in which to
169    work, since the extracted arg is never larger than the input string.
170    
171    The argument vector is always kept terminated with a @code{NULL} arg
172    pointer, so it can be passed to @code{freeargv} at any time, or
173    returned, as appropriate.
174    
         Argv is always kept terminated with a NULL arg pointer, so it can  
         be passed to freeargv at any time, or returned, as appropriate.  
175  */  */
176    
177  char **buildargv (input)  char **buildargv (input)
178  char *input;       const char *input;
179  {  {
180    char *arg;    char *arg;
181    char *copybuf;    char *copybuf;
# Line 337  char *input; Line 308  char *input;
308    
309  /* Simple little test driver. */  /* Simple little test driver. */
310    
311  static char *tests[] =  static const char *const tests[] =
312  {  {
313    "a simple command line",    "a simple command line",
314    "arg 'foo' is single quoted",    "arg 'foo' is single quoted",
# Line 354  static char *tests[] = Line 325  static char *tests[] =
325    NULL    NULL
326  };  };
327    
328  main ()  int main ()
329  {  {
330    char **argv;    char **argv;
331    char **test;    const char *const *test;
332    char **targs;    char **targs;
333    
334    for (test = tests; *test != NULL; test++)    for (test = tests; *test != NULL; test++)
# Line 378  main () Line 349  main ()
349        freeargv (argv);        freeargv (argv);
350      }      }
351    
352      return 0;
353  }  }
354    
355  #endif  /* MAIN */  #endif  /* MAIN */

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26