xref: /illumos-gate/usr/src/cmd/oawk/tran.c (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "stdio.h"
34 #include "awk.def"
35 #include "awk.h"
36 
37 CELL *symtab[MAXSYM];	/* symbol table pointers */
38 
39 
40 wchar_t	**FS;	/* initial field sep */
41 wchar_t	**RS;	/* initial record sep */
42 wchar_t	**OFS;	/* output field sep */
43 wchar_t	**ORS;	/* output record sep */
44 wchar_t	**OFMT;	/* output format for numbers */
45 awkfloat *NF;	/* number of fields in current record */
46 awkfloat *NR;	/* number of current record */
47 wchar_t	**FILENAME;	/* current filename argument */
48 
49 
50 CELL	*recloc;	/* location of record */
51 CELL	*nrloc;		/* NR */
52 CELL	*nfloc;		/* NF */
53 
54 
55 syminit()
56 {
57 	static wchar_t L_0[] = L"0";
58 	static wchar_t L_zeronull[] = L"$zero&null";
59 	static wchar_t L_record[] = L"$record";
60 	static wchar_t L_FS[] = L"FS";
61 	static wchar_t L_OFS[] = L"OFS";
62 	static wchar_t L_ORS[] = L"ORS";
63 	static wchar_t L_RS[] = L"RS";
64 	static wchar_t L_OFMT[] = L"OFMT";
65 	static wchar_t L_space[] = L" ";
66 	static wchar_t L_newline[] = L"\n";
67 	static wchar_t L_dot6g[] = L"%.6g";
68 	static wchar_t L_FILENAME[] = L"FILENAME";
69 	static wchar_t L_NF[] = L"NF";
70 	static wchar_t L_NR[] = L"NR";
71 
72 
73 	setsymtab(L_0, tostring(L_0), 0.0, NUM|STR|CON|FLD, symtab);
74 	/* this one is used for if (x)... tests: */
75 	setsymtab(L_zeronull, tostring(L_NULL), 0.0, NUM|STR|CON|FLD, symtab);
76 	recloc = setsymtab(L_record, record, 0.0, STR|FLD, symtab);
77 	dprintf("recloc %o lookup %o\n",
78 		recloc, lookup(L_record, symtab, 0), NULL);
79 	FS = &setsymtab(L_FS, tostring(L_space), 0.0, STR|FLD, symtab)->sval;
80 	RS = &setsymtab(L_RS, tostring(L_newline), 0.0, STR|FLD, symtab)->sval;
81 	OFS = &setsymtab(L_OFS, tostring(L_space), 0.0, STR|FLD, symtab)->sval;
82 	ORS = &setsymtab(L_ORS, tostring(L_newline), 0.0, STR|FLD,
83 		symtab)->sval;
84 	OFMT = &setsymtab(L_OFMT, tostring(L_dot6g), 0.0, STR|FLD,
85 		symtab)->sval;
86 	FILENAME = &setsymtab(L_FILENAME, NULL, 0.0, STR|FLD, symtab)->sval;
87 	nfloc = setsymtab(L_NF, NULL, 0.0, NUM, symtab);
88 	NF = &nfloc->fval;
89 	nrloc = setsymtab(L_NR, NULL, 0.0, NUM, symtab);
90 	NR = &nrloc->fval;
91 }
92 
93 
94 CELL **makesymtab()
95 {
96 	int i;
97 	CELL **cp;
98 
99 
100 	cp = (CELL **) malloc(MAXSYM * sizeof (CELL *));
101 	if (cp == NULL)
102 		error(FATAL, "out of space in makesymtab");
103 	for (i = 0; i < MAXSYM; i++)
104 		cp[i] = 0;
105 	return (cp);
106 }
107 
108 
109 freesymtab(ap)	/* free symbol table */
110 CELL *ap;
111 {
112 	CELL *cp, **tp, *next;
113 	int i;
114 
115 
116 	if (!(ap->tval & ARR))
117 		return;
118 	tp = (CELL **) ap->sval;
119 	for (i = 0; i < MAXSYM; i++) {
120 		for (cp = tp[i]; cp != NULL; cp = next) {
121 			next = cp->nextval;
122 			xfree(cp->nval);
123 			xfree(cp->sval);
124 			free(cp);
125 		}
126 	}
127 	xfree(tp);
128 }
129 
130 
131 CELL *setsymtab(n, s, f, t, tab)
132 wchar_t *n, *s;
133 awkfloat f;
134 unsigned t;
135 CELL **tab;
136 {
137 	register h;
138 	register CELL *p;
139 	CELL *lookup();
140 
141 
142 	if (n != NULL && (p = lookup(n, tab, 0)) != NULL) {
143 		xfree(s);
144 		dprintf("setsymtab found %o: %ws\n", p, p->nval, NULL);
145 		dprintf(" %ws %g %o\n", p->sval, p->fval, p->tval);
146 		return (p);
147 	}
148 	p = (CELL *) malloc(sizeof (CELL));
149 	if (p == NULL)
150 		error(FATAL, "symbol table overflow at %ws", n);
151 	p->nval = tostring(n);
152 	p->sval = s;
153 	p->fval = f;
154 	p->tval = t;
155 	h = hash(n);
156 	p->nextval = tab[h];
157 	tab[h] = p;
158 	dprintf("setsymtab set %o: %ws\n", p, p->nval, NULL);
159 	dprintf(" %ws %g %o\n", p->sval, p->fval, p->tval);
160 	return (p);
161 }
162 
163 
164 hash(s)	/* form hash value for string s */
165 register wchar_t *s;
166 {
167 	register unsigned hashval;
168 
169 
170 	for (hashval = 0; *s != '\0'; /* dummy */)
171 		hashval += *s++;
172 	return (hashval % MAXSYM);
173 }
174 
175 
176 CELL *lookup(s, tab, flag)	/* look for s in tab, flag must match */
177 register wchar_t *s;
178 CELL **tab;
179 {
180 	register CELL *p;
181 
182 
183 	for (p = tab[hash(s)]; p != NULL; p = p->nextval)
184 		if (wscmp(s, p->nval) == 0 &&
185 			(flag == 0 || flag == p->tval))
186 			return (p);	/* found it */
187 	return (NULL);	/* not found */
188 }
189 
190 
191 awkfloat setfval(vp, f)
192 register CELL *vp;
193 awkfloat f;
194 {
195 	dprintf("setfval: %o %g\n", vp, f, NULL);
196 /* imb */
197 	if (vp->tval & ARR)
198 		error(FATAL, "illegal reference to array %s", vp->nval);
199 	if ((vp->tval & (NUM | STR)) == 0)
200 		error(FATAL, "funny variable %o: %ws %ws %g %o", vp, vp->nval,
201 			vp->sval, vp->fval, vp->tval);
202 /* imb */
203 	if (vp == recloc)
204 		error(FATAL, "can't set $0");
205 	vp->tval &= ~STR;	/* mark string invalid */
206 	vp->tval |= NUM;	/* mark number ok */
207 	if ((vp->tval & FLD) && vp->nval == 0) {
208 		/*
209 		 * FLD really means that the string value was not
210 		 * "malloc"ed and should not be freed.  All fields
211 		 * have this property, but not all cells with this
212 		 * property are fields.  However, all cells with
213 		 * this property and with a NULL "nval" are fields.
214 		 * If we are setting the value of a field, indicate
215 		 * that the value of the record has to be recomputed,
216 		 * and if it's a higher field than the last one we
217 		 * assigned to, remember it for when we clear the
218 		 * fields out for the next record.
219 		 */
220 		donerec = 0;
221 		if (vp > maxmfld)
222 			maxmfld = vp;
223 	}
224 	return (vp->fval = f);
225 }
226 
227 
228 wchar_t *setsval(vp, s)
229 register CELL *vp;
230 wchar_t *s;
231 {
232 	dprintf("setsval: %o %ws\n", vp, s, NULL);
233 	if (vp->tval & ARR)
234 		error(FATAL, "illegal reference to array %ws", vp->nval);
235 	if ((vp->tval & (NUM | STR)) == 0)
236 		error(FATAL, "funny variable %o: %ws %ws %g %o", vp, vp->nval,
237 			vp->sval, vp->fval, vp->tval);
238 	if (vp == recloc)
239 		error(FATAL, "can't set $0");
240 	vp->tval &= ~NUM;
241 	vp->tval |= STR;
242 	if ((vp->tval & FLD) && vp->nval == 0) {
243 		/*
244 		 * See comment in "setfval".
245 		 */
246 		donerec = 0;
247 		if (vp > maxmfld)
248 			maxmfld = vp;
249 	}
250 	if (!(vp->tval&FLD))
251 		xfree(vp->sval);
252 	vp->tval &= ~FLD;
253 	return (vp->sval = tostring(s));
254 }
255 
256 
257 awkfloat getfval(vp)
258 register CELL *vp;
259 {
260 
261 
262 	if (vp->sval == record && donerec == 0)
263 		recbld();
264 	dprintf("getfval: %o", vp, NULL, NULL);
265 	if (vp->tval & ARR)
266 		error(FATAL, "illegal reference to array %ws", vp->nval);
267 	if ((vp->tval & (NUM | STR)) == 0)
268 		error(FATAL, "funny variable %o: %ws %ws %g %o", vp, vp->nval,
269 			vp->sval, vp->fval, vp->tval);
270 	if ((vp->tval & NUM) == 0) {
271 		/* the problem is to make non-numeric things */
272 		/* have unlikely numeric variables, so that */
273 		/* $1 == $2 comparisons sort of make sense when */
274 		/* one or the other is numeric */
275 		if (isanumber(vp->sval)) {
276 			vp->fval = watof(vp->sval);
277 			if (!(vp->tval & CON))
278 				/* don't change type of a constant */
279 				vp->tval |= NUM;
280 		}
281 		else
282 			vp->fval = 0.0;	/* not a very good idea */
283 	}
284 	dprintf("  %g\n", vp->fval, NULL, NULL);
285 	return (vp->fval);
286 }
287 
288 
289 wchar_t *getsval(vp)
290 register CELL *vp;
291 {
292 	char s[100];
293 	wchar_t ws[100];
294 
295 
296 	if (vp->sval == record && donerec == 0)
297 		recbld();
298 	dprintf("getsval: %o", vp, NULL, NULL);
299 	if (vp->tval & ARR)
300 		error(FATAL, "illegal reference to array %ws", vp->nval);
301 	if ((vp->tval & (NUM | STR)) == 0)
302 		error(FATAL, "funny variable %o: %ws %ws %g %o", vp, vp->nval,
303 			vp->sval, vp->fval, vp->tval);
304 	if ((vp->tval & STR) == 0) {
305 		if (!(vp->tval&FLD))
306 			xfree(vp->sval);
307 		if ((long long)vp->fval==vp->fval)
308 			sprintf(s, "%.20g", vp->fval);
309 		else
310 			sprintf(s, toeuccode(*OFMT), vp->fval);
311 		mbstowcs(ws, s, sizeof (ws) / sizeof (wchar_t));
312 		vp->sval = tostring(ws);
313 		vp->tval &= ~FLD;
314 		vp->tval |= STR;
315 	}
316 	dprintf("  %ws\n", vp->sval, NULL, NULL);
317 	return (vp->sval);
318 }
319 
320 
321 wchar_t *tostring(s)
322 register wchar_t *s;
323 {
324 	register wchar_t *p;
325 
326 
327 	p = (wchar_t *) malloc((wslen(s)+1)*sizeof (wchar_t));
328 	if (p == NULL)
329 		error(FATAL, "out of space in tostring on %ws", s);
330 	wscpy(p, s);
331 	return (p);
332 }
333 
334 
335 #ifndef yfree
336 yfree(a) char *a;
337 {
338 	printf("%o\n", a);
339 	free(a);
340 }
341 #endif
342