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