This commit sets new users to see the DragonFly-tips fortunes instead
[dragonfly.git] / contrib / awk20040207 / main.c
blob2c061bb4bce5aac9e65e23a59f6ca2358b1a6afc
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 20040207";
27 #define DEBUG
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <locale.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <signal.h>
34 #include "awk.h"
35 #include "ytab.h"
37 extern char **environ;
38 extern int nfields;
40 int dbg = 0;
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 char *pfile[20]; /* program filenames from -f's */
49 int npfile = 0; /* number of filenames */
50 int curpfile = 0; /* current filename */
52 int safe = 0; /* 1 => "safe" mode */
54 int main(int argc, char *argv[])
56 const char *fs = NULL;
58 setlocale(LC_CTYPE, "");
59 setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
60 cmdname = argv[0];
61 if (argc == 1) {
62 fprintf(stderr, "Usage: %s [-f programfile | 'program'] [-Ffieldsep] [-v var=value] [files]\n", cmdname);
63 exit(1);
65 signal(SIGFPE, fpecatch);
66 yyin = NULL;
67 symtab = makesymtab(NSYMTAB);
68 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
69 if (strcmp(argv[1], "--") == 0) { /* explicit end of args */
70 argc--;
71 argv++;
72 break;
74 switch (argv[1][1]) {
75 case 's':
76 if (strcmp(argv[1], "-safe") == 0)
77 safe = 1;
78 break;
79 case 'f': /* next argument is program filename */
80 argc--;
81 argv++;
82 if (argc <= 1)
83 FATAL("no program filename");
84 pfile[npfile++] = argv[1];
85 break;
86 case 'F': /* set field separator */
87 if (argv[1][2] != 0) { /* arg is -Fsomething */
88 if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
89 fs = "\t";
90 else if (argv[1][2] != 0)
91 fs = &argv[1][2];
92 } else { /* arg is -F something */
93 argc--; argv++;
94 if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
95 fs = "\t";
96 else if (argc > 1 && argv[1][0] != 0)
97 fs = &argv[1][0];
99 if (fs == NULL || *fs == '\0')
100 WARNING("field separator FS is empty");
101 break;
102 case 'v': /* -v a=1 to be done NOW. one -v for each */
103 if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1]))
104 setclvar(argv[1]);
105 break;
106 case 'm': /* more memory: -mr=record, -mf=fields */
107 /* no longer supported */
108 WARNING("obsolete option %s ignored", argv[1]);
109 break;
110 case 'd':
111 dbg = atoi(&argv[1][2]);
112 if (dbg == 0)
113 dbg = 1;
114 printf("awk %s\n", version);
115 break;
116 case 'V': /* added for exptools "standard" */
117 printf("awk %s\n", version);
118 exit(0);
119 break;
120 default:
121 WARNING("unknown option %s ignored", argv[1]);
122 break;
124 argc--;
125 argv++;
127 /* argv[1] is now the first argument */
128 if (npfile == 0) { /* no -f; first argument is program */
129 if (argc <= 1) {
130 if (dbg)
131 exit(0);
132 FATAL("no program given");
134 dprintf( ("program = |%s|\n", argv[1]) );
135 lexprog = argv[1];
136 argc--;
137 argv++;
139 recinit(recsize);
140 syminit();
141 compile_time = 1;
142 argv[0] = cmdname; /* put prog name at front of arglist */
143 dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
144 arginit(argc, argv);
145 if (!safe)
146 envinit(environ);
147 yyparse();
148 setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
149 if (fs)
150 *FS = qstring(fs, '\0');
151 dprintf( ("errorflag=%d\n", errorflag) );
152 if (errorflag == 0) {
153 compile_time = 0;
154 run(winner);
155 } else
156 bracecheck();
157 return(errorflag);
160 int pgetc(void) /* get 1 character from awk program */
162 int c;
164 for (;;) {
165 if (yyin == NULL) {
166 if (curpfile >= npfile)
167 return EOF;
168 if (strcmp(pfile[curpfile], "-") == 0)
169 yyin = stdin;
170 else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
171 FATAL("can't open file %s", pfile[curpfile]);
172 lineno = 1;
174 if ((c = getc(yyin)) != EOF)
175 return c;
176 if (yyin != stdin)
177 fclose(yyin);
178 yyin = NULL;
179 curpfile++;
183 char *cursource(void) /* current source file name */
185 if (npfile > 0)
186 return pfile[curpfile];
187 else
188 return NULL;