Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / sh-quote.c
bloba549c46ef6b740ee113ac1e2afd46d6e6f082a8b
1 /* Shell quoting.
2 Copyright (C) 2001-2004 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 2, or (at your option)
8 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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 /* Specification. */
24 #include "sh-quote.h"
26 #include <string.h>
28 #include "quotearg.h"
29 #include "xalloc.h"
31 /* Describes quoting for sh compatible shells. */
32 static struct quoting_options *sh_quoting_options;
34 /* Initializes the sh_quoting_options variable. */
35 static void
36 init_sh_quoting_options ()
38 sh_quoting_options = clone_quoting_options (NULL);
39 set_quoting_style (sh_quoting_options, shell_quoting_style);
42 /* Returns the number of bytes needed for the quoted string. */
43 size_t
44 shell_quote_length (const char *string)
46 if (sh_quoting_options == NULL)
47 init_sh_quoting_options ();
48 return quotearg_buffer (NULL, 0, string, strlen (string),
49 sh_quoting_options);
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)
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);
63 /* Returns the freshly allocated quoted string. */
64 char *
65 shell_quote (const char *string)
67 if (sh_quoting_options == NULL)
68 init_sh_quoting_options ();
69 return quotearg_alloc (string, strlen (string), sh_quoting_options);
72 /* Returns a freshly allocated string containing all argument strings, quoted,
73 separated through spaces. */
74 char *
75 shell_quote_argv (char **argv)
77 if (*argv != NULL)
79 char **argp;
80 size_t length;
81 char *command;
82 char *p;
84 length = 0;
85 for (argp = argv; ; )
87 length += shell_quote_length (*argp) + 1;
88 argp++;
89 if (*argp == NULL)
90 break;
93 command = (char *) xmalloc (length);
95 p = command;
96 for (argp = argv; ; )
98 p = shell_quote_copy (p, *argp);
99 argp++;
100 if (*argp == NULL)
101 break;
102 *p++ = ' ';
104 *p = '\0';
106 return command;
108 else
109 return xstrdup ("");