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 char *version = "version 19990602";
40 char *cmdname; /* gets argv[0] for error messages */
41 extern Biobuf *yyin; /* lex input file */
42 char *lexprog; /* points to program argument if it exists */
43 extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
44 int compile_time = 2; /* for error printing: */
45 /* 2 = cmdline, 1 = compile, 0 = running */
47 char *pfile[20]; /* program filenames from -f's */
48 int npfile = 0; /* number of filenames */
49 int curpfile = 0; /* current filename */
51 int safe = 0; /* 1 => "safe" mode */
53 void main(int argc, char *argv[])
55 char *fs = nil, *marg;
58 Binit(&stdin, 0, OREAD);
59 Binit(&stdout, 1, OWRITE);
60 Binit(&stderr, 2, OWRITE);
64 Bprint(&stderr, "Usage: %s [-F fieldsep] [-mf n] [-mr n] [-v var=value] [-f programfile | 'program'] [file ...]\n", cmdname);
70 symtab = makesymtab(NSYMTAB);
71 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
72 if (strcmp(argv[1], "--") == 0) { /* explicit end of args */
79 if (strcmp(argv[1], "-safe") == 0)
82 case 'f': /* next argument is program filename */
86 FATAL("no program filename");
87 pfile[npfile++] = argv[1];
89 case 'F': /* set field separator */
90 if (argv[1][2] != 0) { /* arg is -Fsomething */
91 if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
93 else if (argv[1][2] != 0)
95 } else { /* arg is -F something */
97 if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
99 else if (argc > 1 && argv[1][0] != 0)
102 if (fs == nil || *fs == '\0')
103 WARNING("field separator FS is empty");
105 case 'v': /* -v a=1 to be done NOW. one -v for each */
106 if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
109 case 'm': /* more memory: -mr=record, -mf=fields */
110 /* no longer needed */
113 temp = atoi(&argv[1][3]);
116 temp = atoi(&argv[1][0]);
119 case 'r': recsize = temp; break;
120 case 'f': nfields = temp; break;
121 default: FATAL("unknown option %s\n", marg);
125 dbg = atoi(&argv[1][2]);
128 print("awk %s\n", version);
130 case 'V': /* added for exptools "standard" */
131 print("awk %s\n", version);
135 WARNING("unknown option %s ignored", argv[1]);
141 /* argv[1] is now the first argument */
142 if (npfile == 0) { /* no -f; first argument is program */
146 FATAL("no program given");
148 dprint( ("program = |%s|\n", argv[1]) );
156 argv[0] = cmdname; /* put prog name at front of arglist */
157 dprint( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
161 *FS = qstring(fs, '\0');
162 dprint( ("errorflag=%d\n", errorflag) );
163 if (errorflag == 0) {
173 int pgetc(void) /* get 1 character from awk program */
179 if (curpfile >= npfile)
181 if (strcmp(pfile[curpfile], "-") == 0)
183 else if ((yyin = Bopen(pfile[curpfile], OREAD)) == nil)
184 FATAL("can't open file %s", pfile[curpfile]);
187 if ((c = Bgetc(yyin)) != Beof)
196 char *cursource(void) /* current source file name */
199 return pfile[curpfile];