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