xref: /titanic_53/usr/src/cmd/oawk/lib.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include "awk.def"
35*7c478bd9Sstevel@tonic-gate #include "awk.h"
36*7c478bd9Sstevel@tonic-gate #include <ctype.h>
37*7c478bd9Sstevel@tonic-gate #include <wctype.h>
38*7c478bd9Sstevel@tonic-gate #include "awktype.h"
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate FILE	*infile	= NULL;
42*7c478bd9Sstevel@tonic-gate wchar_t *file;
43*7c478bd9Sstevel@tonic-gate #define	RECSIZE (5 * 512)
44*7c478bd9Sstevel@tonic-gate wchar_t record[RECSIZE];
45*7c478bd9Sstevel@tonic-gate wchar_t fields[RECSIZE];
46*7c478bd9Sstevel@tonic-gate wchar_t L_NULL[] = L"";
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #define	MAXFLD	100
50*7c478bd9Sstevel@tonic-gate int	donefld;	/* 1 = implies rec broken into fields */
51*7c478bd9Sstevel@tonic-gate int	donerec;	/* 1 = record is valid (no flds have changed) */
52*7c478bd9Sstevel@tonic-gate int	mustfld;	/* 1 = NF seen, so always break */
53*7c478bd9Sstevel@tonic-gate static wchar_t L_record[] = L"$record";
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #define	FINIT	{ OCELL, CFLD, 0, L_NULL, 0.0, FLD|STR }
57*7c478bd9Sstevel@tonic-gate CELL fldtab[MAXFLD] = {		/* room for fields */
58*7c478bd9Sstevel@tonic-gate 	{ OCELL, CFLD, L_record, record, 0.0, STR|FLD},
59*7c478bd9Sstevel@tonic-gate 		FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
60*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
61*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
62*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
63*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
64*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
65*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
66*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
67*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
68*7c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT
69*7c478bd9Sstevel@tonic-gate };
70*7c478bd9Sstevel@tonic-gate int	maxfld	= 0;	/* last used field */
71*7c478bd9Sstevel@tonic-gate /* pointer to CELL for maximum field assigned to */
72*7c478bd9Sstevel@tonic-gate CELL	*maxmfld = &fldtab[0];
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate getrec()
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	register wchar_t *rr, *er;
80*7c478bd9Sstevel@tonic-gate 	register c, sep;
81*7c478bd9Sstevel@tonic-gate 	register FILE *inf;
82*7c478bd9Sstevel@tonic-gate 	extern int svargc;
83*7c478bd9Sstevel@tonic-gate 	extern wchar_t **svargv;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	dprintf("**RS=%o, **FS=%o\n", **RS, **FS, NULL);
87*7c478bd9Sstevel@tonic-gate 	donefld = 0;
88*7c478bd9Sstevel@tonic-gate 	donerec = 1;
89*7c478bd9Sstevel@tonic-gate 	record[0] = 0;
90*7c478bd9Sstevel@tonic-gate 	er = record + RECSIZE;
91*7c478bd9Sstevel@tonic-gate 	while (svargc > 0) {
92*7c478bd9Sstevel@tonic-gate 		dprintf("svargc=%d, *svargv=%ws\n", svargc, *svargv, NULL);
93*7c478bd9Sstevel@tonic-gate 		if (infile == NULL) {	/* have to open a new file */
94*7c478bd9Sstevel@tonic-gate 			if (member('=', *svargv)) {
95*7c478bd9Sstevel@tonic-gate 				/* it's a var=value argument */
96*7c478bd9Sstevel@tonic-gate 				setclvar(*svargv);
97*7c478bd9Sstevel@tonic-gate 				if (svargc > 1) {
98*7c478bd9Sstevel@tonic-gate 					svargv++;
99*7c478bd9Sstevel@tonic-gate 					svargc--;
100*7c478bd9Sstevel@tonic-gate 					continue;
101*7c478bd9Sstevel@tonic-gate 				}
102*7c478bd9Sstevel@tonic-gate 				*svargv = L"-";
103*7c478bd9Sstevel@tonic-gate 			}
104*7c478bd9Sstevel@tonic-gate 			*FILENAME = file = *svargv;
105*7c478bd9Sstevel@tonic-gate 			dprintf("opening file %ws\n", file, NULL, NULL);
106*7c478bd9Sstevel@tonic-gate 			if (*file == (wchar_t)L'-')
107*7c478bd9Sstevel@tonic-gate 				infile = stdin;
108*7c478bd9Sstevel@tonic-gate 			else if ((infile = fopen(toeuccode(file), "r")) == NULL)
109*7c478bd9Sstevel@tonic-gate 				error(FATAL, "can't open %ws", file);
110*7c478bd9Sstevel@tonic-gate 		}
111*7c478bd9Sstevel@tonic-gate 		if ((sep = **RS) == 0)
112*7c478bd9Sstevel@tonic-gate 			sep = '\n';
113*7c478bd9Sstevel@tonic-gate 		inf = infile;
114*7c478bd9Sstevel@tonic-gate 		for (rr = record; /* dummy */; /* dummy */) {
115*7c478bd9Sstevel@tonic-gate 			for (; (c = getwc(inf)) != sep && c != EOF && rr < er;
116*7c478bd9Sstevel@tonic-gate 			    *rr++ = c)
117*7c478bd9Sstevel@tonic-gate 				;
118*7c478bd9Sstevel@tonic-gate 			if (rr >= er)
119*7c478bd9Sstevel@tonic-gate 				error(FATAL, "record `%.20ws...' too long",
120*7c478bd9Sstevel@tonic-gate 				    record);
121*7c478bd9Sstevel@tonic-gate 			if (**RS == sep || c == EOF)
122*7c478bd9Sstevel@tonic-gate 				break;
123*7c478bd9Sstevel@tonic-gate 			if ((c = getwc(inf)) == '\n' || c == EOF)
124*7c478bd9Sstevel@tonic-gate 			/* 2 in a row */
125*7c478bd9Sstevel@tonic-gate 				break;
126*7c478bd9Sstevel@tonic-gate 			*rr++ = '\n';
127*7c478bd9Sstevel@tonic-gate 			*rr++ = c;
128*7c478bd9Sstevel@tonic-gate 		}
129*7c478bd9Sstevel@tonic-gate 		if (rr >= er)
130*7c478bd9Sstevel@tonic-gate 			error(FATAL, "record `%.20ws...' too long", record);
131*7c478bd9Sstevel@tonic-gate 		*rr = 0;
132*7c478bd9Sstevel@tonic-gate 		if (mustfld)
133*7c478bd9Sstevel@tonic-gate 			fldbld();
134*7c478bd9Sstevel@tonic-gate 		if (c != EOF || rr > record) {	/* normal record */
135*7c478bd9Sstevel@tonic-gate 			recloc->tval &= ~NUM;
136*7c478bd9Sstevel@tonic-gate 			recloc->tval |= STR;
137*7c478bd9Sstevel@tonic-gate 			++nrloc->fval;
138*7c478bd9Sstevel@tonic-gate 			nrloc->tval &= ~STR;
139*7c478bd9Sstevel@tonic-gate 			nrloc->tval |= NUM;
140*7c478bd9Sstevel@tonic-gate 			return (1);
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 		/* EOF arrived on this file; set up next */
143*7c478bd9Sstevel@tonic-gate 		if (infile != stdin)
144*7c478bd9Sstevel@tonic-gate 			fclose(infile);
145*7c478bd9Sstevel@tonic-gate 		infile = NULL;
146*7c478bd9Sstevel@tonic-gate 		svargc--;
147*7c478bd9Sstevel@tonic-gate 		svargv++;
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	return (0);	/* true end of file */
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate setclvar(s)	/* set var=value from s */
154*7c478bd9Sstevel@tonic-gate wchar_t *s;
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	wchar_t *p;
157*7c478bd9Sstevel@tonic-gate 	CELL *q;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	for (p = s; *p != '='; p++)
161*7c478bd9Sstevel@tonic-gate 		;
162*7c478bd9Sstevel@tonic-gate 	*p++ = 0;
163*7c478bd9Sstevel@tonic-gate 	q = setsymtab(s, tostring(p), 0.0, STR, symtab);
164*7c478bd9Sstevel@tonic-gate 	setsval(q, p);
165*7c478bd9Sstevel@tonic-gate 	dprintf("command line set %ws to |%ws|\n", s, p, NULL);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate fldbld()
170*7c478bd9Sstevel@tonic-gate {
171*7c478bd9Sstevel@tonic-gate 	register wchar_t *r, *fr, sep, c;
172*7c478bd9Sstevel@tonic-gate 	static wchar_t L_NF[] = L"NF";
173*7c478bd9Sstevel@tonic-gate 	CELL *p, *q;
174*7c478bd9Sstevel@tonic-gate 	int i, j;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	r = record;
178*7c478bd9Sstevel@tonic-gate 	fr = fields;
179*7c478bd9Sstevel@tonic-gate 	i = 0;	/* number of fields accumulated here */
180*7c478bd9Sstevel@tonic-gate 	if ((sep = **FS) == ' ')
181*7c478bd9Sstevel@tonic-gate 		for (i = 0; /* dummy */; /* dummy */) {
182*7c478bd9Sstevel@tonic-gate 			c = *r;
183*7c478bd9Sstevel@tonic-gate 			while (iswblank(c) || c == '\t' || c == '\n')
184*7c478bd9Sstevel@tonic-gate 				c = *(++r);
185*7c478bd9Sstevel@tonic-gate 			if (*r == 0)
186*7c478bd9Sstevel@tonic-gate 				break;
187*7c478bd9Sstevel@tonic-gate 			i++;
188*7c478bd9Sstevel@tonic-gate 			if (i >= MAXFLD)
189*7c478bd9Sstevel@tonic-gate 				error(FATAL,
190*7c478bd9Sstevel@tonic-gate 			"record `%.20ws...' has too many fields", record);
191*7c478bd9Sstevel@tonic-gate 			if (!(fldtab[i].tval&FLD))
192*7c478bd9Sstevel@tonic-gate 				xfree(fldtab[i].sval);
193*7c478bd9Sstevel@tonic-gate 			fldtab[i].sval = fr;
194*7c478bd9Sstevel@tonic-gate 			fldtab[i].tval = FLD | STR;
195*7c478bd9Sstevel@tonic-gate 			do {
196*7c478bd9Sstevel@tonic-gate 				*fr++ = *r++;
197*7c478bd9Sstevel@tonic-gate 				c = *r;
198*7c478bd9Sstevel@tonic-gate 			} while (! iswblank(c) && c != '\t' &&
199*7c478bd9Sstevel@tonic-gate 				c != '\n' && c != '\0');
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 			*fr++ = 0;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	} else if (*r != 0)	/* if 0, it's a null field */
205*7c478bd9Sstevel@tonic-gate 		for (;;) {
206*7c478bd9Sstevel@tonic-gate 			i++;
207*7c478bd9Sstevel@tonic-gate 			if (i >= MAXFLD)
208*7c478bd9Sstevel@tonic-gate 				error(FATAL,
209*7c478bd9Sstevel@tonic-gate 			"record `%.20ws...' has too many fields", record);
210*7c478bd9Sstevel@tonic-gate 			if (!(fldtab[i].tval&FLD))
211*7c478bd9Sstevel@tonic-gate 				xfree(fldtab[i].sval);
212*7c478bd9Sstevel@tonic-gate 			fldtab[i].sval = fr;
213*7c478bd9Sstevel@tonic-gate 			fldtab[i].tval = FLD | STR;
214*7c478bd9Sstevel@tonic-gate 			while ((c = *r) != sep && c != '\n' && c != '\0')
215*7c478bd9Sstevel@tonic-gate 				/* \n always a separator */
216*7c478bd9Sstevel@tonic-gate 				*fr++ = *r++;
217*7c478bd9Sstevel@tonic-gate 			*fr++ = 0;
218*7c478bd9Sstevel@tonic-gate 			if (*r++ == 0)
219*7c478bd9Sstevel@tonic-gate 				break;
220*7c478bd9Sstevel@tonic-gate 		}
221*7c478bd9Sstevel@tonic-gate 	*fr = 0;
222*7c478bd9Sstevel@tonic-gate 	/* clean out junk from previous record */
223*7c478bd9Sstevel@tonic-gate 	for (p = maxmfld, q = &fldtab[i]; p > q; p--) {
224*7c478bd9Sstevel@tonic-gate 		if (!(p->tval&FLD))
225*7c478bd9Sstevel@tonic-gate 			xfree(p->sval);
226*7c478bd9Sstevel@tonic-gate 		p->tval = STR | FLD;
227*7c478bd9Sstevel@tonic-gate 		p->sval = L_NULL;
228*7c478bd9Sstevel@tonic-gate 	}
229*7c478bd9Sstevel@tonic-gate 	maxfld = i;
230*7c478bd9Sstevel@tonic-gate 	maxmfld = &fldtab[i];
231*7c478bd9Sstevel@tonic-gate 	donefld = 1;
232*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxfld; i++)
233*7c478bd9Sstevel@tonic-gate 		if (isanumber(fldtab[i].sval)) {
234*7c478bd9Sstevel@tonic-gate 			fldtab[i].fval = watof(fldtab[i].sval);
235*7c478bd9Sstevel@tonic-gate 			fldtab[i].tval |= NUM;
236*7c478bd9Sstevel@tonic-gate 		}
237*7c478bd9Sstevel@tonic-gate 	setfval(lookup(L_NF, symtab, 0), (awkfloat) maxfld);
238*7c478bd9Sstevel@tonic-gate 	if (dbg)
239*7c478bd9Sstevel@tonic-gate 		for (i = 0; i <= maxfld; i++)
240*7c478bd9Sstevel@tonic-gate 			printf("field %d: |%ws|\n", i, fldtab[i].sval);
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate recbld()
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate 	int i;
247*7c478bd9Sstevel@tonic-gate 	register wchar_t *r, *p;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (donefld == 0 || donerec == 1)
251*7c478bd9Sstevel@tonic-gate 		return;
252*7c478bd9Sstevel@tonic-gate 	r = record;
253*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= *NF; i++) {
254*7c478bd9Sstevel@tonic-gate 		p = getsval(&fldtab[i]);
255*7c478bd9Sstevel@tonic-gate 		while (*r++ = *p++)
256*7c478bd9Sstevel@tonic-gate 			;
257*7c478bd9Sstevel@tonic-gate 		*(r-1) = **OFS;
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 	*(r-1) = '\0';
260*7c478bd9Sstevel@tonic-gate 	dprintf("in recbld FS=%o, recloc=%o\n", **FS, recloc, NULL);
261*7c478bd9Sstevel@tonic-gate 	recloc->tval = STR | FLD;
262*7c478bd9Sstevel@tonic-gate 	dprintf("in recbld FS=%o, recloc=%o\n", **FS, recloc, NULL);
263*7c478bd9Sstevel@tonic-gate 	if (r > record+RECSIZE)
264*7c478bd9Sstevel@tonic-gate 		error(FATAL, "built giant record `%.20ws...'", record);
265*7c478bd9Sstevel@tonic-gate 	dprintf("recbld = |%ws|\n", record, NULL, NULL);
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate CELL *
270*7c478bd9Sstevel@tonic-gate fieldadr(n)
271*7c478bd9Sstevel@tonic-gate {
272*7c478bd9Sstevel@tonic-gate 	if (n < 0 || n >= MAXFLD)
273*7c478bd9Sstevel@tonic-gate 		error(FATAL, "trying to access field %d", n);
274*7c478bd9Sstevel@tonic-gate 	return (&fldtab[n]);
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate int	errorflag	= 0;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate yyerror(char *s)
282*7c478bd9Sstevel@tonic-gate {
283*7c478bd9Sstevel@tonic-gate 	fprintf(stderr,
284*7c478bd9Sstevel@tonic-gate 		gettext("awk: %s near line %lld\n"), gettext(s), lineno);
285*7c478bd9Sstevel@tonic-gate 	errorflag = 2;
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate error(f, s, a1, a2, a3, a4, a5, a6, a7)
290*7c478bd9Sstevel@tonic-gate {
291*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "awk: ");
292*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext((char *)s), a1, a2, a3, a4, a5, a6, a7);
293*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
294*7c478bd9Sstevel@tonic-gate 	if (NR && *NR > 0)
295*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(" record number %g\n"), *NR);
296*7c478bd9Sstevel@tonic-gate 	if (f)
297*7c478bd9Sstevel@tonic-gate 		exit(2);
298*7c478bd9Sstevel@tonic-gate }
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate PUTS(s) char *s; {
302*7c478bd9Sstevel@tonic-gate 	dprintf("%s\n", s, NULL, NULL);
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate #define	MAXEXPON	38	/* maximum exponenet for fp number */
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate isanumber(s)
310*7c478bd9Sstevel@tonic-gate register wchar_t *s;
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	register d1, d2;
313*7c478bd9Sstevel@tonic-gate 	int point;
314*7c478bd9Sstevel@tonic-gate 	wchar_t *es;
315*7c478bd9Sstevel@tonic-gate 	extern wchar_t	radixpoint;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	d1 = d2 = point = 0;
318*7c478bd9Sstevel@tonic-gate 	while (*s == ' ' || *s == '\t' || *s == '\n')
319*7c478bd9Sstevel@tonic-gate 		s++;
320*7c478bd9Sstevel@tonic-gate 	if (*s == '\0')
321*7c478bd9Sstevel@tonic-gate 		return (0);	/* empty stuff isn't number */
322*7c478bd9Sstevel@tonic-gate 	if (*s == '+' || *s == '-')
323*7c478bd9Sstevel@tonic-gate 		s++;
324*7c478bd9Sstevel@tonic-gate 	/*
325*7c478bd9Sstevel@tonic-gate 	 * Since, iswdigit() will include digit from other than code set 0,
326*7c478bd9Sstevel@tonic-gate 	 * we have to check it from code set 0 or not.
327*7c478bd9Sstevel@tonic-gate 	 */
328*7c478bd9Sstevel@tonic-gate 	if (!(iswdigit(*s) && iswascii(*s)) && *s != radixpoint)
329*7c478bd9Sstevel@tonic-gate 		return (0);
330*7c478bd9Sstevel@tonic-gate 	if (iswdigit(*s) && iswascii(*s)) {
331*7c478bd9Sstevel@tonic-gate 		do {
332*7c478bd9Sstevel@tonic-gate 			d1++;
333*7c478bd9Sstevel@tonic-gate 			s++;
334*7c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 	if (d1 >= MAXEXPON)
337*7c478bd9Sstevel@tonic-gate 		return (0);	/* too many digits to convert */
338*7c478bd9Sstevel@tonic-gate 	if (*s == radixpoint) {
339*7c478bd9Sstevel@tonic-gate 		point++;
340*7c478bd9Sstevel@tonic-gate 		s++;
341*7c478bd9Sstevel@tonic-gate 	}
342*7c478bd9Sstevel@tonic-gate 	if (iswdigit(*s) && iswascii(*s)) {
343*7c478bd9Sstevel@tonic-gate 		d2++;
344*7c478bd9Sstevel@tonic-gate 		do {
345*7c478bd9Sstevel@tonic-gate 			s++;
346*7c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
347*7c478bd9Sstevel@tonic-gate 	}
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	if (!(d1 || point && d2))
351*7c478bd9Sstevel@tonic-gate 		return (0);
352*7c478bd9Sstevel@tonic-gate 	if (*s == 'e' || *s == 'E') {
353*7c478bd9Sstevel@tonic-gate 		s++;
354*7c478bd9Sstevel@tonic-gate 		if (*s == '+' || *s == '-')
355*7c478bd9Sstevel@tonic-gate 			s++;
356*7c478bd9Sstevel@tonic-gate 		if (!(iswdigit(*s) && iswascii(*s)))
357*7c478bd9Sstevel@tonic-gate 			return (0);
358*7c478bd9Sstevel@tonic-gate 		es = s;
359*7c478bd9Sstevel@tonic-gate 		do {
360*7c478bd9Sstevel@tonic-gate 			s++;
361*7c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 		if (s - es > 2)
365*7c478bd9Sstevel@tonic-gate 			return (0);
366*7c478bd9Sstevel@tonic-gate 		else if (s - es == 2 &&
367*7c478bd9Sstevel@tonic-gate 			10 * (*es-'0') + *(es+1)-'0' >= MAXEXPON)
368*7c478bd9Sstevel@tonic-gate 			return (0);
369*7c478bd9Sstevel@tonic-gate 	}
370*7c478bd9Sstevel@tonic-gate 	while (*s == ' ' || *s == '\t' || *s == '\n')
371*7c478bd9Sstevel@tonic-gate 		s++;
372*7c478bd9Sstevel@tonic-gate 	if (*s == '\0')
373*7c478bd9Sstevel@tonic-gate 		return (1);
374*7c478bd9Sstevel@tonic-gate 	else
375*7c478bd9Sstevel@tonic-gate 		return (0);
376*7c478bd9Sstevel@tonic-gate }
377*7c478bd9Sstevel@tonic-gate char *
378*7c478bd9Sstevel@tonic-gate toeuccode(str)
379*7c478bd9Sstevel@tonic-gate wchar_t *str;
380*7c478bd9Sstevel@tonic-gate {
381*7c478bd9Sstevel@tonic-gate 	static char euccode[RECSIZE];
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	(void) wcstombs(euccode, str, RECSIZE);
384*7c478bd9Sstevel@tonic-gate 	return (euccode);
385*7c478bd9Sstevel@tonic-gate }
386