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 20100523";
27 #if HAVE_NBTOOL_CONFIG_H
28 #include "nbtool_config.h"
41 extern char **environ
;
45 unsigned int srand_seed
;
46 char *cmdname
; /* gets argv[0] for error messages */
47 extern FILE *yyin
; /* lex input file */
48 char *lexprog
; /* points to program argument if it exists */
49 extern int errorflag
; /* non-zero if any syntax errors; set by yyerror */
50 int compile_time
= 2; /* for error printing: */
51 /* 2 = cmdline, 1 = compile, 0 = running */
53 static char **pfile
= NULL
; /* program filenames from -f's */
54 static size_t maxpfile
= 0; /* max program filenames */
55 static size_t npfile
= 0; /* number of filenames */
56 static size_t curpfile
= 0; /* current filename */
58 int safe
= 0; /* 1 => "safe" mode */
65 if (p
[0] == 't' && p
[1] == 0)
74 __dead
static void fpecatch(int n
76 , siginfo_t
*si
, void *uc
81 static const char *emsg
[] = {
83 "Integer divide by zero",
85 "Floating point divide by zero",
86 "Floating point overflow",
87 "Floating point underflow",
88 "Floating point inexact result",
89 "Invalid Floating point operation",
90 "Subscript out of range",
93 FATAL("floating point exception"
95 ": %s\n", emsg
[si
->si_code
>= 1 && si
->si_code
<= 8 ?
101 int main(int argc
, char *argv
[])
103 const char *fs
= NULL
;
105 setlocale(LC_ALL
, "");
106 setlocale(LC_NUMERIC
, "C"); /* for parsing cmdline & prog */
110 "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
118 sa
.sa_sigaction
= fpecatch
;
119 sa
.sa_flags
= SA_SIGINFO
;
120 sigemptyset(&sa
.sa_mask
);
121 (void)sigaction(SIGFPE
, &sa
, NULL
);
124 (void)signal(SIGFPE
, fpecatch
);
126 /* Set and keep track of the random seed */
131 symtab
= makesymtab(NSYMTAB
/NSYMTAB
);
132 while (argc
> 1 && argv
[1][0] == '-' && argv
[1][1] != '\0') {
133 if (strcmp(argv
[1],"-version") == 0 || strcmp(argv
[1],"--version") == 0) {
134 printf("awk %s\n", version
);
138 if (strncmp(argv
[1], "--", 2) == 0) { /* explicit end of args */
143 switch (argv
[1][1]) {
145 if (strcmp(argv
[1], "-safe") == 0)
148 case 'f': /* next argument is program filename */
152 FATAL("no program filename");
153 if (npfile
>= maxpfile
) {
155 pfile
= realloc(pfile
,
156 maxpfile
* sizeof(*pfile
));
158 FATAL("error allocating space for "
161 pfile
[npfile
++] = argv
[1];
163 case 'F': /* set field separator */
164 if (argv
[1][2] != 0) { /* arg is -Fsomething */
165 fs
= setfs(argv
[1] + 2);
166 } else { /* arg is -F something */
171 if (fs
== NULL
|| *fs
== '\0')
172 WARNING("field separator FS is empty");
174 case 'v': /* -v a=1 to be done NOW. one -v for each */
175 if (argv
[1][2] == '\0' && --argc
> 1 && isclvar((++argv
)[1]))
177 else if (argv
[1][2] != '\0')
178 setclvar(&argv
[1][2]);
181 dbg
= atoi(&argv
[1][2]);
184 printf("awk %s\n", version
);
187 WARNING("unknown option %s ignored", argv
[1]);
193 /* argv[1] is now the first argument */
194 if (npfile
== 0) { /* no -f; first argument is program */
198 FATAL("no program given");
200 dprintf( ("program = |%s|\n", argv
[1]) );
208 argv
[0] = cmdname
; /* put prog name at front of arglist */
209 dprintf( ("argc=%d, argv[0]=%s\n", argc
, argv
[0]) );
215 *FS
= qstring(fs
, '\0');
216 dprintf( ("errorflag=%d\n", errorflag
) );
217 if (errorflag
== 0) {
225 int pgetc(void) /* get 1 character from awk program */
231 if (curpfile
>= npfile
)
233 if (strcmp(pfile
[curpfile
], "-") == 0)
235 else if ((yyin
= fopen(pfile
[curpfile
], "r")) == NULL
)
236 FATAL("can't open file %s", pfile
[curpfile
]);
239 if ((c
= getc(yyin
)) != EOF
)
248 char *cursource(void) /* current source file name */
251 return pfile
[curpfile
];