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