1*18fd37a7SXin LI /* Parse arguments from a string and prepend them to an argv.
2*18fd37a7SXin LI
3*18fd37a7SXin LI Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4*18fd37a7SXin LI
5*18fd37a7SXin LI This program is free software; you can redistribute it and/or modify
6*18fd37a7SXin LI it under the terms of the GNU General Public License as published by
7*18fd37a7SXin LI the Free Software Foundation; either version 2, or (at your option)
8*18fd37a7SXin LI any later version.
9*18fd37a7SXin LI
10*18fd37a7SXin LI This program is distributed in the hope that it will be useful,
11*18fd37a7SXin LI but WITHOUT ANY WARRANTY; without even the implied warranty of
12*18fd37a7SXin LI MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*18fd37a7SXin LI GNU General Public License for more details.
14*18fd37a7SXin LI
15*18fd37a7SXin LI You should have received a copy of the GNU General Public License
16*18fd37a7SXin LI along with this program; if not, write to the Free Software
17*18fd37a7SXin LI Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18*18fd37a7SXin LI 02111-1307, USA. */
19*18fd37a7SXin LI
20*18fd37a7SXin LI /* Written by Paul Eggert <eggert@twinsun.com>. */
21*18fd37a7SXin LI
22*18fd37a7SXin LI #ifdef HAVE_CONFIG_H
23*18fd37a7SXin LI # include <config.h>
24*18fd37a7SXin LI #endif
25*18fd37a7SXin LI #include "prepargs.h"
26*18fd37a7SXin LI #include <string.h>
27*18fd37a7SXin LI #include <sys/types.h>
28*18fd37a7SXin LI #include <xalloc.h>
29*18fd37a7SXin LI
30*18fd37a7SXin LI #include <ctype.h>
31*18fd37a7SXin LI
32*18fd37a7SXin LI /* IN_CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given
33*18fd37a7SXin LI as an argument to <ctype.h> macros like "isspace". */
34*18fd37a7SXin LI #ifdef STDC_HEADERS
35*18fd37a7SXin LI # define IN_CTYPE_DOMAIN(c) 1
36*18fd37a7SXin LI #else
37*18fd37a7SXin LI # define IN_CTYPE_DOMAIN(c) ((c) <= 0177)
38*18fd37a7SXin LI #endif
39*18fd37a7SXin LI
40*18fd37a7SXin LI #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
41*18fd37a7SXin LI
42*18fd37a7SXin LI /* Find the white-space-separated options specified by OPTIONS, and
43*18fd37a7SXin LI using BUF to store copies of these options, set ARGV[0], ARGV[1],
44*18fd37a7SXin LI etc. to the option copies. Return the number N of options found.
45*18fd37a7SXin LI Do not set ARGV[N]. If ARGV is zero, do not store ARGV[0] etc.
46*18fd37a7SXin LI Backslash can be used to escape whitespace (and backslashes). */
47*18fd37a7SXin LI static int
prepend_args(char const * options,char * buf,char ** argv)48*18fd37a7SXin LI prepend_args (char const *options, char *buf, char **argv)
49*18fd37a7SXin LI {
50*18fd37a7SXin LI char const *o = options;
51*18fd37a7SXin LI char *b = buf;
52*18fd37a7SXin LI int n = 0;
53*18fd37a7SXin LI
54*18fd37a7SXin LI for (;;)
55*18fd37a7SXin LI {
56*18fd37a7SXin LI while (ISSPACE ((unsigned char) *o))
57*18fd37a7SXin LI o++;
58*18fd37a7SXin LI if (!*o)
59*18fd37a7SXin LI return n;
60*18fd37a7SXin LI if (argv)
61*18fd37a7SXin LI argv[n] = b;
62*18fd37a7SXin LI n++;
63*18fd37a7SXin LI
64*18fd37a7SXin LI do
65*18fd37a7SXin LI if ((*b++ = *o++) == '\\' && *o)
66*18fd37a7SXin LI b[-1] = *o++;
67*18fd37a7SXin LI while (*o && ! ISSPACE ((unsigned char) *o));
68*18fd37a7SXin LI
69*18fd37a7SXin LI *b++ = '\0';
70*18fd37a7SXin LI }
71*18fd37a7SXin LI }
72*18fd37a7SXin LI
73*18fd37a7SXin LI /* Prepend the whitespace-separated options in OPTIONS to the argument
74*18fd37a7SXin LI vector of a main program with argument count *PARGC and argument
75*18fd37a7SXin LI vector *PARGV. */
76*18fd37a7SXin LI void
prepend_default_options(char const * options,int * pargc,char *** pargv)77*18fd37a7SXin LI prepend_default_options (char const *options, int *pargc, char ***pargv)
78*18fd37a7SXin LI {
79*18fd37a7SXin LI if (options)
80*18fd37a7SXin LI {
81*18fd37a7SXin LI char *buf = xmalloc (strlen (options) + 1);
82*18fd37a7SXin LI int prepended = prepend_args (options, buf, (char **) 0);
83*18fd37a7SXin LI int argc = *pargc;
84*18fd37a7SXin LI char * const *argv = *pargv;
85*18fd37a7SXin LI char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
86*18fd37a7SXin LI *pargc = prepended + argc;
87*18fd37a7SXin LI *pargv = pp;
88*18fd37a7SXin LI *pp++ = *argv++;
89*18fd37a7SXin LI pp += prepend_args (options, buf, pp);
90*18fd37a7SXin LI while ((*pp++ = *argv++))
91*18fd37a7SXin LI continue;
92*18fd37a7SXin LI }
93*18fd37a7SXin LI }
94