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