Blame lib/sh-quote.c

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