annotate lib/sh-quote.c @ 17363:5a51fb7777a9

sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin Problem reported by Marco Atzeri in <http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>. * lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]: Simply delegate to the system <sys/select.h> in this case too. Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to be needed on Solaris either. * lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]: Simply delgate to the system <sys/time.h> in this case.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 19 Mar 2013 09:08:47 -0700
parents e542fd46ad6f
children 344018b6e5d7
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.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16830
diff changeset
2 Copyright (C) 2001-2004, 2006, 2009-2013 Free Software Foundation, Inc.
5598
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
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
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
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
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
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
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
18 #include <config.h>
5598
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "sh-quote.h"
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 #include <string.h>
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "quotearg.h"
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include "xalloc.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 /* Describes quoting for sh compatible shells. */
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 static struct quoting_options *sh_quoting_options;
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 /* Initializes the sh_quoting_options variable. */
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 static void
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 init_sh_quoting_options ()
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 sh_quoting_options = clone_quoting_options (NULL);
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 set_quoting_style (sh_quoting_options, shell_quoting_style);
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
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* Returns the number of bytes needed for the quoted string. */
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 size_t
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 shell_quote_length (const char *string)
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 {
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 if (sh_quoting_options == NULL)
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 init_sh_quoting_options ();
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 return quotearg_buffer (NULL, 0, string, strlen (string),
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
46 sh_quoting_options);
5598
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 }
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 /* Copies the quoted string to p and returns the incremented p.
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 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
51 char *
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 shell_quote_copy (char *p, const char *string)
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 if (sh_quoting_options == NULL)
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 init_sh_quoting_options ();
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 return p + quotearg_buffer (p, (size_t)(-1), string, strlen (string),
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
57 sh_quoting_options);
5598
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 }
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* Returns the freshly allocated quoted string. */
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 char *
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 shell_quote (const char *string)
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 {
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 if (sh_quoting_options == NULL)
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 init_sh_quoting_options ();
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 return quotearg_alloc (string, strlen (string), sh_quoting_options);
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 }
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 /* Returns a freshly allocated string containing all argument strings, quoted,
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 separated through spaces. */
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 char *
16830
d137600880f6 sh-quote: Improve shell_quote_argv's signature.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
72 shell_quote_argv (char * const *argv)
5598
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 {
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 if (*argv != NULL)
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 {
16830
d137600880f6 sh-quote: Improve shell_quote_argv's signature.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
76 char * const *argp;
5598
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 size_t length;
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 char *command;
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 char *p;
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 length = 0;
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 for (argp = argv; ; )
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
83 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
84 length += shell_quote_length (*argp) + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
85 argp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
86 if (*argp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
87 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
88 }
5598
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
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 p = command;
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 for (argp = argv; ; )
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
94 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
95 p = shell_quote_copy (p, *argp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
96 argp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
97 if (*argp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
98 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
99 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
100 }
5598
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 *p = '\0';
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 return command;
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 }
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 else
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 return xstrdup ("");
09d49a2e321b New module 'sh-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 }