xref: /illumos-gate/usr/src/cmd/oawk/main.c (revision 440a8a36792bdf9ef51639066aab0b7771ffcab8)
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 /*
24  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
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	int 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 
56 extern void run(NODE *a);
57 extern void syminit(void);
58 
59 int
60 main(int argc, char *argv[])
61 {
62 	wchar_t	*p, **wargv;
63 	int i;
64 	static wchar_t L_dash[] = L"-";
65 	static wchar_t L_dashd[] = L"-d";
66 	char	*nl_radix;
67 
68 	/*
69 	 * At this point, numbers are still scanned as in
70 	 * the POSIX locale.
71 	 * (POSIX.2, volume 2, P867, L4742-4757)
72 	 */
73 	(void) setlocale(LC_ALL, "");
74 	(void) setlocale(LC_NUMERIC, "C");
75 #ifndef	TEXT_DOMAIN
76 #define	TEXT_DOMAIN	"SYS_TEST"
77 #endif
78 
79 	textdomain(TEXT_DOMAIN);
80 
81 	getwidth(&eucwidth);
82 	if (argc == 1) {
83 		fprintf(stderr,
84 gettext("awk: Usage: awk [-Fc] [-f source | 'cmds'] [files]\n"));
85 		exit(2);
86 	}
87 	syminit();
88 	if ((wargv = (wchar_t **)malloc((argc+1) * sizeof (wchar_t *))) == NULL)
89 		error(FATAL, "Insuffcient memory on argv");
90 	for (i = 0; i < argc; i++) {
91 		if ((p = (wchar_t *)malloc((strlen(argv[i]) + 1)
92 						* sizeof (wchar_t))) == NULL)
93 			error(FATAL, "Insuffcient memory on argv");
94 		mbstowcs(p, argv[i], strlen(argv[i]) + 1);
95 		wargv[i] = p;
96 	}
97 	wargv[argc] = NULL;
98 	while (argc > 1) {
99 		argc--;
100 		wargv++;
101 		/* this nonsense is because gcos argument handling */
102 		/* folds -F into -f.  accordingly, one checks the next */
103 		/* character after f to see if it's -f file or -Fx. */
104 		if (wargv[0][0] == L'-' && wargv[0][1] == L'f' &&
105 			wargv[0][2] == 0) {
106 			if (argc <= 1)
107 				error(FATAL, "no argument for -f");
108 			yyin = (wscmp(wargv[1], L_dash) == 0)
109 					? stdin
110 					: fopen(toeuccode(wargv[1]), "r");
111 			if (yyin == NULL)
112 				error(FATAL, "can't open %ws", wargv[1]);
113 			argc--;
114 			wargv++;
115 			break;
116 		} else if (wargv[0][0] == L'-' && wargv[0][1] == L'F') {
117 			if (wargv[0][2] == L't')
118 				**FS = L'\t';
119 			else
120 				/* set field sep */
121 				**FS = wargv[0][2];
122 			continue;
123 		} else if (wargv[0][0] != L'-') {
124 			dprintf("cmds=|%ws|\n", wargv[0], NULL, NULL);
125 			yyin = NULL;
126 			lexprog = wargv[0];
127 			wargv[0] = wargv[-1];   /* need this space */
128 			break;
129 		} else if (wscmp(L_dashd, wargv[0]) == 0) {
130 			dbg = 1;
131 		}
132 	}
133 	if (argc <= 1) {
134 		wargv[0][0] = L'-';
135 		wargv[0][1] = 0;
136 		argc++;
137 		wargv--;
138 	}
139 	svargc = --argc;
140 	svargv = ++wargv;
141 	dprintf("svargc=%d svargv[0]=%ws\n", svargc, svargv[0], NULL);
142 	*FILENAME = *svargv;    /* initial file name */
143 	yyparse();
144 	dprintf("errorflag=%d\n", errorflag, NULL, NULL);
145 	/*
146 	 * done parsing, so now activate the LC_NUMERIC
147 	 */
148 	(void) setlocale(LC_ALL, "");
149 	nl_radix = nl_langinfo(RADIXCHAR);
150 	if (nl_radix) {
151 		radixpoint = (wchar_t)*nl_radix;
152 	}
153 	if (errorflag)
154 		exit(errorflag);
155 	run(winner);
156 	return (errorflag);
157 }
158 
159 int
160 yywrap()
161 {
162 	return (1);
163 }
164