1 7c478bd9Sstevel@tonic-gate /*
2 7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3 7c478bd9Sstevel@tonic-gate *
4 7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5 80148899SSurya Prakki * Common Development and Distribution License (the "License").
6 80148899SSurya Prakki * You may not use this file except in compliance with the License.
7 7c478bd9Sstevel@tonic-gate *
8 7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
10 7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
11 7c478bd9Sstevel@tonic-gate * and limitations under the License.
12 7c478bd9Sstevel@tonic-gate *
13 7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
14 7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
16 7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
17 7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
18 7c478bd9Sstevel@tonic-gate *
19 7c478bd9Sstevel@tonic-gate * CDDL HEADER END
20 7c478bd9Sstevel@tonic-gate */
21 1ee2e5faSnakanon
22 7c478bd9Sstevel@tonic-gate /*
23 80148899SSurya Prakki * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 1ee2e5faSnakanon * Use is subject to license terms.
25 7c478bd9Sstevel@tonic-gate */
26 1ee2e5faSnakanon
27 7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29 7c478bd9Sstevel@tonic-gate
30 7c478bd9Sstevel@tonic-gate #include <stdio.h>
31 7c478bd9Sstevel@tonic-gate #include <ctype.h>
32 7c478bd9Sstevel@tonic-gate #include <signal.h>
33 7c478bd9Sstevel@tonic-gate #include <locale.h>
34 7c478bd9Sstevel@tonic-gate #include <libintl.h>
35 7c478bd9Sstevel@tonic-gate #include <stdarg.h>
36 7c478bd9Sstevel@tonic-gate #include <errno.h>
37 7c478bd9Sstevel@tonic-gate #include <values.h>
38 7c478bd9Sstevel@tonic-gate #include <langinfo.h>
39 7c478bd9Sstevel@tonic-gate #include "awk.h"
40 7c478bd9Sstevel@tonic-gate #include "y.tab.h"
41 7c478bd9Sstevel@tonic-gate
42 7c478bd9Sstevel@tonic-gate char *version = "version Oct 11, 1989";
43 7c478bd9Sstevel@tonic-gate
44 7c478bd9Sstevel@tonic-gate int dbg = 0;
45 7c478bd9Sstevel@tonic-gate uchar *cmdname; /* gets argv[0] for error messages */
46 7c478bd9Sstevel@tonic-gate uchar *lexprog; /* points to program argument if it exists */
47 7c478bd9Sstevel@tonic-gate int compile_time = 2; /* for error printing: */
48 7c478bd9Sstevel@tonic-gate /* 2 = cmdline, 1 = compile, 0 = running */
49 7c478bd9Sstevel@tonic-gate char radixpoint = '.';
50 7c478bd9Sstevel@tonic-gate
51 1ee2e5faSnakanon static uchar **pfile = NULL; /* program filenames from -f's */
52 1ee2e5faSnakanon static int npfile = 0; /* number of filenames */
53 1ee2e5faSnakanon static int curpfile = 0; /* current filename */
54 1ee2e5faSnakanon
55 1ee2e5faSnakanon int
main(int argc,char * argv[],char * envp[])56 1ee2e5faSnakanon main(int argc, char *argv[], char *envp[])
57 7c478bd9Sstevel@tonic-gate {
58 7c478bd9Sstevel@tonic-gate uchar *fs = NULL;
59 7c478bd9Sstevel@tonic-gate char *nl_radix;
60 7c478bd9Sstevel@tonic-gate /*
61 7c478bd9Sstevel@tonic-gate * At this point, numbers are still scanned as in
62 7c478bd9Sstevel@tonic-gate * the POSIX locale.
63 7c478bd9Sstevel@tonic-gate * (POSIX.2, volume 2, P867, L4742-4757)
64 7c478bd9Sstevel@tonic-gate */
65 7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
66 7c478bd9Sstevel@tonic-gate (void) setlocale(LC_NUMERIC, "C");
67 7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
68 7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
69 7c478bd9Sstevel@tonic-gate #endif
70 7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
71 1ee2e5faSnakanon cmdname = (uchar *)argv[0];
72 7c478bd9Sstevel@tonic-gate if (argc == 1) {
73 1ee2e5faSnakanon (void) fprintf(stderr, gettext(
74 7c478bd9Sstevel@tonic-gate "Usage: %s [-f programfile | 'program'] [-Ffieldsep] "
75 7c478bd9Sstevel@tonic-gate "[-v var=value] [files]\n"), cmdname);
76 7c478bd9Sstevel@tonic-gate exit(1);
77 7c478bd9Sstevel@tonic-gate }
78 1ee2e5faSnakanon (void) signal(SIGFPE, fpecatch);
79 7c478bd9Sstevel@tonic-gate yyin = NULL;
80 7c478bd9Sstevel@tonic-gate syminit();
81 7c478bd9Sstevel@tonic-gate while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
82 7c478bd9Sstevel@tonic-gate if (strcmp(argv[1], "--") == 0) {
83 7c478bd9Sstevel@tonic-gate /* explicit end of args */
84 7c478bd9Sstevel@tonic-gate argc--;
85 7c478bd9Sstevel@tonic-gate argv++;
86 7c478bd9Sstevel@tonic-gate break;
87 7c478bd9Sstevel@tonic-gate }
88 7c478bd9Sstevel@tonic-gate switch (argv[1][1]) {
89 7c478bd9Sstevel@tonic-gate case 'f': /* next argument is program filename */
90 7c478bd9Sstevel@tonic-gate argc--;
91 7c478bd9Sstevel@tonic-gate argv++;
92 7c478bd9Sstevel@tonic-gate if (argc <= 1)
93 7c478bd9Sstevel@tonic-gate ERROR "no program filename" FATAL;
94 1ee2e5faSnakanon pfile = realloc(pfile, sizeof (uchar *) * (npfile + 1));
95 1ee2e5faSnakanon if (pfile == NULL)
96 1ee2e5faSnakanon ERROR "out of space in main" FATAL;
97 1ee2e5faSnakanon pfile[npfile++] = (uchar *)argv[1];
98 7c478bd9Sstevel@tonic-gate break;
99 7c478bd9Sstevel@tonic-gate case 'F': /* set field separator */
100 7c478bd9Sstevel@tonic-gate if (argv[1][2] != 0) { /* arg is -Fsomething */
101 7c478bd9Sstevel@tonic-gate /* wart: t=>\t */
102 7c478bd9Sstevel@tonic-gate if (argv[1][2] == 't' && argv[1][3] == 0)
103 7c478bd9Sstevel@tonic-gate fs = (uchar *) "\t";
104 7c478bd9Sstevel@tonic-gate else if (argv[1][2] != 0)
105 1ee2e5faSnakanon fs = (uchar *)&argv[1][2];
106 7c478bd9Sstevel@tonic-gate } else { /* arg is -F something */
107 7c478bd9Sstevel@tonic-gate argc--; argv++;
108 7c478bd9Sstevel@tonic-gate if (argc > 1) {
109 7c478bd9Sstevel@tonic-gate /* wart: t=>\t */
110 7c478bd9Sstevel@tonic-gate if (argv[1][0] == 't' &&
111 7c478bd9Sstevel@tonic-gate argv[1][1] == 0)
112 7c478bd9Sstevel@tonic-gate fs = (uchar *) "\t";
113 7c478bd9Sstevel@tonic-gate else if (argv[1][0] != 0)
114 1ee2e5faSnakanon fs = (uchar *)&argv[1][0];
115 7c478bd9Sstevel@tonic-gate }
116 7c478bd9Sstevel@tonic-gate }
117 7c478bd9Sstevel@tonic-gate if (fs == NULL || *fs == '\0')
118 7c478bd9Sstevel@tonic-gate ERROR "field separator FS is empty" WARNING;
119 7c478bd9Sstevel@tonic-gate break;
120 7c478bd9Sstevel@tonic-gate case 'v': /* -v a=1 to be done NOW. one -v for each */
121 7c478bd9Sstevel@tonic-gate if (argv[1][2] == '\0' && --argc > 1 &&
122 1ee2e5faSnakanon isclvar((uchar *)(++argv)[1]))
123 1ee2e5faSnakanon setclvar((uchar *)argv[1]);
124 7c478bd9Sstevel@tonic-gate break;
125 7c478bd9Sstevel@tonic-gate case 'd':
126 7c478bd9Sstevel@tonic-gate dbg = atoi(&argv[1][2]);
127 7c478bd9Sstevel@tonic-gate if (dbg == 0)
128 7c478bd9Sstevel@tonic-gate dbg = 1;
129 1ee2e5faSnakanon (void) printf("awk %s\n", version);
130 7c478bd9Sstevel@tonic-gate break;
131 7c478bd9Sstevel@tonic-gate default:
132 7c478bd9Sstevel@tonic-gate ERROR "unknown option %s ignored", argv[1] WARNING;
133 7c478bd9Sstevel@tonic-gate break;
134 7c478bd9Sstevel@tonic-gate }
135 7c478bd9Sstevel@tonic-gate argc--;
136 7c478bd9Sstevel@tonic-gate argv++;
137 7c478bd9Sstevel@tonic-gate }
138 7c478bd9Sstevel@tonic-gate /* argv[1] is now the first argument */
139 7c478bd9Sstevel@tonic-gate if (npfile == 0) { /* no -f; first argument is program */
140 *0717e03dSYuri Pankov if (argc <= 1) {
141 *0717e03dSYuri Pankov if (dbg)
142 *0717e03dSYuri Pankov exit(0);
143 7c478bd9Sstevel@tonic-gate ERROR "no program given" FATAL;
144 *0717e03dSYuri Pankov }
145 7c478bd9Sstevel@tonic-gate dprintf(("program = |%s|\n", argv[1]));
146 1ee2e5faSnakanon lexprog = (uchar *)argv[1];
147 7c478bd9Sstevel@tonic-gate argc--;
148 7c478bd9Sstevel@tonic-gate argv++;
149 7c478bd9Sstevel@tonic-gate }
150 7c478bd9Sstevel@tonic-gate compile_time = 1;
151 1ee2e5faSnakanon argv[0] = (char *)cmdname; /* put prog name at front of arglist */
152 7c478bd9Sstevel@tonic-gate dprintf(("argc=%d, argv[0]=%s\n", argc, argv[0]));
153 1ee2e5faSnakanon arginit(argc, (uchar **)argv);
154 1ee2e5faSnakanon envinit((uchar **)envp);
155 80148899SSurya Prakki (void) yyparse();
156 7c478bd9Sstevel@tonic-gate if (fs)
157 1ee2e5faSnakanon *FS = qstring(fs, '\0');
158 7c478bd9Sstevel@tonic-gate dprintf(("errorflag=%d\n", errorflag));
159 7c478bd9Sstevel@tonic-gate /*
160 7c478bd9Sstevel@tonic-gate * done parsing, so now activate the LC_NUMERIC
161 7c478bd9Sstevel@tonic-gate */
162 7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
163 7c478bd9Sstevel@tonic-gate nl_radix = nl_langinfo(RADIXCHAR);
164 7c478bd9Sstevel@tonic-gate if (nl_radix)
165 7c478bd9Sstevel@tonic-gate radixpoint = *nl_radix;
166 7c478bd9Sstevel@tonic-gate
167 7c478bd9Sstevel@tonic-gate if (errorflag == 0) {
168 7c478bd9Sstevel@tonic-gate compile_time = 0;
169 7c478bd9Sstevel@tonic-gate run(winner);
170 7c478bd9Sstevel@tonic-gate } else
171 7c478bd9Sstevel@tonic-gate bracecheck();
172 1ee2e5faSnakanon return (errorflag);
173 7c478bd9Sstevel@tonic-gate }
174 7c478bd9Sstevel@tonic-gate
175 1ee2e5faSnakanon int
pgetc(void)176 1ee2e5faSnakanon pgetc(void) /* get program character */
177 7c478bd9Sstevel@tonic-gate {
178 7c478bd9Sstevel@tonic-gate int c;
179 7c478bd9Sstevel@tonic-gate
180 7c478bd9Sstevel@tonic-gate for (;;) {
181 7c478bd9Sstevel@tonic-gate if (yyin == NULL) {
182 7c478bd9Sstevel@tonic-gate if (curpfile >= npfile)
183 7c478bd9Sstevel@tonic-gate return (EOF);
184 7c478bd9Sstevel@tonic-gate yyin = (strcmp((char *)pfile[curpfile], "-") == 0) ?
185 7c478bd9Sstevel@tonic-gate stdin : fopen((char *)pfile[curpfile], "r");
186 1ee2e5faSnakanon if (yyin == NULL) {
187 7c478bd9Sstevel@tonic-gate ERROR "can't open file %s",
188 7c478bd9Sstevel@tonic-gate pfile[curpfile] FATAL;
189 7c478bd9Sstevel@tonic-gate }
190 1ee2e5faSnakanon }
191 7c478bd9Sstevel@tonic-gate if ((c = getc(yyin)) != EOF)
192 7c478bd9Sstevel@tonic-gate return (c);
193 1ee2e5faSnakanon (void) fclose(yyin);
194 7c478bd9Sstevel@tonic-gate yyin = NULL;
195 7c478bd9Sstevel@tonic-gate curpfile++;
196 7c478bd9Sstevel@tonic-gate }
197 7c478bd9Sstevel@tonic-gate }
198