xref: /minix3/external/historical/nawk/dist/main.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
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 const char	*version = "version 20121220";
26 
27 #if HAVE_NBTOOL_CONFIG_H
28 #include "nbtool_config.h"
29 #endif
30 
31 #define DEBUG
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <locale.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <signal.h>
38 #include "awk.h"
39 #include "awkgram.h"
40 
41 extern	char	**environ;
42 extern	int	nfields;
43 
44 int	dbg	= 0;
45 unsigned int srand_seed = 1;
46 char	*cmdname;	/* gets argv[0] for error messages */
47 extern	FILE	*yyin;	/* lex input file */
48 char	*lexprog;	/* points to program argument if it exists */
49 extern	int errorflag;	/* non-zero if any syntax errors; set by yyerror */
50 int	compile_time = 2;	/* for error printing: */
51 				/* 2 = cmdline, 1 = compile, 0 = running */
52 
53 static char	**pfile = NULL;	/* program filenames from -f's */
54 static size_t 	maxpfile = 0;	/* max program filenames */
55 static size_t	npfile = 0;	/* number of filenames */
56 static size_t	curpfile = 0;	/* current filename */
57 
58 int	safe	= 0;	/* 1 => "safe" mode */
59 
60 static char *
setfs(char * p)61 setfs(char *p)
62 {
63 #ifdef notdef
64 	/* wart: t=>\t */
65 	if (p[0] == 't' && p[1] == 0)
66 		return "\t";
67 	else
68 #endif
69 	if (p[0] != 0)
70 		return p;
71 	return NULL;
72 }
73 
fpecatch(int n,siginfo_t * si,void * uc)74 __dead static void fpecatch(int n
75 #ifdef SA_SIGINFO
76 	, siginfo_t *si, void *uc
77 #endif
78 )
79 {
80 #ifdef SA_SIGINFO
81 	static const char *emsg[] = {
82 	    "Unknown error",
83 	    "Integer divide by zero",
84 	    "Integer overflow",
85 	    "Floating point divide by zero",
86 	    "Floating point overflow",
87 	    "Floating point underflow",
88 	    "Floating point inexact result",
89 	    "Invalid Floating point operation",
90 	    "Subscript out of range",
91 	};
92 #endif
93 	FATAL("floating point exception"
94 #ifdef SA_SIGINFO
95 	    ": %s\n", emsg[si->si_code >= 1 && si->si_code <= 8 ?
96 	    si->si_code : 0]
97 #endif
98 	    );
99 }
100 
main(int argc,char * argv[])101 int main(int argc, char *argv[])
102 {
103 	const char *fs = NULL;
104 	char *fn;
105 
106 	setlocale(LC_ALL, "");
107 	setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
108 	cmdname = argv[0];
109 	if (argc == 1) {
110 		fprintf(stderr,
111 		  "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
112 		  cmdname);
113 		exit(1);
114 	}
115 
116 #ifdef SA_SIGINFO
117 	{
118 		struct sigaction sa;
119 		sa.sa_sigaction = fpecatch;
120 		sa.sa_flags = SA_SIGINFO;
121 		sigemptyset(&sa.sa_mask);
122 		(void)sigaction(SIGFPE, &sa, NULL);
123 	}
124 #else
125 	(void)signal(SIGFPE, fpecatch);
126 #endif
127 	/* Set and keep track of the random seed */
128 	srand_seed = 1;
129 	srand(srand_seed);
130 
131 	yyin = NULL;
132 	symtab = makesymtab(NSYMTAB/NSYMTAB);
133 	while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
134 		if (strcmp(argv[1],"-version") == 0 || strcmp(argv[1],"--version") == 0) {
135 			printf("awk %s\n", version);
136 			exit(0);
137 			break;
138 		}
139 		if (strncmp(argv[1], "--", 2) == 0) {	/* explicit end of args */
140 			argc--;
141 			argv++;
142 			break;
143 		}
144 		switch (argv[1][1]) {
145 		case 's':
146 			if (strcmp(argv[1], "-safe") == 0)
147 				safe = 1;
148 			break;
149 		case 'f':	/* next argument is program filename */
150 			if (argv[1][2] != 0) /* arg is -fsomething */
151 				fn = &argv[1][2];
152 			else {
153 				argc--;
154 				argv++;
155 				if (argc <= 1)
156 					FATAL("no program filename");
157 				fn = argv[1];
158 			}
159 			if (npfile >= maxpfile) {
160 				maxpfile += 20;
161 				pfile = realloc(pfile,
162 				    maxpfile * sizeof(*pfile));
163 				if (pfile == NULL)
164 					FATAL("error allocating space for "
165 					    "-f options");
166 			}
167 			pfile[npfile++] = fn;
168 			break;
169 		case 'F':	/* set field separator */
170 			if (argv[1][2] != 0) {	/* arg is -Fsomething */
171 				fs = setfs(argv[1] + 2);
172 			} else {		/* arg is -F something */
173 				argc--; argv++;
174 				if (argc > 1)
175 					fs = setfs(argv[1]);
176 			}
177 			if (fs == NULL || *fs == '\0')
178 				WARNING("field separator FS is empty");
179 			break;
180 		case 'v':	/* -v a=1 to be done NOW.  one -v for each */
181 			if (argv[1][2] != 0) {  /* arg is -vsomething */
182 				if (isclvar(&argv[1][2]))
183 					setclvar(&argv[1][2]);
184 				else
185 					FATAL("invalid -v option argument: %s", &argv[1][2]);
186 			} else {		/* arg is -v something */
187 				argc--; argv++;
188 				if (argc <= 1)
189 					FATAL("no variable name");
190 				if (isclvar(argv[1]))
191 					setclvar(argv[1]);
192 				else
193 					FATAL("invalid -v option argument: %s", argv[1]);
194 			}
195 			break;
196 		case 'd':
197 			dbg = atoi(&argv[1][2]);
198 			if (dbg == 0)
199 				dbg = 1;
200 			printf("awk %s\n", version);
201 			break;
202 		default:
203 			WARNING("unknown option %s ignored", argv[1]);
204 			break;
205 		}
206 		argc--;
207 		argv++;
208 	}
209 	/* argv[1] is now the first argument */
210 	if (npfile == 0) {	/* no -f; first argument is program */
211 		if (argc <= 1) {
212 			if (dbg)
213 				exit(0);
214 			FATAL("no program given");
215 		}
216 		   dprintf( ("program = |%s|\n", argv[1]) );
217 		lexprog = argv[1];
218 		argc--;
219 		argv++;
220 	}
221 	recinit(recsize);
222 	syminit();
223 	compile_time = 1;
224 	argv[0] = cmdname;	/* put prog name at front of arglist */
225 	   dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
226 	arginit(argc, argv);
227 	if (!safe)
228 		envinit(environ);
229 	yyparse();
230 	if (fs)
231 		*FS = qstring(fs, '\0');
232 	   dprintf( ("errorflag=%d\n", errorflag) );
233 	if (errorflag == 0) {
234 		compile_time = 0;
235 		run(winner);
236 	} else
237 		bracecheck();
238 	return(errorflag);
239 }
240 
pgetc(void)241 int pgetc(void)		/* get 1 character from awk program */
242 {
243 	int c;
244 
245 	for (;;) {
246 		if (yyin == NULL) {
247 			if (curpfile >= npfile)
248 				return EOF;
249 			if (strcmp(pfile[curpfile], "-") == 0)
250 				yyin = stdin;
251 			else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
252 				FATAL("can't open file %s", pfile[curpfile]);
253 			lineno = 1;
254 		}
255 		if ((c = getc(yyin)) != EOF)
256 			return c;
257 		if (yyin != stdin)
258 			fclose(yyin);
259 		yyin = NULL;
260 		curpfile++;
261 	}
262 }
263 
cursource(void)264 char *cursource(void)	/* current source file name */
265 {
266 	if (npfile > 0)
267 		return pfile[curpfile];
268 	else
269 		return NULL;
270 }
271