1 /****************************************************************
2 Copyright (C) Lucent Technologies 1997
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 name Lucent Technologies or any of
11 its entities not be used in advertising or publicity pertaining
12 to distribution of the software without specific, written prior
15 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23 ****************************************************************/
25 const char *version = "version 20070501 (FreeBSD)";
37 extern char **environ;
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 */
48 #define MAX_PFILE 20 /* max number of -f's */
50 char *pfile[MAX_PFILE]; /* program filenames from -f's */
51 int npfile = 0; /* number of filenames */
52 int curpfile = 0; /* current filename */
54 int safe = 0; /* 1 => "safe" mode */
56 int main(int argc, char *argv[])
58 const char *fs = NULL;
60 setlocale(LC_CTYPE, "");
61 setlocale(LC_COLLATE, "");
62 setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
66 "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
70 signal(SIGFPE, fpecatch);
72 symtab = makesymtab(NSYMTAB/NSYMTAB);
73 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
74 if (strcmp(argv[1],"-version") == 0 || strcmp(argv[1],"--version") == 0) {
75 printf("awk %s\n", version);
79 if (strncmp(argv[1], "--", 2) == 0) { /* explicit end of args */
86 if (strcmp(argv[1], "-safe") == 0)
89 case 'f': /* next argument is program filename */
90 if (argv[1][2] != 0) { /* arg is -fsomething */
91 if (npfile >= MAX_PFILE - 1)
92 FATAL("too many -f options");
93 pfile[npfile++] = &argv[1][2];
94 } else { /* arg is -f something */
97 FATAL("no program filename");
98 if (npfile >= MAX_PFILE - 1)
99 FATAL("too many -f options");
100 pfile[npfile++] = argv[1];
103 case 'F': /* set field separator */
104 if (argv[1][2] != 0) { /* arg is -Fsomething */
105 if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
107 else if (argv[1][2] != 0)
109 } else { /* arg is -F something */
111 if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
113 else if (argc > 1 && argv[1][0] != 0)
116 if (fs == NULL || *fs == '\0')
117 WARNING("field separator FS is empty");
119 case 'v': /* -v a=1 to be done NOW. one -v for each */
120 if (argv[1][2] != 0) { /* arg is -vsomething */
122 setclvar(&argv[1][2]);
123 } else { /* arg is -v something */
125 if (argc > 1 && isclvar(argv[1]))
130 dbg = atoi(&argv[1][2]);
133 printf("awk %s\n", version);
136 WARNING("unknown option %s ignored", argv[1]);
142 /* argv[1] is now the first argument */
143 if (npfile == 0) { /* no -f; first argument is program */
147 FATAL("no program given");
149 dprintf( ("program = |%s|\n", argv[1]) );
157 argv[0] = cmdname; /* put prog name at front of arglist */
158 dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
163 setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
165 *FS = qstring(fs, '\0');
166 dprintf( ("errorflag=%d\n", errorflag) );
167 if (errorflag == 0) {
175 int pgetc(void) /* get 1 character from awk program */
181 if (curpfile >= npfile)
183 if (strcmp(pfile[curpfile], "-") == 0)
185 else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
186 FATAL("can't open file %s", pfile[curpfile]);
189 if ((c = getc(yyin)) != EOF)
198 char *cursource(void) /* current source file name */
201 return pfile[curpfile];