xref: /illumos-gate/usr/src/cmd/oawk/main.c (revision 2a8bcb4e)
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 #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include "awk.def"
347c478bd9Sstevel@tonic-gate #include "awk.h"
357c478bd9Sstevel@tonic-gate #include <wctype.h>
367c478bd9Sstevel@tonic-gate #include <getwidth.h>
377c478bd9Sstevel@tonic-gate #include <langinfo.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate int	dbg	= 0;
417c478bd9Sstevel@tonic-gate int	svargc;
427c478bd9Sstevel@tonic-gate wchar_t **svargv;
437c478bd9Sstevel@tonic-gate eucwidth_t eucwidth;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate extern FILE	*yyin;	/* lex input file */
467c478bd9Sstevel@tonic-gate wchar_t *lexprog;	/* points to program argument if it exists */
47*dc5a8425Srobbin extern	int errorflag;	/* non-zero if any syntax errors; set by yyerror */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate wchar_t	radixpoint = L'.';
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int filefd, symnum, ansfd;
527c478bd9Sstevel@tonic-gate extern int maxsym, errno;
53*dc5a8425Srobbin 
54*dc5a8425Srobbin extern void run(NODE *a);
55*dc5a8425Srobbin extern void syminit(void);
56*dc5a8425Srobbin 
57*dc5a8425Srobbin int
main(int argc,char * argv[])58*dc5a8425Srobbin main(int argc, char *argv[])
59*dc5a8425Srobbin {
60*dc5a8425Srobbin 	wchar_t	*p, **wargv;
61*dc5a8425Srobbin 	int i;
627c478bd9Sstevel@tonic-gate 	static wchar_t L_dash[] = L"-";
637c478bd9Sstevel@tonic-gate 	static wchar_t L_dashd[] = L"-d";
647c478bd9Sstevel@tonic-gate 	char	*nl_radix;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/*
677c478bd9Sstevel@tonic-gate 	 * At this point, numbers are still scanned as in
687c478bd9Sstevel@tonic-gate 	 * the POSIX locale.
697c478bd9Sstevel@tonic-gate 	 * (POSIX.2, volume 2, P867, L4742-4757)
707c478bd9Sstevel@tonic-gate 	 */
717c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
727c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_NUMERIC, "C");
737c478bd9Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
747c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	textdomain(TEXT_DOMAIN);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	getwidth(&eucwidth);
807c478bd9Sstevel@tonic-gate 	if (argc == 1) {
817c478bd9Sstevel@tonic-gate 		fprintf(stderr,
827c478bd9Sstevel@tonic-gate gettext("awk: Usage: awk [-Fc] [-f source | 'cmds'] [files]\n"));
837c478bd9Sstevel@tonic-gate 		exit(2);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	syminit();
867c478bd9Sstevel@tonic-gate 	if ((wargv = (wchar_t **)malloc((argc+1) * sizeof (wchar_t *))) == NULL)
877c478bd9Sstevel@tonic-gate 		error(FATAL, "Insuffcient memory on argv");
887c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
897c478bd9Sstevel@tonic-gate 		if ((p = (wchar_t *)malloc((strlen(argv[i]) + 1)
907c478bd9Sstevel@tonic-gate 						* sizeof (wchar_t))) == NULL)
917c478bd9Sstevel@tonic-gate 			error(FATAL, "Insuffcient memory on argv");
927c478bd9Sstevel@tonic-gate 		mbstowcs(p, argv[i], strlen(argv[i]) + 1);
937c478bd9Sstevel@tonic-gate 		wargv[i] = p;
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	wargv[argc] = NULL;
967c478bd9Sstevel@tonic-gate 	while (argc > 1) {
977c478bd9Sstevel@tonic-gate 		argc--;
987c478bd9Sstevel@tonic-gate 		wargv++;
997c478bd9Sstevel@tonic-gate 		/* this nonsense is because gcos argument handling */
1007c478bd9Sstevel@tonic-gate 		/* folds -F into -f.  accordingly, one checks the next */
1017c478bd9Sstevel@tonic-gate 		/* character after f to see if it's -f file or -Fx. */
1027c478bd9Sstevel@tonic-gate 		if (wargv[0][0] == L'-' && wargv[0][1] == L'f' &&
1037c478bd9Sstevel@tonic-gate 			wargv[0][2] == 0) {
1047c478bd9Sstevel@tonic-gate 			if (argc <= 1)
1057c478bd9Sstevel@tonic-gate 				error(FATAL, "no argument for -f");
1067c478bd9Sstevel@tonic-gate 			yyin = (wscmp(wargv[1], L_dash) == 0)
1077c478bd9Sstevel@tonic-gate 					? stdin
1087c478bd9Sstevel@tonic-gate 					: fopen(toeuccode(wargv[1]), "r");
1097c478bd9Sstevel@tonic-gate 			if (yyin == NULL)
1107c478bd9Sstevel@tonic-gate 				error(FATAL, "can't open %ws", wargv[1]);
1117c478bd9Sstevel@tonic-gate 			argc--;
1127c478bd9Sstevel@tonic-gate 			wargv++;
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		} else if (wargv[0][0] == L'-' && wargv[0][1] == L'F') {
1157c478bd9Sstevel@tonic-gate 			if (wargv[0][2] == L't')
1167c478bd9Sstevel@tonic-gate 				**FS = L'\t';
1177c478bd9Sstevel@tonic-gate 			else
1187c478bd9Sstevel@tonic-gate 				/* set field sep */
1197c478bd9Sstevel@tonic-gate 				**FS = wargv[0][2];
1207c478bd9Sstevel@tonic-gate 			continue;
1217c478bd9Sstevel@tonic-gate 		} else if (wargv[0][0] != L'-') {
1227c478bd9Sstevel@tonic-gate 			dprintf("cmds=|%ws|\n", wargv[0], NULL, NULL);
1237c478bd9Sstevel@tonic-gate 			yyin = NULL;
1247c478bd9Sstevel@tonic-gate 			lexprog = wargv[0];
1257c478bd9Sstevel@tonic-gate 			wargv[0] = wargv[-1];   /* need this space */
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 		} else if (wscmp(L_dashd, wargv[0]) == 0) {
1287c478bd9Sstevel@tonic-gate 			dbg = 1;
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	if (argc <= 1) {
1327c478bd9Sstevel@tonic-gate 		wargv[0][0] = L'-';
1337c478bd9Sstevel@tonic-gate 		wargv[0][1] = 0;
1347c478bd9Sstevel@tonic-gate 		argc++;
1357c478bd9Sstevel@tonic-gate 		wargv--;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	svargc = --argc;
1387c478bd9Sstevel@tonic-gate 	svargv = ++wargv;
1397c478bd9Sstevel@tonic-gate 	dprintf("svargc=%d svargv[0]=%ws\n", svargc, svargv[0], NULL);
1407c478bd9Sstevel@tonic-gate 	*FILENAME = *svargv;    /* initial file name */
1417c478bd9Sstevel@tonic-gate 	yyparse();
1427c478bd9Sstevel@tonic-gate 	dprintf("errorflag=%d\n", errorflag, NULL, NULL);
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * done parsing, so now activate the LC_NUMERIC
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1477c478bd9Sstevel@tonic-gate 	nl_radix = nl_langinfo(RADIXCHAR);
1487c478bd9Sstevel@tonic-gate 	if (nl_radix) {
1497c478bd9Sstevel@tonic-gate 		radixpoint = (wchar_t)*nl_radix;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	if (errorflag)
1527c478bd9Sstevel@tonic-gate 		exit(errorflag);
1537c478bd9Sstevel@tonic-gate 	run(winner);
154*dc5a8425Srobbin 	return (errorflag);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
157*dc5a8425Srobbin int
yywrap()1587c478bd9Sstevel@tonic-gate yywrap()
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	return (1);
1617c478bd9Sstevel@tonic-gate }
162