GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/awk/parse.c Lines: 94 106 88.7 %
Date: 2017-11-07 Branches: 22 32 68.8 %

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
{
35
	Node *x;
36
37
52790
	x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *));
38
26395
	if (x == NULL)
39
		FATAL("out of space in nodealloc");
40
26395
	x->nnext = NULL;
41
26395
	x->lineno = lineno;
42
26395
	return(x);
43
}
44
45
Node *exptostat(Node *a)
46
{
47
4196
	a->ntype = NSTAT;
48
2098
	return(a);
49
}
50
51
Node *node1(int a, Node *b)
52
{
53
	Node *x;
54
55
28864
	x = nodealloc(1);
56
14432
	x->nobj = a;
57
14432
	x->narg[0]=b;
58
14432
	return(x);
59
}
60
61
Node *node2(int a, Node *b, Node *c)
62
{
63
	Node *x;
64
65
14736
	x = nodealloc(2);
66
7368
	x->nobj = a;
67
7368
	x->narg[0] = b;
68
7368
	x->narg[1] = c;
69
7368
	return(x);
70
}
71
72
Node *node3(int a, Node *b, Node *c, Node *d)
73
{
74
	Node *x;
75
76
8480
	x = nodealloc(3);
77
4240
	x->nobj = a;
78
4240
	x->narg[0] = b;
79
4240
	x->narg[1] = c;
80
4240
	x->narg[2] = d;
81
4240
	return(x);
82
}
83
84
Node *node4(int a, Node *b, Node *c, Node *d, Node *e)
85
{
86
	Node *x;
87
88
710
	x = nodealloc(4);
89
355
	x->nobj = a;
90
355
	x->narg[0] = b;
91
355
	x->narg[1] = c;
92
355
	x->narg[2] = d;
93
355
	x->narg[3] = e;
94
355
	return(x);
95
}
96
97
Node *stat1(int a, Node *b)
98
{
99
	Node *x;
100
101
450
	x = node1(a,b);
102
225
	x->ntype = NSTAT;
103
225
	return(x);
104
}
105
106
Node *stat2(int a, Node *b, Node *c)
107
{
108
	Node *x;
109
110
1844
	x = node2(a,b,c);
111
922
	x->ntype = NSTAT;
112
922
	return(x);
113
}
114
115
Node *stat3(int a, Node *b, Node *c, Node *d)
116
{
117
	Node *x;
118
119
7700
	x = node3(a,b,c,d);
120
3850
	x->ntype = NSTAT;
121
3850
	return(x);
122
}
123
124
Node *stat4(int a, Node *b, Node *c, Node *d, Node *e)
125
{
126
	Node *x;
127
128
704
	x = node4(a,b,c,d,e);
129
352
	x->ntype = NSTAT;
130
352
	return(x);
131
}
132
133
Node *op1(int a, Node *b)
134
{
135
	Node *x;
136
137
4068
	x = node1(a,b);
138
2034
	x->ntype = NEXPR;
139
2034
	return(x);
140
}
141
142
Node *op2(int a, Node *b, Node *c)
143
{
144
	Node *x;
145
146
12892
	x = node2(a,b,c);
147
6446
	x->ntype = NEXPR;
148
6446
	return(x);
149
}
150
151
Node *op3(int a, Node *b, Node *c, Node *d)
152
{
153
	Node *x;
154
155
780
	x = node3(a,b,c,d);
156
390
	x->ntype = NEXPR;
157
390
	return(x);
158
}
159
160
Node *op4(int a, Node *b, Node *c, Node *d, Node *e)
161
{
162
	Node *x;
163
164
6
	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
{
171
	Node *x;
172
173
24346
	a->ctype = OCELL;
174
12173
	a->csub = b;
175
12173
	x = node1(0, (Node *) a);
176
12173
	x->ntype = NVALUE;
177
12173
	return(x);
178
}
179
180
Node *rectonode(void)	/* make $0 into a Node */
181
{
182
	extern Cell *literal0;
183
274
	return op1(INDIRECT, celltonode(literal0, CUNK));
184
}
185
186
Node *makearr(Node *p)
187
{
188
	Cell *cp;
189
190
592
	if (isvalue(p)) {
191
296
		cp = (Cell *) (p->narg[0]);
192
296
		if (isfcn(cp))
193
			SYNTAX( "%s is a function, not an array", cp->nval );
194
296
		else if (!isarr(cp)) {
195
46
			xfree(cp->sval);
196
23
			cp->sval = (char *) makesymtab(NSYMTAB);
197
23
			cp->tval = ARR;
198
23
		}
199
	}
200
296
	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
{
220
	Node *c;
221
222
10368
	if (errorflag)	/* don't link things that are wrong */
223
		return a;
224
5184
	if (a == NULL)
225
511
		return(b);
226
4673
	else if (b == NULL)
227
1143
		return(a);
228
74172
	for (c = a; c->nnext != NULL; c = c->nnext)
229
		;
230
3530
	c->nnext = b;
231
3530
	return(a);
232
5184
}
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
62
	if (isarr(v)) {
240
		SYNTAX( "`%s' is an array name and a function name", v->nval );
241
		return;
242
	}
243
31
	if (isarg(v->nval) != -1) {
244
		SYNTAX( "`%s' is both function name and argument name", v->nval );
245
		return;
246
	}
247
248
31
	v->tval = FCN;
249
31
	v->sval = (char *) st;
250
	n = 0;	/* count arguments */
251
122
	for (p = vl; p; p = p->nnext)
252
30
		n++;
253
31
	v->fval = n;
254
31
	dprintf( ("defining func %s (%d args)\n", v->nval, n) );
255
62
}
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
490
	Node *p = arglist;
261
	int n;
262
263
886
	for (n = 0; p != 0; p = p->nnext, n++)
264
252
		if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
265
54
			return n;
266
191
	return -1;
267
245
}
268
269
int ptoi(void *p)	/* convert pointer to integer */
270
{
271
8723518
	return (int) (long) p;	/* swearing that p fits, of course */
272
}
273
274
Node *itonp(int i)	/* and vice versa */
275
{
276
1992
	return (Node *) (long) i;
277
}