Mercurial > hg > octave-kai > gnulib-hg
annotate lib/sh-quote.c @ 11527:4fe203c3f828
Replace wcrtomb, wcsrtombs, wcsnrtombs if mbstate_t has to be replaced.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Fri, 01 May 2009 18:01:52 +0200 |
parents | bbbbbf4cd1c5 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
5598 | 1 /* Shell quoting. |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
2 Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc. |
5598 | 3 Written by Bruno Haible <haible@clisp.cons.org>, 2001. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
5598 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
8 (at your option) any later version. |
5598 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5598 | 17 |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
18 #include <config.h> |
5598 | 19 |
20 /* Specification. */ | |
21 #include "sh-quote.h" | |
22 | |
23 #include <string.h> | |
24 | |
25 #include "quotearg.h" | |
26 #include "xalloc.h" | |
27 | |
28 /* Describes quoting for sh compatible shells. */ | |
29 static struct quoting_options *sh_quoting_options; | |
30 | |
31 /* Initializes the sh_quoting_options variable. */ | |
32 static void | |
33 init_sh_quoting_options () | |
34 { | |
35 sh_quoting_options = clone_quoting_options (NULL); | |
36 set_quoting_style (sh_quoting_options, shell_quoting_style); | |
37 } | |
38 | |
39 /* Returns the number of bytes needed for the quoted string. */ | |
40 size_t | |
41 shell_quote_length (const char *string) | |
42 { | |
43 if (sh_quoting_options == NULL) | |
44 init_sh_quoting_options (); | |
45 return quotearg_buffer (NULL, 0, string, strlen (string), | |
46 sh_quoting_options); | |
47 } | |
48 | |
49 /* Copies the quoted string to p and returns the incremented p. | |
50 There must be room for shell_quote_length (string) + 1 bytes at p. */ | |
51 char * | |
52 shell_quote_copy (char *p, const char *string) | |
53 { | |
54 if (sh_quoting_options == NULL) | |
55 init_sh_quoting_options (); | |
56 return p + quotearg_buffer (p, (size_t)(-1), string, strlen (string), | |
57 sh_quoting_options); | |
58 } | |
59 | |
60 /* Returns the freshly allocated quoted string. */ | |
61 char * | |
62 shell_quote (const char *string) | |
63 { | |
64 if (sh_quoting_options == NULL) | |
65 init_sh_quoting_options (); | |
66 return quotearg_alloc (string, strlen (string), sh_quoting_options); | |
67 } | |
68 | |
69 /* Returns a freshly allocated string containing all argument strings, quoted, | |
70 separated through spaces. */ | |
71 char * | |
72 shell_quote_argv (char **argv) | |
73 { | |
74 if (*argv != NULL) | |
75 { | |
76 char **argp; | |
77 size_t length; | |
78 char *command; | |
79 char *p; | |
80 | |
81 length = 0; | |
82 for (argp = argv; ; ) | |
83 { | |
84 length += shell_quote_length (*argp) + 1; | |
85 argp++; | |
86 if (*argp == NULL) | |
87 break; | |
88 } | |
89 | |
7603
23f14c284219
Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
90 command = XNMALLOC (length, char); |
5598 | 91 |
92 p = command; | |
93 for (argp = argv; ; ) | |
94 { | |
95 p = shell_quote_copy (p, *argp); | |
96 argp++; | |
97 if (*argp == NULL) | |
98 break; | |
99 *p++ = ' '; | |
100 } | |
101 *p = '\0'; | |
102 | |
103 return command; | |
104 } | |
105 else | |
106 return xstrdup (""); | |
107 } |