GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/awk/parse.c Lines: 97 125 77.6 %
Date: 2016-12-06 Branches: 13 32 40.6 %

Line Branch Exec Source
1
/*	$OpenBSD: parse.c,v 1.6 2002/12/19 21:24:28 millert Exp $	*/
2
/****************************************************************
3
Copyright (C) Lucent Technologies 1997
4
All Rights Reserved
5
6
Permission to use, copy, modify, and distribute this software and
7
its documentation for any purpose and without fee is hereby
8
granted, provided that the above copyright notice appear in all
9
copies and that both that the copyright notice and this
10
permission notice and warranty disclaimer appear in supporting
11
documentation, and that the name Lucent Technologies or any of
12
its entities not be used in advertising or publicity pertaining
13
to distribution of the software without specific, written prior
14
permission.
15
16
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
18
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
19
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
21
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23
THIS SOFTWARE.
24
****************************************************************/
25
26
#define DEBUG
27
#include <stdio.h>
28
#include <string.h>
29
#include <stdlib.h>
30
#include "awk.h"
31
#include "ytab.h"
32
33
Node *nodealloc(int n)
34
2417
{
35
	Node *x;
36
37
2417
	x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *));
38
2417
	if (x == NULL)
39
		FATAL("out of space in nodealloc");
40
2417
	x->nnext = NULL;
41
2417
	x->lineno = lineno;
42
2417
	return(x);
