xref: /titanic_41/usr/src/cmd/awk/main.c (revision f8919bdadda3ebb97bd55cc14a16e0271ed57615)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <signal.h>
36 #include <locale.h>
37 #include <libintl.h>
38 #include <stdarg.h>
39 #include <errno.h>
40 #include <values.h>
41 #include <langinfo.h>
42 #include "awk.h"
43 #include "y.tab.h"
44 
45 char	*version = "version Oct 11, 1989";
46 
47 int	dbg	= 0;
48 uchar	*cmdname;	/* gets argv[0] for error messages */
49 uchar	*lexprog;	/* points to program argument if it exists */
50 int	compile_time = 2;	/* for error printing: */
51 				/* 2 = cmdline, 1 = compile, 0 = running */
52 char	radixpoint = '.';
53 
54 static uchar	**pfile = NULL;	/* program filenames from -f's */
55 static int	npfile = 0;	/* number of filenames */
56 static int	curpfile = 0;	/* current filename */
57 
58 int
59 main(int argc, char *argv[], char *envp[])
60 {
61 	uchar *fs = NULL;
62 	char	*nl_radix;
63 	/*
64 	 * At this point, numbers are still scanned as in
65 	 * the POSIX locale.
66 	 * (POSIX.2, volume 2, P867, L4742-4757)
67 	 */
68 	(void) setlocale(LC_ALL, "");
69 	(void) setlocale(LC_NUMERIC, "C");
70 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
71 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
72 #endif
73 	(void) textdomain(TEXT_DOMAIN);
74 	cmdname = (uchar *)argv[0];
75 	if (argc == 1) {
76 		(void) fprintf(stderr, gettext(
77 		    "Usage: %s [-f programfile | 'program'] [-Ffieldsep] "
78 		    "[-v var=value] [files]\n"), cmdname);
79 		exit(1);
80 	}
81 	(void) signal(SIGFPE, fpecatch);
82 	yyin = NULL;
83 	syminit();
84 	while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
85 		if (strcmp(argv[1], "--") == 0) {
86 			/* explicit end of args */
87 			argc--;
88 			argv++;
89 			break;
90 		}
91 		switch (argv[1][1]) {
92 		case 'f':	/* next argument is program filename */
93 			argc--;
94 			argv++;
95 			if (argc <= 1)
96 				ERROR "no program filename" FATAL;
97 			pfile = realloc(pfile, sizeof (uchar *) * (npfile + 1));
98 			if (pfile == NULL)
99 				ERROR "out of space in main" FATAL;
100 			pfile[npfile++] = (uchar *)argv[1];
101 			break;
102 		case 'F':	/* set field separator */
103 			if (argv[1][2] != 0) {	/* arg is -Fsomething */
104 				/* wart: t=>\t */
105 				if (argv[1][2] == 't' && argv[1][3] == 0)
106 					fs = (uchar *) "\t";
107 				else if (argv[1][2] != 0)
108 					fs = (uchar *)&argv[1][2];
109 			} else {		/* arg is -F something */
110 				argc--; argv++;
111 				if (argc > 1) {
112 					/* wart: t=>\t */
113 					if (argv[1][0] == 't' &&
114 					    argv[1][1] == 0)
115 						fs = (uchar *) "\t";
116 					else if (argv[1][0] != 0)
117 						fs = (uchar *)&argv[1][0];
118 				}
119 			}
120 			if (fs == NULL || *fs == '\0')
121 				ERROR "field separator FS is empty" WARNING;
122 			break;
123 		case 'v':	/* -v a=1 to be done NOW.  one -v for each */
124 			if (argv[1][2] == '\0' && --argc > 1 &&
125 			    isclvar((uchar *)(++argv)[1]))
126 				setclvar((uchar *)argv[1]);
127 			break;
128 		case 'd':
129 			dbg = atoi(&argv[1][2]);
130 			if (dbg == 0)
131 				dbg = 1;
132 			(void) printf("awk %s\n", version);
133 			break;
134 		default:
135 			ERROR "unknown option %s ignored", argv[1] WARNING;
136 			break;
137 		}
138 		argc--;
139 		argv++;
140 	}
141 	/* argv[1] is now the first argument */
142 	if (npfile == 0) {	/* no -f; first argument is program */
143 		if (argc <= 1)
144 			ERROR "no program given" FATAL;
145 		dprintf(("program = |%s|\n", argv[1]));
146 		lexprog = (uchar *)argv[1];
147 		argc--;
148 		argv++;
149 	}
150 	compile_time = 1;
151 	argv[0] = (char *)cmdname;	/* put prog name at front of arglist */
152 	dprintf(("argc=%d, argv[0]=%s\n", argc, argv[0]));
153 	arginit(argc, (uchar **)argv);
154 	envinit((uchar **)envp);
155 	yyparse();
156 	if (fs)
157 		*FS = qstring(fs, '\0');
158 	dprintf(("errorflag=%d\n", errorflag));
159 	/*
160 	 * done parsing, so now activate the LC_NUMERIC
161 	 */
162 	(void) setlocale(LC_ALL, "");
163 	nl_radix = nl_langinfo(RADIXCHAR);
164 	if (nl_radix)
165 		radixpoint = *nl_radix;
166 
167 	if (errorflag == 0) {
168 		compile_time = 0;
169 		run(winner);
170 	} else
171 		bracecheck();
172 	return (errorflag);
173 }
174 
175 int
176 pgetc(void)		/* get program character */
177 {
178 	int c;
179 
180 	for (;;) {
181 		if (yyin == NULL) {
182 			if (curpfile >= npfile)
183 				return (EOF);
184 			yyin = (strcmp((char *)pfile[curpfile], "-") == 0) ?
185 			    stdin : fopen((char *)pfile[curpfile], "r");
186 			if (yyin == NULL) {
187 				ERROR "can't open file %s",
188 				    pfile[curpfile] FATAL;
189 			}
190 		}
191 		if ((c = getc(yyin)) != EOF)
192 			return (c);
193 		(void) fclose(yyin);
194 		yyin = NULL;
195 		curpfile++;
196 	}
197 }
198