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