6198 Let's EOL cachefs
[illumos-gate.git] / usr / src / lib / libast / common / misc / optesc.c
blob26c6892fab553e5838a117cc5f41bc305541327c
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 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
24 * Glenn Fowler
25 * AT&T Research
27 * escape optget() special chars in s and write to sp
28 * esc == '?' or ':' also escaped
31 #include <optlib.h>
32 #include <ctype.h>
34 int
35 optesc(Sfio_t* sp, register const char* s, int esc)
37 register const char* m;
38 register int c;
40 if (*s == '[' && *(s + 1) == '+' && *(s + 2) == '?')
42 c = strlen(s);
43 if (s[c - 1] == ']')
45 sfprintf(sp, "%-.*s", c - 4, s + 3);
46 return 0;
49 if (esc != '?' && esc != ':')
50 esc = 0;
51 while (c = *s++)
53 if (isalnum(c))
55 for (m = s - 1; isalnum(*s); s++);
56 if (isalpha(c) && *s == '(' && isdigit(*(s + 1)) && *(s + 2) == ')')
58 sfputc(sp, '\b');
59 sfwrite(sp, m, s - m);
60 sfputc(sp, '\b');
61 sfwrite(sp, s, 3);
62 s += 3;
64 else
65 sfwrite(sp, m, s - m);
67 else if (c == '-' && *s == '-' || c == '<')
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 == ',')
78 sfputc(sp, '\b');
79 sfwrite(sp, m, s - m);
80 sfputc(sp, '\b');
82 else
83 sfwrite(sp, m, s - m);
85 else
87 if (c == ']' || c == esc)
88 sfputc(sp, c);
89 sfputc(sp, c);
92 return 0;