Blame lib/system-quote.h

Packit 33f14e
/* Quoting for a system command.
Packit 33f14e
   Copyright (C) 2001-2017 Free Software Foundation, Inc.
Packit 33f14e
   Written by Bruno Haible <bruno@clisp.org>, 2012.
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
#ifndef _SYSTEM_QUOTE_H
Packit 33f14e
#define _SYSTEM_QUOTE_H
Packit 33f14e
Packit 33f14e
/* When passing a command the system's command interpreter, we must quote the
Packit 33f14e
   program name and arguments, since
Packit 33f14e
     - Unix shells interpret characters like " ", "'", "<", ">", "$", '*', '?'
Packit 33f14e
       etc. in a special way,
Packit 33f14e
     - Windows CreateProcess() interprets characters like ' ', '\t', '\\', '"'
Packit 33f14e
       etc. (but not '<' and '>') in a special way,
Packit 33f14e
     - Windows cmd.exe also interprets characters like '<', '>', '&', '%', etc.
Packit 33f14e
       in a special way.  Note that it is impossible to pass arguments that
Packit 33f14e
       contain newlines or carriage return characters to programs through
Packit 33f14e
       cmd.exe.
Packit 33f14e
     - Windows programs usually perform wildcard expansion when they receive
Packit 33f14e
       arguments that contain unquoted '*', '?' characters.
Packit 33f14e
Packit 33f14e
  With this module, you can build a command that will invoke a program with
Packit 33f14e
  specific strings as arguments.
Packit 33f14e
Packit 33f14e
  Note: If you want wildcard expansion to happen, you have to first do wildcard
Packit 33f14e
  expansion through the 'glob' module, then quote the resulting strings through
Packit 33f14e
  this module, and then invoke the system's command interpreter.
Packit 33f14e
Packit 33f14e
  Limitations:
Packit 33f14e
    - When invoking native Windows programs on Windows Vista or newer,
Packit 33f14e
      wildcard expansion will occur in the invoked program nevertheless.
Packit 33f14e
    - On native Windows, for SCI_SYSTEM and SCI_WINDOWS_CMD, newlines and
Packit 33f14e
      carriage return characters are not supported.  Their undesired effect
Packit 33f14e
      is to truncate the entire command line.
Packit 33f14e
 */
Packit 33f14e
Packit 33f14e
#include <stddef.h>
Packit 33f14e
Packit 33f14e
#ifdef __cplusplus
Packit 33f14e
extern "C" {
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
/* Identifier for the kind of interpreter of the command.  */
Packit 33f14e
enum system_command_interpreter
Packit 33f14e
{
Packit 33f14e
  /* The interpreter used by the system() and popen() functions.
Packit 33f14e
     This is equivalent to SCI_POSIX_SH on Unix platforms and
Packit 33f14e
     SCI_WINDOWS_CMD on native Windows platforms.  */
Packit 33f14e
  SCI_SYSTEM                    = 0
Packit 33f14e
  /* The POSIX /bin/sh.  */
Packit 33f14e
  , SCI_POSIX_SH                = 1
Packit 33f14e
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 33f14e
  /* The native Windows CreateProcess() function.  */
Packit 33f14e
  , SCI_WINDOWS_CREATEPROCESS   = 2
Packit 33f14e
  /* The native Windows cmd.exe interpreter.  */
Packit 33f14e
  , SCI_WINDOWS_CMD             = 3
Packit 33f14e
#endif
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
/* Returns the number of bytes needed for the quoted string.  */
Packit 33f14e
extern size_t
Packit 33f14e
       system_quote_length (enum system_command_interpreter interpreter,
Packit 33f14e
                            const char *string);
Packit 33f14e
Packit 33f14e
/* Copies the quoted string to p and returns the incremented p.
Packit 33f14e
   There must be room for system_quote_length (string) + 1 bytes at p.  */
Packit 33f14e
extern char *
Packit 33f14e
       system_quote_copy (char *p,
Packit 33f14e
                          enum system_command_interpreter interpreter,
Packit 33f14e
                          const char *string);
Packit 33f14e
Packit 33f14e
/* Returns the freshly allocated quoted string.  */
Packit 33f14e
extern char *
Packit 33f14e
       system_quote (enum system_command_interpreter interpreter,
Packit 33f14e
                     const char *string);
Packit 33f14e
Packit 33f14e
/* Returns a freshly allocated string containing all argument strings, quoted,
Packit 33f14e
   separated through spaces.  */
Packit 33f14e
extern char *
Packit 33f14e
       system_quote_argv (enum system_command_interpreter interpreter,
Packit 33f14e
                          char * const *argv);
Packit 33f14e
Packit 33f14e
#ifdef __cplusplus
Packit 33f14e
}
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
#endif /* _SYSTEM_QUOTE_H */