xref: /titanic_51/usr/src/cmd/oawk/main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
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 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <ctype.h>
35*7c478bd9Sstevel@tonic-gate #include "awk.def"
36*7c478bd9Sstevel@tonic-gate #include "awk.h"
37*7c478bd9Sstevel@tonic-gate #include <wctype.h>
38*7c478bd9Sstevel@tonic-gate #include <getwidth.h>
39*7c478bd9Sstevel@tonic-gate #include <langinfo.h>
40*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate int	dbg	= 0;
43*7c478bd9Sstevel@tonic-gate int	svargc;
44*7c478bd9Sstevel@tonic-gate wchar_t **svargv;
45*7c478bd9Sstevel@tonic-gate eucwidth_t eucwidth;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate extern FILE	*yyin;	/* lex input file */
48*7c478bd9Sstevel@tonic-gate wchar_t *lexprog;	/* points to program argument if it exists */
49*7c478bd9Sstevel@tonic-gate extern	errorflag;	/* non-zero if any syntax errors; set by yyerror */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate wchar_t	radixpoint = L'.';
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate int filefd, symnum, ansfd;
54*7c478bd9Sstevel@tonic-gate extern int maxsym, errno;
55*7c478bd9Sstevel@tonic-gate main(argc, argv) int argc; char **argv; {
56*7c478bd9Sstevel@tonic-gate 	register wchar_t	*p, **wargv;
57*7c478bd9Sstevel@tonic-gate 	register int		i;
58*7c478bd9Sstevel@tonic-gate 	static wchar_t L_dash[] = L"-";
59*7c478bd9Sstevel@tonic-gate 	static wchar_t L_dashd[] = L"-d";
60*7c478bd9Sstevel@tonic-gate 	char	*nl_radix;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	/*
63*7c478bd9Sstevel@tonic-gate 	 * At this point, numbers are still scanned as in
64*7c478bd9Sstevel@tonic-gate 	 * the POSIX locale.
65*7c478bd9Sstevel@tonic-gate 	 * (POSIX.2, volume 2, P867, L4742-4757)
66*7c478bd9Sstevel@tonic-gate 	 */
67*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
68*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_NUMERIC, "C");
69*7c478bd9Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
70*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
71*7c478bd9Sstevel@tonic-gate #endif
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	textdomain(TEXT_DOMAIN);
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	getwidth(&eucwidth);
76*7c478bd9Sstevel@tonic-gate 	if (argc == 1) {
77*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
78*7c478bd9Sstevel@tonic-gate gettext("awk: Usage: awk [-Fc] [-f source | 'cmds'] [files]\n"));
79*7c478bd9Sstevel@tonic-gate 		exit(2);
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 	syminit();
82*7c478bd9Sstevel@tonic-gate 	if ((wargv = (wchar_t **)malloc((argc+1) * sizeof (wchar_t *))) == NULL)
83*7c478bd9Sstevel@tonic-gate 		error(FATAL, "Insuffcient memory on argv");
84*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
85*7c478bd9Sstevel@tonic-gate 		if ((p = (wchar_t *)malloc((strlen(argv[i]) + 1)
86*7c478bd9Sstevel@tonic-gate 						* sizeof (wchar_t))) == NULL)
87*7c478bd9Sstevel@tonic-gate 			error(FATAL, "Insuffcient memory on argv");
88*7c478bd9Sstevel@tonic-gate 		mbstowcs(p, argv[i], strlen(argv[i]) + 1);
89*7c478bd9Sstevel@tonic-gate 		wargv[i] = p;
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 	wargv[argc] = NULL;
92*7c478bd9Sstevel@tonic-gate 	while (argc > 1) {
93*7c478bd9Sstevel@tonic-gate 		argc--;
94*7c478bd9Sstevel@tonic-gate 		wargv++;
95*7c478bd9Sstevel@tonic-gate 		/* this nonsense is because gcos argument handling */
96*7c478bd9Sstevel@tonic-gate 		/* folds -F into -f.  accordingly, one checks the next */
97*7c478bd9Sstevel@tonic-gate 		/* character after f to see if it's -f file or -Fx. */
98*7c478bd9Sstevel@tonic-gate 		if (wargv[0][0] == L'-' && wargv[0][1] == L'f' &&
99*7c478bd9Sstevel@tonic-gate 			wargv[0][2] == 0) {
100*7c478bd9Sstevel@tonic-gate 			if (argc <= 1)
101*7c478bd9Sstevel@tonic-gate 				error(FATAL, "no argument for -f");
102*7c478bd9Sstevel@tonic-gate 			yyin = (wscmp(wargv[1], L_dash) == 0)
103*7c478bd9Sstevel@tonic-gate 					? stdin
104*7c478bd9Sstevel@tonic-gate 					: fopen(toeuccode(wargv[1]), "r");
105*7c478bd9Sstevel@tonic-gate 			if (yyin == NULL)
106*7c478bd9Sstevel@tonic-gate 				error(FATAL, "can't open %ws", wargv[1]);
107*7c478bd9Sstevel@tonic-gate 			argc--;
108*7c478bd9Sstevel@tonic-gate 			wargv++;
109*7c478bd9Sstevel@tonic-gate 			break;
110*7c478bd9Sstevel@tonic-gate 		} else if (wargv[0][0] == L'-' && wargv[0][1] == L'F') {
111*7c478bd9Sstevel@tonic-gate 			if (wargv[0][2] == L't')
112*7c478bd9Sstevel@tonic-gate 				**FS = L'\t';
113*7c478bd9Sstevel@tonic-gate 			else
114*7c478bd9Sstevel@tonic-gate 				/* set field sep */
115*7c478bd9Sstevel@tonic-gate 				**FS = wargv[0][2];
116*7c478bd9Sstevel@tonic-gate 			continue;
117*7c478bd9Sstevel@tonic-gate 		} else if (wargv[0][0] != L'-') {
118*7c478bd9Sstevel@tonic-gate 			dprintf("cmds=|%ws|\n", wargv[0], NULL, NULL);
119*7c478bd9Sstevel@tonic-gate 			yyin = NULL;
120*7c478bd9Sstevel@tonic-gate 			lexprog = wargv[0];
121*7c478bd9Sstevel@tonic-gate 			wargv[0] = wargv[-1];   /* need this space */
122*7c478bd9Sstevel@tonic-gate 			break;
123*7c478bd9Sstevel@tonic-gate 		} else if (wscmp(L_dashd, wargv[0]) == 0) {
124*7c478bd9Sstevel@tonic-gate 			dbg = 1;
125*7c478bd9Sstevel@tonic-gate 		}
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 	if (argc <= 1) {
128*7c478bd9Sstevel@tonic-gate 		wargv[0][0] = L'-';
129*7c478bd9Sstevel@tonic-gate 		wargv[0][1] = 0;
130*7c478bd9Sstevel@tonic-gate 		argc++;
131*7c478bd9Sstevel@tonic-gate 		wargv--;
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	svargc = --argc;
134*7c478bd9Sstevel@tonic-gate 	svargv = ++wargv;
135*7c478bd9Sstevel@tonic-gate 	dprintf("svargc=%d svargv[0]=%ws\n", svargc, svargv[0], NULL);
136*7c478bd9Sstevel@tonic-gate 	*FILENAME = *svargv;    /* initial file name */
137*7c478bd9Sstevel@tonic-gate 	yyparse();
138*7c478bd9Sstevel@tonic-gate 	dprintf("errorflag=%d\n", errorflag, NULL, NULL);
139*7c478bd9Sstevel@tonic-gate 	/*
140*7c478bd9Sstevel@tonic-gate 	 * done parsing, so now activate the LC_NUMERIC
141*7c478bd9Sstevel@tonic-gate 	 */
142*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
143*7c478bd9Sstevel@tonic-gate 	nl_radix = nl_langinfo(RADIXCHAR);
144*7c478bd9Sstevel@tonic-gate 	if (nl_radix) {
145*7c478bd9Sstevel@tonic-gate 		radixpoint = (wchar_t)*nl_radix;
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	if (errorflag)
148*7c478bd9Sstevel@tonic-gate 		exit(errorflag);
149*7c478bd9Sstevel@tonic-gate 	run(winner);
150*7c478bd9Sstevel@tonic-gate 	exit(errorflag);
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate yywrap()
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	return (1);
156*7c478bd9Sstevel@tonic-gate }
157