Delete old versions of awk and unversion directory name.
[dragonfly.git] / contrib / awk / main.c
CommitLineData
e90ffc14
SK
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 20100523";
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
48#define MAX_PFILE 20 /* max number of -f's */
49
50char *pfile[MAX_PFILE]; /* program filenames from -f's */
51int npfile = 0; /* number of filenames */
52int curpfile = 0; /* current filename */
53
54int safe = 0; /* 1 => "safe" mode */
55
56int main(int argc, char *argv[])
57{
58 const char *fs = NULL;
59
60 setlocale(LC_CTYPE, "");
61 setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
62 cmdname = argv[0];
63 if (argc == 1) {
64 fprintf(stderr,
65 "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
66 cmdname);
67 exit(1);
68 }
69 signal(SIGFPE, fpecatch);
70 yyin = NULL;
71 symtab = makesymtab(NSYMTAB/NSYMTAB);
72 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
73 if (strcmp(argv[1],"-version") == 0 || strcmp(argv[1],"--version") == 0) {
74 printf("awk %s\n", version);
75 exit(0);
76 break;
77 }
78 if (strncmp(argv[1], "--", 2) == 0) { /* explicit end of args */
79 argc--;
80 argv++;
81 break;
82 }
83 switch (argv[1][1]) {
84 case 's':
85 if (strcmp(argv[1], "-safe") == 0)
86 safe = 1;
87 break;
88 case 'f': /* next argument is program filename */
89 argc--;
90 argv++;
91 if (argc <= 1)
92 FATAL("no program filename");
93 if (npfile >= MAX_PFILE - 1)
94 FATAL("too many -f options");
95 pfile[npfile++] = argv[1];
96 break;
97 case 'F': /* set field separator */
98 if (argv[1][2] != 0) { /* arg is -Fsomething */
99 if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
100 fs = "\t";
101 else if (argv[1][2] != 0)
102 fs = &argv[1][2];
103 } else { /* arg is -F something */
104 argc--; argv++;
105 if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
106 fs = "\t";
107 else if (argc > 1 && argv[1][0] != 0)
108 fs = &argv[1][0];
109 }
110 if (fs == NULL || *fs == '\0')
111 WARNING("field separator FS is empty");
112 break;
113 case 'v': /* -v a=1 to be done NOW. one -v for each */
114 if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
115 setclvar(argv[1]);
116 else if (argv[1][2] != '\0')
117 setclvar(&argv[1][2]);
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 WARNING("unknown option %s ignored", argv[1]);
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 FATAL("no program given");
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 if (!safe)
151 envinit(environ);
152 yyparse();
153 setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
154 if (fs)
155 *FS = qstring(fs, '\0');
156 dprintf( ("errorflag=%d\n", errorflag) );
157 if (errorflag == 0) {
158 compile_time = 0;
159 run(winner);
160 } else
161 bracecheck();
162 return(errorflag);
163}
164
165int pgetc(void) /* get 1 character from awk program */
166{
167 int c;
168
169 for (;;) {
170 if (yyin == NULL) {
171 if (curpfile >= npfile)
172 return EOF;
173 if (strcmp(pfile[curpfile], "-") == 0)
174 yyin = stdin;
175 else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
176 FATAL("can't open file %s", pfile[curpfile]);
177 lineno = 1;
178 }
179 if ((c = getc(yyin)) != EOF)
180 return c;
181 if (yyin != stdin)
182 fclose(yyin);
183 yyin = NULL;
184 curpfile++;
185 }
186}
187
188char *cursource(void) /* current source file name */
189{
190 if (npfile > 0)
191 return pfile[curpfile];
192 else
193 return NULL;
194}