xref: /titanic_51/usr/src/cmd/awk/main.c (revision 1ee2e5fa222f6d33d1ff1c48f155973a5e146434)
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*1ee2e5faSnakanon 
237c478bd9Sstevel@tonic-gate /*
24*1ee2e5faSnakanon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*1ee2e5faSnakanon  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
27*1ee2e5faSnakanon 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
31*1ee2e5faSnakanon #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <locale.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <stdarg.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <values.h>
417c478bd9Sstevel@tonic-gate #include <langinfo.h>
427c478bd9Sstevel@tonic-gate #include "awk.h"
437c478bd9Sstevel@tonic-gate #include "y.tab.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate char	*version = "version Oct 11, 1989";
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate int	dbg	= 0;
487c478bd9Sstevel@tonic-gate uchar	*cmdname;	/* gets argv[0] for error messages */
497c478bd9Sstevel@tonic-gate uchar	*lexprog;	/* points to program argument if it exists */
507c478bd9Sstevel@tonic-gate int	compile_time = 2;	/* for error printing: */
517c478bd9Sstevel@tonic-gate 				/* 2 = cmdline, 1 = compile, 0 = running */
527c478bd9Sstevel@tonic-gate char	radixpoint = '.';
537c478bd9Sstevel@tonic-gate 
54*1ee2e5faSnakanon static uchar	**pfile = NULL;	/* program filenames from -f's */
55*1ee2e5faSnakanon static int	npfile = 0;	/* number of filenames */
56*1ee2e5faSnakanon static int	curpfile = 0;	/* current filename */
57*1ee2e5faSnakanon 
58*1ee2e5faSnakanon int
59*1ee2e5faSnakanon main(int argc, char *argv[], char *envp[])
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	uchar *fs = NULL;
627c478bd9Sstevel@tonic-gate 	char	*nl_radix;
637c478bd9Sstevel@tonic-gate 	/*
647c478bd9Sstevel@tonic-gate 	 * At this point, numbers are still scanned as in
657c478bd9Sstevel@tonic-gate 	 * the POSIX locale.
667c478bd9Sstevel@tonic-gate 	 * (POSIX.2, volume 2, P867, L4742-4757)
677c478bd9Sstevel@tonic-gate 	 */
687c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
697c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_NUMERIC, "C");
707c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
717c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
74*1ee2e5faSnakanon 	cmdname = (uchar *)argv[0];
757c478bd9Sstevel@tonic-gate 	if (argc == 1) {
76*1ee2e5faSnakanon 		(void) fprintf(stderr, gettext(
777c478bd9Sstevel@tonic-gate 		    "Usage: %s [-f programfile | 'program'] [-Ffieldsep] "
787c478bd9Sstevel@tonic-gate 		    "[-v var=value] [files]\n"), cmdname);
797c478bd9Sstevel@tonic-gate 		exit(1);
807c478bd9Sstevel@tonic-gate 	}
81*1ee2e5faSnakanon 	(void) signal(SIGFPE, fpecatch);
827c478bd9Sstevel@tonic-gate 	yyin = NULL;
837c478bd9Sstevel@tonic-gate 	syminit();
847c478bd9Sstevel@tonic-gate 	while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
857c478bd9Sstevel@tonic-gate 		if (strcmp(argv[1], "--") == 0) {
867c478bd9Sstevel@tonic-gate 			/* explicit end of args */
877c478bd9Sstevel@tonic-gate 			argc--;
887c478bd9Sstevel@tonic-gate 			argv++;
897c478bd9Sstevel@tonic-gate 			break;
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 		switch (argv[1][1]) {
927c478bd9Sstevel@tonic-gate 		case 'f':	/* next argument is program filename */
937c478bd9Sstevel@tonic-gate 			argc--;
947c478bd9Sstevel@tonic-gate 			argv++;
957c478bd9Sstevel@tonic-gate 			if (argc <= 1)
967c478bd9Sstevel@tonic-gate 				ERROR "no program filename" FATAL;
97*1ee2e5faSnakanon 			pfile = realloc(pfile, sizeof (uchar *) * (npfile + 1));
98*1ee2e5faSnakanon 			if (pfile == NULL)
99*1ee2e5faSnakanon 				ERROR "out of space in main" FATAL;
100*1ee2e5faSnakanon 			pfile[npfile++] = (uchar *)argv[1];
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		case 'F':	/* set field separator */
1037c478bd9Sstevel@tonic-gate 			if (argv[1][2] != 0) {	/* arg is -Fsomething */
1047c478bd9Sstevel@tonic-gate 				/* wart: t=>\t */
1057c478bd9Sstevel@tonic-gate 				if (argv[1][2] == 't' && argv[1][3] == 0)
1067c478bd9Sstevel@tonic-gate 					fs = (uchar *) "\t";
1077c478bd9Sstevel@tonic-gate 				else if (argv[1][2] != 0)
108*1ee2e5faSnakanon 					fs = (uchar *)&argv[1][2];
1097c478bd9Sstevel@tonic-gate 			} else {		/* arg is -F something */
1107c478bd9Sstevel@tonic-gate 				argc--; argv++;
1117c478bd9Sstevel@tonic-gate 				if (argc > 1) {
1127c478bd9Sstevel@tonic-gate 					/* wart: t=>\t */
1137c478bd9Sstevel@tonic-gate 					if (argv[1][0] == 't' &&
1147c478bd9Sstevel@tonic-gate 					    argv[1][1] == 0)
1157c478bd9Sstevel@tonic-gate 						fs = (uchar *) "\t";
1167c478bd9Sstevel@tonic-gate 					else if (argv[1][0] != 0)
117*1ee2e5faSnakanon 						fs = (uchar *)&argv[1][0];
1187c478bd9Sstevel@tonic-gate 				}
1197c478bd9Sstevel@tonic-gate 			}
1207c478bd9Sstevel@tonic-gate 			if (fs == NULL || *fs == '\0')
1217c478bd9Sstevel@tonic-gate 				ERROR "field separator FS is empty" WARNING;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 		case 'v':	/* -v a=1 to be done NOW.  one -v for each */
1247c478bd9Sstevel@tonic-gate 			if (argv[1][2] == '\0' && --argc > 1 &&
125*1ee2e5faSnakanon 			    isclvar((uchar *)(++argv)[1]))
126*1ee2e5faSnakanon 				setclvar((uchar *)argv[1]);
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 		case 'd':
1297c478bd9Sstevel@tonic-gate 			dbg = atoi(&argv[1][2]);
1307c478bd9Sstevel@tonic-gate 			if (dbg == 0)
1317c478bd9Sstevel@tonic-gate 				dbg = 1;
132*1ee2e5faSnakanon 			(void) printf("awk %s\n", version);
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 		default:
1357c478bd9Sstevel@tonic-gate 			ERROR "unknown option %s ignored", argv[1] WARNING;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 		argc--;
1397c478bd9Sstevel@tonic-gate 		argv++;
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	/* argv[1] is now the first argument */
1427c478bd9Sstevel@tonic-gate 	if (npfile == 0) {	/* no -f; first argument is program */
1437c478bd9Sstevel@tonic-gate 		if (argc <= 1)
1447c478bd9Sstevel@tonic-gate 			ERROR "no program given" FATAL;
1457c478bd9Sstevel@tonic-gate 		dprintf(("program = |%s|\n", argv[1]));
146*1ee2e5faSnakanon 		lexprog = (uchar *)argv[1];
1477c478bd9Sstevel@tonic-gate 		argc--;
1487c478bd9Sstevel@tonic-gate 		argv++;
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	compile_time = 1;
151*1ee2e5faSnakanon 	argv[0] = (char *)cmdname;	/* put prog name at front of arglist */
1527c478bd9Sstevel@tonic-gate 	dprintf(("argc=%d, argv[0]=%s\n", argc, argv[0]));
153*1ee2e5faSnakanon 	arginit(argc, (uchar **)argv);
154*1ee2e5faSnakanon 	envinit((uchar **)envp);
1557c478bd9Sstevel@tonic-gate 	yyparse();
1567c478bd9Sstevel@tonic-gate 	if (fs)
157*1ee2e5faSnakanon 		*FS = qstring(fs, '\0');
1587c478bd9Sstevel@tonic-gate 	dprintf(("errorflag=%d\n", errorflag));
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * done parsing, so now activate the LC_NUMERIC
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1637c478bd9Sstevel@tonic-gate 	nl_radix = nl_langinfo(RADIXCHAR);
1647c478bd9Sstevel@tonic-gate 	if (nl_radix)
1657c478bd9Sstevel@tonic-gate 		radixpoint = *nl_radix;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (errorflag == 0) {
1687c478bd9Sstevel@tonic-gate 		compile_time = 0;
1697c478bd9Sstevel@tonic-gate 		run(winner);
1707c478bd9Sstevel@tonic-gate 	} else
1717c478bd9Sstevel@tonic-gate 		bracecheck();
172*1ee2e5faSnakanon 	return (errorflag);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
175*1ee2e5faSnakanon int
176*1ee2e5faSnakanon pgetc(void)		/* get program character */
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	int c;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	for (;;) {
1817c478bd9Sstevel@tonic-gate 		if (yyin == NULL) {
1827c478bd9Sstevel@tonic-gate 			if (curpfile >= npfile)
1837c478bd9Sstevel@tonic-gate 				return (EOF);
1847c478bd9Sstevel@tonic-gate 			yyin = (strcmp((char *)pfile[curpfile], "-") == 0) ?
1857c478bd9Sstevel@tonic-gate 			    stdin : fopen((char *)pfile[curpfile], "r");
186*1ee2e5faSnakanon 			if (yyin == NULL) {
1877c478bd9Sstevel@tonic-gate 				ERROR "can't open file %s",
1887c478bd9Sstevel@tonic-gate 				    pfile[curpfile] FATAL;
1897c478bd9Sstevel@tonic-gate 			}
190*1ee2e5faSnakanon 		}
1917c478bd9Sstevel@tonic-gate 		if ((c = getc(yyin)) != EOF)
1927c478bd9Sstevel@tonic-gate 			return (c);
193*1ee2e5faSnakanon 		(void) fclose(yyin);
1947c478bd9Sstevel@tonic-gate 		yyin = NULL;
1957c478bd9Sstevel@tonic-gate 		curpfile++;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate }
198