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