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