1 /*-
2  * This code contains changes by
3  *      Gunnar Ritter, Freiburg i. Br., Germany, 2002. All rights reserved.
4  *
5  * Conditions 1, 2, and 4 and the no-warranty notice below apply
6  * to these changes.
7  *
8  *
9  * Copyright (c) 1991
10  * 	The Regents of the University of California.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  * 	This product includes software developed by the University of
23  * 	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *
41  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  *   Redistributions of source code and documentation must retain the
47  *    above copyright notice, this list of conditions and the following
48  *    disclaimer.
49  *   Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  *   All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *      This product includes software developed or owned by Caldera
55  *      International, Inc.
56  *   Neither the name of Caldera International, Inc. nor the names of
57  *    other contributors may be used to endorse or promote products
58  *    derived from this software without specific prior written permission.
59  *
60  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
61  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
65  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
66  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
68  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
69  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
70  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
71  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72  */
73 
74 #ifdef	notdef
75 char copyright[] =
76 "Copyright (c) 1991 The Regents of the University of California.\n\
77  All rights reserved.\n";
78 #endif
79 
80 /*	from 4.4BSD /usr/src/old/awk/main.c	4.6 (Berkeley) 4/17/91	*/
81 /*	Sccsid @(#)main.c	1.11 (gritter) 12/12/04>	*/
82 
83 #include "stdio.h"
84 #include "ctype.h"
85 #include "awk.def"
86 #include "awk.h"
87 #include <libgen.h>
88 #include <locale.h>
89 #include <langinfo.h>
90 
91 int	dbg	= 0;
92 int	ldbg	= 0;
93 #ifdef	notdef
94 int	svflg	= 0;
95 #endif
96 int	rstflg	= 0;
97 int	svargc;
98 char	**svargv, **xargv;
99 extern FILE	*yyin;	/* lex input file */
100 char	*lexprog;	/* points to program argument if it exists */
101 extern	int errorflag;	/* non-zero if any syntax errors; set by yyerror */
102 
103 int filefd, symnum, ansfd, mb_cur_max;
104 const char *radixchar = ".";
105 int radixcharlen = 1;
106 char *filelist;
107 char *progname;
108 extern int maxsym;
109 int
main(int argc,char ** argv)110 main(int argc, char **argv) {
111 	setlocale(LC_COLLATE, "");
112 	setlocale(LC_CTYPE, "");
113 	setlocale(LC_NUMERIC, "C");
114 	mb_cur_max = MB_CUR_MAX;
115 	progname = tostring(basename(argv[0]));
116 	if (argc == 1)
117 		error(FATAL, "Usage: %s [-f source | 'cmds'] [files]",
118 				progname);
119 	fldinit();
120 	syminit();
121 	growrec();
122 	while (argc > 1) {
123 		argc--;
124 		argv++;
125 		/* this nonsense is because gcos argument handling */
126 		/* folds -F into -f.  accordingly, one checks the next */
127 		/* character after f to see if it's -f file or -Fx.
128 		*/
129 		if (argv[0][0] == '-' && argv[0][1] == 'f' &&
130 				argv[0][2] == '\0') {
131 			if (argv[1] == NULL)
132 				error(FATAL, "no argument for -f");
133 			if (argv[1][0] == '-' && argv[1][1] == '\0')
134 				yyin = stdin;
135 			else {
136 				yyin = fopen(argv[1], "r");
137 				if (yyin == NULL)
138 					error(FATAL, "can't open %s", argv[1]);
139 			}
140 			argc--;
141 			argv++;
142 			break;
143 		} else if (argv[0][0] == '-' && argv[0][1] == 'F') {	/* set field sep */
144 			if (argv[0][2] == 't')	/* special case for tab */
145 				**FS = '\t';
146 			else
147 				*FS = tostring(&argv[0][2]);
148 			continue;
149 		} else if (argv[0][0] != '-') {
150 			dprintf("cmds=|%s|\n", argv[0]);
151 			yyin = NULL;
152 			lexprog = argv[0];
153 			argv[0] = argv[-1];	/* need this space */
154 			break;
155 		} else if (strcmp("-d", argv[0])==0) {
156 			dbg = 1;
157 #ifdef	notdef
158 		}
159 		else if (strcmp("-l", argv[0])==0) {
160 			ldbg = 1;
161 		}
162 		else if(strcmp("-S", argv[0]) == 0) {
163 			svflg = 1;
164 		}
165 		else if(strncmp("-R", argv[0], 2) == 0) {
166 			if(thaw(argv[0] + 2) == 0)
167 				rstflg = 1;
168 			else {
169 				fprintf(stderr, "not restored\n");
170 				exit(1);
171 			}
172 #endif	/* !notdef */
173 		}
174 	}
175 	if (argc <= 1) {
176 		argv[0][0] = '-';
177 		argv[0][1] = '\0';
178 		argc++;
179 		argv--;
180 	}
181 	svargc = --argc;
182 	svargv = ++argv;
183 	dprintf("svargc=%d svargv[0]=%s\n", svargc, svargv[0]);
184 	*FILENAME = *svargv;	/* initial file name */
185 	if(rstflg == 0)
186 		yyparse();
187 	dprintf("errorflag=%d\n", errorflag);
188 	if (errorflag)
189 		exit(errorflag);
190 #ifdef	notdef
191 	if(svflg) {
192 		svflg = 0;
193 		if(freeze("awk.out") != 0)
194 			fprintf(stderr, "not saved\n");
195 		exit(0);
196 	}
197 #endif
198 	setlocale(LC_NUMERIC, "");
199 	if (*(radixchar = nl_langinfo(RADIXCHAR)) != '\0')
200 		radixchar = tostring(radixchar);
201 	else
202 		radixchar = ".";
203 	radixcharlen = strlen(radixchar);
204 	run();
205 	exit(errorflag);
206 }
207 
208 int
yywrap(void)209 yywrap(void)
210 {
211 	return(1);
212 }
213