Switch to CVS 1.12.11
[dragonfly.git] / gnu / usr.bin / cvs / cvs / prepend_args.c
1 /* prepend_args.c - utilility programs for manpiulating argv[]
2    Copyright (C) 1999 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17    02111-1307, USA.  */
18
19 /* $FreeBSD: src/gnu/usr.bin/cvs/cvs/prepend_args.c,v 1.3.2.2 2002/12/19 21:18:01 peter Exp $ */
20 /* $DragonFly: src/gnu/usr.bin/cvs/cvs/prepend_args.c,v 1.3 2004/01/20 05:42:03 asmodai Exp $ */
21
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 #include "cvs.h"
27 #include "prepend_args.h"
28
29
30 /* Find the white-space-separated options specified by OPTIONS, and
31    using BUF to store copies of these options, set ARGV[0], ARGV[1],
32    etc. to the option copies.  Return the number N of options found.
33    Do not set ARGV[N] to NULL.  If ARGV is NULL, do not store ARGV[0]
34    etc.  Backslash can be used to escape whitespace (and backslashes).  */
35 static int
36 prepend_args (char const *options, char *buf, char **argv)
37 {
38   char const *o = options;
39   char *b = buf;
40   int n = 0;
41
42   for (;;)
43     {
44       while (isspace ((unsigned char) *o))
45         o++;
46       if (!*o)
47         return n;
48       if (argv)
49         argv[n] = b;
50       n++;
51
52       do
53         if ((*b++ = *o++) == '\\' && *o)
54           b[-1] = *o++;
55       while (*o && ! isspace ((unsigned char) *o));
56
57       *b++ = '\0';
58     }
59 }
60
61 /* Prepend the whitespace-separated options in OPTIONS to the argument
62    vector of a main program with argument count *PARGC and argument
63    vector *PARGV.  */
64 void
65 prepend_default_options (char const *options, int *pargc, char ***pargv)
66 {
67   if (options)
68     {
69       char *buf = xmalloc (strlen (options) + 1);
70       int prepended = prepend_args (options, buf, (char **) NULL);
71       int argc = *pargc;
72       char * const *argv = *pargv;
73       char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
74       *pargc = prepended + argc;
75       *pargv = pp;
76       *pp++ = *argv++;
77       pp += prepend_args (options, buf, pp);
78       while ((*pp++ = *argv++))
79         continue;
80     }
81 }