xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/string/stresc.c (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Research
26  *
27  * convert \X character constants in s in place
28  * the length of the converted s is returned (may have embedded \0's)
29  * wide chars absent locale guidance default to UTF-8
30  * strexp() FMT_EXP_* flags passed to chrexp() for selective conversion
31  */
32 
33 #include <ast.h>
34 
35 int
strexp(register char * s,int flags)36 strexp(register char* s, int flags)
37 {
38 	register char*		t;
39 	register unsigned int	c;
40 	char*			b;
41 	char*			e;
42 	int			w;
43 
44 	b = t = s;
45 	while (c = *s++)
46 	{
47 		if (c == '\\')
48 		{
49 			c = chrexp(s - 1, &e, &w, flags);
50 			s = e;
51 			if (w)
52 			{
53 				t += mbwide() ? mbconv(t, c) : wc2utf8(t, c);
54 				continue;
55 			}
56 		}
57 		*t++ = c;
58 	}
59 	*t = 0;
60 	return t - b;
61 }
62 
63 int
stresc(register char * s)64 stresc(register char* s)
65 {
66 	return strexp(s, FMT_EXP_CHAR|FMT_EXP_LINE|FMT_EXP_WIDE);
67 }
68