1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin /*
24da2e3ebdSchin * Glenn Fowler
25da2e3ebdSchin * AT&T Research
26da2e3ebdSchin */
27da2e3ebdSchin
28da2e3ebdSchin #include <ast.h>
29da2e3ebdSchin #include <ctype.h>
30da2e3ebdSchin
31da2e3ebdSchin /*
32da2e3ebdSchin * parse option expression in s using options in tab with element size siz
33da2e3ebdSchin * first element in tab must be a char*
34da2e3ebdSchin * options match
35da2e3ebdSchin *
36da2e3ebdSchin * [no]name[[:]=['"]value["']][, ]...
37da2e3ebdSchin *
38da2e3ebdSchin * f is called for each option
39da2e3ebdSchin *
40da2e3ebdSchin * (*f)(void* a, char* p, int n, char* v)
41da2e3ebdSchin *
42da2e3ebdSchin * a from stropt
43da2e3ebdSchin * p matching tab entry, or name if no table
44da2e3ebdSchin * n 0 if option had ``no'' prefix, -1 if :=, 1 otherwise
45da2e3ebdSchin * v option value pointer
46da2e3ebdSchin *
47da2e3ebdSchin * for unmatched options p is 0 and v is the offending option
48da2e3ebdSchin *
49da2e3ebdSchin * names in s may be shorter than tab names
50da2e3ebdSchin * longer names must have a prefix that matches a tab name
51da2e3ebdSchin * the first match is returned
52da2e3ebdSchin * \ escapes value using chresc()
53da2e3ebdSchin */
54da2e3ebdSchin
55da2e3ebdSchin int
stropt(const char * as,const void * tab,int siz,int (* f)(void *,const void *,int,const char *),void * a)56da2e3ebdSchin stropt(const char* as, const void* tab, int siz, int(*f)(void*, const void*, int, const char*), void* a)
57da2e3ebdSchin {
58da2e3ebdSchin register int c;
59da2e3ebdSchin register char* s;
60da2e3ebdSchin register char* v;
61da2e3ebdSchin register char* t;
62da2e3ebdSchin char** p;
63da2e3ebdSchin char* u;
64da2e3ebdSchin char* x;
65da2e3ebdSchin char* e;
66da2e3ebdSchin int n;
67da2e3ebdSchin int ql;
68da2e3ebdSchin int qr;
69da2e3ebdSchin int qc;
70da2e3ebdSchin
71da2e3ebdSchin if (!as) n = 0;
72da2e3ebdSchin else if (!(x = s = strdup(as))) n = -1;
73da2e3ebdSchin else
74da2e3ebdSchin {
75da2e3ebdSchin for (;;)
76da2e3ebdSchin {
77da2e3ebdSchin while (isspace(*s) || *s == ',') s++;
78da2e3ebdSchin if (*s == 'n' && *(s + 1) == 'o')
79da2e3ebdSchin {
80da2e3ebdSchin s += 2;
81da2e3ebdSchin n = 0;
82da2e3ebdSchin }
83da2e3ebdSchin else n = 1;
84da2e3ebdSchin if (!*s)
85da2e3ebdSchin {
86da2e3ebdSchin n = 0;
87da2e3ebdSchin break;
88da2e3ebdSchin }
89da2e3ebdSchin if (tab)
90da2e3ebdSchin {
91da2e3ebdSchin for (p = (char**)tab; t = *p; p = (char**)((char*)p + siz))
92da2e3ebdSchin {
93da2e3ebdSchin for (v = s; *t && *t++ == *v; v++);
94da2e3ebdSchin if (!*t || isspace(*v) || *v == ',' || *v == '=')
95da2e3ebdSchin break;
96da2e3ebdSchin if (*v == ':' && *(v + 1) == '=')
97da2e3ebdSchin {
98da2e3ebdSchin v++;
99da2e3ebdSchin n = -1;
100da2e3ebdSchin break;
101da2e3ebdSchin }
102da2e3ebdSchin }
103da2e3ebdSchin if (!t)
104da2e3ebdSchin {
105da2e3ebdSchin u = v = s;
106da2e3ebdSchin p = 0;
107da2e3ebdSchin }
108da2e3ebdSchin }
109da2e3ebdSchin else
110da2e3ebdSchin {
111da2e3ebdSchin p = (char**)(v = s);
112da2e3ebdSchin t = 0;
113da2e3ebdSchin }
114da2e3ebdSchin while (*v && !isspace(*v) && *v != '=' && *v != ',')
115da2e3ebdSchin if (*v++ == ':' && *v == '=')
116da2e3ebdSchin {
117da2e3ebdSchin if (!t)
118da2e3ebdSchin *(v - 1) = 0;
119da2e3ebdSchin n = -n;
120da2e3ebdSchin break;
121da2e3ebdSchin }
122da2e3ebdSchin if (*v == '=')
123da2e3ebdSchin {
124da2e3ebdSchin if (!t)
125da2e3ebdSchin *v = 0;
126da2e3ebdSchin t = s = ++v;
127da2e3ebdSchin ql = qr = 0;
128da2e3ebdSchin while (c = *s++)
129da2e3ebdSchin {
130da2e3ebdSchin if (c == '\\')
131da2e3ebdSchin {
132da2e3ebdSchin *t++ = chresc(s - 1, &e);
133da2e3ebdSchin s = e;
134da2e3ebdSchin }
135da2e3ebdSchin else if (c == qr)
136da2e3ebdSchin {
137da2e3ebdSchin if (qr != ql)
138da2e3ebdSchin *t++ = c;
139da2e3ebdSchin if (--qc <= 0)
140da2e3ebdSchin qr = ql = 0;
141da2e3ebdSchin }
142da2e3ebdSchin else if (c == ql)
143da2e3ebdSchin {
144da2e3ebdSchin *t++ = c;
145da2e3ebdSchin qc++;
146da2e3ebdSchin }
147da2e3ebdSchin else if (qr)
148da2e3ebdSchin *t++ = c;
149da2e3ebdSchin else if (c == ',' || isspace(c))
150da2e3ebdSchin break;
151da2e3ebdSchin else if (c == '"' || c == '\'')
152da2e3ebdSchin {
153da2e3ebdSchin ql = qr = c;
154da2e3ebdSchin qc = 1;
155da2e3ebdSchin }
156da2e3ebdSchin else
157da2e3ebdSchin {
158da2e3ebdSchin *t++ = c;
159da2e3ebdSchin if (c == '{')
160da2e3ebdSchin {
161da2e3ebdSchin ql = c;
162da2e3ebdSchin qr = '}';
163da2e3ebdSchin qc = 1;
164da2e3ebdSchin }
165da2e3ebdSchin else if (c == '(')
166da2e3ebdSchin {
167da2e3ebdSchin ql = c;
168da2e3ebdSchin qr = ')';
169da2e3ebdSchin qc = 1;
170da2e3ebdSchin }
171da2e3ebdSchin }
172da2e3ebdSchin }
173da2e3ebdSchin *t = 0;
174da2e3ebdSchin }
175da2e3ebdSchin else
176da2e3ebdSchin {
177da2e3ebdSchin s = v;
178da2e3ebdSchin c = *s;
179da2e3ebdSchin *s++ = 0;
180da2e3ebdSchin }
181da2e3ebdSchin n = p ? (*f)(a, p, n, v) : (*f)(a, p, v - u, u);
182da2e3ebdSchin if (n || !c)
183da2e3ebdSchin break;
184da2e3ebdSchin }
185da2e3ebdSchin free(x);
186da2e3ebdSchin }
187da2e3ebdSchin return n;
188da2e3ebdSchin }
189