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