2 Copyright (C) 2001-2004, 2006, 2009-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
28 /* Describes quoting for sh compatible shells. */
29 static struct quoting_options
*sh_quoting_options
;
31 /* Initializes the sh_quoting_options variable. */
33 init_sh_quoting_options (void)
35 sh_quoting_options
= clone_quoting_options (NULL
);
36 set_quoting_style (sh_quoting_options
, shell_quoting_style
);
39 /* Returns the number of bytes needed for the quoted string. */
41 shell_quote_length (const char *string
)
43 if (sh_quoting_options
== NULL
)
44 init_sh_quoting_options ();
45 return quotearg_buffer (NULL
, 0, string
, strlen (string
),
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. */
52 shell_quote_copy (char *p
, const char *string
)
54 if (sh_quoting_options
== NULL
)
55 init_sh_quoting_options ();
56 return p
+ quotearg_buffer (p
, (size_t)(-1), string
, strlen (string
),
60 /* Returns the freshly allocated quoted string. */
62 shell_quote (const char *string
)
64 if (sh_quoting_options
== NULL
)
65 init_sh_quoting_options ();
66 return quotearg_alloc (string
, strlen (string
), sh_quoting_options
);
69 /* Returns a freshly allocated string containing all argument strings, quoted,
70 separated through spaces. */
72 shell_quote_argv (const char * const *argv
)
76 const char * const *argp
;
84 length
+= shell_quote_length (*argp
) + 1;
90 command
= XNMALLOC (length
, char);
95 p
= shell_quote_copy (p
, *argp
);