1 63a68686 2008-11-03 jas /****************************************************************
2 63a68686 2008-11-03 jas Copyright (C) Lucent Technologies 1997
3 63a68686 2008-11-03 jas All Rights Reserved
5 63a68686 2008-11-03 jas Permission to use, copy, modify, and distribute this software and
6 63a68686 2008-11-03 jas its documentation for any purpose and without fee is hereby
7 63a68686 2008-11-03 jas granted, provided that the above copyright notice appear in all
8 63a68686 2008-11-03 jas copies and that both that the copyright notice and this
9 63a68686 2008-11-03 jas permission notice and warranty disclaimer appear in supporting
10 63a68686 2008-11-03 jas documentation, and that the name Lucent Technologies or any of
11 63a68686 2008-11-03 jas its entities not be used in advertising or publicity pertaining
12 63a68686 2008-11-03 jas to distribution of the software without specific, written prior
15 63a68686 2008-11-03 jas LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 63a68686 2008-11-03 jas INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 63a68686 2008-11-03 jas IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 63a68686 2008-11-03 jas SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 63a68686 2008-11-03 jas WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 63a68686 2008-11-03 jas IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 63a68686 2008-11-03 jas ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22 63a68686 2008-11-03 jas THIS SOFTWARE.
23 63a68686 2008-11-03 jas ****************************************************************/
25 63a68686 2008-11-03 jas char *version = "version 19990602";
27 63a68686 2008-11-03 jas #define DEBUG
28 63a68686 2008-11-03 jas #include <stdio.h>
29 63a68686 2008-11-03 jas #include <ctype.h>
30 63a68686 2008-11-03 jas #include <stdlib.h>
31 63a68686 2008-11-03 jas #include <string.h>
32 63a68686 2008-11-03 jas #include <signal.h>
33 63a68686 2008-11-03 jas #include "awk.h"
34 63a68686 2008-11-03 jas #include "y.tab.h"
36 63a68686 2008-11-03 jas extern char **environ;
37 63a68686 2008-11-03 jas extern int nfields;
40 63a68686 2008-11-03 jas char *cmdname; /* gets argv[0] for error messages */
41 63a68686 2008-11-03 jas extern FILE *yyin; /* lex input file */
42 63a68686 2008-11-03 jas char *lexprog; /* points to program argument if it exists */
43 63a68686 2008-11-03 jas extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
44 63a68686 2008-11-03 jas int compile_time = 2; /* for error printing: */
45 63a68686 2008-11-03 jas /* 2 = cmdline, 1 = compile, 0 = running */
47 63a68686 2008-11-03 jas char *pfile[20]; /* program filenames from -f's */
48 63a68686 2008-11-03 jas int npfile = 0; /* number of filenames */
49 63a68686 2008-11-03 jas int curpfile = 0; /* current filename */
51 63a68686 2008-11-03 jas int safe = 0; /* 1 => "safe" mode */
53 63a68686 2008-11-03 jas int main(int argc, char *argv[])
55 63a68686 2008-11-03 jas char *fs = NULL, *marg;
58 63a68686 2008-11-03 jas cmdname = argv[0];
59 63a68686 2008-11-03 jas if (argc == 1) {
60 63a68686 2008-11-03 jas fprintf(stderr, "Usage: %s [-F fieldsep] [-mf n] [-mr n] [-v var=value] [-f programfile | 'program'] [file ...]\n", cmdname);
63 63a68686 2008-11-03 jas signal(SIGFPE, fpecatch);
65 63a68686 2008-11-03 jas symtab = makesymtab(NSYMTAB);
66 63a68686 2008-11-03 jas while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
67 63a68686 2008-11-03 jas if (strcmp(argv[1], "--") == 0) { /* explicit end of args */
72 63a68686 2008-11-03 jas switch (argv[1][1]) {
74 63a68686 2008-11-03 jas if (strcmp(argv[1], "-safe") == 0)
77 63a68686 2008-11-03 jas case 'f': /* next argument is program filename */
80 63a68686 2008-11-03 jas if (argc <= 1)
81 63a68686 2008-11-03 jas FATAL("no program filename");
82 63a68686 2008-11-03 jas pfile[npfile++] = argv[1];
84 63a68686 2008-11-03 jas case 'F': /* set field separator */
85 63a68686 2008-11-03 jas if (argv[1][2] != 0) { /* arg is -Fsomething */
86 63a68686 2008-11-03 jas if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
88 63a68686 2008-11-03 jas else if (argv[1][2] != 0)
89 63a68686 2008-11-03 jas fs = &argv[1][2];
90 63a68686 2008-11-03 jas } else { /* arg is -F something */
91 63a68686 2008-11-03 jas argc--; argv++;
92 63a68686 2008-11-03 jas if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
94 63a68686 2008-11-03 jas else if (argc > 1 && argv[1][0] != 0)
95 63a68686 2008-11-03 jas fs = &argv[1][0];
97 63a68686 2008-11-03 jas if (fs == NULL || *fs == '\0')
98 63a68686 2008-11-03 jas WARNING("field separator FS is empty");
100 63a68686 2008-11-03 jas case 'v': /* -v a=1 to be done NOW. one -v for each */
101 63a68686 2008-11-03 jas if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
102 63a68686 2008-11-03 jas setclvar(argv[1]);
104 63a68686 2008-11-03 jas case 'm': /* more memory: -mr=record, -mf=fields */
105 63a68686 2008-11-03 jas /* no longer needed */
106 63a68686 2008-11-03 jas marg = argv[1];
107 63a68686 2008-11-03 jas if (argv[1][3])
108 63a68686 2008-11-03 jas temp = atoi(&argv[1][3]);
110 63a68686 2008-11-03 jas argv++; argc--;
111 63a68686 2008-11-03 jas temp = atoi(&argv[1][0]);
113 63a68686 2008-11-03 jas switch (marg[2]) {
114 63a68686 2008-11-03 jas case 'r': recsize = temp; break;
115 63a68686 2008-11-03 jas case 'f': nfields = temp; break;
116 63a68686 2008-11-03 jas default: FATAL("unknown option %s\n", marg);
120 63a68686 2008-11-03 jas dbg = atoi(&argv[1][2]);
121 63a68686 2008-11-03 jas if (dbg == 0)
123 63a68686 2008-11-03 jas printf("awk %s\n", version);
125 63a68686 2008-11-03 jas case 'V': /* added for exptools "standard" */
126 63a68686 2008-11-03 jas printf("awk %s\n", version);
130 63a68686 2008-11-03 jas WARNING("unknown option %s ignored", argv[1]);
136 63a68686 2008-11-03 jas /* argv[1] is now the first argument */
137 63a68686 2008-11-03 jas if (npfile == 0) { /* no -f; first argument is program */
138 63a68686 2008-11-03 jas if (argc <= 1) {
141 63a68686 2008-11-03 jas FATAL("no program given");
143 63a68686 2008-11-03 jas dprintf( ("program = |%s|\n", argv[1]) );
144 63a68686 2008-11-03 jas lexprog = argv[1];
148 63a68686 2008-11-03 jas recinit(recsize);
150 63a68686 2008-11-03 jas compile_time = 1;
151 63a68686 2008-11-03 jas argv[0] = cmdname; /* put prog name at front of arglist */
152 63a68686 2008-11-03 jas dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
153 63a68686 2008-11-03 jas arginit(argc, argv);
155 63a68686 2008-11-03 jas envinit(environ);
158 63a68686 2008-11-03 jas *FS = qstring(fs, '\0');
159 63a68686 2008-11-03 jas dprintf( ("errorflag=%d\n", errorflag) );
160 63a68686 2008-11-03 jas if (errorflag == 0) {
161 63a68686 2008-11-03 jas compile_time = 0;
162 63a68686 2008-11-03 jas run(winner);
164 63a68686 2008-11-03 jas bracecheck();
165 63a68686 2008-11-03 jas return(errorflag);
168 63a68686 2008-11-03 jas int pgetc(void) /* get 1 character from awk program */
173 63a68686 2008-11-03 jas if (yyin == NULL) {
174 63a68686 2008-11-03 jas if (curpfile >= npfile)
176 63a68686 2008-11-03 jas if (strcmp(pfile[curpfile], "-") == 0)
177 63a68686 2008-11-03 jas yyin = stdin;
178 63a68686 2008-11-03 jas else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
179 63a68686 2008-11-03 jas FATAL("can't open file %s", pfile[curpfile]);
182 63a68686 2008-11-03 jas if ((c = getc(yyin)) != EOF)
184 63a68686 2008-11-03 jas if (yyin != stdin)
185 63a68686 2008-11-03 jas fclose(yyin);
186 63a68686 2008-11-03 jas yyin = NULL;
191 63a68686 2008-11-03 jas char *cursource(void) /* current source file name */
193 63a68686 2008-11-03 jas if (npfile > 0)
194 63a68686 2008-11-03 jas return pfile[curpfile];
196 63a68686 2008-11-03 jas return NULL;