shquote.c revision 1.3 1 1.3 cgd /* $NetBSD: shquote.c,v 1.3 2001/03/10 22:37:54 cgd 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.1 cgd * 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.1 cgd #include <stdlib.h>
47 1.1 cgd #ifdef SHQUOTE_USE_MULTIBYTE
48 1.2 christos #include <limits.h>
49 1.1 cgd #include <stdio.h>
50 1.1 cgd #include <wchar.h>
51 1.1 cgd #endif
52 1.1 cgd
53 1.1 cgd /*
54 1.1 cgd * shquote():
55 1.1 cgd *
56 1.1 cgd * Requotes arguments so that they'll be interpreted properly by the
57 1.1 cgd * shell (/bin/sh).
58 1.1 cgd *
59 1.1 cgd * Wraps single quotes around the string, and replaces single quotes
60 1.1 cgd * in the string with the sequence:
61 1.1 cgd * '\''
62 1.1 cgd *
63 1.1 cgd * Returns the number of characters required to hold the resulting quoted
64 1.1 cgd * argument.
65 1.1 cgd *
66 1.1 cgd * The buffer supplied is filled in and NUL-terminated. If 'bufsize'
67 1.1 cgd * indicates that the buffer is too short to hold the output string, the
68 1.1 cgd * first (bufsize - 1) bytes of quoted argument are filled in and the
69 1.1 cgd * buffer is NUL-terminated.
70 1.1 cgd *
71 1.1 cgd * Changes could be made to optimize the length of strings output by this
72 1.1 cgd * function:
73 1.1 cgd *
74 1.1 cgd * * if there are no metacharacters or whitespace in the input,
75 1.1 cgd * the output could be the input string.
76 1.1 cgd */
77 1.1 cgd
78 1.1 cgd #ifdef SHQUOTE_USE_MULTIBYTE
79 1.1 cgd
80 1.1 cgd #define XLATE_OUTCH(x) wcrtomb(outch, (x), &mbso)
81 1.1 cgd #define XLATE_INCH() \
82 1.1 cgd do { \
83 1.1 cgd n = mbrtowc(&c, arg, MB_CUR_MAX, &mbsi); \
84 1.2 christos } while (/*LINTED const cond*/0)
85 1.1 cgd
86 1.1 cgd #else
87 1.1 cgd
88 1.1 cgd #define XLATE_OUTCH(x) (outch[0] = (x), 1)
89 1.1 cgd #define XLATE_INCH() \
90 1.1 cgd do { \
91 1.1 cgd n = ((c = *arg) != '\0') ? 1 : 0; \
92 1.2 christos } while (/*LINTED const cond*/0)
93 1.1 cgd
94 1.1 cgd #endif
95 1.1 cgd
96 1.1 cgd #define PUT(x) \
97 1.1 cgd do { \
98 1.1 cgd outchlen = XLATE_OUTCH(x); \
99 1.1 cgd if (outchlen == (size_t)-1) \
100 1.1 cgd goto bad; \
101 1.1 cgd rv += outchlen; \
102 1.1 cgd if (bufsize != 0) { \
103 1.1 cgd if (bufsize < outchlen || \
104 1.1 cgd (bufsize == outchlen && \
105 1.1 cgd outch[outchlen - 1] != '\0')) { \
106 1.1 cgd *buf = '\0'; \
107 1.1 cgd bufsize = 0; \
108 1.1 cgd } else { \
109 1.1 cgd memcpy(buf, outch, outchlen); \
110 1.1 cgd buf += outchlen; \
111 1.1 cgd bufsize -= outchlen; \
112 1.1 cgd } \
113 1.1 cgd } \
114 1.2 christos } while (/*LINTED const cond*/0)
115 1.1 cgd
116 1.1 cgd size_t
117 1.1 cgd shquote(const char *arg, char *buf, size_t bufsize)
118 1.1 cgd {
119 1.1 cgd #ifdef SHQUOTE_USE_MULTIBYTE
120 1.2 christos char outch[MB_LEN_MAX];
121 1.1 cgd mbstate_t mbsi, mbso;
122 1.1 cgd wchar_t c, lastc;
123 1.1 cgd size_t outchlen;
124 1.1 cgd #else
125 1.1 cgd char outch[1];
126 1.1 cgd char c, lastc;
127 1.1 cgd size_t outchlen;
128 1.1 cgd #endif
129 1.1 cgd size_t rv;
130 1.1 cgd int n;
131 1.1 cgd
132 1.1 cgd rv = 0;
133 1.1 cgd lastc = 0;
134 1.1 cgd #ifdef SHQUOTE_USE_MULTIBYTE
135 1.1 cgd memset(&mbsi, 0, sizeof mbsi);
136 1.1 cgd memset(&mbso, 0, sizeof mbso);
137 1.1 cgd #endif
138 1.1 cgd
139 1.1 cgd if (*arg != '\'')
140 1.1 cgd PUT('\'');
141 1.1 cgd for (;;) {
142 1.1 cgd XLATE_INCH();
143 1.1 cgd if (n == (size_t)-1)
144 1.1 cgd goto bad;
145 1.1 cgd if (n <= 0)
146 1.1 cgd break;
147 1.1 cgd arg += n;
148 1.1 cgd lastc = c;
149 1.1 cgd
150 1.1 cgd if (c == '\'') {
151 1.1 cgd if (rv != 0)
152 1.1 cgd PUT('\'');
153 1.1 cgd PUT('\\');
154 1.1 cgd PUT('\'');
155 1.1 cgd for (;;) {
156 1.1 cgd XLATE_INCH();
157 1.1 cgd if (n <= 0 || c != '\'')
158 1.1 cgd break;
159 1.1 cgd PUT('\\');
160 1.1 cgd PUT('\'');
161 1.1 cgd arg += n;
162 1.1 cgd }
163 1.1 cgd if (n > 0)
164 1.1 cgd PUT('\'');
165 1.1 cgd } else
166 1.1 cgd PUT(c);
167 1.1 cgd }
168 1.1 cgd if (lastc != '\'')
169 1.1 cgd PUT('\'');
170 1.1 cgd
171 1.1 cgd /* Put multibyte or NUL terminator, but don't count the NUL. */
172 1.1 cgd PUT('\0');
173 1.1 cgd rv--;
174 1.1 cgd
175 1.1 cgd return rv;
176 1.1 cgd
177 1.1 cgd bad:
178 1.1 cgd /* A multibyte character encoding or decoding error occurred. */
179 1.2 christos return (size_t)-1;
180 1.1 cgd }
181