1 63a68686 2008-11-03 jas /****************************************************************
2 63a68686 2008-11-03 jas Copyright (C) Lucent Technologies 1997
3 63a68686 2008-11-03 jas All Rights Reserved
5 63a68686 2008-11-03 jas Permission to use, copy, modify, and distribute this software and
6 63a68686 2008-11-03 jas its documentation for any purpose and without fee is hereby
7 63a68686 2008-11-03 jas granted, provided that the above copyright notice appear in all
8 63a68686 2008-11-03 jas copies and that both that the copyright notice and this
9 63a68686 2008-11-03 jas permission notice and warranty disclaimer appear in supporting
10 63a68686 2008-11-03 jas documentation, and that the name Lucent Technologies or any of
11 63a68686 2008-11-03 jas its entities not be used in advertising or publicity pertaining
12 63a68686 2008-11-03 jas to distribution of the software without specific, written prior
15 63a68686 2008-11-03 jas LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 63a68686 2008-11-03 jas INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 63a68686 2008-11-03 jas IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 63a68686 2008-11-03 jas SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 63a68686 2008-11-03 jas WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 63a68686 2008-11-03 jas IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 63a68686 2008-11-03 jas ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22 63a68686 2008-11-03 jas THIS SOFTWARE.
23 63a68686 2008-11-03 jas ****************************************************************/
25 63a68686 2008-11-03 jas #define DEBUG
26 63a68686 2008-11-03 jas #include <stdio.h>
27 63a68686 2008-11-03 jas #include <string.h>
28 63a68686 2008-11-03 jas #include <stdlib.h>
29 63a68686 2008-11-03 jas #include "awk.h"
30 63a68686 2008-11-03 jas #include "y.tab.h"
32 63a68686 2008-11-03 jas Node *nodealloc(int n)
36 63a68686 2008-11-03 jas x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *));
37 63a68686 2008-11-03 jas if (x == NULL)
38 63a68686 2008-11-03 jas FATAL("out of space in nodealloc");
39 63a68686 2008-11-03 jas x->nnext = NULL;
40 63a68686 2008-11-03 jas x->lineno = lineno;
44 63a68686 2008-11-03 jas Node *exptostat(Node *a)
46 63a68686 2008-11-03 jas a->ntype = NSTAT;
50 63a68686 2008-11-03 jas Node *node1(int a, Node *b)
54 63a68686 2008-11-03 jas x = nodealloc(1);
56 63a68686 2008-11-03 jas x->narg[0]=b;
60 63a68686 2008-11-03 jas Node *node2(int a, Node *b, Node *c)
64 63a68686 2008-11-03 jas x = nodealloc(2);
66 63a68686 2008-11-03 jas x->narg[0] = b;
67 63a68686 2008-11-03 jas x->narg[1] = c;
71 63a68686 2008-11-03 jas Node *node3(int a, Node *b, Node *c, Node *d)
75 63a68686 2008-11-03 jas x = nodealloc(3);
77 63a68686 2008-11-03 jas x->narg[0] = b;
78 63a68686 2008-11-03 jas x->narg[1] = c;
79 63a68686 2008-11-03 jas x->narg[2] = d;
83 63a68686 2008-11-03 jas Node *node4(int a, Node *b, Node *c, Node *d, Node *e)
87 63a68686 2008-11-03 jas x = nodealloc(4);
89 63a68686 2008-11-03 jas x->narg[0] = b;
90 63a68686 2008-11-03 jas x->narg[1] = c;
91 63a68686 2008-11-03 jas x->narg[2] = d;
92 63a68686 2008-11-03 jas x->narg[3] = e;
96 63a68686 2008-11-03 jas Node *stat1(int a, Node *b)
100 63a68686 2008-11-03 jas x = node1(a,b);
101 63a68686 2008-11-03 jas x->ntype = NSTAT;
105 63a68686 2008-11-03 jas Node *stat2(int a, Node *b, Node *c)
109 63a68686 2008-11-03 jas x = node2(a,b,c);
110 63a68686 2008-11-03 jas x->ntype = NSTAT;
114 63a68686 2008-11-03 jas Node *stat3(int a, Node *b, Node *c, Node *d)
118 63a68686 2008-11-03 jas x = node3(a,b,c,d);
119 63a68686 2008-11-03 jas x->ntype = NSTAT;
123 63a68686 2008-11-03 jas Node *stat4(int a, Node *b, Node *c, Node *d, Node *e)
127 63a68686 2008-11-03 jas x = node4(a,b,c,d,e);
128 63a68686 2008-11-03 jas x->ntype = NSTAT;
132 63a68686 2008-11-03 jas Node *op1(int a, Node *b)
136 63a68686 2008-11-03 jas x = node1(a,b);
137 63a68686 2008-11-03 jas x->ntype = NEXPR;
141 63a68686 2008-11-03 jas Node *op2(int a, Node *b, Node *c)
145 63a68686 2008-11-03 jas x = node2(a,b,c);
146 63a68686 2008-11-03 jas x->ntype = NEXPR;
150 63a68686 2008-11-03 jas Node *op3(int a, Node *b, Node *c, Node *d)
154 63a68686 2008-11-03 jas x = node3(a,b,c,d);
155 63a68686 2008-11-03 jas x->ntype = NEXPR;
159 63a68686 2008-11-03 jas Node *op4(int a, Node *b, Node *c, Node *d, Node *e)
163 63a68686 2008-11-03 jas x = node4(a,b,c,d,e);
164 63a68686 2008-11-03 jas x->ntype = NEXPR;
168 63a68686 2008-11-03 jas Node *celltonode(Cell *a, int b)
172 63a68686 2008-11-03 jas a->ctype = OCELL;
173 63a68686 2008-11-03 jas a->csub = b;
174 63a68686 2008-11-03 jas x = node1(0, (Node *) a);
175 63a68686 2008-11-03 jas x->ntype = NVALUE;
179 63a68686 2008-11-03 jas Node *rectonode(void) /* make $0 into a Node */
181 63a68686 2008-11-03 jas extern Cell *literal0;
182 63a68686 2008-11-03 jas return op1(INDIRECT, celltonode(literal0, CUNK));
185 63a68686 2008-11-03 jas Node *makearr(Node *p)
189 63a68686 2008-11-03 jas if (isvalue(p)) {
190 63a68686 2008-11-03 jas cp = (Cell *) (p->narg[0]);
191 63a68686 2008-11-03 jas if (isfcn(cp))
192 63a68686 2008-11-03 jas SYNTAX( "%s is a function, not an array", cp->nval );
193 63a68686 2008-11-03 jas else if (!isarr(cp)) {
194 63a68686 2008-11-03 jas xfree(cp->sval);
195 63a68686 2008-11-03 jas cp->sval = (char *) makesymtab(NSYMTAB);
196 63a68686 2008-11-03 jas cp->tval = ARR;
202 63a68686 2008-11-03 jas #define PA2NUM 50 /* max number of pat,pat patterns allowed */
203 63a68686 2008-11-03 jas int paircnt; /* number of them in use */
204 63a68686 2008-11-03 jas int pairstack[PA2NUM]; /* state of each pat,pat */
206 63a68686 2008-11-03 jas Node *pa2stat(Node *a, Node *b, Node *c) /* pat, pat {...} */
210 63a68686 2008-11-03 jas x = node4(PASTAT2, a, b, c, itonp(paircnt));
211 63a68686 2008-11-03 jas if (paircnt++ >= PA2NUM)
212 63a68686 2008-11-03 jas SYNTAX( "limited to %d pat,pat statements", PA2NUM );
213 63a68686 2008-11-03 jas x->ntype = NSTAT;
217 63a68686 2008-11-03 jas Node *linkum(Node *a, Node *b)
221 63a68686 2008-11-03 jas if (errorflag) /* don't link things that are wrong */
223 63a68686 2008-11-03 jas if (a == NULL)
225 63a68686 2008-11-03 jas else if (b == NULL)
227 63a68686 2008-11-03 jas for (c = a; c->nnext != NULL; c = c->nnext)
229 63a68686 2008-11-03 jas c->nnext = b;
233 63a68686 2008-11-03 jas void defn(Cell *v, Node *vl, Node *st) /* turn on FCN bit in definition, */
234 63a68686 2008-11-03 jas { /* body of function, arglist */
238 63a68686 2008-11-03 jas if (isarr(v)) {
239 63a68686 2008-11-03 jas SYNTAX( "`%s' is an array name and a function name", v->nval );
242 63a68686 2008-11-03 jas v->tval = FCN;
243 63a68686 2008-11-03 jas v->sval = (char *) st;
244 63a68686 2008-11-03 jas n = 0; /* count arguments */
245 63a68686 2008-11-03 jas for (p = vl; p; p = p->nnext)
247 63a68686 2008-11-03 jas v->fval = n;
248 63a68686 2008-11-03 jas dprintf( ("defining func %s (%d args)\n", v->nval, n) );
251 63a68686 2008-11-03 jas int isarg(char *s) /* is s in argument list for current function? */
252 63a68686 2008-11-03 jas { /* return -1 if not, otherwise arg # */
253 63a68686 2008-11-03 jas extern Node *arglist;
254 63a68686 2008-11-03 jas Node *p = arglist;
257 63a68686 2008-11-03 jas for (n = 0; p != 0; p = p->nnext, n++)
258 63a68686 2008-11-03 jas if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
263 63a68686 2008-11-03 jas int ptoi(void *p) /* convert pointer to integer */
265 63a68686 2008-11-03 jas return (int) (long) p; /* swearing that p fits, of course */
268 63a68686 2008-11-03 jas Node *itonp(int i) /* and vice versa */
270 63a68686 2008-11-03 jas return (Node *) (long) i;