1da2e3ebchin/***********************************************************************
2da2e3ebchin*                                                                      *
3da2e3ebchin*               This software is part of the ast package               *
43e14f97Roger A. Faulkner*          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebchin*                      and is licensed under the                       *
6da2e3ebchin*                  Common Public License, Version 1.0                  *
77c2fbfbApril Chin*                    by AT&T Intellectual Property                     *
8da2e3ebchin*                                                                      *
9da2e3ebchin*                A copy of the License is available at                 *
10da2e3ebchin*            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebchin*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebchin*                                                                      *
13da2e3ebchin*              Information and Software Systems Research               *
14da2e3ebchin*                            AT&T Research                             *
15da2e3ebchin*                           Florham Park NJ                            *
16da2e3ebchin*                                                                      *
17da2e3ebchin*                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebchin*                  David Korn <dgk@research.att.com>                   *
19da2e3ebchin*                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebchin*                                                                      *
21da2e3ebchin***********************************************************************/
22da2e3ebchin#pragma prototyped
23da2e3ebchin/*
24da2e3ebchin * Glenn Fowler
25da2e3ebchin * AT&T Research
26da2e3ebchin *
27da2e3ebchin * return string with expanded escape chars
28da2e3ebchin */
29da2e3ebchin
30da2e3ebchin#include <ast.h>
31da2e3ebchin#include <ccode.h>
32da2e3ebchin#include <ctype.h>
33da2e3ebchin
34da2e3ebchin/*
35da2e3ebchin * quote string as of length n with qb...qe
36da2e3ebchin * (flags&FMT_ALWAYS) always quotes, otherwise quote output only if necessary
37da2e3ebchin * qe and the usual suspects are \... escaped
38da2e3ebchin * (flags&FMT_WIDE) doesn't escape 8 bit chars
39da2e3ebchin * (flags&FMT_ESCAPED) doesn't \... escape the usual suspects
40da2e3ebchin * (flags&FMT_SHELL) escape $`"#;~&|()<>[]*?
41da2e3ebchin */
42da2e3ebchin
43da2e3ebchinchar*
44da2e3ebchinfmtquote(const char* as, const char* qb, const char* qe, size_t n, int flags)
45da2e3ebchin{
46da2e3ebchin	register unsigned char*	s = (unsigned char*)as;
47da2e3ebchin	register unsigned char*	e = s + n;
48da2e3ebchin	register char*		b;
49da2e3ebchin	register int		c;
50da2e3ebchin	register int		escaped;
51da2e3ebchin	register int		spaced;
52da2e3ebchin	register int		doublequote;
53da2e3ebchin	register int		singlequote;
54da2e3ebchin	int			shell;
55da2e3ebchin	char*			f;
56da2e3ebchin	char*			buf;
57da2e3ebchin
58da2e3ebchin	c = 4 * (n + 1);
59da2e3ebchin	if (qb)
60da2e3ebchin		c += strlen((char*)qb);
61da2e3ebchin	if (qe)
62da2e3ebchin		c += strlen((char*)qe);
63da2e3ebchin	b = buf = fmtbuf(c);
64da2e3ebchin	shell = 0;
65da2e3ebchin	doublequote = 0;
66da2e3ebchin	singlequote = 0;
67da2e3ebchin	if (qb)
68da2e3ebchin	{
69da2e3ebchin		if (qb[0] == '$' && qb[1] == '\'' && qb[2] == 0)
70da2e3ebchin			shell = 1;
71da2e3ebchin		else if ((flags & FMT_SHELL) && qb[1] == 0)
72da2e3ebchin		{
73da2e3ebchin			if (qb[0] == '"')
74da2e3ebchin				doublequote = 1;
75da2e3ebchin			else if (qb[0] == '\'')
76da2e3ebchin				singlequote = 1;
77da2e3ebchin		}
78da2e3ebchin		while (*b = *qb++)
79da2e3ebchin			b++;
80da2e3ebchin	}
81da2e3ebchin	else if (flags & FMT_SHELL)
82da2e3ebchin		doublequote = 1;
83da2e3ebchin	f = b;
84da2e3ebchin	escaped = spaced = !!(flags & FMT_ALWAYS);
85da2e3ebchin	while (s < e)
86da2e3ebchin	{
87da2e3ebchin		if ((c = mbsize(s)) > 1)
88da2e3ebchin		{
89da2e3ebchin			while (c-- && s < e)
90da2e3ebchin				*b++ = *s++;
91da2e3ebchin		}
92da2e3ebchin		else
93da2e3ebchin		{
94da2e3ebchin			c = *s++;
95da2e3ebchin			if (!(flags & FMT_ESCAPED) && (iscntrl(c) || !isprint(c) || c == '\\'))
96da2e3ebchin			{
97da2e3ebchin				escaped = 1;
98da2e3ebchin				*b++ = '\\';
99da2e3ebchin				switch (c)
100da2e3ebchin				{
101da2e3ebchin				case CC_bel:
102da2e3ebchin					c = 'a';
103da2e3ebchin					break;
104da2e3ebchin				case '\b':
105da2e3ebchin					c = 'b';
106da2e3ebchin					break;
107da2e3ebchin				case '\f':
108da2e3ebchin					c = 'f';
109da2e3ebchin					break;
110da2e3ebchin				case '\n':
111da2e3ebchin					c = 'n';
112da2e3ebchin					break;
113da2e3ebchin				case '\r':
114da2e3ebchin					c = 'r';
115da2e3ebchin					break;
116da2e3ebchin				case '\t':
117da2e3ebchin					c = 't';
118da2e3ebchin					break;
119da2e3ebchin				case CC_vt:
120da2e3ebchin					c = 'v';
121da2e3ebchin					break;
122da2e3ebchin				case CC_esc:
123da2e3ebchin					c = 'E';
124da2e3ebchin					break;
125da2e3ebchin				case '\\':
126da2e3ebchin					break;
127da2e3ebchin				default:
128da2e3ebchin					if (!(flags & FMT_WIDE) || !(c & 0200))
129da2e3ebchin					{
130da2e3ebchin						*b++ = '0' + ((c >> 6) & 07);
131da2e3ebchin						*b++ = '0' + ((c >> 3) & 07);
132da2e3ebchin						c = '0' + (c & 07);
133da2e3ebchin					}
134da2e3ebchin					else
135da2e3ebchin						b--;
136da2e3ebchin					break;
137da2e3ebchin				}
138da2e3ebchin			}
139da2e3ebchin			else if (c == '\\')
140da2e3ebchin			{
141da2e3ebchin				escaped = 1;
142da2e3ebchin				*b++ = c;
143da2e3ebchin				if (*s)
144da2e3ebchin					c = *s++;
145da2e3ebchin			}
146da2e3ebchin			else if (qe && strchr(qe, c))
147da2e3ebchin			{
148da2e3ebchin				if (singlequote && c == '\'')
149da2e3ebchin				{
150da2e3ebchin					spaced = 1;
151da2e3ebchin					*b++ = '\'';
152da2e3ebchin					*b++ = '\\';
153da2e3ebchin					*b++ = '\'';
154da2e3ebchin					c = '\'';
155da2e3ebchin				}
156da2e3ebchin				else
157da2e3ebchin				{
158da2e3ebchin					escaped = 1;
159da2e3ebchin					*b++ = '\\';
160da2e3ebchin				}
161da2e3ebchin			}
162da2e3ebchin			else if (c == '$' || c == '`')
163da2e3ebchin			{
164da2e3ebchin				if (c == '$' && (flags & FMT_PARAM) && (*s == '{' || *s == '('))
165da2e3ebchin				{
166da2e3ebchin					if (singlequote || shell)
167da2e3ebchin					{
168da2e3ebchin						escaped = 1;
169da2e3ebchin						*b++ = '\'';
170da2e3ebchin						*b++ = c;
171da2e3ebchin						*b++ = *s++;
172da2e3ebchin						if (shell)
173da2e3ebchin						{
174da2e3ebchin							spaced = 1;
175da2e3ebchin							*b++ = '$';
176da2e3ebchin						}
177da2e3ebchin						c = '\'';
178da2e3ebchin					}
179da2e3ebchin					else
180da2e3ebchin					{
181da2e3ebchin						escaped = 1;
182da2e3ebchin						*b++ = c;
183da2e3ebchin						c = *s++;
184da2e3ebchin					}
185da2e3ebchin				}
186da2e3ebchin				else if (doublequote)
187da2e3ebchin					*b++ = '\\';
188da2e3ebchin				else if (singlequote || (flags & FMT_SHELL))
189da2e3ebchin					spaced = 1;
190da2e3ebchin			}
191da2e3ebchin			else if (!spaced && !escaped && (isspace(c) || ((flags & FMT_SHELL) || shell) && (strchr("\";~&|()<>[]*?", c) || c == '#' && (b == f || isspace(*(b - 1))))))
192da2e3ebchin				spaced = 1;
193da2e3ebchin			*b++ = c;
194da2e3ebchin		}
195da2e3ebchin	}
196da2e3ebchin	if (qb)
197da2e3ebchin	{
198da2e3ebchin		if (!escaped)
199da2e3ebchin			buf += shell + !spaced;
200da2e3ebchin		if (qe && (escaped || spaced))
201da2e3ebchin			while (*b = *qe++)
202da2e3ebchin				b++;
203da2e3ebchin	}
204da2e3ebchin	*b = 0;
205da2e3ebchin	return buf;
206da2e3ebchin}
207da2e3ebchin
208da2e3ebchin/*
209da2e3ebchin * escape the usual suspects and quote chars in qs
210da2e3ebchin * in length n string as
211da2e3ebchin */
212da2e3ebchin
213da2e3ebchinchar*
214da2e3ebchinfmtnesq(const char* as, const char* qs, size_t n)
215da2e3ebchin{
216da2e3ebchin	return fmtquote(as, NiL, qs, n, 0);
217da2e3ebchin}
218da2e3ebchin
219da2e3ebchin/*
220da2e3ebchin * escape the usual suspects and quote chars in qs
221da2e3ebchin */
222da2e3ebchin
223da2e3ebchinchar*
224da2e3ebchinfmtesq(const char* as, const char* qs)
225da2e3ebchin{
226da2e3ebchin	return fmtquote(as, NiL, qs, strlen((char*)as), 0);
227da2e3ebchin}
228da2e3ebchin
229da2e3ebchin/*
230da2e3ebchin * escape the usual suspects
231da2e3ebchin */
232da2e3ebchin
233da2e3ebchinchar*
234da2e3ebchinfmtesc(const char* as)
235da2e3ebchin{
236da2e3ebchin	return fmtquote(as, NiL, NiL, strlen((char*)as), 0);
237da2e3ebchin}
238