annotate lib/sh-quote.c @ 5598:09d49a2e321b

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