1 /* Parse arguments from a string and prepend them to an argv.
3 Copyright (C) 1999-2002, 2006, 2009-2010 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert <eggert@twinsun.com>. */
24 #include <sys/types.h>
29 /* IN_CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given
30 as an argument to <ctype.h> macros like "isspace". */
32 # define IN_CTYPE_DOMAIN(c) 1
34 # define IN_CTYPE_DOMAIN(c) ((c) <= 0177)
37 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
39 /* Find the white-space-separated options specified by OPTIONS, and
40 using BUF to store copies of these options, set ARGV[0], ARGV[1],
41 etc. to the option copies. Return the number N of options found.
42 Do not set ARGV[N]. If ARGV is zero, do not store ARGV[0] etc.
43 Backslash can be used to escape whitespace (and backslashes). */
45 prepend_args (char const *options, char *buf, char **argv)
47 char const *o = options;
53 while (ISSPACE ((unsigned char) *o))
62 if ((*b++ = *o++) == '\\' && *o)
64 while (*o && ! ISSPACE ((unsigned char) *o));
70 /* Prepend the whitespace-separated options in OPTIONS to the argument
71 vector of a main program with argument count *PARGC and argument
74 prepend_default_options (char const *options, int *pargc, char ***pargv)
78 char *buf = xmalloc (strlen (options) + 1);
79 int prepended = prepend_args (options, buf, (char **) 0);
81 char * const *argv = *pargv;
82 char **pp = xmalloc ((prepended + argc + 1) * sizeof *pp);
83 *pargc = prepended + argc;
86 pp += prepend_args (options, buf, pp);
87 while ((*pp++ = *argv++))