summaryrefslogtreecommitdiff
path: root/usr/src/lib/libpp/common/ppmisc.c
blob: f4dea77f5c7e9af29cff7b6b80feb86a506e069a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1986-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * Glenn Fowler
 * AT&T Research
 *
 * miscellaneous preprocessor support
 */

#include "pplib.h"

/*
 * macro symbol def|ref
 */

struct ppsymbol*
pprefmac(char* name, int ref)
{
	register struct ppsymbol*	sym;

	if (!(sym = ppsymget(pp.symtab, name)) && (ref <= REF_NORMAL && pp.macref || ref == REF_CREATE || ref == REF_DELETE && (pp.mode & (INIT|READONLY))))
	{
		if ((pp.state & COMPILE) && pp.truncate && strlen(name) > pp.truncate)
			name[pp.truncate] = 0;
		sym = ppsymset(pp.symtab, NiL);
	}
	if (sym && ref <= REF_NORMAL)
	{
		if (pp.macref) (*pp.macref)(sym, error_info.file, error_info.line, ref == REF_NORMAL && (pp.state & CONDITIONAL) ? REF_IF : ref, 0L);
		if (!sym->macro) sym = 0;
	}
#if COMPATIBLE
	if (!(pp.state & COMPATIBILITY))
#endif
	if (ref == REF_IF && sym && (sym->flags & SYM_PREDEFINED) && *name != '_' && !(pp.mode & (HOSTED|INACTIVE)))
	{
		if (pp.state & STRICT)
		{
			error(1, "%s: obsolete predefined symbol reference disabled", name);
			return(0);
		}
		error(1, "%s: obsolete predefined symbol referenced", name);
	}
	return(sym);
}

/*
 * common predicate assertion operations
 * op is DEFINE or UNDEF
 */

void
ppassert(int op, char* pred, char* args)
{
	register struct pplist*		a;
	register struct ppsymbol*	sym;
	register struct pplist*		p;
	register struct pplist*		q;

	if (!args) switch (op)
	{
	case DEFINE:
		goto mark;
	case UNDEF:
		a = 0;
		goto unmark;
	}
	if (a = (struct pplist*)hashget(pp.prdtab, pred))
	{
		p = 0;
		q = a;
		while (q)
		{
			if (streq(q->value, args))
			{
				if (op == DEFINE) return;
				q = q->next;
				if (p) p->next = q;
				else a = q;
			}
			else
			{
				p = q;
				q = q->next;
			}
		}
		if (op == UNDEF)
		{
		unmark:
			hashput(pp.prdtab, pred, a);
			if (sym = ppsymref(pp.symtab, pred))
				sym->flags &= ~SYM_PREDICATE;
			return;
		}
	}
	if (op == DEFINE)
	{
		p = newof(0, struct pplist, 1, 0);
		p->next = a;
		p->value = strdup(args);
		hashput(pp.prdtab, NiL, p);
	mark:
		if ((pp.state & COMPILE) && pp.truncate) return;
		if (sym = ppsymset(pp.symtab, pred))
			sym->flags |= SYM_PREDICATE;
	}
}

/*
 * parse a predicate argument list
 * the args are placed in pp.args
 * the first non-space/paren argument token type is returned
 * forms:
 *
 *	predicate <identifier>			type=T_ID
 *	predicate ( <identifier> )		type=T_ID
 *	predicate ( )				type=0
 *	predicate ( <balanced-paren-list> )	type=T_STRING
 *	otherwise				type=<other>
 */

int
pppredargs(void)
{
	register int	c;
	register int	n;
	register int	type;
	char*		pptoken;

	pptoken = pp.token;
	pp.token = pp.args;
	switch (type = pplex())
	{
	case '(':
		type = 0;
		n = 1;
		pp.state |= HEADER;
		pp.state &= ~STRIP;
		c = pplex();
		pp.state &= ~NOSPACE;
		for (;;)
		{
			switch (c)
			{
			case '(':
				n++;
				break;
			case '\n':
				ungetchr(c);
				error(2, "missing %d )%s in predicate argument list", n, n == 1 ? "" : "'s");
				type = 0;
				goto done;
			case ')':
				if (!--n) goto done;
				break;
			}
			pp.token = pp.toknxt;
			if (c != ' ')
			{
				if (type) type = T_STRING;
				else type = (c == T_ID) ? T_ID : T_STRING;
			}
			c = pplex();
		}
	done:
		pp.state &= ~HEADER;
		pp.state |= NOSPACE|STRIP;
		if (pp.token > pp.args && *(pp.token - 1) == ' ') pp.token--;
		*pp.token = 0;
		break;
	case '\n':
		ungetchr('\n');
		type = 0;
		break;
	}
	pp.token = pptoken;
	return(type);
}

/*
 * sync output line number
 */

int
ppsync(void)
{
	long	m;

	if ((pp.state & (ADD|HIDDEN)))
	{
		if (pp.state & ADD)
		{
			pp.state &= ~ADD;
			m = pp.addp - pp.addbuf;
			pp.addp = pp.addbuf;
			ppprintf("%-.*s", m, pp.addbuf);
		}
		if (pp.linesync)
		{
			if ((pp.state & SYNCLINE) || pp.hidden >= MAXHIDDEN)
			{
				pp.hidden = 0;
				pp.state &= ~(HIDDEN|SYNCLINE);
				if (error_info.line)
					(*pp.linesync)(error_info.line, error_info.file);
			}
			else
			{
				m = pp.hidden;
				pp.hidden = 0;
				pp.state &= ~HIDDEN;
				while (m-- > 0)
					ppputchar('\n');
			}
		}
		else
		{
			pp.hidden = 0;
			pp.state &= ~HIDDEN;
			ppputchar('\n');
		}
	}
	return 0;
}