Annotation of src/usr.bin/awk/main.c, Revision 1.2
1.1 tholo 1: /****************************************************************
2: Copyright (C) AT&T and Lucent Technologies 1996
3: All Rights Reserved
4:
5: Permission to use, copy, modify, and distribute this software and
6: its documentation for any purpose and without fee is hereby
7: granted, provided that the above copyright notice appear in all
8: copies and that both that the copyright notice and this
9: permission notice and warranty disclaimer appear in supporting
10: documentation, and that the names of AT&T or Lucent Technologies
11: or any of their entities not be used in advertising or publicity
12: pertaining to distribution of the software without specific,
13: written prior permission.
14:
15: AT&T AND LUCENT DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16: SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17: FITNESS. IN NO EVENT SHALL AT&T OR LUCENT OR ANY OF THEIR
18: ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
19: DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20: DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21: OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
22: USE OR PERFORMANCE OF THIS SOFTWARE.
23: ****************************************************************/
24:
25: char *version = "version May 27, 1996";
26:
27: #define DEBUG
28: #include <stdio.h>
29: #include <ctype.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <signal.h>
1.2 ! tholo 33: #include <locale.h>
1.1 tholo 34: #include "awk.h"
35: #include "awkgram.h"
36:
37: extern char **environ;
38: extern int nfields;
39:
40: int dbg = 0;
41: char *cmdname; /* gets argv[0] for error messages */
42: extern FILE *yyin; /* lex input file */
43: char *lexprog; /* points to program argument if it exists */
44: extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
45: int compile_time = 2; /* for error printing: */
46: /* 2 = cmdline, 1 = compile, 0 = running */
47:
48: char *pfile[20]; /* program filenames from -f's */
49: int npfile = 0; /* number of filenames */
50: int curpfile = 0; /* current filename */
51:
52: int main(int argc, char *argv[])
53: {
54: char *fs = NULL, *marg;
55: int temp;
1.2 ! tholo 56:
! 57: setlocale(LC_ALL, "");
1.1 tholo 58:
59: if ((cmdname = strrchr(argv[0], '/')) != NULL)
60: cmdname++;
61: else
62: cmdname = argv[0];
63: if (argc == 1) {
64: fprintf(stderr, "Usage: %s [-f programfile | 'program'] [-Ffieldsep] [-v var=value] [-mf n] [-mr n] [files]\n", cmdname);
65: exit(1);
66: }
67: gs = newGstring(); /* for lex */
68: signal(SIGFPE, fpecatch);
69: yyin = NULL;
70: symtab = makesymtab(NSYMTAB);
71: while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
72: if (strcmp((char *) argv[1], "--") == 0) { /* explicit end of args */
73: argc--;
74: argv++;
75: break;
76: }
77: switch (argv[1][1]) {
78: case 'f': /* next argument is program filename */
79: argc--;
80: argv++;
81: if (argc <= 1)
82: ERROR "no program filename" FATAL;
83: pfile[npfile++] = argv[1];
84: break;
85: case 'F': /* set field separator */
86: if (argv[1][2] != 0) { /* arg is -Fsomething */
87: if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
88: fs = (char *) "\t";
89: else if (argv[1][2] != 0)
90: fs = &argv[1][2];
91: } else { /* arg is -F something */
92: argc--; argv++;
93: if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
94: fs = (char *) "\t";
95: else if (argc > 1 && argv[1][0] != 0)
96: fs = &argv[1][0];
97: }
98: if (fs == NULL || *fs == '\0')
99: ERROR "field separator FS is empty" WARNING;
100: break;
101: case 'v': /* -v a=1 to be done NOW. one -v for each */
102: if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
103: setclvar(argv[1]);
104: break;
105: case 'm': /* more memory: -mr=record, -mf=fields */
106: marg = argv[1];
107: if (argv[1][3])
108: temp = atoi(&argv[1][3]);
109: else {
110: argv++; argc--;
111: temp = atoi(&argv[1][0]);
112: }
113: switch (marg[2]) {
114: case 'r': recsize = temp; break;
115: case 'f': nfields = temp; break;
116: default: ERROR "unknown option %s\n", marg FATAL;
117: }
118: break;
119: case 'd':
120: dbg = atoi(&argv[1][2]);
121: if (dbg == 0)
122: dbg = 1;
123: printf("awk %s\n", version);
124: break;
125: default:
126: ERROR "unknown option %s ignored", argv[1] WARNING;
127: break;
128: }
129: argc--;
130: argv++;
131: }
132: /* argv[1] is now the first argument */
133: if (npfile == 0) { /* no -f; first argument is program */
134: if (argc <= 1) {
135: if (dbg)
136: exit(0);
137: ERROR "no program given" FATAL;
138: }
139: dprintf( ("program = |%s|\n", argv[1]) );
140: lexprog = argv[1];
141: argc--;
142: argv++;
143: }
144: recinit(recsize);
145: syminit();
146: compile_time = 1;
147: argv[0] = cmdname; /* put prog name at front of arglist */
148: dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
149: arginit(argc, argv);
150: envinit(environ);
151: yyparse();
152: if (fs)
153: *FS = tostring(qstring(fs, '\0'));
154: dprintf( ("errorflag=%d\n", errorflag) );
155: if (errorflag == 0) {
156: compile_time = 0;
157: run(winner);
158: } else
159: bracecheck();
160: return(errorflag);
161: }
162:
163: int pgetc(void) /* get 1 character from awk program */
164: {
165: int c;
166:
167: for (;;) {
168: if (yyin == NULL) {
169: if (curpfile >= npfile)
170: return EOF;
171: if (strcmp((char *) pfile[curpfile], "-") == 0)
172: yyin = stdin;
173: else if ((yyin = fopen((char *) pfile[curpfile], "r")) == NULL)
174: ERROR "can't open file %s", pfile[curpfile] FATAL;
175: }
176: if ((c = getc(yyin)) != EOF)
177: return c;
178: if (yyin != stdin)
179: fclose(yyin);
180: yyin = NULL;
181: curpfile++;
182: }
183: }