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