xref: /titanic_50/usr/src/lib/libast/common/misc/optesc.c (revision b5a2d8455dfa3190fc977c4bec53e91c99012767)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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  * escape optget() special chars in s and write to sp
28  * esc == '?' or ':' also escaped
29  */
30 
31 #include <optlib.h>
32 #include <ctype.h>
33 
34 int
35 optesc(Sfio_t* sp, register const char* s, int esc)
36 {
37 	register const char*	m;
38 	register int		c;
39 
40 	if (*s == '[' && *(s + 1) == '+' && *(s + 2) == '?')
41 	{
42 		c = strlen(s);
43 		if (s[c - 1] == ']')
44 		{
45 			sfprintf(sp, "%-.*s", c - 4, s + 3);
46 			return 0;
47 		}
48 	}
49 	if (esc != '?' && esc != ':')
50 		esc = 0;
51 	while (c = *s++)
52 	{
53 		if (isalnum(c))
54 		{
55 			for (m = s - 1; isalnum(*s); s++);
56 			if (isalpha(c) && *s == '(' && isdigit(*(s + 1)) && *(s + 2) == ')')
57 			{
58 				sfputc(sp, '\b');
59 				sfwrite(sp, m, s - m);
60 				sfputc(sp, '\b');
61 				sfwrite(sp, s, 3);
62 				s += 3;
63 			}
64 			else
65 				sfwrite(sp, m, s - m);
66 		}
67 		else if (c == '-' && *s == '-' || c == '<')
68 		{
69 			m = s - 1;
70 			if (c == '-')
71 				s++;
72 			else if (*s == '/')
73 				s++;
74 			while (isalnum(*s))
75 				s++;
76 			if (c == '<' && *s == '>' || isspace(*s) || *s == 0 || *s == '=' || *s == ':' || *s == ';' || *s == '.' || *s == ',')
77 			{
78 				sfputc(sp, '\b');
79 				sfwrite(sp, m, s - m);
80 				sfputc(sp, '\b');
81 			}
82 			else
83 				sfwrite(sp, m, s - m);
84 		}
85 		else
86 		{
87 			if (c == ']' || c == esc)
88 				sfputc(sp, c);
89 			sfputc(sp, c);
90 		}
91 	}
92 	return 0;
93 }
94