xref: /illumos-gate/usr/src/cmd/awk/main.c (revision 3ee4fc2a)
1*3ee4fc2aSCody Peter Mello /*
2*3ee4fc2aSCody Peter Mello  * Copyright (C) Lucent Technologies 1997
3*3ee4fc2aSCody Peter Mello  * All Rights Reserved
4*3ee4fc2aSCody Peter Mello  *
5*3ee4fc2aSCody Peter Mello  * Permission to use, copy, modify, and distribute this software and
6*3ee4fc2aSCody Peter Mello  * its documentation for any purpose and without fee is hereby
7*3ee4fc2aSCody Peter Mello  * granted, provided that the above copyright notice appear in all
8*3ee4fc2aSCody Peter Mello  * copies and that both that the copyright notice and this
9*3ee4fc2aSCody Peter Mello  * permission notice and warranty disclaimer appear in supporting
10*3ee4fc2aSCody Peter Mello  * documentation, and that the name Lucent Technologies or any of
11*3ee4fc2aSCody Peter Mello  * its entities not be used in advertising or publicity pertaining
12*3ee4fc2aSCody Peter Mello  * to distribution of the software without specific, written prior
13*3ee4fc2aSCody Peter Mello  * permission.
14*3ee4fc2aSCody Peter Mello  *
15*3ee4fc2aSCody Peter Mello  * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16*3ee4fc2aSCody Peter Mello  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17*3ee4fc2aSCody Peter Mello  * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18*3ee4fc2aSCody Peter Mello  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*3ee4fc2aSCody Peter Mello  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20*3ee4fc2aSCody Peter Mello  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21*3ee4fc2aSCody Peter Mello  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22*3ee4fc2aSCody Peter Mello  * THIS SOFTWARE.
23*3ee4fc2aSCody Peter Mello  */
24*3ee4fc2aSCody Peter Mello 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * CDDL HEADER START
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
2980148899SSurya Prakki  * Common Development and Distribution License (the "License").
3080148899SSurya Prakki  * You may not use this file except in compliance with the License.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
337c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
347c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
357c478bd9Sstevel@tonic-gate  * and limitations under the License.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
387c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
397c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
407c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
417c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * CDDL HEADER END
447c478bd9Sstevel@tonic-gate  */
451ee2e5faSnakanon 
467c478bd9Sstevel@tonic-gate /*
4780148899SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
481ee2e5faSnakanon  * Use is subject to license terms.
497c478bd9Sstevel@tonic-gate  */
501ee2e5faSnakanon 
517c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
527c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <stdio.h>
557c478bd9Sstevel@tonic-gate #include <ctype.h>
567c478bd9Sstevel@tonic-gate #include <signal.h>
577c478bd9Sstevel@tonic-gate #include <locale.h>
587c478bd9Sstevel@tonic-gate #include <libintl.h>
597c478bd9Sstevel@tonic-gate #include <stdarg.h>
607c478bd9Sstevel@tonic-gate #include <errno.h>
617c478bd9Sstevel@tonic-gate #include <values.h>
627c478bd9Sstevel@tonic-gate #include <langinfo.h>
637c478bd9Sstevel@tonic-gate #include "awk.h"
647c478bd9Sstevel@tonic-gate #include "y.tab.h"
657c478bd9Sstevel@tonic-gate 
66*3ee4fc2aSCody Peter Mello char	*version = "version Aug 27, 2018";
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int	dbg	= 0;
69*3ee4fc2aSCody Peter Mello Awkfloat	srand_seed = 1;
70*3ee4fc2aSCody Peter Mello char	*cmdname;	/* gets argv[0] for error messages */
71*3ee4fc2aSCody Peter Mello char	*lexprog;	/* points to program argument if it exists */
727c478bd9Sstevel@tonic-gate int	compile_time = 2;	/* for error printing: */
737c478bd9Sstevel@tonic-gate 				/* 2 = cmdline, 1 = compile, 0 = running */
747c478bd9Sstevel@tonic-gate 
75*3ee4fc2aSCody Peter Mello static char	**pfile = NULL;	/* program filenames from -f's */
761ee2e5faSnakanon static int	npfile = 0;	/* number of filenames */
771ee2e5faSnakanon static int	curpfile = 0;	/* current filename */
781ee2e5faSnakanon 
79*3ee4fc2aSCody Peter Mello int	safe	= 0;	/* 1 => "safe" mode */
80*3ee4fc2aSCody Peter Mello 
811ee2e5faSnakanon int
main(int argc,char * argv[],char * envp[])821ee2e5faSnakanon main(int argc, char *argv[], char *envp[])
837c478bd9Sstevel@tonic-gate {
84*3ee4fc2aSCody Peter Mello 	const char *fs = NULL;
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * At this point, numbers are still scanned as in
877c478bd9Sstevel@tonic-gate 	 * the POSIX locale.
887c478bd9Sstevel@tonic-gate 	 * (POSIX.2, volume 2, P867, L4742-4757)
897c478bd9Sstevel@tonic-gate 	 */
907c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
917c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_NUMERIC, "C");
927c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
937c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
96*3ee4fc2aSCody Peter Mello 	cmdname = argv[0];
977c478bd9Sstevel@tonic-gate 	if (argc == 1) {
981ee2e5faSnakanon 		(void) fprintf(stderr, gettext(
991ee2e5faSnakanon 		    "Usage: %s [-f programfile | 'program'] [-Ffieldsep] "
1001ee2e5faSnakanon 		    "[-v var=value] [files]\n"), cmdname);
1017c478bd9Sstevel@tonic-gate 		exit(1);
1027c478bd9Sstevel@tonic-gate 	}
1031ee2e5faSnakanon 	(void) signal(SIGFPE, fpecatch);
104*3ee4fc2aSCody Peter Mello 
105*3ee4fc2aSCody Peter Mello 	srand_seed = 1;
106*3ee4fc2aSCody Peter Mello 	srand((unsigned int)srand_seed);
107*3ee4fc2aSCody Peter Mello 
1087c478bd9Sstevel@tonic-gate 	yyin = NULL;
109*3ee4fc2aSCody Peter Mello 	symtab = makesymtab(NSYMTAB/NSYMTAB);
1107c478bd9Sstevel@tonic-gate 	while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
111*3ee4fc2aSCody Peter Mello 		if (strcmp(argv[1], "-version") == 0 ||
112*3ee4fc2aSCody Peter Mello 		    strcmp(argv[1], "--version") == 0) {
113*3ee4fc2aSCody Peter Mello 			(void) printf("awk %s\n", version);
114*3ee4fc2aSCody Peter Mello 			exit(0);
115*3ee4fc2aSCody Peter Mello 			break;
116*3ee4fc2aSCody Peter Mello 		}
1177c478bd9Sstevel@tonic-gate 		if (strcmp(argv[1], "--") == 0) {
1187c478bd9Sstevel@tonic-gate 			/* explicit end of args */
1197c478bd9Sstevel@tonic-gate 			argc--;
1207c478bd9Sstevel@tonic-gate 			argv++;
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 		switch (argv[1][1]) {
124*3ee4fc2aSCody Peter Mello 		case 's':
125*3ee4fc2aSCody Peter Mello 			if (strcmp(argv[1], "-safe") == 0)
126*3ee4fc2aSCody Peter Mello 				safe = 1;
127*3ee4fc2aSCody Peter Mello 			break;
1287c478bd9Sstevel@tonic-gate 		case 'f':	/* next argument is program filename */
129*3ee4fc2aSCody Peter Mello 			if (argv[1][2] != 0) {  /* arg is -fsomething */
130*3ee4fc2aSCody Peter Mello 				pfile = realloc(pfile,
131*3ee4fc2aSCody Peter Mello 				    sizeof (char *) * (npfile + 1));
132*3ee4fc2aSCody Peter Mello 				if (pfile == NULL)
133*3ee4fc2aSCody Peter Mello 					FATAL("out of space in main");
134*3ee4fc2aSCody Peter Mello 				pfile[npfile++] = &argv[1][2];
135*3ee4fc2aSCody Peter Mello 			} else {		/* arg is -f something */
136*3ee4fc2aSCody Peter Mello 				argc--; argv++;
137*3ee4fc2aSCody Peter Mello 				if (argc <= 1)
138*3ee4fc2aSCody Peter Mello 					FATAL("no program filename");
139*3ee4fc2aSCody Peter Mello 				pfile = realloc(pfile,
140*3ee4fc2aSCody Peter Mello 				    sizeof (char *) * (npfile + 1));
141*3ee4fc2aSCody Peter Mello 				if (pfile == NULL)
142*3ee4fc2aSCody Peter Mello 					FATAL("out of space in main");
143*3ee4fc2aSCody Peter Mello 				pfile[npfile++] = argv[1];
144*3ee4fc2aSCody Peter Mello 			}
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		case 'F':	/* set field separator */
1477c478bd9Sstevel@tonic-gate 			if (argv[1][2] != 0) {	/* arg is -Fsomething */
1487c478bd9Sstevel@tonic-gate 				/* wart: t=>\t */
1497c478bd9Sstevel@tonic-gate 				if (argv[1][2] == 't' && argv[1][3] == 0)
150*3ee4fc2aSCody Peter Mello 					fs = "\t";
1517c478bd9Sstevel@tonic-gate 				else if (argv[1][2] != 0)
152*3ee4fc2aSCody Peter Mello 					fs = &argv[1][2];
1537c478bd9Sstevel@tonic-gate 			} else {		/* arg is -F something */
1547c478bd9Sstevel@tonic-gate 				argc--; argv++;
1557c478bd9Sstevel@tonic-gate 				if (argc > 1) {
1567c478bd9Sstevel@tonic-gate 					/* wart: t=>\t */
1577c478bd9Sstevel@tonic-gate 					if (argv[1][0] == 't' &&
1581ee2e5faSnakanon 					    argv[1][1] == 0)
159*3ee4fc2aSCody Peter Mello 						fs = "\t";
1607c478bd9Sstevel@tonic-gate 					else if (argv[1][0] != 0)
161*3ee4fc2aSCody Peter Mello 						fs = &argv[1][0];
1627c478bd9Sstevel@tonic-gate 				}
1637c478bd9Sstevel@tonic-gate 			}
1647c478bd9Sstevel@tonic-gate 			if (fs == NULL || *fs == '\0')
165*3ee4fc2aSCody Peter Mello 				WARNING("field separator FS is empty");
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		case 'v':	/* -v a=1 to be done NOW.  one -v for each */
168*3ee4fc2aSCody Peter Mello 			if (argv[1][2] != 0) {  /* arg is -vsomething */
169*3ee4fc2aSCody Peter Mello 				if (isclvar(&argv[1][2]))
170*3ee4fc2aSCody Peter Mello 					setclvar(&argv[1][2]);
171*3ee4fc2aSCody Peter Mello 				else
172*3ee4fc2aSCody Peter Mello 					FATAL("invalid -v option argument: %s",
173*3ee4fc2aSCody Peter Mello 					    &argv[1][2]);
174*3ee4fc2aSCody Peter Mello 			} else {		/* arg is -v something */
175*3ee4fc2aSCody Peter Mello 				argc--; argv++;
176*3ee4fc2aSCody Peter Mello 				if (argc <= 1)
177*3ee4fc2aSCody Peter Mello 					FATAL("no variable name");
178*3ee4fc2aSCody Peter Mello 				if (isclvar(argv[1]))
179*3ee4fc2aSCody Peter Mello 					setclvar(argv[1]);
180*3ee4fc2aSCody Peter Mello 				else
181*3ee4fc2aSCody Peter Mello 					FATAL("invalid -v option argument: %s",
182*3ee4fc2aSCody Peter Mello 					    argv[1]);
183*3ee4fc2aSCody Peter Mello 			}
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 		case 'd':
1867c478bd9Sstevel@tonic-gate 			dbg = atoi(&argv[1][2]);
1877c478bd9Sstevel@tonic-gate 			if (dbg == 0)
1887c478bd9Sstevel@tonic-gate 				dbg = 1;
1891ee2e5faSnakanon 			(void) printf("awk %s\n", version);
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 		default:
192*3ee4fc2aSCody Peter Mello 			WARNING("unknown option %s ignored", argv[1]);
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		argc--;
1967c478bd9Sstevel@tonic-gate 		argv++;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	/* argv[1] is now the first argument */
1997c478bd9Sstevel@tonic-gate 	if (npfile == 0) {	/* no -f; first argument is program */
2000717e03dSYuri Pankov 		if (argc <= 1) {
2010717e03dSYuri Pankov 			if (dbg)
2020717e03dSYuri Pankov 				exit(0);
203*3ee4fc2aSCody Peter Mello 			FATAL("no program given");
2040717e03dSYuri Pankov 		}
2057c478bd9Sstevel@tonic-gate 		dprintf(("program = |%s|\n", argv[1]));
206*3ee4fc2aSCody Peter Mello 		lexprog = argv[1];
2077c478bd9Sstevel@tonic-gate 		argc--;
2087c478bd9Sstevel@tonic-gate 		argv++;
2097c478bd9Sstevel@tonic-gate 	}
210*3ee4fc2aSCody Peter Mello 	recinit(recsize);
211*3ee4fc2aSCody Peter Mello 	syminit();
2127c478bd9Sstevel@tonic-gate 	compile_time = 1;
213*3ee4fc2aSCody Peter Mello 	argv[0] = cmdname;	/* put prog name at front of arglist */
2147c478bd9Sstevel@tonic-gate 	dprintf(("argc=%d, argv[0]=%s\n", argc, argv[0]));
215*3ee4fc2aSCody Peter Mello 	arginit(argc, argv);
216*3ee4fc2aSCody Peter Mello 	if (!safe)
217*3ee4fc2aSCody Peter Mello 		envinit(envp);
21880148899SSurya Prakki 	(void) yyparse();
2197c478bd9Sstevel@tonic-gate 	if (fs)
2201ee2e5faSnakanon 		*FS = qstring(fs, '\0');
2217c478bd9Sstevel@tonic-gate 	dprintf(("errorflag=%d\n", errorflag));
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * done parsing, so now activate the LC_NUMERIC
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (errorflag == 0) {
2287c478bd9Sstevel@tonic-gate 		compile_time = 0;
2297c478bd9Sstevel@tonic-gate 		run(winner);
2307c478bd9Sstevel@tonic-gate 	} else
2317c478bd9Sstevel@tonic-gate 		bracecheck();
2321ee2e5faSnakanon 	return (errorflag);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2351ee2e5faSnakanon int
pgetc(void)236*3ee4fc2aSCody Peter Mello pgetc(void)		/* get 1 character from awk program */
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int c;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	for (;;) {
2417c478bd9Sstevel@tonic-gate 		if (yyin == NULL) {
2427c478bd9Sstevel@tonic-gate 			if (curpfile >= npfile)
2437c478bd9Sstevel@tonic-gate 				return (EOF);
244*3ee4fc2aSCody Peter Mello 			yyin = (strcmp(pfile[curpfile], "-") == 0) ?
245*3ee4fc2aSCody Peter Mello 			    stdin : fopen(pfile[curpfile], "rF");
2461ee2e5faSnakanon 			if (yyin == NULL) {
247*3ee4fc2aSCody Peter Mello 				FATAL("can't open file %s", pfile[curpfile]);
2481ee2e5faSnakanon 			}
249*3ee4fc2aSCody Peter Mello 			lineno = 1;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		if ((c = getc(yyin)) != EOF)
2527c478bd9Sstevel@tonic-gate 			return (c);
253*3ee4fc2aSCody Peter Mello 		if (yyin != stdin)
254*3ee4fc2aSCody Peter Mello 			(void) fclose(yyin);
2557c478bd9Sstevel@tonic-gate 		yyin = NULL;
2567c478bd9Sstevel@tonic-gate 		curpfile++;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate }
259*3ee4fc2aSCody Peter Mello 
260*3ee4fc2aSCody Peter Mello char *
cursource(void)261*3ee4fc2aSCody Peter Mello cursource(void)	/* current source file name */
262*3ee4fc2aSCody Peter Mello {
263*3ee4fc2aSCody Peter Mello 	if (curpfile < npfile)
264*3ee4fc2aSCody Peter Mello 		return (pfile[curpfile]);
265*3ee4fc2aSCody Peter Mello 	else
266*3ee4fc2aSCody Peter Mello 		return (NULL);
267*3ee4fc2aSCody Peter Mello }
268