Annotation of src/usr.bin/awk/main.c, Revision 1.60
1.60 ! jmc 1: /* $OpenBSD: main.c,v 1.59 2023/09/17 14:49:44 millert Exp $ */
1.1 tholo 2: /****************************************************************
1.4 kstailey 3: Copyright (C) Lucent Technologies 1997
1.1 tholo 4: All Rights Reserved
5:
6: Permission to use, copy, modify, and distribute this software and
7: its documentation for any purpose and without fee is hereby
8: granted, provided that the above copyright notice appear in all
9: copies and that both that the copyright notice and this
10: permission notice and warranty disclaimer appear in supporting
1.4 kstailey 11: documentation, and that the name Lucent Technologies or any of
12: its entities not be used in advertising or publicity pertaining
13: to distribution of the software without specific, written prior
14: permission.
15:
16: LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
18: IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
19: SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
21: IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23: THIS SOFTWARE.
1.1 tholo 24: ****************************************************************/
25:
1.59 millert 26: const char *version = "version 20230913";
1.1 tholo 27:
28: #define DEBUG
29: #include <stdio.h>
30: #include <ctype.h>
1.3 millert 31: #include <locale.h>
1.1 tholo 32: #include <stdlib.h>
33: #include <string.h>
34: #include <signal.h>
1.18 doug 35: #include <unistd.h>
1.1 tholo 36: #include "awk.h"
37:
38: extern char **environ;
39: extern int nfields;
1.6 millert 40: extern char *__progname;
1.1 tholo 41:
42: int dbg = 0;
1.17 millert 43: Awkfloat srand_seed = 1;
1.1 tholo 44: char *cmdname; /* gets argv[0] for error messages */
45: extern FILE *yyin; /* lex input file */
46: char *lexprog; /* points to program argument if it exists */
47: extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
1.34 millert 48: enum compile_states compile_time = ERROR_PRINTING;
1.1 tholo 49:
1.37 millert 50: static char **pfile; /* program filenames from -f's */
51: static size_t maxpfile; /* max program filename */
52: static size_t npfile; /* number of filenames */
53: static size_t curpfile; /* current filename */
1.15 millert 54:
1.59 millert 55: bool CSV = false; /* true for csv input */
1.40 millert 56: bool safe = false; /* true => "safe" mode */
57: bool do_posix = false; /* true => POSIX mode */
1.37 millert 58:
1.38 millert 59: static noreturn void fpecatch(int n
1.37 millert 60: #ifdef SA_SIGINFO
61: , siginfo_t *si, void *uc
62: #endif
63: )
64: {
65: extern Node *curnode;
66: #ifdef SA_SIGINFO
67: static const char *emsg[] = {
68: [0] = "Unknown error",
69: [FPE_INTDIV] = "Integer divide by zero",
70: [FPE_INTOVF] = "Integer overflow",
71: [FPE_FLTDIV] = "Floating point divide by zero",
72: [FPE_FLTOVF] = "Floating point overflow",
73: [FPE_FLTUND] = "Floating point underflow",
74: [FPE_FLTRES] = "Floating point inexact result",
75: [FPE_FLTINV] = "Invalid Floating point operation",
76: [FPE_FLTSUB] = "Subscript out of range",
77: };
78: #endif
79: dprintf(STDERR_FILENO, "floating point exception%s%s\n",
80: #ifdef SA_SIGINFO
81: ": ", (size_t)si->si_code < sizeof(emsg) / sizeof(emsg[0]) &&
82: emsg[si->si_code] ? emsg[si->si_code] : emsg[0]
83: #else
84: "", ""
85: #endif
86: );
87:
88: if (compile_time != 2 && NR && *NR > 0) {
89: dprintf(STDERR_FILENO, " input record number %d", (int) (*FNR));
90: if (strcmp(*FILENAME, "-") != 0) {
91: dprintf(STDERR_FILENO, ", file %s", *FILENAME);
92: }
93: dprintf(STDERR_FILENO, "\n");
94: }
95: if (compile_time != 2 && curnode) {
96: dprintf(STDERR_FILENO, " source line number %d", curnode->lineno);
97: } else if (compile_time != 2 && lineno) {
98: dprintf(STDERR_FILENO, " source line number %d", lineno);
99: }
100: if (compile_time == 1 && cursource() != NULL) {
101: dprintf(STDERR_FILENO, " source file %s", cursource());
102: }
103: dprintf(STDERR_FILENO, "\n");
104: if (dbg > 1) /* core dump if serious debugging on */
105: abort();
106: _exit(2);
107: }
1.1 tholo 108:
1.37 millert 109: static const char *
110: setfs(char *p)
111: {
112: /* wart: t=>\t */
113: if (p[0] == 't' && p[1] == '\0')
114: return "\t";
1.48 millert 115: return p;
1.37 millert 116: }
117:
118: static char *
119: getarg(int *argc, char ***argv, const char *msg)
120: {
121: if ((*argv)[1][2] != '\0') { /* arg is -fsomething */
122: return &(*argv)[1][2];
123: } else { /* arg is -f something */
124: (*argc)--; (*argv)++;
125: if (*argc <= 1)
126: FATAL("%s", msg);
127: return (*argv)[1];
128: }
129: }
1.4 kstailey 130:
1.1 tholo 131: int main(int argc, char *argv[])
132: {
1.12 millert 133: const char *fs = NULL;
1.37 millert 134: char *fn, *vn;
1.2 tholo 135:
1.39 millert 136: setlocale(LC_CTYPE, "");
1.19 deraadt 137: setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
138:
1.20 tb 139: cmdname = __progname;
1.18 doug 140: if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1) {
141: fprintf(stderr, "%s: pledge: incorrect arguments\n",
142: cmdname);
143: exit(1);
144: }
145:
1.1 tholo 146: if (argc == 1) {
1.60 ! jmc 147: fprintf(stderr, "usage: %s [-safe] [-V] [-d[n]] "
! 148: "[-f fs | --csv] [-v var=value]\n"
! 149: "\t [prog | -f progfile] file ...\n",
1.10 aaron 150: cmdname);
1.1 tholo 151: exit(1);
152: }
1.37 millert 153: #ifdef SA_SIGINFO
154: {
155: struct sigaction sa;
156: sa.sa_sigaction = fpecatch;
157: sa.sa_flags = SA_SIGINFO;
158: sigemptyset(&sa.sa_mask);
159: (void)sigaction(SIGFPE, &sa, NULL);
160: }
161: #else
162: (void)signal(SIGFPE, fpecatch);
163: #endif
1.40 millert 164:
165: do_posix = (getenv("POSIXLY_CORRECT") != NULL);
1.17 millert 166:
1.1 tholo 167: yyin = NULL;
168: symtab = makesymtab(NSYMTAB);
169: while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
1.4 kstailey 170: if (strcmp(argv[1], "--") == 0) { /* explicit end of args */
1.1 tholo 171: argc--;
172: argv++;
173: break;
174: }
1.59 millert 175: if (strcmp(argv[1], "--csv") == 0) { /* turn on csv input processing */
176: CSV = true;
177: argc--;
178: argv++;
179: continue;
180: }
1.1 tholo 181: switch (argv[1][1]) {
1.4 kstailey 182: case 's':
183: if (strcmp(argv[1], "-safe") == 0)
1.34 millert 184: safe = true;
1.4 kstailey 185: break;
1.1 tholo 186: case 'f': /* next argument is program filename */
1.37 millert 187: fn = getarg(&argc, &argv, "no program filename");
188: if (npfile >= maxpfile) {
189: maxpfile += 20;
1.59 millert 190: pfile = (char **) reallocarray(pfile, maxpfile, sizeof(*pfile));
1.37 millert 191: if (pfile == NULL)
192: FATAL("error allocating space for -f options");
193: }
194: pfile[npfile++] = fn;
195: break;
1.1 tholo 196: case 'F': /* set field separator */
1.37 millert 197: fs = setfs(getarg(&argc, &argv, "no field separator"));
1.1 tholo 198: break;
199: case 'v': /* -v a=1 to be done NOW. one -v for each */
1.37 millert 200: vn = getarg(&argc, &argv, "no variable name");
201: if (isclvar(vn))
202: setclvar(vn);
203: else
204: FATAL("invalid -v option argument: %s", vn);
1.1 tholo 205: break;
206: case 'd':
207: dbg = atoi(&argv[1][2]);
208: if (dbg == 0)
209: dbg = 1;
210: printf("awk %s\n", version);
1.6 millert 211: break;
212: case 'V': /* added for exptools "standard" */
213: printf("awk %s\n", version);
214: exit(0);
1.1 tholo 215: break;
216: default:
1.9 millert 217: WARNING("unknown option %s ignored", argv[1]);
1.1 tholo 218: break;
219: }
220: argc--;
221: argv++;
222: }
1.18 doug 223:
224: if (safe) {
225: if (pledge("stdio rpath", NULL) == -1) {
226: fprintf(stderr, "%s: pledge: incorrect arguments\n",
227: cmdname);
228: exit(1);
229: }
230: }
231:
1.1 tholo 232: /* argv[1] is now the first argument */
233: if (npfile == 0) { /* no -f; first argument is program */
234: if (argc <= 1) {
235: if (dbg)
236: exit(0);
1.9 millert 237: FATAL("no program given");
1.1 tholo 238: }
1.42 millert 239: DPRINTF("program = |%s|\n", argv[1]);
1.1 tholo 240: lexprog = argv[1];
241: argc--;
242: argv++;
243: }
244: recinit(recsize);
245: syminit();
1.34 millert 246: compile_time = COMPILING;
1.1 tholo 247: argv[0] = cmdname; /* put prog name at front of arglist */
1.42 millert 248: DPRINTF("argc=%d, argv[0]=%s\n", argc, argv[0]);
1.1 tholo 249: arginit(argc, argv);
1.4 kstailey 250: if (!safe)
251: envinit(environ);
1.1 tholo 252: yyparse();
1.37 millert 253: #if 0
254: // Doing this would comply with POSIX, but is not compatible with
255: // other awks and with what most users expect. So comment it out.
1.13 millert 256: setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
1.37 millert 257: #endif
1.1 tholo 258: if (fs)
1.4 kstailey 259: *FS = qstring(fs, '\0');
1.42 millert 260: DPRINTF("errorflag=%d\n", errorflag);
1.1 tholo 261: if (errorflag == 0) {
1.34 millert 262: compile_time = RUNNING;
1.1 tholo 263: run(winner);
264: } else
265: bracecheck();
266: return(errorflag);
267: }
268:
269: int pgetc(void) /* get 1 character from awk program */
270: {
271: int c;
272:
273: for (;;) {
274: if (yyin == NULL) {
275: if (curpfile >= npfile)
276: return EOF;
1.4 kstailey 277: if (strcmp(pfile[curpfile], "-") == 0)
1.1 tholo 278: yyin = stdin;
1.4 kstailey 279: else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
1.9 millert 280: FATAL("can't open file %s", pfile[curpfile]);
1.7 millert 281: lineno = 1;
1.1 tholo 282: }
283: if ((c = getc(yyin)) != EOF)
284: return c;
285: if (yyin != stdin)
286: fclose(yyin);
287: yyin = NULL;
288: curpfile++;
289: }
1.7 millert 290: }
291:
292: char *cursource(void) /* current source file name */
293: {
294: if (npfile > 0)
1.44 millert 295: return pfile[curpfile < npfile ? curpfile : curpfile - 1];
1.7 millert 296: else
297: return NULL;
1.1 tholo 298: }