blob: f3936341ae75e43e3cfd53d5414b9c6a569fd8cd [file] [log] [blame]
Brian Kernighan87b94932012-12-22 10:35:39 -05001/****************************************************************
2Copyright (C) Lucent Technologies 1997
3All Rights Reserved
4
5Permission to use, copy, modify, and distribute this software and
6its documentation for any purpose and without fee is hereby
7granted, provided that the above copyright notice appear in all
8copies and that both that the copyright notice and this
9permission notice and warranty disclaimer appear in supporting
10documentation, and that the name Lucent Technologies or any of
11its entities not be used in advertising or publicity pertaining
12to distribution of the software without specific, written prior
13permission.
14
15LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22THIS SOFTWARE.
23****************************************************************/
24
Arnold D. Robbinsc0f4e972021-02-15 20:33:15 +020025const char *version = "version 20210215";
Brian Kernighan87b94932012-12-22 10:35:39 -050026
27#define DEBUG
28#include <stdio.h>
29#include <ctype.h>
30#include <locale.h>
31#include <stdlib.h>
32#include <string.h>
33#include <signal.h>
34#include "awk.h"
Brian Kernighan87b94932012-12-22 10:35:39 -050035
36extern char **environ;
37extern int nfields;
38
39int dbg = 0;
40Awkfloat srand_seed = 1;
41char *cmdname; /* gets argv[0] for error messages */
42extern FILE *yyin; /* lex input file */
43char *lexprog; /* points to program argument if it exists */
44extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
Arnold D. Robbins108224b2019-11-10 21:19:18 +020045enum compile_states compile_time = ERROR_PRINTING;
Brian Kernighan87b94932012-12-22 10:35:39 -050046
zoulasc94e4c042020-02-18 14:20:27 -050047static char **pfile; /* program filenames from -f's */
48static size_t maxpfile; /* max program filename */
49static size_t npfile; /* number of filenames */
50static size_t curpfile; /* current filename */
Brian Kernighan87b94932012-12-22 10:35:39 -050051
Arnold D. Robbins108224b2019-11-10 21:19:18 +020052bool safe = false; /* true => "safe" mode */
Brian Kernighan87b94932012-12-22 10:35:39 -050053
awkfan77bb538fe2020-04-05 19:10:52 +010054static noreturn void fpecatch(int n
zoulasc94e4c042020-02-18 14:20:27 -050055#ifdef SA_SIGINFO
56 , siginfo_t *si, void *uc
57#endif
58)
Arnold D. Robbinsed6ff8c2020-02-18 21:26:24 +020059{
zoulasc94e4c042020-02-18 14:20:27 -050060#ifdef SA_SIGINFO
61 static const char *emsg[] = {
Arnold D. Robbinsed6ff8c2020-02-18 21:26:24 +020062 [0] = "Unknown error",
63 [FPE_INTDIV] = "Integer divide by zero",
64 [FPE_INTOVF] = "Integer overflow",
65 [FPE_FLTDIV] = "Floating point divide by zero",
66 [FPE_FLTOVF] = "Floating point overflow",
67 [FPE_FLTUND] = "Floating point underflow",
68 [FPE_FLTRES] = "Floating point inexact result",
69 [FPE_FLTINV] = "Invalid Floating point operation",
70 [FPE_FLTSUB] = "Subscript out of range",
zoulasc94e4c042020-02-18 14:20:27 -050071 };
72#endif
73 FATAL("floating point exception"
74#ifdef SA_SIGINFO
Arnold D. Robbinsed6ff8c2020-02-18 21:26:24 +020075 ": %s", (size_t)si->si_code < sizeof(emsg) / sizeof(emsg[0]) &&
76 emsg[si->si_code] ? emsg[si->si_code] : emsg[0]
zoulasc94e4c042020-02-18 14:20:27 -050077#endif
78 );
79}
80
Brian Kernighan3ed9e242018-08-15 10:45:03 -040081/* Can this work with recursive calls? I don't think so.
82void segvcatch(int n)
83{
84 FATAL("segfault. Do you have an unbounded recursive call?", n);
85}
86*/
87
zoulasc94e4c042020-02-18 14:20:27 -050088static const char *
89setfs(char *p)
90{
91 /* wart: t=>\t */
92 if (p[0] == 't' && p[1] == '\0')
93 return "\t";
94 else if (p[0] != '\0')
95 return p;
96 return NULL;
97}
98
99static char *
100getarg(int *argc, char ***argv, const char *msg)
101{
102 if ((*argv)[1][2] != '\0') { /* arg is -fsomething */
103 return &(*argv)[1][2];
104 } else { /* arg is -f something */
105 (*argc)--; (*argv)++;
106 if (*argc <= 1)
107 FATAL("%s", msg);
108 return (*argv)[1];
109 }
110}
111
Brian Kernighan87b94932012-12-22 10:35:39 -0500112int main(int argc, char *argv[])
113{
114 const char *fs = NULL;
zoulasc94e4c042020-02-18 14:20:27 -0500115 char *fn, *vn;
Brian Kernighan87b94932012-12-22 10:35:39 -0500116
117 setlocale(LC_CTYPE, "");
118 setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
119 cmdname = argv[0];
120 if (argc == 1) {
Arnold D. Robbins795a06b2019-07-28 05:51:52 -0600121 fprintf(stderr,
122 "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
Brian Kernighan87b94932012-12-22 10:35:39 -0500123 cmdname);
124 exit(1);
125 }
zoulasc94e4c042020-02-18 14:20:27 -0500126#ifdef SA_SIGINFO
127 {
128 struct sigaction sa;
129 sa.sa_sigaction = fpecatch;
130 sa.sa_flags = SA_SIGINFO;
131 sigemptyset(&sa.sa_mask);
132 (void)sigaction(SIGFPE, &sa, NULL);
133 }
134#else
135 (void)signal(SIGFPE, fpecatch);
136#endif
Brian Kernighan3ed9e242018-08-15 10:45:03 -0400137 /*signal(SIGSEGV, segvcatch); experiment */
Brian Kernighan87b94932012-12-22 10:35:39 -0500138
zoulasc94e4c042020-02-18 14:20:27 -0500139 /* Set and keep track of the random seed */
Brian Kernighan87b94932012-12-22 10:35:39 -0500140 srand_seed = 1;
pfgc70b9fe2014-09-19 18:24:02 +0000141 srandom((unsigned long) srand_seed);
Brian Kernighan87b94932012-12-22 10:35:39 -0500142
143 yyin = NULL;
144 symtab = makesymtab(NSYMTAB/NSYMTAB);
145 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
Arnold D. Robbinsed6ff8c2020-02-18 21:26:24 +0200146 if (strcmp(argv[1], "-version") == 0 || strcmp(argv[1], "--version") == 0) {
Brian Kernighan87b94932012-12-22 10:35:39 -0500147 printf("awk %s\n", version);
zoulasc94e4c042020-02-18 14:20:27 -0500148 return 0;
Brian Kernighan87b94932012-12-22 10:35:39 -0500149 }
Cody Peter Mello9b093ea2018-10-09 11:46:57 -0700150 if (strcmp(argv[1], "--") == 0) { /* explicit end of args */
Brian Kernighan87b94932012-12-22 10:35:39 -0500151 argc--;
152 argv++;
153 break;
154 }
155 switch (argv[1][1]) {
156 case 's':
157 if (strcmp(argv[1], "-safe") == 0)
Arnold D. Robbins108224b2019-11-10 21:19:18 +0200158 safe = true;
Brian Kernighan87b94932012-12-22 10:35:39 -0500159 break;
160 case 'f': /* next argument is program filename */
zoulasc94e4c042020-02-18 14:20:27 -0500161 fn = getarg(&argc, &argv, "no program filename");
162 if (npfile >= maxpfile) {
163 maxpfile += 20;
Arnold D. Robbins3b42cfa2020-10-13 20:52:43 +0300164 pfile = (char **) realloc(pfile, maxpfile * sizeof(*pfile));
zoulasc94e4c042020-02-18 14:20:27 -0500165 if (pfile == NULL)
Arnold D. Robbinsed6ff8c2020-02-18 21:26:24 +0200166 FATAL("error allocating space for -f options");
zoulasc94e4c042020-02-18 14:20:27 -0500167 }
168 pfile[npfile++] = fn;
169 break;
Brian Kernighan87b94932012-12-22 10:35:39 -0500170 case 'F': /* set field separator */
zoulasc94e4c042020-02-18 14:20:27 -0500171 fs = setfs(getarg(&argc, &argv, "no field separator"));
172 if (fs == NULL)
Brian Kernighan87b94932012-12-22 10:35:39 -0500173 WARNING("field separator FS is empty");
174 break;
175 case 'v': /* -v a=1 to be done NOW. one -v for each */
zoulasc94e4c042020-02-18 14:20:27 -0500176 vn = getarg(&argc, &argv, "no variable name");
177 if (isclvar(vn))
178 setclvar(vn);
179 else
180 FATAL("invalid -v option argument: %s", vn);
Brian Kernighan87b94932012-12-22 10:35:39 -0500181 break;
182 case 'd':
183 dbg = atoi(&argv[1][2]);
184 if (dbg == 0)
185 dbg = 1;
186 printf("awk %s\n", version);
187 break;
188 default:
189 WARNING("unknown option %s ignored", argv[1]);
190 break;
191 }
192 argc--;
193 argv++;
194 }
195 /* argv[1] is now the first argument */
196 if (npfile == 0) { /* no -f; first argument is program */
197 if (argc <= 1) {
198 if (dbg)
199 exit(0);
200 FATAL("no program given");
201 }
Todd C. Miller292d39f2020-06-25 12:32:34 -0600202 DPRINTF("program = |%s|\n", argv[1]);
Brian Kernighan87b94932012-12-22 10:35:39 -0500203 lexprog = argv[1];
204 argc--;
205 argv++;
206 }
207 recinit(recsize);
208 syminit();
Arnold D. Robbins108224b2019-11-10 21:19:18 +0200209 compile_time = COMPILING;
Brian Kernighan87b94932012-12-22 10:35:39 -0500210 argv[0] = cmdname; /* put prog name at front of arglist */
Todd C. Miller292d39f2020-06-25 12:32:34 -0600211 DPRINTF("argc=%d, argv[0]=%s\n", argc, argv[0]);
Brian Kernighan87b94932012-12-22 10:35:39 -0500212 arginit(argc, argv);
213 if (!safe)
214 envinit(environ);
215 yyparse();
Arnold D. Robbins2017c2e2020-02-28 13:47:42 +0200216#if 0
217 // Doing this would comply with POSIX, but is not compatible with
218 // other awks and with what most users expect. So comment it out.
219 setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
220#endif
Brian Kernighan87b94932012-12-22 10:35:39 -0500221 if (fs)
222 *FS = qstring(fs, '\0');
Todd C. Miller292d39f2020-06-25 12:32:34 -0600223 DPRINTF("errorflag=%d\n", errorflag);
Brian Kernighan87b94932012-12-22 10:35:39 -0500224 if (errorflag == 0) {
Arnold D. Robbins108224b2019-11-10 21:19:18 +0200225 compile_time = RUNNING;
Brian Kernighan87b94932012-12-22 10:35:39 -0500226 run(winner);
227 } else
228 bracecheck();
229 return(errorflag);
230}
231
232int pgetc(void) /* get 1 character from awk program */
233{
234 int c;
235
236 for (;;) {
237 if (yyin == NULL) {
238 if (curpfile >= npfile)
239 return EOF;
240 if (strcmp(pfile[curpfile], "-") == 0)
241 yyin = stdin;
242 else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
243 FATAL("can't open file %s", pfile[curpfile]);
244 lineno = 1;
245 }
246 if ((c = getc(yyin)) != EOF)
247 return c;
248 if (yyin != stdin)
249 fclose(yyin);
250 yyin = NULL;
251 curpfile++;
252 }
253}
254
255char *cursource(void) /* current source file name */
256{
257 if (npfile > 0)
Todd C. Miller453ce862020-07-29 12:31:29 -0600258 return pfile[curpfile < npfile ? curpfile : curpfile - 1];
Brian Kernighan87b94932012-12-22 10:35:39 -0500259 else
260 return NULL;
261}