modified: n.fq
[GalaxyCodeBases.git] / tools / bioawk / main.c
blobf82112a009feaa01a53fff28419c0835cbcf83f2
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 20110810";
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 Awkfloat srand_seed = 1;
42 char *cmdname; /* gets argv[0] for error messages */
43 extern FILE *yyin; /* lex input file */
44 char *lexprog; /* points to program argument if it exists */
45 extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
46 int compile_time = 2; /* for error printing: */
47 /* 2 = cmdline, 1 = compile, 0 = running */
49 #define MAX_PFILE 20 /* max number of -f's */
51 char *pfile[MAX_PFILE]; /* program filenames from -f's */
52 int npfile = 0; /* number of filenames */
53 int curpfile = 0; /* current filename */
55 int safe = 0; /* 1 => "safe" mode */
57 int main(int argc, char *argv[])
59 const char *fs = NULL;
60 char tmp[16];
62 setlocale(LC_CTYPE, "");
63 setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
64 cmdname = argv[0];
65 if (argc == 1) {
66 fprintf(stderr,
67 "usage: %s [-F fs] [-v var=value] [-c fmt] [-tH] [-f progfile | 'prog'] [file ...]\n",
68 cmdname);
69 exit(1);
71 signal(SIGFPE, fpecatch);
73 srand_seed = 1;
74 srand(srand_seed);
76 yyin = NULL;
77 symtab = makesymtab(NSYMTAB/NSYMTAB);
78 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
79 if (strcmp(argv[1],"-version") == 0 || strcmp(argv[1],"--version") == 0) {
80 printf("awk %s\n", version);
81 exit(0);
82 break;
84 if (strncmp(argv[1], "--", 2) == 0) { /* explicit end of args */
85 argc--;
86 argv++;
87 break;
89 switch (argv[1][1]) {
90 case 's':
91 if (strcmp(argv[1], "-safe") == 0)
92 safe = 1;
93 break;
94 case 'f': /* next argument is program filename */
95 if (argv[1][2] != 0) { /* arg is -fsomething */
96 if (npfile >= MAX_PFILE - 1)
97 FATAL("too many -f options");
98 pfile[npfile++] = &argv[1][2];
99 } else { /* arg is -f something */
100 argc--; argv++;
101 if (argc <= 1)
102 FATAL("no program filename");
103 if (npfile >= MAX_PFILE - 1)
104 FATAL("too many -f options");
105 pfile[npfile++] = argv[1];
107 break;
108 case 'F': /* set field separator */
109 if (argv[1][2] != 0) { /* arg is -Fsomething */
110 if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */
111 fs = "\t";
112 else if (argv[1][2] != 0)
113 fs = &argv[1][2];
114 } else { /* arg is -F something */
115 argc--; argv++;
116 if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */
117 fs = "\t";
118 else if (argc > 1 && argv[1][0] != 0)
119 fs = &argv[1][0];
121 if (fs == NULL || *fs == '\0')
122 WARNING("field separator FS is empty");
123 break;
124 case 't':
125 strcpy(tmp, "FS=\\t"); setclvar(tmp);
126 strcpy(tmp, "OFS=\\t"); setclvar(tmp);
127 break;
128 case 'v': /* -v a=1 to be done NOW. one -v for each */
129 if (argv[1][2] != 0) { /* arg is -vsomething */
130 if (isclvar(&argv[1][2]))
131 setclvar(&argv[1][2]);
132 else
133 FATAL("invalid -v option argument: %s", &argv[1][2]);
134 } else { /* arg is -v something */
135 argc--; argv++;
136 if (argc <= 1)
137 FATAL("no variable name");
138 if (isclvar(argv[1]))
139 setclvar(argv[1]);
140 else
141 FATAL("invalid -v option argument: %s", argv[1]);
143 break;
144 case 'd':
145 dbg = atoi(&argv[1][2]);
146 if (dbg == 0)
147 dbg = 1;
148 printf("awk %s\n", version);
149 break;
150 case 'H':
151 bio_flag |= BIO_SHOW_HDR;
152 break;
153 case 'c':
154 if (argv[1][2] != 0) { /* arg is -csomething */
155 if ((bio_fmt = bio_get_fmt(&argv[1][2])) == BIO_NULL) return 1;
156 } else { /* arg is -c something */
157 argc--; argv++;
158 if (argc <= 1)
159 FATAL("no variable name");
160 if ((bio_fmt = bio_get_fmt(argv[1])) == BIO_NULL) return 1;
162 break;
163 default:
164 WARNING("unknown option %s ignored", argv[1]);
165 break;
167 if ((argv[1][1] == 't' || argv[1][1] == 'H') && argv[1][2] != 0) { /* dealing with for example "-tc help" */
168 char *p;
169 for (p = &argv[1][2]; *p; ++p) *(p-1) = *p;
170 *(p-1) = *p;
171 } else --argc, ++argv;
173 /* argv[1] is now the first argument */
174 if (npfile == 0) { /* no -f; first argument is program */
175 if (argc <= 1) {
176 if (dbg)
177 exit(0);
178 FATAL("no program given");
180 dprintf( ("program = |%s|\n", argv[1]) );
181 lexprog = argv[1];
182 argc--;
183 argv++;
185 recinit(recsize);
186 syminit();
187 compile_time = 1;
188 argv[0] = cmdname; /* put prog name at front of arglist */
189 dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
190 arginit(argc, argv);
191 if (!safe)
192 envinit(environ);
193 yyparse();
194 setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */
195 if (fs)
196 *FS = qstring(fs, '\0');
197 dprintf( ("errorflag=%d\n", errorflag) );
198 if (errorflag == 0) {
199 compile_time = 0;
200 run(winner);
201 } else
202 bracecheck();
203 return(errorflag);
206 int pgetc(void) /* get 1 character from awk program */
208 int c;
210 for (;;) {
211 if (yyin == NULL) {
212 if (curpfile >= npfile)
213 return EOF;
214 if (strcmp(pfile[curpfile], "-") == 0)
215 yyin = stdin;
216 else if ((yyin = fopen(pfile[curpfile], "r")) == NULL)
217 FATAL("can't open file %s", pfile[curpfile]);
218 lineno = 1;
220 if ((c = getc(yyin)) != EOF)
221 return c;
222 if (yyin != stdin)
223 fclose(yyin);
224 yyin = NULL;
225 curpfile++;
229 char *cursource(void) /* current source file name */
231 if (npfile > 0)
232 return pfile[curpfile];
233 else
234 return NULL;