xref: /illumos-gate/usr/src/cmd/oawk/lib.c (revision 4774dff6)
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  */
227c478bd9Sstevel@tonic-gate /*
23baaf2753Sceastha  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24cb4658fbSceastha  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27cb4658fbSceastha /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28cb4658fbSceastha /*	  All Rights Reserved  	*/
29cb4658fbSceastha 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include "awk.def"
327c478bd9Sstevel@tonic-gate #include "awk.h"
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <wctype.h>
357c478bd9Sstevel@tonic-gate #include "awktype.h"
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*4774dff6SToomas Soome #include <stdarg.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate FILE	*infile	= NULL;
407c478bd9Sstevel@tonic-gate wchar_t *file;
417c478bd9Sstevel@tonic-gate #define	RECSIZE (5 * 512)
427c478bd9Sstevel@tonic-gate wchar_t record[RECSIZE];
437c478bd9Sstevel@tonic-gate wchar_t fields[RECSIZE];
447c478bd9Sstevel@tonic-gate wchar_t L_NULL[] = L"";
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	MAXFLD	100
487c478bd9Sstevel@tonic-gate int	donefld;	/* 1 = implies rec broken into fields */
497c478bd9Sstevel@tonic-gate int	donerec;	/* 1 = record is valid (no flds have changed) */
507c478bd9Sstevel@tonic-gate int	mustfld;	/* 1 = NF seen, so always break */
517c478bd9Sstevel@tonic-gate static wchar_t L_record[] = L"$record";
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	FINIT	{ OCELL, CFLD, 0, L_NULL, 0.0, FLD|STR }
557c478bd9Sstevel@tonic-gate CELL fldtab[MAXFLD] = {		/* room for fields */
567c478bd9Sstevel@tonic-gate 	{ OCELL, CFLD, L_record, record, 0.0, STR|FLD},
577c478bd9Sstevel@tonic-gate 		FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
587c478bd9Sstevel@tonic-gate 	FINIT, 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 };
687c478bd9Sstevel@tonic-gate int	maxfld	= 0;	/* last used field */
697c478bd9Sstevel@tonic-gate /* pointer to CELL for maximum field assigned to */
707c478bd9Sstevel@tonic-gate CELL	*maxmfld = &fldtab[0];
717c478bd9Sstevel@tonic-gate 
72cb4658fbSceastha static int isclvar(wchar_t *);
73dc5a8425Srobbin static void setclvar(wchar_t *);
74dc5a8425Srobbin void fldbld(void);
757c478bd9Sstevel@tonic-gate 
76dc5a8425Srobbin int
getrec(void)77dc5a8425Srobbin getrec(void)
787c478bd9Sstevel@tonic-gate {
79cb4658fbSceastha 	wchar_t *rr, *er;
80cb4658fbSceastha 	int c, sep;
81cb4658fbSceastha 	FILE *inf;
827c478bd9Sstevel@tonic-gate 	extern int svargc;
837c478bd9Sstevel@tonic-gate 	extern wchar_t **svargv;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	dprintf("**RS=%o, **FS=%o\n", **RS, **FS, NULL);
877c478bd9Sstevel@tonic-gate 	donefld = 0;
887c478bd9Sstevel@tonic-gate 	donerec = 1;
897c478bd9Sstevel@tonic-gate 	record[0] = 0;
907c478bd9Sstevel@tonic-gate 	er = record + RECSIZE;
917c478bd9Sstevel@tonic-gate 	while (svargc > 0) {
927c478bd9Sstevel@tonic-gate 		dprintf("svargc=%d, *svargv=%ws\n", svargc, *svargv, NULL);
937c478bd9Sstevel@tonic-gate 		if (infile == NULL) {	/* have to open a new file */
94cb4658fbSceastha 			/*
95cb4658fbSceastha 			 * If the argument contains a '=', determine if the
96cb4658fbSceastha 			 * argument needs to be treated as a variable assignment
97cb4658fbSceastha 			 * or as the pathname of a file.
98cb4658fbSceastha 			 */
99cb4658fbSceastha 			if (isclvar(*svargv)) {
1007c478bd9Sstevel@tonic-gate 				/* it's a var=value argument */
1017c478bd9Sstevel@tonic-gate 				setclvar(*svargv);
1027c478bd9Sstevel@tonic-gate 				if (svargc > 1) {
1037c478bd9Sstevel@tonic-gate 					svargv++;
1047c478bd9Sstevel@tonic-gate 					svargc--;
1057c478bd9Sstevel@tonic-gate 					continue;
1067c478bd9Sstevel@tonic-gate 				}
1077c478bd9Sstevel@tonic-gate 				*svargv = L"-";
1087c478bd9Sstevel@tonic-gate 			}
1097c478bd9Sstevel@tonic-gate 			*FILENAME = file = *svargv;
1107c478bd9Sstevel@tonic-gate 			dprintf("opening file %ws\n", file, NULL, NULL);
1117c478bd9Sstevel@tonic-gate 			if (*file == (wchar_t)L'-')
1127c478bd9Sstevel@tonic-gate 				infile = stdin;
1137c478bd9Sstevel@tonic-gate 			else if ((infile = fopen(toeuccode(file), "r")) == NULL)
1147c478bd9Sstevel@tonic-gate 				error(FATAL, "can't open %ws", file);
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 		if ((sep = **RS) == 0)
1177c478bd9Sstevel@tonic-gate 			sep = '\n';
1187c478bd9Sstevel@tonic-gate 		inf = infile;
1197c478bd9Sstevel@tonic-gate 		for (rr = record; /* dummy */; /* dummy */) {
1207c478bd9Sstevel@tonic-gate 			for (; (c = getwc(inf)) != sep && c != EOF && rr < er;
1217c478bd9Sstevel@tonic-gate 			    *rr++ = c)
1227c478bd9Sstevel@tonic-gate 				;
1237c478bd9Sstevel@tonic-gate 			if (rr >= er)
1247c478bd9Sstevel@tonic-gate 				error(FATAL, "record `%.20ws...' too long",
1257c478bd9Sstevel@tonic-gate 				    record);
1267c478bd9Sstevel@tonic-gate 			if (**RS == sep || c == EOF)
1277c478bd9Sstevel@tonic-gate 				break;
1287c478bd9Sstevel@tonic-gate 			if ((c = getwc(inf)) == '\n' || c == EOF)
1297c478bd9Sstevel@tonic-gate 			/* 2 in a row */
1307c478bd9Sstevel@tonic-gate 				break;
1317c478bd9Sstevel@tonic-gate 			*rr++ = '\n';
1327c478bd9Sstevel@tonic-gate 			*rr++ = c;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		if (rr >= er)
1357c478bd9Sstevel@tonic-gate 			error(FATAL, "record `%.20ws...' too long", record);
1367c478bd9Sstevel@tonic-gate 		*rr = 0;
1377c478bd9Sstevel@tonic-gate 		if (mustfld)
1387c478bd9Sstevel@tonic-gate 			fldbld();
1397c478bd9Sstevel@tonic-gate 		if (c != EOF || rr > record) {	/* normal record */
1407c478bd9Sstevel@tonic-gate 			recloc->tval &= ~NUM;
1417c478bd9Sstevel@tonic-gate 			recloc->tval |= STR;
1427c478bd9Sstevel@tonic-gate 			++nrloc->fval;
1437c478bd9Sstevel@tonic-gate 			nrloc->tval &= ~STR;
1447c478bd9Sstevel@tonic-gate 			nrloc->tval |= NUM;
1457c478bd9Sstevel@tonic-gate 			return (1);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		/* EOF arrived on this file; set up next */
1487c478bd9Sstevel@tonic-gate 		if (infile != stdin)
1497c478bd9Sstevel@tonic-gate 			fclose(infile);
1507c478bd9Sstevel@tonic-gate 		infile = NULL;
1517c478bd9Sstevel@tonic-gate 		svargc--;
1527c478bd9Sstevel@tonic-gate 		svargv++;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	return (0);	/* true end of file */
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
157cb4658fbSceastha /*
158cb4658fbSceastha  * isclvar()
159cb4658fbSceastha  *
160cb4658fbSceastha  * Returns 1 if the input string, arg, is a variable assignment,
161cb4658fbSceastha  * otherwise returns 0.
162cb4658fbSceastha  *
163cb4658fbSceastha  * An argument to awk can be either a pathname of a file, or a variable
164cb4658fbSceastha  * assignment.  An operand that begins with an undersore or alphabetic
165cb4658fbSceastha  * character from the portable character set, followed by a sequence of
166cb4658fbSceastha  * underscores, digits, and alphabetics from the portable character set,
167cb4658fbSceastha  * followed by the '=' character, shall specify a variable assignment
168cb4658fbSceastha  * rather than a pathname.
169cb4658fbSceastha  */
170cb4658fbSceastha static int
isclvar(wchar_t * arg)171cb4658fbSceastha isclvar(wchar_t *arg)
172cb4658fbSceastha {
173cb4658fbSceastha 	wchar_t	*tmpptr = arg;
174cb4658fbSceastha 
175cb4658fbSceastha 	if (tmpptr != NULL) {
176cb4658fbSceastha 
177cb4658fbSceastha 		/* Begins with an underscore or alphabetic character */
178cb4658fbSceastha 		if (iswalpha(*tmpptr) || *tmpptr == '_') {
179cb4658fbSceastha 
180cb4658fbSceastha 			/*
181cb4658fbSceastha 			 * followed by a sequence of underscores, digits,
182cb4658fbSceastha 			 * and alphabetics
183cb4658fbSceastha 			 */
184cb4658fbSceastha 			for (tmpptr++; *tmpptr; tmpptr++) {
185baaf2753Sceastha 				if (!(iswalnum(*tmpptr) || (*tmpptr == '_'))) {
186cb4658fbSceastha 					break;
187cb4658fbSceastha 				}
188cb4658fbSceastha 			}
189cb4658fbSceastha 			return (*tmpptr == '=');
190cb4658fbSceastha 		}
191cb4658fbSceastha 	}
192cb4658fbSceastha 
193cb4658fbSceastha 	return (0);
194cb4658fbSceastha }
1957c478bd9Sstevel@tonic-gate 
196dc5a8425Srobbin static void
setclvar(wchar_t * s)197dc5a8425Srobbin setclvar(wchar_t *s)	/* set var=value from s */
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	wchar_t *p;
2007c478bd9Sstevel@tonic-gate 	CELL *q;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	for (p = s; *p != '='; p++)
2047c478bd9Sstevel@tonic-gate 		;
2057c478bd9Sstevel@tonic-gate 	*p++ = 0;
2067c478bd9Sstevel@tonic-gate 	q = setsymtab(s, tostring(p), 0.0, STR, symtab);
2077c478bd9Sstevel@tonic-gate 	setsval(q, p);
2087c478bd9Sstevel@tonic-gate 	dprintf("command line set %ws to |%ws|\n", s, p, NULL);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
212dc5a8425Srobbin void
fldbld(void)213dc5a8425Srobbin fldbld(void)
2147c478bd9Sstevel@tonic-gate {
215cb4658fbSceastha 	wchar_t *r, *fr, sep, c;
2167c478bd9Sstevel@tonic-gate 	static wchar_t L_NF[] = L"NF";
2177c478bd9Sstevel@tonic-gate 	CELL *p, *q;
2187c478bd9Sstevel@tonic-gate 	int i, j;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	r = record;
2227c478bd9Sstevel@tonic-gate 	fr = fields;
2237c478bd9Sstevel@tonic-gate 	i = 0;	/* number of fields accumulated here */
2247c478bd9Sstevel@tonic-gate 	if ((sep = **FS) == ' ')
2257c478bd9Sstevel@tonic-gate 		for (i = 0; /* dummy */; /* dummy */) {
2267c478bd9Sstevel@tonic-gate 			c = *r;
2277c478bd9Sstevel@tonic-gate 			while (iswblank(c) || c == '\t' || c == '\n')
2287c478bd9Sstevel@tonic-gate 				c = *(++r);
2297c478bd9Sstevel@tonic-gate 			if (*r == 0)
2307c478bd9Sstevel@tonic-gate 				break;
2317c478bd9Sstevel@tonic-gate 			i++;
2327c478bd9Sstevel@tonic-gate 			if (i >= MAXFLD)
2337c478bd9Sstevel@tonic-gate 				error(FATAL,
2347c478bd9Sstevel@tonic-gate 			"record `%.20ws...' has too many fields", record);
2357c478bd9Sstevel@tonic-gate 			if (!(fldtab[i].tval&FLD))
2367c478bd9Sstevel@tonic-gate 				xfree(fldtab[i].sval);
2377c478bd9Sstevel@tonic-gate 			fldtab[i].sval = fr;
2387c478bd9Sstevel@tonic-gate 			fldtab[i].tval = FLD | STR;
2397c478bd9Sstevel@tonic-gate 			do {
2407c478bd9Sstevel@tonic-gate 				*fr++ = *r++;
2417c478bd9Sstevel@tonic-gate 				c = *r;
2427c478bd9Sstevel@tonic-gate 			} while (! iswblank(c) && c != '\t' &&
243cb4658fbSceastha 			    c != '\n' && c != '\0');
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 			*fr++ = 0;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	} else if (*r != 0)	/* if 0, it's a null field */
2497c478bd9Sstevel@tonic-gate 		for (;;) {
2507c478bd9Sstevel@tonic-gate 			i++;
2517c478bd9Sstevel@tonic-gate 			if (i >= MAXFLD)
2527c478bd9Sstevel@tonic-gate 				error(FATAL,
2537c478bd9Sstevel@tonic-gate 			"record `%.20ws...' has too many fields", record);
2547c478bd9Sstevel@tonic-gate 			if (!(fldtab[i].tval&FLD))
2557c478bd9Sstevel@tonic-gate 				xfree(fldtab[i].sval);
2567c478bd9Sstevel@tonic-gate 			fldtab[i].sval = fr;
2577c478bd9Sstevel@tonic-gate 			fldtab[i].tval = FLD | STR;
2587c478bd9Sstevel@tonic-gate 			while ((c = *r) != sep && c != '\n' && c != '\0')
2597c478bd9Sstevel@tonic-gate 				/* \n always a separator */
2607c478bd9Sstevel@tonic-gate 				*fr++ = *r++;
2617c478bd9Sstevel@tonic-gate 			*fr++ = 0;
2627c478bd9Sstevel@tonic-gate 			if (*r++ == 0)
2637c478bd9Sstevel@tonic-gate 				break;
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 	*fr = 0;
2667c478bd9Sstevel@tonic-gate 	/* clean out junk from previous record */
2677c478bd9Sstevel@tonic-gate 	for (p = maxmfld, q = &fldtab[i]; p > q; p--) {
2687c478bd9Sstevel@tonic-gate 		if (!(p->tval&FLD))
2697c478bd9Sstevel@tonic-gate 			xfree(p->sval);
2707c478bd9Sstevel@tonic-gate 		p->tval = STR | FLD;
2717c478bd9Sstevel@tonic-gate 		p->sval = L_NULL;
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	maxfld = i;
2747c478bd9Sstevel@tonic-gate 	maxmfld = &fldtab[i];
2757c478bd9Sstevel@tonic-gate 	donefld = 1;
2767c478bd9Sstevel@tonic-gate 	for (i = 1; i <= maxfld; i++)
2777c478bd9Sstevel@tonic-gate 		if (isanumber(fldtab[i].sval)) {
2787c478bd9Sstevel@tonic-gate 			fldtab[i].fval = watof(fldtab[i].sval);
2797c478bd9Sstevel@tonic-gate 			fldtab[i].tval |= NUM;
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 	setfval(lookup(L_NF, symtab, 0), (awkfloat) maxfld);
2827c478bd9Sstevel@tonic-gate 	if (dbg)
2837c478bd9Sstevel@tonic-gate 		for (i = 0; i <= maxfld; i++)
2847c478bd9Sstevel@tonic-gate 			printf("field %d: |%ws|\n", i, fldtab[i].sval);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 
288dc5a8425Srobbin void
recbld(void)289dc5a8425Srobbin recbld(void)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	int i;
292cb4658fbSceastha 	wchar_t *r, *p;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (donefld == 0 || donerec == 1)
2967c478bd9Sstevel@tonic-gate 		return;
2977c478bd9Sstevel@tonic-gate 	r = record;
2987c478bd9Sstevel@tonic-gate 	for (i = 1; i <= *NF; i++) {
2997c478bd9Sstevel@tonic-gate 		p = getsval(&fldtab[i]);
3007c478bd9Sstevel@tonic-gate 		while (*r++ = *p++)
3017c478bd9Sstevel@tonic-gate 			;
3027c478bd9Sstevel@tonic-gate 		*(r-1) = **OFS;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 	*(r-1) = '\0';
3057c478bd9Sstevel@tonic-gate 	dprintf("in recbld FS=%o, recloc=%o\n", **FS, recloc, NULL);
3067c478bd9Sstevel@tonic-gate 	recloc->tval = STR | FLD;
3077c478bd9Sstevel@tonic-gate 	dprintf("in recbld FS=%o, recloc=%o\n", **FS, recloc, NULL);
3087c478bd9Sstevel@tonic-gate 	if (r > record+RECSIZE)
3097c478bd9Sstevel@tonic-gate 		error(FATAL, "built giant record `%.20ws...'", record);
3107c478bd9Sstevel@tonic-gate 	dprintf("recbld = |%ws|\n", record, NULL, NULL);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate CELL *
fieldadr(int n)315*4774dff6SToomas Soome fieldadr(int n)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	if (n < 0 || n >= MAXFLD)
3187c478bd9Sstevel@tonic-gate 		error(FATAL, "trying to access field %d", n);
3197c478bd9Sstevel@tonic-gate 	return (&fldtab[n]);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate int	errorflag	= 0;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 
326dc5a8425Srobbin int
yyerror(char * s)3277c478bd9Sstevel@tonic-gate yyerror(char *s)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	fprintf(stderr,
330cb4658fbSceastha 	    gettext("awk: %s near line %lld\n"), gettext(s), lineno);
3317c478bd9Sstevel@tonic-gate 	errorflag = 2;
332dc5a8425Srobbin 	return (0);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 
336dc5a8425Srobbin void
error(int f,char * fmt,...)337*4774dff6SToomas Soome error(int f, char *fmt, ...)
3387c478bd9Sstevel@tonic-gate {
339*4774dff6SToomas Soome 	va_list ap;
340*4774dff6SToomas Soome 
341*4774dff6SToomas Soome 	va_start(ap, fmt);
3427c478bd9Sstevel@tonic-gate 	fprintf(stderr, "awk: ");
343*4774dff6SToomas Soome 	vfprintf(stderr, gettext(fmt), ap);
344*4774dff6SToomas Soome 	va_end(ap);
3457c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
3467c478bd9Sstevel@tonic-gate 	if (NR && *NR > 0)
3477c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(" record number %g\n"), *NR);
3487c478bd9Sstevel@tonic-gate 	if (f)
3497c478bd9Sstevel@tonic-gate 		exit(2);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 
353dc5a8425Srobbin void
PUTS(char * s)354dc5a8425Srobbin PUTS(char *s)
355dc5a8425Srobbin {
3567c478bd9Sstevel@tonic-gate 	dprintf("%s\n", s, NULL, NULL);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate #define	MAXEXPON	38	/* maximum exponenet for fp number */
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 
363dc5a8425Srobbin int
isanumber(wchar_t * s)364dc5a8425Srobbin isanumber(wchar_t *s)
3657c478bd9Sstevel@tonic-gate {
366cb4658fbSceastha 	int d1, d2;
3677c478bd9Sstevel@tonic-gate 	int point;
3687c478bd9Sstevel@tonic-gate 	wchar_t *es;
3697c478bd9Sstevel@tonic-gate 	extern wchar_t	radixpoint;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	d1 = d2 = point = 0;
3727c478bd9Sstevel@tonic-gate 	while (*s == ' ' || *s == '\t' || *s == '\n')
3737c478bd9Sstevel@tonic-gate 		s++;
3747c478bd9Sstevel@tonic-gate 	if (*s == '\0')
3757c478bd9Sstevel@tonic-gate 		return (0);	/* empty stuff isn't number */
3767c478bd9Sstevel@tonic-gate 	if (*s == '+' || *s == '-')
3777c478bd9Sstevel@tonic-gate 		s++;
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * Since, iswdigit() will include digit from other than code set 0,
3807c478bd9Sstevel@tonic-gate 	 * we have to check it from code set 0 or not.
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	if (!(iswdigit(*s) && iswascii(*s)) && *s != radixpoint)
3837c478bd9Sstevel@tonic-gate 		return (0);
3847c478bd9Sstevel@tonic-gate 	if (iswdigit(*s) && iswascii(*s)) {
3857c478bd9Sstevel@tonic-gate 		do {
3867c478bd9Sstevel@tonic-gate 			d1++;
3877c478bd9Sstevel@tonic-gate 			s++;
3887c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 	if (d1 >= MAXEXPON)
3917c478bd9Sstevel@tonic-gate 		return (0);	/* too many digits to convert */
3927c478bd9Sstevel@tonic-gate 	if (*s == radixpoint) {
3937c478bd9Sstevel@tonic-gate 		point++;
3947c478bd9Sstevel@tonic-gate 		s++;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	if (iswdigit(*s) && iswascii(*s)) {
3977c478bd9Sstevel@tonic-gate 		d2++;
3987c478bd9Sstevel@tonic-gate 		do {
3997c478bd9Sstevel@tonic-gate 			s++;
4007c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (!(d1 || point && d2))
4057c478bd9Sstevel@tonic-gate 		return (0);
4067c478bd9Sstevel@tonic-gate 	if (*s == 'e' || *s == 'E') {
4077c478bd9Sstevel@tonic-gate 		s++;
4087c478bd9Sstevel@tonic-gate 		if (*s == '+' || *s == '-')
4097c478bd9Sstevel@tonic-gate 			s++;
4107c478bd9Sstevel@tonic-gate 		if (!(iswdigit(*s) && iswascii(*s)))
4117c478bd9Sstevel@tonic-gate 			return (0);
4127c478bd9Sstevel@tonic-gate 		es = s;
4137c478bd9Sstevel@tonic-gate 		do {
4147c478bd9Sstevel@tonic-gate 			s++;
4157c478bd9Sstevel@tonic-gate 		} while (iswdigit(*s) && iswascii(*s));
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		if (s - es > 2)
4197c478bd9Sstevel@tonic-gate 			return (0);
4207c478bd9Sstevel@tonic-gate 		else if (s - es == 2 &&
421baaf2753Sceastha 		    10 * (*es-'0') + *(es+1)-'0' >= MAXEXPON)
4227c478bd9Sstevel@tonic-gate 			return (0);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	while (*s == ' ' || *s == '\t' || *s == '\n')
4257c478bd9Sstevel@tonic-gate 		s++;
4267c478bd9Sstevel@tonic-gate 	if (*s == '\0')
4277c478bd9Sstevel@tonic-gate 		return (1);
4287c478bd9Sstevel@tonic-gate 	else
4297c478bd9Sstevel@tonic-gate 		return (0);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate char *
toeuccode(str)4327c478bd9Sstevel@tonic-gate toeuccode(str)
4337c478bd9Sstevel@tonic-gate wchar_t *str;
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	static char euccode[RECSIZE];
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	(void) wcstombs(euccode, str, RECSIZE);
4387c478bd9Sstevel@tonic-gate 	return (euccode);
4397c478bd9Sstevel@tonic-gate }
440