xref: /onnv-gate/usr/src/cmd/oawk/main.c (revision 731:eed80a95c4f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*731Srobbin 
230Sstevel@tonic-gate /*
24*731Srobbin  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
25*731Srobbin  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include "awk.def"
360Sstevel@tonic-gate #include "awk.h"
370Sstevel@tonic-gate #include <wctype.h>
380Sstevel@tonic-gate #include <getwidth.h>
390Sstevel@tonic-gate #include <langinfo.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate int	dbg	= 0;
430Sstevel@tonic-gate int	svargc;
440Sstevel@tonic-gate wchar_t **svargv;
450Sstevel@tonic-gate eucwidth_t eucwidth;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate extern FILE	*yyin;	/* lex input file */
480Sstevel@tonic-gate wchar_t *lexprog;	/* points to program argument if it exists */
49*731Srobbin extern	int errorflag;	/* non-zero if any syntax errors; set by yyerror */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate wchar_t	radixpoint = L'.';
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int filefd, symnum, ansfd;
540Sstevel@tonic-gate extern int maxsym, errno;
55*731Srobbin 
56*731Srobbin extern void run(NODE *a);
57*731Srobbin extern void syminit(void);
58*731Srobbin 
59*731Srobbin int
main(int argc,char * argv[])60*731Srobbin main(int argc, char *argv[])
61*731Srobbin {
62*731Srobbin 	wchar_t	*p, **wargv;
63*731Srobbin 	int i;
640Sstevel@tonic-gate 	static wchar_t L_dash[] = L"-";
650Sstevel@tonic-gate 	static wchar_t L_dashd[] = L"-d";
660Sstevel@tonic-gate 	char	*nl_radix;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/*
690Sstevel@tonic-gate 	 * At this point, numbers are still scanned as in
700Sstevel@tonic-gate 	 * the POSIX locale.
710Sstevel@tonic-gate 	 * (POSIX.2, volume 2, P867, L4742-4757)
720Sstevel@tonic-gate 	 */
730Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
740Sstevel@tonic-gate 	(void) setlocale(LC_NUMERIC, "C");
750Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
760Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	textdomain(TEXT_DOMAIN);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	getwidth(&eucwidth);
820Sstevel@tonic-gate 	if (argc == 1) {
830Sstevel@tonic-gate 		fprintf(stderr,
840Sstevel@tonic-gate gettext("awk: Usage: awk [-Fc] [-f source | 'cmds'] [files]\n"));
850Sstevel@tonic-gate 		exit(2);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	syminit();
880Sstevel@tonic-gate 	if ((wargv = (wchar_t **)malloc((argc+1) * sizeof (wchar_t *))) == NULL)
890Sstevel@tonic-gate 		error(FATAL, "Insuffcient memory on argv");
900Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
910Sstevel@tonic-gate 		if ((p = (wchar_t *)malloc((strlen(argv[i]) + 1)
920Sstevel@tonic-gate 						* sizeof (wchar_t))) == NULL)
930Sstevel@tonic-gate 			error(FATAL, "Insuffcient memory on argv");
940Sstevel@tonic-gate 		mbstowcs(p, argv[i], strlen(argv[i]) + 1);
950Sstevel@tonic-gate 		wargv[i] = p;
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	wargv[argc] = NULL;
980Sstevel@tonic-gate 	while (argc > 1) {
990Sstevel@tonic-gate 		argc--;
1000Sstevel@tonic-gate 		wargv++;
1010Sstevel@tonic-gate 		/* this nonsense is because gcos argument handling */
1020Sstevel@tonic-gate 		/* folds -F into -f.  accordingly, one checks the next */
1030Sstevel@tonic-gate 		/* character after f to see if it's -f file or -Fx. */
1040Sstevel@tonic-gate 		if (wargv[0][0] == L'-' && wargv[0][1] == L'f' &&
1050Sstevel@tonic-gate 			wargv[0][2] == 0) {
1060Sstevel@tonic-gate 			if (argc <= 1)
1070Sstevel@tonic-gate 				error(FATAL, "no argument for -f");
1080Sstevel@tonic-gate 			yyin = (wscmp(wargv[1], L_dash) == 0)
1090Sstevel@tonic-gate 					? stdin
1100Sstevel@tonic-gate 					: fopen(toeuccode(wargv[1]), "r");
1110Sstevel@tonic-gate 			if (yyin == NULL)
1120Sstevel@tonic-gate 				error(FATAL, "can't open %ws", wargv[1]);
1130Sstevel@tonic-gate 			argc--;
1140Sstevel@tonic-gate 			wargv++;
1150Sstevel@tonic-gate 			break;
1160Sstevel@tonic-gate 		} else if (wargv[0][0] == L'-' && wargv[0][1] == L'F') {
1170Sstevel@tonic-gate 			if (wargv[0][2] == L't')
1180Sstevel@tonic-gate 				**FS = L'\t';
1190Sstevel@tonic-gate 			else
1200Sstevel@tonic-gate 				/* set field sep */
1210Sstevel@tonic-gate 				**FS = wargv[0][2];
1220Sstevel@tonic-gate 			continue;
1230Sstevel@tonic-gate 		} else if (wargv[0][0] != L'-') {
1240Sstevel@tonic-gate 			dprintf("cmds=|%ws|\n", wargv[0], NULL, NULL);
1250Sstevel@tonic-gate 			yyin = NULL;
1260Sstevel@tonic-gate 			lexprog = wargv[0];
1270Sstevel@tonic-gate 			wargv[0] = wargv[-1];   /* need this space */
1280Sstevel@tonic-gate 			break;
1290Sstevel@tonic-gate 		} else if (wscmp(L_dashd, wargv[0]) == 0) {
1300Sstevel@tonic-gate 			dbg = 1;
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	if (argc <= 1) {
1340Sstevel@tonic-gate 		wargv[0][0] = L'-';
1350Sstevel@tonic-gate 		wargv[0][1] = 0;
1360Sstevel@tonic-gate 		argc++;
1370Sstevel@tonic-gate 		wargv--;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	svargc = --argc;
1400Sstevel@tonic-gate 	svargv = ++wargv;
1410Sstevel@tonic-gate 	dprintf("svargc=%d svargv[0]=%ws\n", svargc, svargv[0], NULL);
1420Sstevel@tonic-gate 	*FILENAME = *svargv;    /* initial file name */
1430Sstevel@tonic-gate 	yyparse();
1440Sstevel@tonic-gate 	dprintf("errorflag=%d\n", errorflag, NULL, NULL);
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * done parsing, so now activate the LC_NUMERIC
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1490Sstevel@tonic-gate 	nl_radix = nl_langinfo(RADIXCHAR);
1500Sstevel@tonic-gate 	if (nl_radix) {
1510Sstevel@tonic-gate 		radixpoint = (wchar_t)*nl_radix;
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 	if (errorflag)
1540Sstevel@tonic-gate 		exit(errorflag);
1550Sstevel@tonic-gate 	run(winner);
156*731Srobbin 	return (errorflag);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
159*731Srobbin int
yywrap()1600Sstevel@tonic-gate yywrap()
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	return (1);
1630Sstevel@tonic-gate }
164