5598
|
1 /* Shell quoting. |
|
2 Copyright (C) 2001-2004 Free Software Foundation, Inc. |
|
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001. |
|
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 2, or (at your option) |
|
8 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, write to the Free Software Foundation, |
|
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
|
18 |
|
19 #ifdef HAVE_CONFIG_H |
|
20 # include <config.h> |
|
21 #endif |
|
22 |
|
23 /* Specification. */ |
|
24 #include "sh-quote.h" |
|
25 |
|
26 #include <string.h> |
|
27 |
|
28 #include "quotearg.h" |
|
29 #include "xalloc.h" |
|
30 |
|
31 /* Describes quoting for sh compatible shells. */ |
|
32 static struct quoting_options *sh_quoting_options; |
|
33 |
|
34 /* Initializes the sh_quoting_options variable. */ |
|
35 static void |
|
36 init_sh_quoting_options () |
|
37 { |
|
38 sh_quoting_options = clone_quoting_options (NULL); |
|
39 set_quoting_style (sh_quoting_options, shell_quoting_style); |
|
40 } |
|
41 |
|
42 /* Returns the number of bytes needed for the quoted string. */ |
|
43 size_t |
|
44 shell_quote_length (const char *string) |
|
45 { |
|
46 if (sh_quoting_options == NULL) |
|
47 init_sh_quoting_options (); |
|
48 return quotearg_buffer (NULL, 0, string, strlen (string), |
|
49 sh_quoting_options); |
|
50 } |
|
51 |
|
52 /* Copies the quoted string to p and returns the incremented p. |
|
53 There must be room for shell_quote_length (string) + 1 bytes at p. */ |
|
54 char * |
|
55 shell_quote_copy (char *p, const char *string) |
|
56 { |
|
57 if (sh_quoting_options == NULL) |
|
58 init_sh_quoting_options (); |
|
59 return p + quotearg_buffer (p, (size_t)(-1), string, strlen (string), |
|
60 sh_quoting_options); |
|
61 } |
|
62 |
|
63 /* Returns the freshly allocated quoted string. */ |
|
64 char * |
|
65 shell_quote (const char *string) |
|
66 { |
|
67 if (sh_quoting_options == NULL) |
|
68 init_sh_quoting_options (); |
|
69 return quotearg_alloc (string, strlen (string), sh_quoting_options); |
|
70 } |
|
71 |
|
72 /* Returns a freshly allocated string containing all argument strings, quoted, |
|
73 separated through spaces. */ |
|
74 char * |
|
75 shell_quote_argv (char **argv) |
|
76 { |
|
77 if (*argv != NULL) |
|
78 { |
|
79 char **argp; |
|
80 size_t length; |
|
81 char *command; |
|
82 char *p; |
|
83 |
|
84 length = 0; |
|
85 for (argp = argv; ; ) |
|
86 { |
|
87 length += shell_quote_length (*argp) + 1; |
|
88 argp++; |
|
89 if (*argp == NULL) |
|
90 break; |
|
91 } |
|
92 |
|
93 command = (char *) xmalloc (length); |
|
94 |
|
95 p = command; |
|
96 for (argp = argv; ; ) |
|
97 { |
|
98 p = shell_quote_copy (p, *argp); |
|
99 argp++; |
|
100 if (*argp == NULL) |
|
101 break; |
|
102 *p++ = ' '; |
|
103 } |
|
104 *p = '\0'; |
|
105 |
|
106 return command; |
|
107 } |
|
108 else |
|
109 return xstrdup (""); |
|
110 } |