xref: /titanic_44/usr/src/cmd/oawk/main.c (revision dc5a8425272d2602e4c21b95b9eeac2b897f45a1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*dc5a8425Srobbin 
237c478bd9Sstevel@tonic-gate /*
24*dc5a8425Srobbin  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
25*dc5a8425Srobbin  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include "awk.def"
367c478bd9Sstevel@tonic-gate #include "awk.h"
377c478bd9Sstevel@tonic-gate #include <wctype.h>
387c478bd9Sstevel@tonic-gate #include <getwidth.h>
397c478bd9Sstevel@tonic-gate #include <langinfo.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate int	dbg	= 0;
437c478bd9Sstevel@tonic-gate int	svargc;
447c478bd9Sstevel@tonic-gate wchar_t **svargv;
457c478bd9Sstevel@tonic-gate eucwidth_t eucwidth;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate extern FILE	*yyin;	/* lex input file */
487c478bd9Sstevel@tonic-gate wchar_t *lexprog;	/* points to program argument if it exists */
49*dc5a8425Srobbin extern	int errorflag;	/* non-zero if any syntax errors; set by yyerror */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate wchar_t	radixpoint = L'.';
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int filefd, symnum, ansfd;
547c478bd9Sstevel@tonic-gate extern int maxsym, errno;
55*dc5a8425Srobbin 
56*dc5a8425Srobbin extern void run(NODE *a);
57*dc5a8425Srobbin extern void syminit(void);
58*dc5a8425Srobbin 
59*dc5a8425Srobbin int
main(int argc,char * argv[])60*dc5a8425Srobbin main(int argc, char *argv[])
61*dc5a8425Srobbin {
62*dc5a8425Srobbin 	wchar_t	*p, **wargv;
63*dc5a8425Srobbin 	int i;
647c478bd9Sstevel@tonic-gate 	static wchar_t L_dash[] = L"-";
657c478bd9Sstevel@tonic-gate 	static wchar_t L_dashd[] = L"-d";
667c478bd9Sstevel@tonic-gate 	char	*nl_radix;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/*
697c478bd9Sstevel@tonic-gate 	 * At this point, numbers are still scanned as in
707c478bd9Sstevel@tonic-gate 	 * the POSIX locale.
717c478bd9Sstevel@tonic-gate 	 * (POSIX.2, volume 2, P867, L4742-4757)
727c478bd9Sstevel@tonic-gate 	 */
737c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
747c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_NUMERIC, "C");
757c478bd9Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
767c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
777c478bd9Sstevel@tonic-gate #endif
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	textdomain(TEXT_DOMAIN);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	getwidth(&eucwidth);
827c478bd9Sstevel@tonic-gate 	if (argc == 1) {
837c478bd9Sstevel@tonic-gate 		fprintf(stderr,
847c478bd9Sstevel@tonic-gate gettext("awk: Usage: awk [-Fc] [-f source | 'cmds'] [files]\n"));
857c478bd9Sstevel@tonic-gate 		exit(2);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	syminit();
887c478bd9Sstevel@tonic-gate 	if ((wargv = (wchar_t **)malloc((argc+1) * sizeof (wchar_t *))) == NULL)
897c478bd9Sstevel@tonic-gate 		error(FATAL, "Insuffcient memory on argv");
907c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
917c478bd9Sstevel@tonic-gate 		if ((p = (wchar_t *)malloc((strlen(argv[i]) + 1)
927c478bd9Sstevel@tonic-gate 						* sizeof (wchar_t))) == NULL)
937c478bd9Sstevel@tonic-gate 			error(FATAL, "Insuffcient memory on argv");
947c478bd9Sstevel@tonic-gate 		mbstowcs(p, argv[i], strlen(argv[i]) + 1);
957c478bd9Sstevel@tonic-gate 		wargv[i] = p;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	wargv[argc] = NULL;
987c478bd9Sstevel@tonic-gate 	while (argc > 1) {
997c478bd9Sstevel@tonic-gate 		argc--;
1007c478bd9Sstevel@tonic-gate 		wargv++;
1017c478bd9Sstevel@tonic-gate 		/* this nonsense is because gcos argument handling */
1027c478bd9Sstevel@tonic-gate 		/* folds -F into -f.  accordingly, one checks the next */
1037c478bd9Sstevel@tonic-gate 		/* character after f to see if it's -f file or -Fx. */
1047c478bd9Sstevel@tonic-gate 		if (wargv[0][0] == L'-' && wargv[0][1] == L'f' &&
1057c478bd9Sstevel@tonic-gate 			wargv[0][2] == 0) {
1067c478bd9Sstevel@tonic-gate 			if (argc <= 1)
1077c478bd9Sstevel@tonic-gate 				error(FATAL, "no argument for -f");
1087c478bd9Sstevel@tonic-gate 			yyin = (wscmp(wargv[1], L_dash) == 0)
1097c478bd9Sstevel@tonic-gate 					? stdin
1107c478bd9Sstevel@tonic-gate 					: fopen(toeuccode(wargv[1]), "r");
1117c478bd9Sstevel@tonic-gate 			if (yyin == NULL)
1127c478bd9Sstevel@tonic-gate 				error(FATAL, "can't open %ws", wargv[1]);
1137c478bd9Sstevel@tonic-gate 			argc--;
1147c478bd9Sstevel@tonic-gate 			wargv++;
1157c478bd9Sstevel@tonic-gate 			break;
1167c478bd9Sstevel@tonic-gate 		} else if (wargv[0][0] == L'-' && wargv[0][1] == L'F') {
1177c478bd9Sstevel@tonic-gate 			if (wargv[0][2] == L't')
1187c478bd9Sstevel@tonic-gate 				**FS = L'\t';
1197c478bd9Sstevel@tonic-gate 			else
1207c478bd9Sstevel@tonic-gate 				/* set field sep */
1217c478bd9Sstevel@tonic-gate 				**FS = wargv[0][2];
1227c478bd9Sstevel@tonic-gate 			continue;
1237c478bd9Sstevel@tonic-gate 		} else if (wargv[0][0] != L'-') {
1247c478bd9Sstevel@tonic-gate 			dprintf("cmds=|%ws|\n", wargv[0], NULL, NULL);
1257c478bd9Sstevel@tonic-gate 			yyin = NULL;
1267c478bd9Sstevel@tonic-gate 			lexprog = wargv[0];
1277c478bd9Sstevel@tonic-gate 			wargv[0] = wargv[-1];   /* need this space */
1287c478bd9Sstevel@tonic-gate 			break;
1297c478bd9Sstevel@tonic-gate 		} else if (wscmp(L_dashd, wargv[0]) == 0) {
1307c478bd9Sstevel@tonic-gate 			dbg = 1;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	if (argc <= 1) {
1347c478bd9Sstevel@tonic-gate 		wargv[0][0] = L'-';
1357c478bd9Sstevel@tonic-gate 		wargv[0][1] = 0;
1367c478bd9Sstevel@tonic-gate 		argc++;
1377c478bd9Sstevel@tonic-gate 		wargv--;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	svargc = --argc;
1407c478bd9Sstevel@tonic-gate 	svargv = ++wargv;
1417c478bd9Sstevel@tonic-gate 	dprintf("svargc=%d svargv[0]=%ws\n", svargc, svargv[0], NULL);
1427c478bd9Sstevel@tonic-gate 	*FILENAME = *svargv;    /* initial file name */
1437c478bd9Sstevel@tonic-gate 	yyparse();
1447c478bd9Sstevel@tonic-gate 	dprintf("errorflag=%d\n", errorflag, NULL, NULL);
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 * done parsing, so now activate the LC_NUMERIC
1477c478bd9Sstevel@tonic-gate 	 */
1487c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1497c478bd9Sstevel@tonic-gate 	nl_radix = nl_langinfo(RADIXCHAR);
1507c478bd9Sstevel@tonic-gate 	if (nl_radix) {
1517c478bd9Sstevel@tonic-gate 		radixpoint = (wchar_t)*nl_radix;
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	if (errorflag)
1547c478bd9Sstevel@tonic-gate 		exit(errorflag);
1557c478bd9Sstevel@tonic-gate 	run(winner);
156*dc5a8425Srobbin 	return (errorflag);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
159*dc5a8425Srobbin int
yywrap()1607c478bd9Sstevel@tonic-gate yywrap()
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	return (1);
1637c478bd9Sstevel@tonic-gate }
164