| 1 | /* $Id: buildargv.h,v 1.1 2006/01/21 13:40:21 rockyb Exp $ */ |
| 2 | /* Create and destroy argument vectors (argv's) |
| 3 | Copyright (C) 1992, 2001 Free Software Foundation, Inc. |
| 4 | Written by Fred Fish @ Cygnus Support |
| 5 | |
| 6 | Copyright (C) 2008 R. Bernstein [email protected] |
| 7 | |
| 8 | This file is part of the libiberty library. |
| 9 | Libiberty is free software; you can redistribute it and/or |
| 10 | modify it under the terms of the GNU Library General Public |
| 11 | License as published by the Free Software Foundation; either |
| 12 | version 2 of the License, or (at your option) any later version. |
| 13 | |
| 14 | Libiberty is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | Library General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU Library General Public |
| 20 | License along with libiberty; see the file COPYING.LIB. If |
| 21 | not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 22 | Boston, MA 02111-1307, USA. */ |
| 23 | |
| 24 | /* rocky: Below we only need/use the freeargv and buildargv routine |
| 25 | from the above. Some small adaption was made to include outside of |
| 26 | libiberty. |
| 27 | */ |
| 28 | /* |
| 29 | |
| 30 | @deftypefn Extension void freeargv (char **@var{vector}) |
| 31 | |
| 32 | Free an argument vector that was built using @code{buildargv}. Simply |
| 33 | scans through @var{vector}, freeing the memory for each argument until |
| 34 | the terminating @code{NULL} is found, and then frees @var{vector} |
| 35 | itself. |
| 36 | |
| 37 | @end deftypefn |
| 38 | |
| 39 | */ |
| 40 | void freeargv (char **vector); |
| 41 | /* |
| 42 | |
| 43 | @deftypefn Extension char** buildargv (char *@var{sp}) |
| 44 | |
| 45 | Given a pointer to a string, parse the string extracting fields |
| 46 | separated by whitespace and optionally enclosed within either single |
| 47 | or double quotes (which are stripped off), and build a vector of |
| 48 | pointers to copies of the string for each field. The input string |
| 49 | remains unchanged. The last element of the vector is followed by a |
| 50 | @code{NULL} element. |
| 51 | |
| 52 | All of the memory for the pointer array and copies of the string |
| 53 | is obtained from @code{malloc}. All of the memory can be returned to the |
| 54 | system with the single function call @code{freeargv}, which takes the |
| 55 | returned result of @code{buildargv}, as it's argument. |
| 56 | |
| 57 | Returns a pointer to the argument vector if successful. Returns |
| 58 | @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient |
| 59 | memory to complete building the argument vector. |
| 60 | |
| 61 | If the input is a null string (as opposed to a @code{NULL} pointer), |
| 62 | then buildarg returns an argument vector that has one arg, a null |
| 63 | string. |
| 64 | |
| 65 | @end deftypefn |
| 66 | |
| 67 | The memory for the argv array is dynamically expanded as necessary. |
| 68 | |
| 69 | In order to provide a working buffer for extracting arguments into, |
| 70 | with appropriate stripping of quotes and translation of backslash |
| 71 | sequences, we allocate a working buffer at least as long as the input |
| 72 | string. This ensures that we always have enough space in which to |
| 73 | work, since the extracted arg is never larger than the input string. |
| 74 | |
| 75 | The argument vector is always kept terminated with a @code{NULL} arg |
| 76 | pointer, so it can be passed to @code{freeargv} at any time, or |
| 77 | returned, as appropriate. |
| 78 | |
| 79 | */ |
| 80 | char **buildargv (const char *input); |
| 81 |