Annotation of src/usr.bin/awk/main.c, Revision 1.15
1.15 ! millert 1: /* $OpenBSD: main.c,v 1.14 2007/01/29 19:40:06 jmc 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.15 ! millert 26: const char *version = "version 20071023";
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>
35: #include "awk.h"
1.4 kstailey 36: #include "ytab.h"
1.1 tholo 37:
38: extern char **environ;
39: extern int nfields;
1.6 millert 40: extern char *__progname;
1.1 tholo 41:
42: int dbg = 0;
43: char *cmdname; /* gets argv[0] for error messages */
44: extern FILE *yyin; /* lex input file */
45: char *lexprog; /* points to program argument if it exists */
46: extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
47: int compile_time = 2; /* for error printing: */
48: /* 2 = cmdline, 1 = compile, 0 = running */
49:
1.15 ! millert 50: #define MAX_PFILE 20 /* max number of -f's */
! 51:
1.8 millert 52: char *pfile[MAX_PFILE]; /* program filenames from -f's */
1.1 tholo 53: int npfile = 0; /* number of filenames */
54: int curpfile = 0; /* current filename */
55:
1.4 kstailey 56: int safe = 0; /* 1 => "safe" mode */
57:
1.1 tholo 58: int main(int argc, char *argv[])
59: {
1.12 millert 60: const char *fs = NULL;
1.2 tholo 61:
62: setlocale(LC_ALL, "");
1.13 millert 63: setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
1.6 millert 64: cmdname = __progname;
1.1 tholo 65: if (argc == 1) {
1.14 jmc 66: fprintf(stderr, "usage: %s [-safe] [-V] [-d[n]] [-F fs] "
67: "[-v var=value] [prog | -f progfile]\n\tfile ...\n",
1.10 aaron 68: cmdname);
1.1 tholo 69: exit(1);
70: }
71: signal(SIGFPE, fpecatch);
72: yyin = NULL;
73: symtab = makesymtab(NSYMTAB);
74: while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
1.4 kstailey 75: if (strcmp(argv[1], "--") == 0) { /* explicit end of args */
1.1 tholo 76: argc--;
77: argv++;
78: break;
79: }
80: switch (argv[1][1]) {
1.4 kstailey 81: case 's':
82: if (strcmp(argv[1], "-safe") == 0)
83: safe = 1;
84: break;
1.1 tholo 85: case 'f': /* next argument is program filename */
86: argc--;
87: argv++;
1.8 millert 88: if (npfile >= MAX_PFILE - 1)
1.9 millert 89: FATAL("too many -f options");
1.1 tholo 90: if (argc <= 1)
1.9 millert 91: FATAL("no program filename");
1.15 ! millert 92: if (npfile >= MAX_PFILE - 1)
! 93: FATAL("too many -f options");
1.1 tholo 94: pfile[npfile++] = argv[1];
95: break;
96: case 'F': /* set field separator */
97: if (argv[1][2] != 0) { /* arg is -Fsomething */
98: if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
1.4 kstailey 99: fs = "\t";
1.1 tholo 100: else if (argv[1][2] != 0)
101: fs = &argv[1][2];
102: } else { /* arg is -F something */
103: argc--; argv++;
104: if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
1.4 kstailey 105: fs = "\t";
1.1 tholo 106: else if (argc > 1 && argv[1][0] != 0)
107: fs = &argv[1][0];
108: }
109: if (fs == NULL || *fs == '\0')
1.9 millert 110: WARNING("field separator FS is empty");
1.1 tholo 111: break;
112: case 'v': /* -v a=1 to be done NOW. one -v for each */
113: if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
114: setclvar(argv[1]);
115: break;
116: case 'd':
117: dbg = atoi(&argv[1][2]);
118: if (dbg == 0)
119: dbg = 1;
120: printf("awk %s\n", version);
1.6 millert 121: break;
122: case 'V': /* added for exptools "standard" */
123: printf("awk %s\n", version);
124: exit(0);
1.1 tholo 125: break;
126: default:
1.9 millert 127: WARNING("unknown option %s ignored", argv[1]);
1.1 tholo 128: break;
129: }
130: argc--;
131: argv++;
132: }
133: /* argv[1] is now the first argument */
134: if (npfile == 0) { /* no -f; first argument is program */
135: if (argc <= 1) {
136: if (dbg)
137: exit(0);
1.9 millert 138: FATAL("no program given");
1.1 tholo 139: }
1.4 kstailey 140: dprintf( ("program = |%s|\n", argv[1]) );
1.1 tholo 141: lexprog = argv[1];
142: argc--;
143: argv++;
144: }
145: recinit(recsize);
146: syminit();
147: compile_time = 1;
148: argv[0] = cmdname; /* put prog name at front of arglist */
1.4 kstailey 149: dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
1.1 tholo 150: arginit(argc, argv);
1.4 kstailey 151: if (!safe)
152: envinit(environ);
1.1 tholo 153: yyparse();
1.13 millert 154: setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
1.1 tholo 155: if (fs)
1.4 kstailey 156: *FS = qstring(fs, '\0');
157: dprintf( ("errorflag=%d\n", errorflag) );
1.1 tholo 158: if (errorflag == 0) {
159: compile_time = 0;
160: run(winner);
161: } else
162: bracecheck();
163: return(errorflag);
164: }
165:
166: int pgetc(void) /* get 1 character from awk program */
167: {
168: int c;
169:
170: for (;;) {
171: if (yyin == NULL) {
172: if (curpfile >= npfile)
173: return EOF;
1.4 kstailey 174: if (strcmp(pfile[curpfile], "-") == 0)
1.1 tholo 175: yyin = stdin;
1.4 kstailey 176: else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
1.9 millert 177: FATAL("can't open file %s", pfile[curpfile]);
1.7 millert 178: lineno = 1;
1.1 tholo 179: }
180: if ((c = getc(yyin)) != EOF)
181: return c;
182: if (yyin != stdin)
183: fclose(yyin);
184: yyin = NULL;
185: curpfile++;
186: }
1.7 millert 187: }
188:
189: char *cursource(void) /* current source file name */
190: {
191: if (npfile > 0)
192: return pfile[curpfile];
193: else
194: return NULL;
1.1 tholo 195: }