summaryrefslogtreecommitdiffstats
path: root/gettext-tools/lib/sh-quote.c
blob: 41c82c3bbc271a41be6d99399492071b3c987d21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Shell quoting.
   Copyright (C) 2001-2002 Free Software Foundation, Inc.
   Written by Bruno Haible <haible@clisp.cons.org>, 2001.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

/* Specification.  */
#include "sh-quote.h"

#include <string.h>

#include "strpbrk.h"
#include "xmalloc.h"


#define SHELL_SPECIAL_CHARS "\t\n !\"#$&'()*;<=>?[\\]`{|}~"

/* Returns the number of bytes needed for the quoted string.  */
size_t
shell_quote_length (const char *string)
{
  if (strpbrk (string, SHELL_SPECIAL_CHARS) == NULL)
    return strlen (string);
  else
    {
      char qchar = '\0';	/* last quote character: none or ' or " */
      size_t length = 0;

      for (; *string != '\0'; string++)
	{
	  char c = *string;
	  char q = (c == '\'' ? '"' : '\'');

	  if (qchar != q)
	    {
	      if (qchar)
		length++;
	      qchar = q;
	      length++;
	    }
	  length++;
	}
      if (qchar)
	length++;

      return length;
    }
}

/* Copies the quoted string to p and returns the incremented p.  */
char *
shell_quote_copy (char *p, const char *string)
{
  if (strpbrk (string, SHELL_SPECIAL_CHARS) == NULL)
    {
      memcpy (p, string, strlen (string));
      return p + strlen (string);
    }
  else
    {
      char qchar = '\0';	/* last quote character: none or ' or " */

      for (; *string != '\0'; string++)
	{
	  char c = *string;
	  char q = (c == '\'' ? '"' : '\'');

	  if (qchar != q)
	    {
	      if (qchar)
		*p++ = qchar;
	      qchar = q;
	      *p++ = qchar;
	    }
	  *p++ = c;
	}
      if (qchar)
	*p++ = qchar;

      return p;
    }
}

/* Returns the freshly allocated quoted string.  */
char *
shell_quote (const char *string)
{
  size_t length = shell_quote_length (string);
  char *quoted = (char *) xmalloc (length + 1);
  char *p = shell_quote_copy (quoted, string);
  *p = '\0';
  return quoted;
}

/* Returns a freshly allocated string containing all argument strings, quoted,
   separated through spaces.  */
char *
shell_quote_argv (char **argv)
{
  if (*argv != NULL)
    {
      char **argp;
      size_t length;
      char *command;
      char *p;

      length = 0;
      for (argp = argv; ; )
	{
	  length += shell_quote_length (*argp) + 1;
	  argp++;
	  if (*argp == NULL)
	    break;
	}

      command = (char *) xmalloc (length);

      p = command;
      for (argp = argv; ; )
	{
	  p = shell_quote_copy (p, *argp);
	  argp++;
	  if (*argp == NULL)
	    break;
	  *p++ = ' ';
	}
      *p = '\0';

      return command;
    }
  else
    return xstrdup ("");
}