Blame lib/sh-quote.c

Packit Service fdd496
/* Shell quoting.
Packit Service fdd496
   Copyright (C) 2001-2004, 2006, 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
   Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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
#include <config.h>
Packit Service fdd496
Packit Service fdd496
/* Specification.  */
Packit Service fdd496
#include "sh-quote.h"
Packit Service fdd496
Packit Service fdd496
#include <string.h>
Packit Service fdd496
Packit Service fdd496
#include "quotearg.h"
Packit Service fdd496
#include "xalloc.h"
Packit Service fdd496
Packit Service fdd496
/* Describes quoting for sh compatible shells.  */
Packit Service fdd496
static struct quoting_options *sh_quoting_options;
Packit Service fdd496
Packit Service fdd496
/* Initializes the sh_quoting_options variable.  */
Packit Service fdd496
static void
Packit Service fdd496
init_sh_quoting_options (void)
Packit Service fdd496
{
Packit Service fdd496
  sh_quoting_options = clone_quoting_options (NULL);
Packit Service fdd496
  set_quoting_style (sh_quoting_options, shell_quoting_style);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Returns the number of bytes needed for the quoted string.  */
Packit Service fdd496
size_t
Packit Service fdd496
shell_quote_length (const char *string)
Packit Service fdd496
{
Packit Service fdd496
  if (sh_quoting_options == NULL)
Packit Service fdd496
    init_sh_quoting_options ();
Packit Service fdd496
  return quotearg_buffer (NULL, 0, string, strlen (string),
Packit Service fdd496
                           sh_quoting_options);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Copies the quoted string to p and returns the incremented p.
Packit Service fdd496
   There must be room for shell_quote_length (string) + 1 bytes at p.  */
Packit Service fdd496
char *
Packit Service fdd496
shell_quote_copy (char *p, const char *string)
Packit Service fdd496
{
Packit Service fdd496
  if (sh_quoting_options == NULL)
Packit Service fdd496
    init_sh_quoting_options ();
Packit Service fdd496
  return p + quotearg_buffer (p, (size_t)(-1), string, strlen (string),
Packit Service fdd496
                              sh_quoting_options);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Returns the freshly allocated quoted string.  */
Packit Service fdd496
char *
Packit Service fdd496
shell_quote (const char *string)
Packit Service fdd496
{
Packit Service fdd496
  if (sh_quoting_options == NULL)
Packit Service fdd496
    init_sh_quoting_options ();
Packit Service fdd496
  return quotearg_alloc (string, strlen (string), sh_quoting_options);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Returns a freshly allocated string containing all argument strings, quoted,
Packit Service fdd496
   separated through spaces.  */
Packit Service fdd496
char *
Packit Service fdd496
shell_quote_argv (char * const *argv)
Packit Service fdd496
{
Packit Service fdd496
  if (*argv != NULL)
Packit Service fdd496
    {
Packit Service fdd496
      char * const *argp;
Packit Service fdd496
      size_t length;
Packit Service fdd496
      char *command;
Packit Service fdd496
      char *p;
Packit Service fdd496
Packit Service fdd496
      length = 0;
Packit Service fdd496
      for (argp = argv; ; )
Packit Service fdd496
        {
Packit Service fdd496
          length += shell_quote_length (*argp) + 1;
Packit Service fdd496
          argp++;
Packit Service fdd496
          if (*argp == NULL)
Packit Service fdd496
            break;
Packit Service fdd496
        }
Packit Service fdd496
Packit Service fdd496
      command = XNMALLOC (length, char);
Packit Service fdd496
Packit Service fdd496
      p = command;
Packit Service fdd496
      for (argp = argv; ; )
Packit Service fdd496
        {
Packit Service fdd496
          p = shell_quote_copy (p, *argp);
Packit Service fdd496
          argp++;
Packit Service fdd496
          if (*argp == NULL)
Packit Service fdd496
            break;
Packit Service fdd496
          *p++ = ' ';
Packit Service fdd496
        }
Packit Service fdd496
      *p = '\0';
Packit Service fdd496
Packit Service fdd496
      return command;
Packit Service fdd496
    }
Packit Service fdd496
  else
Packit Service fdd496
    return xstrdup ("");
Packit Service fdd496
}