add UNLEASHED_OBJ to unleashed.mk
[unleashed/tickless.git] / usr / src / lib / libast / common / string / stropt.c
blobaff59e541bf46d8a184e6d356755094931bb96e7
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
28 #include <ast.h>
29 #include <ctype.h>
32 * parse option expression in s using options in tab with element size siz
33 * first element in tab must be a char*
34 * options match
36 * [no]name[[:]=['"]value["']][, ]...
38 * f is called for each option
40 * (*f)(void* a, char* p, int n, char* v)
42 * a from stropt
43 * p matching tab entry, or name if no table
44 * n 0 if option had ``no'' prefix, -1 if :=, 1 otherwise
45 * v option value pointer
47 * for unmatched options p is 0 and v is the offending option
49 * names in s may be shorter than tab names
50 * longer names must have a prefix that matches a tab name
51 * the first match is returned
52 * \ escapes value using chresc()
55 int
56 stropt(const char* as, const void* tab, int siz, int(*f)(void*, const void*, int, const char*), void* a)
58 register int c;
59 register char* s;
60 register char* v;
61 register char* t;
62 char** p;
63 char* u;
64 char* x;
65 char* e;
66 int n;
67 int ql;
68 int qr;
69 int qc;
71 if (!as) n = 0;
72 else if (!(x = s = strdup(as))) n = -1;
73 else
75 for (;;)
77 while (isspace(*s) || *s == ',') s++;
78 if (*s == 'n' && *(s + 1) == 'o')
80 s += 2;
81 n = 0;
83 else n = 1;
84 if (!*s)
86 n = 0;
87 break;
89 if (tab)
91 for (p = (char**)tab; t = *p; p = (char**)((char*)p + siz))
93 for (v = s; *t && *t++ == *v; v++);
94 if (!*t || isspace(*v) || *v == ',' || *v == '=')
95 break;
96 if (*v == ':' && *(v + 1) == '=')
98 v++;
99 n = -1;
100 break;
103 if (!t)
105 u = v = s;
106 p = 0;
109 else
111 p = (char**)(v = s);
112 t = 0;
114 while (*v && !isspace(*v) && *v != '=' && *v != ',')
115 if (*v++ == ':' && *v == '=')
117 if (!t)
118 *(v - 1) = 0;
119 n = -n;
120 break;
122 if (*v == '=')
124 if (!t)
125 *v = 0;
126 t = s = ++v;
127 ql = qr = 0;
128 while (c = *s++)
130 if (c == '\\')
132 *t++ = chresc(s - 1, &e);
133 s = e;
135 else if (c == qr)
137 if (qr != ql)
138 *t++ = c;
139 if (--qc <= 0)
140 qr = ql = 0;
142 else if (c == ql)
144 *t++ = c;
145 qc++;
147 else if (qr)
148 *t++ = c;
149 else if (c == ',' || isspace(c))
150 break;
151 else if (c == '"' || c == '\'')
153 ql = qr = c;
154 qc = 1;
156 else
158 *t++ = c;
159 if (c == '{')
161 ql = c;
162 qr = '}';
163 qc = 1;
165 else if (c == '(')
167 ql = c;
168 qr = ')';
169 qc = 1;
173 *t = 0;
175 else
177 s = v;
178 c = *s;
179 *s++ = 0;
181 n = p ? (*f)(a, p, n, v) : (*f)(a, p, v - u, u);
182 if (n || !c)
183 break;
185 free(x);
187 return n;