shquote.c revision 1.6 1 1.6 kleink /* $NetBSD: shquote.c,v 1.6 2005/02/09 21:35:46 kleink Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 2001 Christopher G. Demetriou
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed for the
18 1.5 salo * NetBSD Project. See http://www.NetBSD.org/ for
19 1.1 cgd * information about NetBSD.
20 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
21 1.1 cgd * derived from this software without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 cgd *
34 1.1 cgd * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd /*
38 1.1 cgd * Define SHQUOTE_USE_MULTIBYTE if you want shquote() to handle multibyte
39 1.1 cgd * characters using mbrtowc().
40 1.1 cgd *
41 1.1 cgd * Please DO NOT rip this #ifdef out of the code. It's also here to help
42 1.1 cgd * portability.
43 1.1 cgd */
44 1.3 cgd #undef SHQUOTE_USE_MULTIBYTE
45 1.1 cgd
46 1.6 kleink #include "namespace.h"
47 1.1 cgd #include <stdlib.h>
48 1.4 simonb #include <string.h>
49 1.1 cgd #ifdef SHQUOTE_USE_MULTIBYTE
50 1.2 christos #include <limits.h>
51 1.1 cgd #include <stdio.h>
52 1.1 cgd #include <wchar.h>
53 1.1 cgd #endif
54 1.1 cgd
55 1.6 kleink #ifdef __weak_alias
56 1.6 kleink __weak_alias(shquote,_shquote)
57 1.6 kleink #endif
58 1.6 kleink
59 1.1 cgd /*
60 1.1 cgd * shquote():
61 1.1 cgd *
62 1.1 cgd * Requotes arguments so that they'll be interpreted properly by the
63 1.1 cgd * shell (/bin/sh).
64 1.1 cgd *
65 1.1 cgd * Wraps single quotes around the string, and replaces single quotes
66 1.1 cgd * in the string with the sequence:
67 1.1 cgd * '\''
68 1.1 cgd *
69 1.1 cgd * Returns the number of characters required to hold the resulting quoted
70 1.1 cgd * argument.
71 1.1 cgd *
72 1.1 cgd * The buffer supplied is filled in and NUL-terminated. If 'bufsize'
73 1.1 cgd * indicates that the buffer is too short to hold the output string, the
74 1.1 cgd * first (bufsize - 1) bytes of quoted argument are filled in and the
75 1.1 cgd * buffer is NUL-terminated.
76 1.1 cgd *
77 1.1 cgd * Changes could be made to optimize the length of strings output by this
78 1.1 cgd * function:
79 1.1 cgd *
80 1.1 cgd * * if there are no metacharacters or whitespace in the input,
81 1.1 cgd * the output could be the input string.
82 1.1 cgd */
83 1.1 cgd
84 1.1 cgd #ifdef SHQUOTE_USE_MULTIBYTE
85 1.1 cgd
86 1.1 cgd #define XLATE_OUTCH(x) wcrtomb(outch, (x), &mbso)
87 1.1 cgd #define XLATE_INCH() \
88 1.1 cgd do { \
89 1.1 cgd n = mbrtowc(&c, arg, MB_CUR_MAX, &mbsi); \
90 1.2 christos } while (/*LINTED const cond*/0)
91 1.1 cgd
92 1.1 cgd #else
93 1.1 cgd
94 1.1 cgd #define XLATE_OUTCH(x) (outch[0] = (x), 1)
95 1.1 cgd #define XLATE_INCH() \
96 1.1 cgd do { \
97 1.1 cgd n = ((c = *arg) != '\0') ? 1 : 0; \
98 1.2 christos } while (/*LINTED const cond*/0)
99 1.1 cgd
100 1.1 cgd #endif
101 1.1 cgd
102 1.1 cgd #define PUT(x) \
103 1.1 cgd do { \
104 1.1 cgd outchlen = XLATE_OUTCH(x); \
105 1.1 cgd if (outchlen == (size_t)-1) \
106 1.1 cgd goto bad; \
107 1.1 cgd rv += outchlen; \
108 1.1 cgd if (bufsize != 0) { \
109 1.1 cgd if (bufsize < outchlen || \
110 1.1 cgd (bufsize == outchlen && \
111 1.1 cgd outch[outchlen - 1] != '\0')) { \
112 1.1 cgd *buf = '\0'; \
113 1.1 cgd bufsize = 0; \
114 1.1 cgd } else { \
115 1.1 cgd memcpy(buf, outch, outchlen); \
116 1.1 cgd buf += outchlen; \
117 1.1 cgd bufsize -= outchlen; \
118 1.1 cgd } \
119 1.1 cgd } \
120 1.2 christos } while (/*LINTED const cond*/0)
121 1.1 cgd
122 1.1 cgd size_t
123 1.1 cgd shquote(const char *arg, char *buf, size_t bufsize)
124 1.1 cgd {
125 1.1 cgd #ifdef SHQUOTE_USE_MULTIBYTE
126 1.2 christos char outch[MB_LEN_MAX];
127 1.1 cgd mbstate_t mbsi, mbso;
128 1.1 cgd wchar_t c, lastc;
129 1.1 cgd size_t outchlen;
130 1.1 cgd #else
131 1.1 cgd char outch[1];
132 1.1 cgd char c, lastc;
133 1.1 cgd size_t outchlen;
134 1.1 cgd #endif
135 1.1 cgd size_t rv;
136 1.1 cgd int n;
137 1.1 cgd
138 1.1 cgd rv = 0;
139 1.1 cgd lastc = 0;
140 1.1 cgd #ifdef SHQUOTE_USE_MULTIBYTE
141 1.1 cgd memset(&mbsi, 0, sizeof mbsi);
142 1.1 cgd memset(&mbso, 0, sizeof mbso);
143 1.1 cgd #endif
144 1.1 cgd
145 1.1 cgd if (*arg != '\'')
146 1.1 cgd PUT('\'');
147 1.1 cgd for (;;) {
148 1.1 cgd XLATE_INCH();
149 1.1 cgd if (n == (size_t)-1)
150 1.1 cgd goto bad;
151 1.1 cgd if (n <= 0)
152 1.1 cgd break;
153 1.1 cgd arg += n;
154 1.1 cgd lastc = c;
155 1.1 cgd
156 1.1 cgd if (c == '\'') {
157 1.1 cgd if (rv != 0)
158 1.1 cgd PUT('\'');
159 1.1 cgd PUT('\\');
160 1.1 cgd PUT('\'');
161 1.1 cgd for (;;) {
162 1.1 cgd XLATE_INCH();
163 1.1 cgd if (n <= 0 || c != '\'')
164 1.1 cgd break;
165 1.1 cgd PUT('\\');
166 1.1 cgd PUT('\'');
167 1.1 cgd arg += n;
168 1.1 cgd }
169 1.1 cgd if (n > 0)
170 1.1 cgd PUT('\'');
171 1.1 cgd } else
172 1.1 cgd PUT(c);
173 1.1 cgd }
174 1.1 cgd if (lastc != '\'')
175 1.1 cgd PUT('\'');
176 1.1 cgd
177 1.1 cgd /* Put multibyte or NUL terminator, but don't count the NUL. */
178 1.1 cgd PUT('\0');
179 1.1 cgd rv--;
180 1.1 cgd
181 1.1 cgd return rv;
182 1.1 cgd
183 1.1 cgd bad:
184 1.1 cgd /* A multibyte character encoding or decoding error occurred. */
185 1.2 christos return (size_t)-1;
186 1.1 cgd }
187