1 /* -*- buffer-read-only: t -*- vi: set ro: */ 2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */ 3 /* Shell quoting. 4 Copyright (C) 2001-2004, 2006, 2009-2011 Free Software Foundation, Inc. 5 Written by Bruno Haible <haible@clisp.cons.org>, 2001. 6 7 This program is free software: you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include <config.h> 21 22 /* Specification. */ 23 #include "sh-quote.h" 24 25 #include <string.h> 26 27 #include "quotearg.h" 28 #include "xalloc.h" 29 30 /* Describes quoting for sh compatible shells. */ 31 static struct quoting_options *sh_quoting_options; 32 33 /* Initializes the sh_quoting_options variable. */ 34 static void 35 init_sh_quoting_options (void) 36 { 37 sh_quoting_options = clone_quoting_options (NULL); 38 set_quoting_style (sh_quoting_options, shell_quoting_style); 39 } 40 41 /* Returns the number of bytes needed for the quoted string. */ 42 size_t 43 shell_quote_length (const char *string) 44 { 45 if (sh_quoting_options == NULL) 46 init_sh_quoting_options (); 47 return quotearg_buffer (NULL, 0, string, strlen (string), 48 sh_quoting_options); 49 } 50 51 /* Copies the quoted string to p and returns the incremented p. 52 There must be room for shell_quote_length (string) + 1 bytes at p. */ 53 char * 54 shell_quote_copy (char *p, const char *string) 55 { 56 if (sh_quoting_options == NULL) 57 init_sh_quoting_options (); 58 return p + quotearg_buffer (p, (size_t)(-1), string, strlen (string), 59 sh_quoting_options); 60 } 61 62 /* Returns the freshly allocated quoted string. */ 63 char * 64 shell_quote (const char *string) 65 { 66 if (sh_quoting_options == NULL) 67 init_sh_quoting_options (); 68 return quotearg_alloc (string, strlen (string), sh_quoting_options); 69 } 70 71 /* Returns a freshly allocated string containing all argument strings, quoted, 72 separated through spaces. */ 73 char * 74 shell_quote_argv (char **argv) 75 { 76 if (*argv != NULL) 77 { 78 char **argp; 79 size_t length; 80 char *command; 81 char *p; 82 83 length = 0; 84 for (argp = argv; ; ) 85 { 86 length += shell_quote_length (*argp) + 1; 87 argp++; 88 if (*argp == NULL) 89 break; 90 } 91 92 command = XNMALLOC (length, char); 93 94 p = command; 95 for (argp = argv; ; ) 96 { 97 p = shell_quote_copy (p, *argp); 98 argp++; 99 if (*argp == NULL) 100 break; 101 *p++ = ' '; 102 } 103 *p = '\0'; 104 105 return command; 106 } 107 else 108 return xstrdup (""); 109 } 110