d5f29780b7895fc3761fb635a515304c9f36780c
[gnulib.git] / lib / system-quote.h
1 /* Quoting for a system command.
2    Copyright (C) 2001-2012 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2012.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _SYSTEM_QUOTE_H
19 #define _SYSTEM_QUOTE_H
20
21 /* When passing a command the system's command interpreter, we must quote the
22    program name and arguments, since
23      - Unix shells interpret characters like " ", "'", "<", ">", "$" etc. in a
24        special way,
25      - Windows CreateProcess() interprets characters like ' ', '\t', '\\', '"'
26        etc. (but not '<' and '>') in a special way,
27      - Windows cmd.exe also interprets characters like '<', '>', '&', '%', etc.
28        in a special way.  Note that it is impossible to pass arguments that
29        contain newlines or carriage return characters to programs through
30        cmd.exe.  */
31
32 #include <stddef.h>
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /* Identifier for the kind of interpreter of the command.  */
39 enum system_command_interpreter
40 {
41   /* The interpreter used by the system() and popen() functions.
42      This is equivalent to SCI_POSIX_SH on Unix platforms and
43      SCI_WINDOWS_CMD on native Windows platforms.  */
44   SCI_SYSTEM                    = 0
45   /* The POSIX /bin/sh.  */
46   , SCI_POSIX_SH                = 1
47 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
48   /* The native Windows CreateProcess() function.  */
49   , SCI_WINDOWS_CREATEPROCESS   = 2
50   /* The native Windows cmd.exe interpreter.  */
51   , SCI_WINDOWS_CMD             = 3
52 #endif
53 };
54
55 /* Returns the number of bytes needed for the quoted string.  */
56 extern size_t
57        system_quote_length (enum system_command_interpreter interpreter,
58                             const char *string);
59
60 /* Copies the quoted string to p and returns the incremented p.
61    There must be room for system_quote_length (string) + 1 bytes at p.  */
62 extern char *
63        system_quote_copy (char *p,
64                           enum system_command_interpreter interpreter,
65                           const char *string);
66
67 /* Returns the freshly allocated quoted string.  */
68 extern char *
69        system_quote (enum system_command_interpreter interpreter,
70                      const char *string);
71
72 /* Returns a freshly allocated string containing all argument strings, quoted,
73    separated through spaces.  */
74 extern char *
75        system_quote_argv (enum system_command_interpreter interpreter,
76                           char * const *argv);
77
78 #ifdef __cplusplus
79 }
80 #endif
81
82 #endif /* _SYSTEM_QUOTE_H */