43
}
44
45
Node *exptostat(Node *a)
46
345
{
47
345
	a->ntype = NSTAT;
48
345
	return(a);
49
}
50
51
Node *node1(int a, Node *b)
52
1373
{
53
	Node *x;
54
55
1373
	x = nodealloc(1);
56
1373
	x->nobj = a;
57
1373
	x->narg[0]=b;
58
1373
	return(x);
59
}
60
61
Node *node2(int a, Node *b, Node *c)
62
850
{
63
	Node *x;
64
65
850
	x = nodealloc(2);
66
850
	x->nobj = a;
67
850
	x->narg[0] = b;
68
850
	x->narg[1] = c;
69
850
	return(x);
70
}
71
72
Node *node3(int a, Node *b, Node *c, Node *d)
73
168
{
74
	Node *x;
75
76
168
	x = nodealloc(3);
77
168
	x->nobj = a;
78
168
	x->narg[0] = b;
79
168
	x->narg[1] = c;
80
168
	x->narg[2] = d;
81
168
	return(x);
82
}
83
84
Node *node4(int a, Node *b, Node *c, Node *d, Node *e)
85
26
{
86
	Node *x;
87
88
26
	x = nodealloc(4);
89
26
	x->nobj = a;
90
26
	x->narg[0] = b;
91
26
	x->narg[1] = c;
92
26
	x->narg[2] = d;
93
26
	x->narg[3] = e;
94
26
	return(x);
95
}
96
97
Node *stat1(int a, Node *b)
98
18
{
99
	Node *x;
100
101
18
	x = node1(a,b);
102
18
	x->ntype = NSTAT;
103
18
	return(x);
104
}
105
106
Node *stat2(int a, Node *b, Node *c)
107
34
{
108
	Node *x;
109
110
34
	x = node2(a,b,c);
111
34
	x->ntype = NSTAT;
112
34
	return(x);
113
}
114
115
Node *stat3(int a, Node *b, Node *c, Node *d)
116
139
{
117
	Node *x;
118
119
139
	x = node3(a,b,c,d);
120
139
	x->ntype = NSTAT;
121
139
	return(x);
122
}
123
124
Node *stat4(int a, Node *b, Node *c, Node *d, Node *e)
125
23
{
126
	Node *x;
127
128
23
	x = node4(a,b,c,d,e);
129
23
	x->ntype = NSTAT;
130
23
	return(x);
131
}
132
133
Node *op1(int a, Node *b)
134
79
{
135
	Node *x;
136
137
79
	x = node1(a,b);
138
79
	x->ntype = NEXPR;
139
79
	return(x);
140
}
141
142
Node *op2(int a, Node *b, Node *c)
143
816
{
144
	Node *x;
145
146
816
	x = node2(a,b,c);
147
816
	x->ntype = NEXPR;
148
816
	return(x);
149
}
150
151
Node *op3(int a, Node *b, Node *c, Node *d)
152
29
{
153
	Node *x;
154
155
29
	x = node3(a,b,c,d);
156
29
	x->ntype = NEXPR;
157
29
	return(x);
158
}
159
160
Node *op4(int a, Node *b, Node *c, Node *d, Node *e)
161
3
{
162
	Node *x;
163
164
3
	x = node4(a,b,c,d,e);
165
3
	x->ntype = NEXPR;
166
3
	return(x);
167
}
168
169
Node *celltonode(Cell *a, int b)
170
1276
{
171
	Node *x;
172
173
1276
	a->ctype = OCELL;
174
1276
	a->csub = b;
175
1276
	x = node1(0, (Node *) a);
176
1276
	x->ntype = NVALUE;
177
1276
	return(x);
178
}
179
180
Node *rectonode(void)	/* make $0 into a Node */
181
6
{
182
	extern Cell *literal0;
183
6
	return op1(INDIRECT, celltonode(literal0, CUNK));
184
}
185
186
Node *makearr(Node *p)
187
258
{
188
	Cell *cp;
189
190
258
	if (isvalue(p)) {
191
258
		cp = (Cell *) (p->narg[0]);
192
258
		if (isfcn(cp))
193
			SYNTAX( "%s is a function, not an array", cp->nval );
194
258
		else if (!isarr(cp)) {
195
8
			xfree(cp->sval);
196
8
			cp->sval = (char *) makesymtab(NSYMTAB);
197
8
			cp->tval = ARR;
198
		}
199
	}
200
258
	return p;
201
}
202
203
#define PA2NUM	50	/* max number of pat,pat patterns allowed */
204
int	paircnt;		/* number of them in use */
205
int	pairstack[PA2NUM];	/* state of each pat,pat */
206
207
Node *pa2stat(Node *a, Node *b, Node *c)	/* pat, pat {...} */
208
{
209
	Node *x;
210
211
	x = node4(PASTAT2, a, b, c, itonp(paircnt));
212
	if (paircnt++ >= PA2NUM)
213
		SYNTAX( "limited to %d pat,pat statements", PA2NUM );
214
	x->ntype = NSTAT;
215
	return(x);
216
}
217
218
Node *linkum(Node *a, Node *b)
219
514
{
220
	Node *c;
221
222
514
	if (errorflag)	/* don't link things that are wrong */
223
		return a;
224
514
	if (a == NULL)
225
25
		return(b);
226
489
	else if (b == NULL)
227
78
		return(a);
228
411
	for (c = a; c->nnext != NULL; c = c->nnext)
229
		;
230
411
	c->nnext = b;
231
411
	return(a);
232
}
233
234
void defn(Cell *v, Node *vl, Node *st)	/* turn on FCN bit in definition, */
235
{					/*   body of function, arglist */
236
	Node *p;
237
	int n;
238
239
	if (isarr(v)) {
240
		SYNTAX( "`%s' is an array name and a function name", v->nval );
241
		return;
242
	}
243
	if (isarg(v->nval) != -1) {
244
		SYNTAX( "`%s' is both function name and argument name", v->nval );
245
		return;
246
	}
247
248
	v->tval = FCN;
249
	v->sval = (char *) st;
250
	n = 0;	/* count arguments */
251
	for (p = vl; p; p = p->nnext)
252
		n++;
253
	v->fval = n;
254
	dprintf( ("defining func %s (%d args)\n", v->nval, n) );
255
}
256
257
int isarg(const char *s)		/* is s in argument list for current function? */
258
{			/* return -1 if not, otherwise arg # */
259
	extern Node *arglist;
260
	Node *p = arglist;
261
	int n;
262
263
	for (n = 0; p != 0; p = p->nnext, n++)
264
		if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
265
			return n;
266
	return -1;
267
}
268
269
int ptoi(void *p)	/* convert pointer to integer */
270
2202
{
271
2202
	return (int) (long) p;	/* swearing that p fits, of course */
272
}
273
274
Node *itonp(int i)	/* and vice versa */
275
50
{
276
50
	return (Node *) (long) i;
277
}