Initial import from FreeBSD RELENG_4:
[games.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
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25 #include "cvs.h"
26 #include "prepend_args.h"
27
28
29 /* Find the white-space-separated options specified by OPTIONS, and
30    using BUF to store copies of these options, set ARGV[0], ARGV[1],
31    etc. to the option copies.  Return the number N of options found.
32    Do not set ARGV[N] to NULL.  If ARGV is NULL, do not store ARGV[0]
33    etc.  Backslash can be used to escape whitespace (and backslashes).  */
34 static int
35 prepend_args (options, buf, argv)
36      char const *options;
37      char *buf;
38      char **argv;
39 {
40   char const *o = options;
41   char *b = buf;
42   int n = 0;
43
44   for (;;)
45     {
46       while (isspace ((unsigned char) *o))
47         o++;
48       if (!*o)
49         return n;
50       if (argv)
51         argv[n] = b;
52       n++;
53
54       do
55         if ((*b++ = *o++) == '\\' && *o)
56           b[-1] = *o++;
57       while (*o && ! isspace ((unsigned char) *o));
58
59       *b++ = '\0';
60     }
61 }
62
63 /* Prepend the whitespace-separated options in OPTIONS to the argument
64    vector of a main program with argument count *PARGC and argument
65    vector *PARGV.  */
66 void
67 prepend_default_options (options, pargc, pargv)
68      char const *options;
69      int *pargc;
70      char ***pargv;
71 {
72   if (options)
73     {
74       char *buf = xmalloc (strlen (options) + 1);
75       int prepended = prepend_args (options, buf, (char **) NULL);
76       int argc = *pargc;
77       char * const *argv = *pargv;
78       char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
79       *pargc = prepended + argc;
80       *pargv = pp;
81       *pp++ = *argv++;
82       pp += prepend_args (options, buf, pp);
83       while ((*pp++ = *argv++))
84         continue;
85     }
86 }