xref: /illumos-gate/usr/src/cmd/awk/main.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * Copyright (C) Lucent Technologies 1997
3  * All Rights Reserved
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby
7  * granted, provided that the above copyright notice appear in all
8  * copies and that both that the copyright notice and this
9  * permission notice and warranty disclaimer appear in supporting
10  * documentation, and that the name Lucent Technologies or any of
11  * its entities not be used in advertising or publicity pertaining
12  * to distribution of the software without specific, written prior
13  * permission.
14  *
15  * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17  * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22  * THIS SOFTWARE.
23  */
24 
25 /*
26  * CDDL HEADER START
27  *
28  * The contents of this file are subject to the terms of the
29  * Common Development and Distribution License (the "License").
30  * You may not use this file except in compliance with the License.
31  *
32  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
33  * or http://www.opensolaris.org/os/licensing.
34  * See the License for the specific language governing permissions
35  * and limitations under the License.
36  *
37  * When distributing Covered Code, include this CDDL HEADER in each
38  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
39  * If applicable, add the following below this CDDL HEADER, with the
40  * fields enclosed by brackets "[]" replaced with your own identifying
41  * information: Portions Copyright [yyyy] [name of copyright owner]
42  *
43  * CDDL HEADER END
44  */
45 
46 /*
47  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
48  * Use is subject to license terms.
49  */
50 
51 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
52 /*	  All Rights Reserved  	*/
53 
54 #include <stdio.h>
55 #include <ctype.h>
56 #include <signal.h>
57 #include <locale.h>
58 #include <libintl.h>
59 #include <stdarg.h>
60 #include <errno.h>
61 #include <values.h>
62 #include <langinfo.h>
63 #include "awk.h"
64 #include "y.tab.h"
65 
66 char	*version = "version Aug 27, 2018";
67 
68 int	dbg	= 0;
69 Awkfloat	srand_seed = 1;
70 char	*cmdname;	/* gets argv[0] for error messages */
71 char	*lexprog;	/* points to program argument if it exists */
72 int	compile_time = 2;	/* for error printing: */
73 				/* 2 = cmdline, 1 = compile, 0 = running */
74 
75 static char	**pfile = NULL;	/* program filenames from -f's */
76 static int	npfile = 0;	/* number of filenames */
77 static int	curpfile = 0;	/* current filename */
78 
79 int	safe	= 0;	/* 1 => "safe" mode */
80 
81 int
82 main(int argc, char *argv[], char *envp[])
83 {
84 	const char *fs = NULL;
85 	/*
86 	 * At this point, numbers are still scanned as in
87 	 * the POSIX locale.
88 	 * (POSIX.2, volume 2, P867, L4742-4757)
89 	 */
90 	(void) setlocale(LC_ALL, "");
91 	(void) setlocale(LC_NUMERIC, "C");
92 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
93 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
94 #endif
95 	(void) textdomain(TEXT_DOMAIN);
96 	cmdname = argv[0];
97 	if (argc == 1) {
98 		(void) fprintf(stderr, gettext(
99 		    "Usage: %s [-f programfile | 'program'] [-Ffieldsep] "
100 		    "[-v var=value] [files]\n"), cmdname);
101 		exit(1);
102 	}
103 	(void) signal(SIGFPE, fpecatch);
104 
105 	srand_seed = 1;
106 	srand((unsigned int)srand_seed);
107 
108 	yyin = NULL;
109 	symtab = makesymtab(NSYMTAB/NSYMTAB);
110 	while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
111 		if (strcmp(argv[1], "-version") == 0 ||
112 		    strcmp(argv[1], "--version") == 0) {
113 			(void) printf("awk %s\n", version);
114 			exit(0);
115 			break;
116 		}
117 		if (strcmp(argv[1], "--") == 0) {
118 			/* explicit end of args */
119 			argc--;
120 			argv++;
121 			break;
122 		}
123 		switch (argv[1][1]) {
124 		case 's':
125 			if (strcmp(argv[1], "-safe") == 0)
126 				safe = 1;
127 			break;
128 		case 'f':	/* next argument is program filename */
129 			if (argv[1][2] != 0) {  /* arg is -fsomething */
130 				pfile = realloc(pfile,
131 				    sizeof (char *) * (npfile + 1));
132 				if (pfile == NULL)
133 					FATAL("out of space in main");
134 				pfile[npfile++] = &argv[1][2];
135 			} else {		/* arg is -f something */
136 				argc--; argv++;
137 				if (argc <= 1)
138 					FATAL("no program filename");
139 				pfile = realloc(pfile,
140 				    sizeof (char *) * (npfile + 1));
141 				if (pfile == NULL)
142 					FATAL("out of space in main");
143 				pfile[npfile++] = argv[1];
144 			}
145 			break;
146 		case 'F':	/* set field separator */
147 			if (argv[1][2] != 0) {	/* arg is -Fsomething */
148 				/* wart: t=>\t */
149 				if (argv[1][2] == 't' && argv[1][3] == 0)
150 					fs = "\t";
151 				else if (argv[1][2] != 0)
152 					fs = &argv[1][2];
153 			} else {		/* arg is -F something */
154 				argc--; argv++;
155 				if (argc > 1) {
156 					/* wart: t=>\t */
157 					if (argv[1][0] == 't' &&
158 					    argv[1][1] == 0)
159 						fs = "\t";
160 					else if (argv[1][0] != 0)
161 						fs = &argv[1][0];
162 				}
163 			}
164 			if (fs == NULL || *fs == '\0')
165 				WARNING("field separator FS is empty");
166 			break;
167 		case 'v':	/* -v a=1 to be done NOW.  one -v for each */
168 			if (argv[1][2] != 0) {  /* arg is -vsomething */
169 				if (isclvar(&argv[1][2]))
170 					setclvar(&argv[1][2]);
171 				else
172 					FATAL("invalid -v option argument: %s",
173 					    &argv[1][2]);
174 			} else {		/* arg is -v something */
175 				argc--; argv++;
176 				if (argc <= 1)
177 					FATAL("no variable name");
178 				if (isclvar(argv[1]))
179 					setclvar(argv[1]);
180 				else
181 					FATAL("invalid -v option argument: %s",
182 					    argv[1]);
183 			}
184 			break;
185 		case 'd':
186 			dbg = atoi(&argv[1][2]);
187 			if (dbg == 0)
188 				dbg = 1;
189 			(void) printf("awk %s\n", version);
190 			break;
191 		default:
192 			WARNING("unknown option %s ignored", argv[1]);
193 			break;
194 		}
195 		argc--;
196 		argv++;
197 	}
198 	/* argv[1] is now the first argument */
199 	if (npfile == 0) {	/* no -f; first argument is program */
200 		if (argc <= 1) {
201 			if (dbg)
202 				exit(0);
203 			FATAL("no program given");
204 		}
205 		dprintf(("program = |%s|\n", argv[1]));
206 		lexprog = argv[1];
207 		argc--;
208 		argv++;
209 	}
210 	recinit(recsize);
211 	syminit();
212 	compile_time = 1;
213 	argv[0] = cmdname;	/* put prog name at front of arglist */
214 	dprintf(("argc=%d, argv[0]=%s\n", argc, argv[0]));
215 	arginit(argc, argv);
216 	if (!safe)
217 		envinit(envp);
218 	(void) yyparse();
219 	if (fs)
220 		*FS = qstring(fs, '\0');
221 	dprintf(("errorflag=%d\n", errorflag));
222 	/*
223 	 * done parsing, so now activate the LC_NUMERIC
224 	 */
225 	(void) setlocale(LC_ALL, "");
226 
227 	if (errorflag == 0) {
228 		compile_time = 0;
229 		run(winner);
230 	} else
231 		bracecheck();
232 	return (errorflag);
233 }
234 
235 int
236 pgetc(void)		/* get 1 character from awk program */
237 {
238 	int c;
239 
240 	for (;;) {
241 		if (yyin == NULL) {
242 			if (curpfile >= npfile)
243 				return (EOF);
244 			yyin = (strcmp(pfile[curpfile], "-") == 0) ?
245 			    stdin : fopen(pfile[curpfile], "rF");
246 			if (yyin == NULL) {
247 				FATAL("can't open file %s", pfile[curpfile]);
248 			}
249 			lineno = 1;
250 		}
251 		if ((c = getc(yyin)) != EOF)
252 			return (c);
253 		if (yyin != stdin)
254 			(void) fclose(yyin);
255 		yyin = NULL;
256 		curpfile++;
257 	}
258 }
259 
260 char *
261 cursource(void)	/* current source file name */
262 {
263 	if (curpfile < npfile)
264 		return (pfile[curpfile]);
265 	else
266 		return (NULL);
267 }
268