Blame lib/prepargs.c

Packit Service fdd496
/* Parse arguments from a string and prepend them to an argv.
Packit Service fdd496
Packit Service fdd496
   Copyright (C) 1999-2002, 2006, 2009-2013, 2015-2017 Free Software
Packit Service fdd496
   Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation, either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Paul Eggert <eggert@twinsun.com>.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
#include "prepargs.h"
Packit Service fdd496
#include <string.h>
Packit Service fdd496
#include <sys/types.h>
Packit Service fdd496
#include <xalloc.h>
Packit Service fdd496
Packit Service fdd496
#include <ctype.h>
Packit Service fdd496
Packit Service fdd496
/* IN_CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given
Packit Service fdd496
   as an argument to <ctype.h> macros like "isspace".  */
Packit Service fdd496
#ifdef STDC_HEADERS
Packit Service fdd496
# define IN_CTYPE_DOMAIN(c) 1
Packit Service fdd496
#else
Packit Service fdd496
# define IN_CTYPE_DOMAIN(c) ((c) <= 0177)
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
Packit Service fdd496
Packit Service fdd496
/* Find the white-space-separated options specified by OPTIONS, and
Packit Service fdd496
   using BUF to store copies of these options, set ARGV[0], ARGV[1],
Packit Service fdd496
   etc. to the option copies.  Return the number N of options found.
Packit Service fdd496
   Do not set ARGV[N].  If ARGV is zero, do not store ARGV[0] etc.
Packit Service fdd496
   Backslash can be used to escape whitespace (and backslashes).  */
Packit Service fdd496
static int
Packit Service fdd496
prepend_args (char const *options, char *buf, char **argv)
Packit Service fdd496
{
Packit Service fdd496
  char const *o = options;
Packit Service fdd496
  char *b = buf;
Packit Service fdd496
  int n = 0;
Packit Service fdd496
Packit Service fdd496
  for (;;)
Packit Service fdd496
    {
Packit Service fdd496
      while (ISSPACE ((unsigned char) *o))
Packit Service fdd496
	o++;
Packit Service fdd496
      if (!*o)
Packit Service fdd496
	return n;
Packit Service fdd496
      if (argv)
Packit Service fdd496
	argv[n] = b;
Packit Service fdd496
      n++;
Packit Service fdd496
Packit Service fdd496
      do
Packit Service fdd496
	if ((*b++ = *o++) == '\\' && *o)
Packit Service fdd496
	  b[-1] = *o++;
Packit Service fdd496
      while (*o && ! ISSPACE ((unsigned char) *o));
Packit Service fdd496
Packit Service fdd496
      *b++ = '\0';
Packit Service fdd496
    }
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Prepend the whitespace-separated options in OPTIONS to the argument
Packit Service fdd496
   vector of a main program with argument count *PARGC and argument
Packit Service fdd496
   vector *PARGV.  */
Packit Service fdd496
void
Packit Service fdd496
prepend_default_options (char const *options, int *pargc, char ***pargv)
Packit Service fdd496
{
Packit Service fdd496
  if (options)
Packit Service fdd496
    {
Packit Service fdd496
      char *buf = xmalloc (strlen (options) + 1);
Packit Service fdd496
      int prepended = prepend_args (options, buf, (char **) 0);
Packit Service fdd496
      int argc = *pargc;
Packit Service fdd496
      char * const *argv = *pargv;
Packit Service fdd496
      char **pp = xmalloc ((prepended + argc + 1) * sizeof *pp);
Packit Service fdd496
      *pargc = prepended + argc;
Packit Service fdd496
      *pargv = pp;
Packit Service fdd496
      *pp++ = *argv++;
Packit Service fdd496
      pp += prepend_args (options, buf, pp);
Packit Service fdd496
      while ((*pp++ = *argv++))
Packit Service fdd496
	continue;
Packit Service fdd496
    }
Packit Service fdd496
}