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