Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / nawk / main.c
blobb431b9184b76f4ddc469cc3e6edc5d351cc5b42c
1 /****************************************************************
2 Copyright (C) Lucent Technologies 1997
3 All Rights Reserved
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
13 permission.
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
22 THIS SOFTWARE.
23 ****************************************************************/
25 const char *version = "version 20070501";
27 #if HAVE_NBTOOL_CONFIG_H
28 #include "nbtool_config.h"
29 #endif
31 #define DEBUG
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <locale.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <locale.h>
39 #include "awk.h"
40 #include "awkgram.h"
42 extern char **environ;
43 extern int nfields;
45 int dbg = 0;
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 #define MAX_PFILE 20 /* max number of -f's */
55 char *pfile[MAX_PFILE]; /* program filenames from -f's */
56 int npfile = 0; /* number of filenames */
57 int curpfile = 0; /* current filename */
59 int safe = 0; /* 1 => "safe" mode */
61 static char *
62 setfs(char *p)
64 #ifdef notdef
65 /* wart: t=>\t */
66 if (p[0] == 't' && p[1] == 0)
67 return "\t";
68 else
69 #endif
70 if (p[0] != 0)
71 return p;
72 return NULL;
75 static void fpecatch(int n
76 #ifdef SA_SIGINFO
77 , siginfo_t *si, void *uc
78 #endif
81 #ifdef SA_SIGINFO
82 static const char *emsg[] = {
83 "Unknown error",
84 "Integer divide by zero",
85 "Integer overflow",
86 "Floating point divide by zero",
87 "Floating point overflow",
88 "Floating point underflow",
89 "Floating point inexact result",
90 "Invalid Floating point operation",
91 "Subscript out of range",
93 #endif
94 FATAL("floating point exception"
95 #ifdef SA_SIGINFO
96 ": %s\n", emsg[si->si_code >= 1 && si->si_code <= 8 ?
97 si->si_code : 0]
98 #endif
102 int main(int argc, char *argv[])
104 const char *fs = NULL;
106 setlocale(LC_CTYPE, "");
107 setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
108 cmdname = argv[0];
109 if (argc == 1) {
110 fprintf(stderr,
111 "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
112 cmdname);
113 exit(1);
116 (void) setlocale(LC_ALL, "");
118 #ifdef SA_SIGINFO
120 struct sigaction sa;
121 sa.sa_sigaction = fpecatch;
122 sa.sa_flags = SA_SIGINFO;
123 sigemptyset(&sa.sa_mask);
124 (void)sigaction(SIGFPE, &sa, NULL);
126 #else
127 (void)signal(SIGFPE, fpecatch);
128 #endif
129 yyin = NULL;
130 symtab = makesymtab(NSYMTAB/NSYMTAB);
131 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
132 if (strcmp(argv[1],"-version") == 0 || strcmp(argv[1],"--version") == 0) {
133 printf("awk %s\n", version);
134 exit(0);
135 break;
137 if (strncmp(argv[1], "--", 2) == 0) { /* explicit end of args */
138 argc--;
139 argv++;
140 break;
142 switch (argv[1][1]) {
143 case 's':
144 if (strcmp(argv[1], "-safe") == 0)
145 safe = 1;
146 break;
147 case 'f': /* next argument is program filename */
148 argc--;
149 argv++;
150 if (argc <= 1)
151 FATAL("no program filename");
152 if (npfile >= MAX_PFILE - 1)
153 FATAL("too many -f options");
154 pfile[npfile++] = argv[1];
155 break;
156 case 'F': /* set field separator */
157 if (argv[1][2] != 0) { /* arg is -Fsomething */
158 fs = setfs(argv[1] + 2);
159 } else { /* arg is -F something */
160 argc--; argv++;
161 if (argc > 1)
162 fs = setfs(argv[1]);
164 if (fs == NULL || *fs == '\0')
165 WARNING("field separator FS is empty");
166 break;
167 case 'v': /* -v a=1 to be done NOW. one -v for each */
168 if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
169 setclvar(argv[1]);
170 break;
171 case 'd':
172 dbg = atoi(&argv[1][2]);
173 if (dbg == 0)
174 dbg = 1;
175 printf("awk %s\n", version);
176 break;
177 default:
178 WARNING("unknown option %s ignored", argv[1]);
179 break;
181 argc--;
182 argv++;
184 /* argv[1] is now the first argument */
185 if (npfile == 0) { /* no -f; first argument is program */
186 if (argc <= 1) {
187 if (dbg)
188 exit(0);
189 FATAL("no program given");
191 dprintf( ("program = |%s|\n", argv[1]) );
192 lexprog = argv[1];
193 argc--;
194 argv++;
196 recinit(recsize);
197 syminit();
198 compile_time = 1;
199 argv[0] = cmdname; /* put prog name at front of arglist */
200 dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
201 arginit(argc, argv);
202 if (!safe)
203 envinit(environ);
204 yyparse();
205 setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
206 if (fs)
207 *FS = qstring(fs, '\0');
208 dprintf( ("errorflag=%d\n", errorflag) );
209 if (errorflag == 0) {
210 compile_time = 0;
211 run(winner);
212 } else
213 bracecheck();
214 return(errorflag);
217 int pgetc(void) /* get 1 character from awk program */
219 int c;
221 for (;;) {
222 if (yyin == NULL) {
223 if (curpfile >= npfile)
224 return EOF;
225 if (strcmp(pfile[curpfile], "-") == 0)
226 yyin = stdin;
227 else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
228 FATAL("can't open file %s", pfile[curpfile]);
229 lineno = 1;
231 if ((c = getc(yyin)) != EOF)
232 return c;
233 if (yyin != stdin)
234 fclose(yyin);
235 yyin = NULL;
236 curpfile++;
240 char *cursource(void) /* current source file name */
242 if (npfile > 0)
243 return pfile[curpfile];
244 else
245 return NULL;