3 /****************************************************************
4 Copyright (C) Lucent Technologies 1997
7 Permission to use, copy, modify, and distribute this software and
8 its documentation for any purpose and without fee is hereby
9 granted, provided that the above copyright notice appear in all
10 copies and that both that the copyright notice and this
11 permission notice and warranty disclaimer appear in supporting
12 documentation, and that the name Lucent Technologies or any of
13 its entities not be used in advertising or publicity pertaining
14 to distribution of the software without specific, written prior
17 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
19 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
20 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
22 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
23 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 ****************************************************************/
27 const char *version
= "version 20050424";
39 extern char **environ
;
43 char *cmdname
; /* gets argv[0] for error messages */
44 extern FILE *yyin
; /* lex input file */
45 char *lexprog
; /* points to program argument if it exists */
46 extern int errorflag
; /* non-zero if any syntax errors; set by yyerror */
47 int compile_time
= 2; /* for error printing: */
48 /* 2 = cmdline, 1 = compile, 0 = running */
50 #define MAX_PFILE 20 /* max number of -f's */
52 char *pfile
[MAX_PFILE
]; /* program filenames from -f's */
53 int npfile
= 0; /* number of filenames */
54 int curpfile
= 0; /* current filename */
56 int safe
= 0; /* 1 => "safe" mode */
58 int main(int argc
, char *argv
[])
60 const char *fs
= NULL
;
62 setlocale(LC_CTYPE
, "");
63 setlocale(LC_NUMERIC
, "C"); /* for parsing cmdline & prog */
66 fprintf(stderr
, "Usage: %s [-f programfile | 'program'] [-Ffieldsep] [-v var=value] [files]\n", cmdname
);
69 signal(SIGFPE
, fpecatch
);
71 symtab
= makesymtab(NSYMTAB
);
72 while (argc
> 1 && argv
[1][0] == '-' && argv
[1][1] != '\0') {
73 if (strcmp(argv
[1], "--") == 0) { /* explicit end of args */
80 if (strcmp(argv
[1], "-safe") == 0)
83 case 'f': /* next argument is program filename */
87 FATAL("no program filename");
88 if (npfile
>= MAX_PFILE
- 1)
89 FATAL("too many -f options");
90 pfile
[npfile
++] = argv
[1];
92 case 'F': /* set field separator */
93 if (argv
[1][2] != 0) { /* arg is -Fsomething */
94 if (argv
[1][2] == 't' && argv
[1][3] == 0) /* wart: t=>\t */
96 else if (argv
[1][2] != 0)
98 } else { /* arg is -F something */
100 if (argc
> 1 && argv
[1][0] == 't' && argv
[1][1] == 0) /* wart: t=>\t */
102 else if (argc
> 1 && argv
[1][0] != 0)
105 if (fs
== NULL
|| *fs
== '\0')
106 WARNING("field separator FS is empty");
108 case 'v': /* -v a=1 to be done NOW. one -v for each */
109 if (argv
[1][2] == '\0' && --argc
> 1 && isclvar((++argv
)[1]))
112 case 'm': /* more memory: -mr=record, -mf=fields */
113 /* no longer supported */
114 WARNING("obsolete option %s ignored", argv
[1]);
117 dbg
= atoi(&argv
[1][2]);
120 printf("awk %s\n", version
);
122 case 'V': /* added for exptools "standard" */
123 printf("awk %s\n", version
);
127 WARNING("unknown option %s ignored", argv
[1]);
133 /* argv[1] is now the first argument */
134 if (npfile
== 0) { /* no -f; first argument is program */
138 FATAL("no program given");
140 dprintf( ("program = |%s|\n", argv
[1]) );
148 argv
[0] = cmdname
; /* put prog name at front of arglist */
149 dprintf( ("argc=%d, argv[0]=%s\n", argc
, argv
[0]) );
154 setlocale(LC_NUMERIC
, ""); /* back to whatever it is locally */
156 *FS
= qstring(fs
, '\0');
157 dprintf( ("errorflag=%d\n", errorflag
) );
158 if (errorflag
== 0) {
166 int pgetc(void) /* get 1 character from awk program */
172 if (curpfile
>= npfile
)
174 if (strcmp(pfile
[curpfile
], "-") == 0)
176 else if ((yyin
= fopen(pfile
[curpfile
], "r")) == NULL
)
177 FATAL("can't open file %s", pfile
[curpfile
]);
180 if ((c
= getc(yyin
)) != EOF
)
189 char *cursource(void) /* current source file name */
192 return pfile
[curpfile
];