xref: /titanic_53/usr/src/cmd/oawk/lib.c (revision cb4658fbb85e4290093c4fea0eb396a7d98de1fb)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*cb4658fbSceastha /*
23*cb4658fbSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*cb4658fbSceastha  * Use is subject to license terms.
25*cb4658fbSceastha  */
26*cb4658fbSceastha 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include "awk.def"
347c478bd9Sstevel@tonic-gate #include "awk.h"
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <wctype.h>
377c478bd9Sstevel@tonic-gate #include "awktype.h"
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate FILE	*infile	= NULL;
417c478bd9Sstevel@tonic-gate wchar_t *file;
427c478bd9Sstevel@tonic-gate #define	RECSIZE (5 * 512)
437c478bd9Sstevel@tonic-gate wchar_t record[RECSIZE];
447c478bd9Sstevel@tonic-gate wchar_t fields[RECSIZE];
457c478bd9Sstevel@tonic-gate wchar_t L_NULL[] = L"";
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	MAXFLD	100
497c478bd9Sstevel@tonic-gate int	donefld;	/* 1 = implies rec broken into fields */
507c478bd9Sstevel@tonic-gate int	donerec;	/* 1 = record is valid (no flds have changed) */
517c478bd9Sstevel@tonic-gate int	mustfld;	/* 1 = NF seen, so always break */
527c478bd9Sstevel@tonic-gate static wchar_t L_record[] = L"$record";
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	FINIT	{ OCELL, CFLD, 0, L_NULL, 0.0, FLD|STR }
567c478bd9Sstevel@tonic-gate CELL fldtab[MAXFLD] = {		/* room for fields */
577c478bd9Sstevel@tonic-gate 	{ OCELL, CFLD, L_record, record, 0.0, STR|FLD},
587c478bd9Sstevel@tonic-gate 		FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
597c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
607c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
617c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
627c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
637c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
647c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
657c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
667c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
677c478bd9Sstevel@tonic-gate 	FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate int	maxfld	= 0;	/* last used field */
707c478bd9Sstevel@tonic-gate /* pointer to CELL for maximum field assigned to */
717c478bd9Sstevel@tonic-gate CELL	*maxmfld = &fldtab[0];
727c478bd9Sstevel@tonic-gate 
73*cb4658fbSceastha static int isclvar(wchar_t *);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate getrec()
767c478bd9Sstevel@tonic-gate {
77*cb4658fbSceastha 	wchar_t *rr, *er;
78*cb4658fbSceastha 	int c, sep;
79*cb4658fbSceastha 	FILE *inf;
807c478bd9Sstevel@tonic-gate 	extern int svargc;
817c478bd9Sstevel@tonic-gate 	extern wchar_t **svargv;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	dprintf("**RS=%o, **FS=%o\n", **RS, **FS, NULL);
857c478bd9Sstevel@tonic-gate 	donefld = 0;
867c478bd9Sstevel@tonic-gate 	donerec = 1;
877c478bd9Sstevel@tonic-gate 	record[0] = 0;
887c478bd9Sstevel@tonic-gate 	er = record + RECSIZE;
897c478bd9Sstevel@tonic-gate 	while (svargc > 0) {
907c478bd9Sstevel@tonic-gate 		dprintf("svargc=%d, *svargv=%ws\n", svargc, *svargv, NULL);
917c478bd9Sstevel@tonic-gate 		if (infile == NULL) {	/* have to open a new file */
92*cb4658fbSceastha 			/*
93*cb4658fbSceastha 			 * If the argument contains a '=', determine if the
94*cb4658fbSceastha 			 * argument needs to be treated as a variable assignment
95*cb4658fbSceastha 			 * or as the pathname of a file.
96*cb4658fbSceastha 			 */
97*cb4658fbSceastha 			if (isclvar(*svargv)) {
987c478bd9Sstevel@tonic-gate 				/* it's a var=value argument */
997c478bd9Sstevel@tonic-gate 				setclvar(*svargv);
1007c478bd9Sstevel@tonic-gate 				if (svargc > 1) {
1017c478bd9Sstevel@tonic-gate 					svargv++;
1027c478bd9Sstevel@tonic-gate 					svargc--;
1037c478bd9Sstevel@tonic-gate 					continue;
1047c478bd9Sstevel@tonic-gate 				}
1057c478bd9Sstevel@tonic-gate 				*svargv = L"-";
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 			*FILENAME = file = *svargv;
1087c478bd9Sstevel@tonic-gate 			dprintf("opening file %ws\n", file, NULL, NULL);
1097c478bd9Sstevel@tonic-gate 			if (*file == (wchar_t)L'-')
1107c478bd9Sstevel@tonic-gate 				infile = stdin;
1117c478bd9Sstevel@tonic-gate 			else if ((infile = fopen(toeuccode(file), "r")) == NULL)
1127c478bd9Sstevel@tonic-gate 				error(FATAL, "can't open %ws", file);
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 		if ((sep = **RS) == 0)
1157c478bd9Sstevel@tonic-gate 			sep = '\n';
1167c478bd9Sstevel@tonic-gate 		inf = infile;
1177c478bd9Sstevel@tonic-gate 		for (rr = record; /* dummy */; /* dummy */) {
1187c478bd9Sstevel@tonic-gate 			for (; (c = getwc(inf)) != sep && c != EOF && rr < er;
1197c478bd9Sstevel@tonic-gate 			    *rr++ = c)
1207c478bd9Sstevel@tonic-gate 				;
1217c478bd9Sstevel@tonic-gate 			if (rr >= er)
1227c478bd9Sstevel@tonic-gate 				error(FATAL, "record `%.20ws...' too long",
1237c478bd9Sstevel@tonic-gate 				    record);
1247c478bd9Sstevel@tonic-gate 			if (**RS == sep || c == EOF)
1257c478bd9Sstevel@tonic-gate 				break;
1267c478bd9Sstevel@tonic-gate 			if ((c = getwc(inf)) == '\n' || c == EOF)
1277c478bd9Sstevel@tonic-gate 			/* 2 in a row */
1287c478bd9Sstevel@tonic-gate 				break;
1297c478bd9Sstevel@tonic-gate 			*rr++ = '\n';
1307c478bd9Sstevel@tonic-gate 			*rr++ = c;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		if (rr >= er)
1337c478bd9Sstevel@tonic-gate 			error(FATAL, "record `%.20ws...' too long", record);
1347c478bd9Sstevel@tonic-gate 		*rr = 0;
1357c478bd9Sstevel@tonic-gate 		if (mustfld)
1367c478bd9Sstevel@tonic-gate 			fldbld();
1377c478bd9Sstevel@tonic-gate 		if (c != EOF || rr > record) {	/* normal record */
1387c478bd9Sstevel@tonic-gate 			recloc->tval &= ~NUM;
1397c478bd9Sstevel@tonic-gate 			recloc->tval |= STR;
1407c478bd9Sstevel@tonic-gate 			++nrloc->fval;
1417c478bd9Sstevel@tonic-gate 			nrloc->tval &= ~STR;
1427c478bd9Sstevel@tonic-gate 			nrloc->tval |= NUM;
1437c478bd9Sstevel@tonic-gate 			return (1);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		/* EOF arrived on this file; set up next */
1467c478bd9Sstevel@tonic-gate 		if (infile != stdin)
1477c478bd9Sstevel@tonic-gate 			fclose(infile);
1487c478bd9Sstevel@tonic-gate 		infile = NULL;
1497c478bd9Sstevel@tonic-gate 		svargc--;
1507c478bd9Sstevel@tonic-gate 		svargv++;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	return (0);	/* true end of file */
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
155*cb4658fbSceastha /*
156*cb4658fbSceastha  * isclvar()
157*cb4658fbSceastha  *
158*cb4658fbSceastha  * Returns 1 if the input string, arg, is a variable assignment,
159*cb4658fbSceastha  * otherwise returns 0.
160*cb4658fbSceastha  *
161*cb4658fbSceastha  * An argument to awk can be either a pathname of a file, or a variable
162*cb4658fbSceastha  * assignment.  An operand that begins with an undersore or alphabetic
163*cb4658fbSceastha  * character from the portable character set, followed by a sequence of
164*cb4658fbSceastha  * underscores, digits, and alphabetics from the portable character set,
165*cb4658fbSceastha  * followed by the '=' character, shall specify a variable assignment
166*cb4658fbSceastha  * rather than a pathname.
167*cb4658fbSceastha  */
168*cb4658fbSceastha static int
169*cb4658fbSceastha isclvar(wchar_t *arg)
170*cb4658fbSceastha {
171*cb4658fbSceastha 	wchar_t	*tmpptr = arg;
172*cb4658fbSceastha 
173*cb4658fbSceastha 	if (tmpptr != NULL) {
174*cb4658fbSceastha 
175*cb4658fbSceastha 		/* Begins with an underscore or alphabetic character */
176*cb4658fbSceastha 		if (iswalpha(*tmpptr) || *tmpptr == '_') {
177*cb4658fbSceastha 
178*cb4658fbSceastha 			/*
179*cb4658fbSceastha 			 * followed by a sequence of underscores, digits,
180*cb4658fbSceastha 			 * and alphabetics
181*cb4658fbSceastha 			 */
182*cb4658fbSceastha 			for (tmpptr++; *tmpptr; tmpptr++) {
183*cb4658fbSceastha 				if (!(isalnum(*tmpptr) || (*tmpptr == '_'))) {
184*cb4658fbSceastha 					break;
185*cb4658fbSceastha 				}
186*cb4658fbSceastha 			}
187*cb4658fbSceastha 			return (*tmpptr == '=');
188*cb4658fbSceastha 		}
189*cb4658fbSceastha 	}
190*cb4658fbSceastha 
191*cb4658fbSceastha 	return (0);
192*cb4658fbSceastha }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate setclvar(s)	/* set var=value from s */
1957c478bd9Sstevel@tonic-gate wchar_t *s;
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	wchar_t *p;
1987c478bd9Sstevel@tonic-gate 	CELL *q;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	for (p = s; *p != '='; p++)
2027c478bd9Sstevel@tonic-gate 		;
2037c478bd9Sstevel@tonic-gate 	*p++ = 0;
2047c478bd9Sstevel@tonic-gate 	q = setsymtab(s, tostring(p), 0.0, STR, symtab);
2057c478bd9Sstevel@tonic-gate 	setsval(q, p);
2067c478bd9Sstevel@tonic-gate 	dprintf("command line set %ws to |%ws|\n", s, p, NULL);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate fldbld()
2117c478bd9Sstevel@tonic-gate {
212*cb4658fbSceastha 	wchar_t *r, *fr, sep, c;
2137c478bd9Sstevel@tonic-gate 	static wchar_t L_NF[] = L"NF";
2147c478bd9Sstevel@tonic-gate 	CELL *p, *q;
2157c478bd9Sstevel@tonic-gate 	int i, j;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	r = record;
2197c478bd9Sstevel@tonic-gate 	fr = fields;
2207c478bd9Sstevel@tonic-gate 	i = 0;	/* number of fields accumulated here */
2217c478bd9Sstevel@tonic-gate 	if ((sep = **FS) == ' ')
2227c478bd9Sstevel@tonic-gate 		for (i = 0; /* dummy */; /* dummy */) {
2237c478bd9Sstevel@tonic-gate 			c = *r;
2247c478bd9Sstevel@tonic-gate 			while (iswblank(c) || c == '\t' || c == '\n')
2257c478bd9Sstevel@tonic-gate 				c = *(++r);
2267c478bd9Sstevel@tonic-gate 			if (*r == 0)
2277c478bd9Sstevel@tonic-gate 				break;
2287c478bd9Sstevel@tonic-gate 			i++;
2297c478bd9Sstevel@tonic-gate 			if (i >= MAXFLD)
2307c478bd9Sstevel@tonic-gate 				error(FATAL,
2317c478bd9Sstevel@tonic-gate 			"record `%.20ws...' has too many fields", record);
2327c478bd9Sstevel@tonic-gate 			if (!(fldtab[i].tval&FLD))
2337c478bd9Sstevel@tonic-gate 				xfree(fldtab[i].sval);
2347c478bd9Sstevel@tonic-gate 			fldtab[i].sval = fr;
2357c478bd9Sstevel@tonic-gate 			fldtab[i].tval = FLD | STR;
2367c478bd9Sstevel@tonic-gate 			do {
2377c478bd9Sstevel@tonic-gate 				*fr++ = *r++;
2387c478bd9Sstevel@tonic-gate 				c = *r;
2397c478bd9Sstevel@tonic-gate 			} while (! iswblank(c) && c != '\t' &&
2407c478bd9Sstevel@tonic-gate 			    c != '\n' && c != '\0');
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 			*fr++ = 0;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	} else if (*r != 0)	/* if 0, it's a null field */
2467c478bd9Sstevel@tonic-gate 		for (;;) {
2477c478bd9Sstevel@tonic-gate 			i++;
2487c478bd9Sstevel@tonic-gate 			if (i >= MAXFLD)
2497c478bd9Sstevel@tonic-gate 				error(FATAL,
2507c478bd9Sstevel@tonic-gate 			"record `%.20ws...' has too many fields", record);
2517c478bd9Sstevel@tonic-gate 			if (!(fldtab[i].tval&FLD))
2527c478bd9Sstevel@tonic-gate 				xfree(fldtab[i].sval);
2537c478bd9Sstevel@tonic-gate 			fldtab[i].sval = fr;
2547c478bd9Sstevel@tonic-gate 			fldtab[i].tval = FLD | STR;
2557c478bd9Sstevel@tonic-gate 			while ((c = *r) != sep && c != '\n' && c != '\0')
2567c478bd9Sstevel@tonic-gate 				/* \n always a separator */
2577c478bd9Sstevel@tonic-gate 				*fr++ = *r++;
2587c478bd9Sstevel@tonic-gate 			*fr++ = 0;
2597c478bd9Sstevel@tonic-gate 			if (*r++ == 0)
2607c478bd9Sstevel@tonic-gate 				break;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	*fr = 0;
2637c478bd9Sstevel@tonic-gate 	/* clean out junk from previous record */
2647c478bd9Sstevel@tonic-gate 	for (p = maxmfld, q = &fldtab[i]; p > q; p--) {
2657c478bd9Sstevel@tonic-gate 		if (!(p->tval&FLD))
2667c478bd9Sstevel@tonic-gate 			xfree(p->sval);
2677c478bd9Sstevel@tonic-gate 		p->tval = STR | FLD;
2687c478bd9Sstevel@tonic-gate 		p->sval = L_NULL;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	maxfld = i;
2717c478bd9Sstevel@tonic-gate 	maxmfld = &fldtab[i];
2727c478bd9Sstevel@tonic-gate 	donefld = 1;
2737c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxfld; i++)
2747c478bd9Sstevel@tonic-gate 		if (isanumber(fldtab[i].sval)) {
2757c478bd9Sstevel@tonic-gate 			fldtab[i].fval = watof(fldtab[i].sval);
2767c478bd9Sstevel@tonic-gate 			fldtab[i].tval |= NUM;
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 	setfval(lookup(L_NF, symtab, 0), (awkfloat) maxfld);
2797c478bd9Sstevel@tonic-gate 	if (dbg)
2807c478bd9Sstevel@tonic-gate 		for (i = 0; i <= maxfld; i++)
2817c478bd9Sstevel@tonic-gate 			printf("field %d: |%ws|\n", i, fldtab[i].sval);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate recbld()
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	int i;
288*cb4658fbSceastha 	wchar_t *r, *p;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (donefld == 0 || donerec == 1)
2927c478bd9Sstevel@tonic-gate 		return;
2937c478bd9Sstevel@tonic-gate 	r = record;
2947c478bd9Sstevel@tonic-gate 	for (i = 1; i <= *NF; i++) {
2957c478bd9Sstevel@tonic-gate 		p = getsval(&fldtab[i]);
2967c478bd9Sstevel@tonic-gate 		while (*r++ = *p++)
2977c478bd9Sstevel@tonic-gate 			;
2987c478bd9Sstevel@tonic-gate 		*(r-1) = **OFS;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	*(r-1) = '\0';
3017c478bd9Sstevel@tonic-gate 	dprintf("in recbld FS=%o, recloc=%o\n", **FS, recloc, NULL);
3027c478bd9Sstevel@tonic-gate 	recloc->tval = STR | FLD;
3037c478bd9Sstevel@tonic-gate 	dprintf("in recbld FS=%o, recloc=%o\n", **FS, recloc, NULL);
3047c478bd9Sstevel@tonic-gate 	if (r > record+RECSIZE)
3057c478bd9Sstevel@tonic-gate 		error(FATAL, "built giant record `%.20ws...'", record);
3067c478bd9Sstevel@tonic-gate 	dprintf("recbld = |%ws|\n", record, NULL, NULL);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate CELL *
3117c478bd9Sstevel@tonic-gate fieldadr(n)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	if (n < 0 || n >= MAXFLD)
3147c478bd9Sstevel@tonic-gate 		error(FATAL, "trying to access field %d", n);
3157c478bd9Sstevel@tonic-gate 	return (&fldtab[n]);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate int	errorflag	= 0;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate yyerror(char *s)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	fprintf(stderr,
3257c478bd9Sstevel@tonic-gate 	    gettext("awk: %s near line %lld\n"), gettext(s), lineno);
3267c478bd9Sstevel@tonic-gate 	errorflag = 2;
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate error(f, s, a1, a2, a3, a4, a5, a6, a7)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	fprintf(stderr, "awk: ");
3337c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext((char *)s), a1, a2, a3, a4, a5, a6, a7);
3347c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
3357c478bd9Sstevel@tonic-gate 	if (NR && *NR > 0)
3367c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(" record number %g\n"), *NR);
3377c478bd9Sstevel@tonic-gate 	if (f)
3387c478bd9Sstevel@tonic-gate 		exit(2);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate PUTS(s) char *s; {
3437c478bd9Sstevel@tonic-gate 	dprintf("%s\n", s, NULL, NULL);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate #define	MAXEXPON	38	/* maximum exponenet for fp number */
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate isanumber(s)
351*cb4658fbSceastha wchar_t *s;
3527c478bd9Sstevel@tonic-gate {
353*cb4658fbSceastha 	int d1, d2;
3547c478bd9Sstevel@tonic-gate 	int point;
3557c478bd9Sstevel@tonic-gate 	wchar_t *es;
3567c478bd9Sstevel@tonic-gate 	extern wchar_t	radixpoint;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	d1 = d2 = point = 0;
3597c478bd9Sstevel@tonic-gate 	while (*s == ' ' || *s == '\t' || *s == '\n')
3607c478bd9Sstevel@tonic-gate 		s++;
3617c478bd9Sstevel@tonic-gate 	if (*s == '\0')
3627c478bd9Sstevel@tonic-gate 		return (0);	/* empty stuff isn't number */
3637c478bd9Sstevel@tonic-gate 	if (*s == '+' || *s == '-')
3647c478bd9Sstevel@tonic-gate 		s++;
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * Since, iswdigit() will include digit from other than code set 0,
3677c478bd9Sstevel@tonic-gate 	 * we have to check it from code set 0 or not.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	if (!(iswdigit(*s) && iswascii(*s)) && *s != radixpoint)
3707c478bd9Sstevel@tonic-gate 		return (0);
3717c478bd9Sstevel@tonic-gate 	if (iswdigit(*s) && iswascii(*s)) {
3727c478bd9Sstevel@tonic-gate 		do {
3737c478bd9Sstevel@tonic-gate 			d1++;
3747c478bd9Sstevel@tonic-gate 			s++;
3757c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	if (d1 >= MAXEXPON)
3787c478bd9Sstevel@tonic-gate 		return (0);	/* too many digits to convert */
3797c478bd9Sstevel@tonic-gate 	if (*s == radixpoint) {
3807c478bd9Sstevel@tonic-gate 		point++;
3817c478bd9Sstevel@tonic-gate 		s++;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	if (iswdigit(*s) && iswascii(*s)) {
3847c478bd9Sstevel@tonic-gate 		d2++;
3857c478bd9Sstevel@tonic-gate 		do {
3867c478bd9Sstevel@tonic-gate 			s++;
3877c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (!(d1 || point && d2))
3927c478bd9Sstevel@tonic-gate 		return (0);
3937c478bd9Sstevel@tonic-gate 	if (*s == 'e' || *s == 'E') {
3947c478bd9Sstevel@tonic-gate 		s++;
3957c478bd9Sstevel@tonic-gate 		if (*s == '+' || *s == '-')
3967c478bd9Sstevel@tonic-gate 			s++;
3977c478bd9Sstevel@tonic-gate 		if (!(iswdigit(*s) && iswascii(*s)))
3987c478bd9Sstevel@tonic-gate 			return (0);
3997c478bd9Sstevel@tonic-gate 		es = s;
4007c478bd9Sstevel@tonic-gate 		do {
4017c478bd9Sstevel@tonic-gate 			s++;
4027c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		if (s - es > 2)
4067c478bd9Sstevel@tonic-gate 			return (0);
4077c478bd9Sstevel@tonic-gate 		else if (s - es == 2 &&
4087c478bd9Sstevel@tonic-gate 			10 * (*es-'0') + *(es+1)-'0' >= MAXEXPON)
4097c478bd9Sstevel@tonic-gate 			return (0);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	while (*s == ' ' || *s == '\t' || *s == '\n')
4127c478bd9Sstevel@tonic-gate 		s++;
4137c478bd9Sstevel@tonic-gate 	if (*s == '\0')
4147c478bd9Sstevel@tonic-gate 		return (1);
4157c478bd9Sstevel@tonic-gate 	else
4167c478bd9Sstevel@tonic-gate 		return (0);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate char *
4197c478bd9Sstevel@tonic-gate toeuccode(str)
4207c478bd9Sstevel@tonic-gate wchar_t *str;
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	static char euccode[RECSIZE];
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	(void) wcstombs(euccode, str, RECSIZE);
4257c478bd9Sstevel@tonic-gate 	return (euccode);
4267c478bd9Sstevel@tonic-gate }
427