Annotation of src/usr.bin/awk/main.c, Revision 1.8
1.8 ! millert 1: /* $OpenBSD: main.c,v 1.7 1999/04/20 17:31:30 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.7 millert 26: char *version = "version 19990416";
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:
1.8 ! millert 38: #define MAX_PFILE 20
! 39:
1.1 tholo 40: extern char **environ;
41: extern int nfields;
1.6 millert 42: extern char *__progname;
1.1 tholo 43:
44: int dbg = 0;
45: char *cmdname; /* gets argv[0] for error messages */
46: extern FILE *yyin; /* lex input file */
47: char *lexprog; /* points to program argument if it exists */
48: extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
49: int compile_time = 2; /* for error printing: */
50: /* 2 = cmdline, 1 = compile, 0 = running */
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: {
60: char *fs = NULL, *marg;
61: int temp;
1.2 tholo 62:
63: setlocale(LC_ALL, "");
1.1 tholo 64:
1.6 millert 65: cmdname = __progname;
1.1 tholo 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++;
1.8 ! millert 87: if (npfile >= MAX_PFILE - 1)
! 88: ERROR "too many -f options" FATAL;
1.1 tholo 89: if (argc <= 1)
90: ERROR "no program filename" FATAL;
91: pfile[npfile++] = argv[1];
92: break;
93: case 'F': /* set field separator */
94: if (argv[1][2] != 0) { /* arg is -Fsomething */
95: if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
1.4 kstailey 96: fs = "\t";
1.1 tholo 97: else if (argv[1][2] != 0)
98: fs = &argv[1][2];
99: } else { /* arg is -F something */
100: argc--; argv++;
101: if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
1.4 kstailey 102: fs = "\t";
1.1 tholo 103: else if (argc > 1 && argv[1][0] != 0)
104: fs = &argv[1][0];
105: }
106: if (fs == NULL || *fs == '\0')
107: ERROR "field separator FS is empty" WARNING;
108: break;
109: case 'v': /* -v a=1 to be done NOW. one -v for each */
110: if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
111: setclvar(argv[1]);
112: break;
113: case 'm': /* more memory: -mr=record, -mf=fields */
1.4 kstailey 114: /* no longer needed */
1.1 tholo 115: marg = argv[1];
116: if (argv[1][3])
117: temp = atoi(&argv[1][3]);
118: else {
119: argv++; argc--;
120: temp = atoi(&argv[1][0]);
121: }
122: switch (marg[2]) {
123: case 'r': recsize = temp; break;
124: case 'f': nfields = temp; break;
125: default: ERROR "unknown option %s\n", marg FATAL;
126: }
127: break;
128: case 'd':
129: dbg = atoi(&argv[1][2]);
130: if (dbg == 0)
131: dbg = 1;
132: printf("awk %s\n", version);
1.6 millert 133: break;
134: case 'V': /* added for exptools "standard" */
135: printf("awk %s\n", version);
136: exit(0);
1.1 tholo 137: break;
138: default:
139: ERROR "unknown option %s ignored", argv[1] WARNING;
140: break;
141: }
142: argc--;
143: argv++;
144: }
145: /* argv[1] is now the first argument */
146: if (npfile == 0) { /* no -f; first argument is program */
147: if (argc <= 1) {
148: if (dbg)
149: exit(0);
150: ERROR "no program given" FATAL;
151: }
1.4 kstailey 152: dprintf( ("program = |%s|\n", argv[1]) );
1.1 tholo 153: lexprog = argv[1];
154: argc--;
155: argv++;
156: }
157: recinit(recsize);
158: syminit();
159: compile_time = 1;
160: argv[0] = cmdname; /* put prog name at front of arglist */
1.4 kstailey 161: dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
1.1 tholo 162: arginit(argc, argv);
1.4 kstailey 163: if (!safe)
164: envinit(environ);
1.1 tholo 165: yyparse();
166: if (fs)
1.4 kstailey 167: *FS = qstring(fs, '\0');
168: dprintf( ("errorflag=%d\n", errorflag) );
1.1 tholo 169: if (errorflag == 0) {
170: compile_time = 0;
171: run(winner);
172: } else
173: bracecheck();
174: return(errorflag);
175: }
176:
177: int pgetc(void) /* get 1 character from awk program */
178: {
179: int c;
180:
181: for (;;) {
182: if (yyin == NULL) {
183: if (curpfile >= npfile)
184: return EOF;
1.4 kstailey 185: if (strcmp(pfile[curpfile], "-") == 0)
1.1 tholo 186: yyin = stdin;
1.4 kstailey 187: else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
1.1 tholo 188: ERROR "can't open file %s", pfile[curpfile] FATAL;
1.7 millert 189: lineno = 1;
1.1 tholo 190: }
191: if ((c = getc(yyin)) != EOF)
192: return c;
193: if (yyin != stdin)
194: fclose(yyin);
195: yyin = NULL;
196: curpfile++;
197: }
1.7 millert 198: }
199:
200: char *cursource(void) /* current source file name */
201: {
202: if (npfile > 0)
203: return pfile[curpfile];
204: else
205: return NULL;
1.1 tholo 206: }