Blame lib/prepargs.c

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