xref: /illumos-gate/usr/src/cmd/oawk/main.c (revision 2a8bcb4e)
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 #include <stdio.h>
32 #include <ctype.h>
33 #include "awk.def"
34 #include "awk.h"
35 #include <wctype.h>
36 #include <getwidth.h>
37 #include <langinfo.h>
38 #include <stdlib.h>
39 
40 int	dbg	= 0;
41 int	svargc;
42 wchar_t **svargv;
43 eucwidth_t eucwidth;
44 
45 extern FILE	*yyin;	/* lex input file */
46 wchar_t *lexprog;	/* points to program argument if it exists */
47 extern	int errorflag;	/* non-zero if any syntax errors; set by yyerror */
48 
49 wchar_t	radixpoint = L'.';
50 
51 int filefd, symnum, ansfd;
52 extern int maxsym, errno;
53 
54 extern void run(NODE *a);
55 extern void syminit(void);
56 
57 int
main(int argc,char * argv[])58 main(int argc, char *argv[])
59 {
60 	wchar_t	*p, **wargv;
61 	int i;
62 	static wchar_t L_dash[] = L"-";
63 	static wchar_t L_dashd[] = L"-d";
64 	char	*nl_radix;
65 
66 	/*
67 	 * At this point, numbers are still scanned as in
68 	 * the POSIX locale.
69 	 * (POSIX.2, volume 2, P867, L4742-4757)
70 	 */
71 	(void) setlocale(LC_ALL, "");
72 	(void) setlocale(LC_NUMERIC, "C");
73 #ifndef	TEXT_DOMAIN
74 #define	TEXT_DOMAIN	"SYS_TEST"
75 #endif
76 
77 	textdomain(TEXT_DOMAIN);
78 
79 	getwidth(&eucwidth);
80 	if (argc == 1) {
81 		fprintf(stderr,
82 gettext("awk: Usage: awk [-Fc] [-f source | 'cmds'] [files]\n"));
83 		exit(2);
84 	}
85 	syminit();
86 	if ((wargv = (wchar_t **)malloc((argc+1) * sizeof (wchar_t *))) == NULL)
87 		error(FATAL, "Insuffcient memory on argv");
88 	for (i = 0; i < argc; i++) {
89 		if ((p = (wchar_t *)malloc((strlen(argv[i]) + 1)
90 						* sizeof (wchar_t))) == NULL)
91 			error(FATAL, "Insuffcient memory on argv");
92 		mbstowcs(p, argv[i], strlen(argv[i]) + 1);
93 		wargv[i] = p;
94 	}
95 	wargv[argc] = NULL;
96 	while (argc > 1) {
97 		argc--;
98 		wargv++;
99 		/* this nonsense is because gcos argument handling */
100 		/* folds -F into -f.  accordingly, one checks the next */
101 		/* character after f to see if it's -f file or -Fx. */
102 		if (wargv[0][0] == L'-' && wargv[0][1] == L'f' &&
103 			wargv[0][2] == 0) {
104 			if (argc <= 1)
105 				error(FATAL, "no argument for -f");
106 			yyin = (wscmp(wargv[1], L_dash) == 0)
107 					? stdin
108 					: fopen(toeuccode(wargv[1]), "r");
109 			if (yyin == NULL)
110 				error(FATAL, "can't open %ws", wargv[1]);
111 			argc--;
112 			wargv++;
113 			break;
114 		} else if (wargv[0][0] == L'-' && wargv[0][1] == L'F') {
115 			if (wargv[0][2] == L't')
116 				**FS = L'\t';
117 			else
118 				/* set field sep */
119 				**FS = wargv[0][2];
120 			continue;
121 		} else if (wargv[0][0] != L'-') {
122 			dprintf("cmds=|%ws|\n", wargv[0], NULL, NULL);
123 			yyin = NULL;
124 			lexprog = wargv[0];
125 			wargv[0] = wargv[-1];   /* need this space */
126 			break;
127 		} else if (wscmp(L_dashd, wargv[0]) == 0) {
128 			dbg = 1;
129 		}
130 	}
131 	if (argc <= 1) {
132 		wargv[0][0] = L'-';
133 		wargv[0][1] = 0;
134 		argc++;
135 		wargv--;
136 	}
137 	svargc = --argc;
138 	svargv = ++wargv;
139 	dprintf("svargc=%d svargv[0]=%ws\n", svargc, svargv[0], NULL);
140 	*FILENAME = *svargv;    /* initial file name */
141 	yyparse();
142 	dprintf("errorflag=%d\n", errorflag, NULL, NULL);
143 	/*
144 	 * done parsing, so now activate the LC_NUMERIC
145 	 */
146 	(void) setlocale(LC_ALL, "");
147 	nl_radix = nl_langinfo(RADIXCHAR);
148 	if (nl_radix) {
149 		radixpoint = (wchar_t)*nl_radix;
150 	}
151 	if (errorflag)
152 		exit(errorflag);
153 	run(winner);
154 	return (errorflag);
155 }
156 
157 int
yywrap()158 yywrap()
159 {
160 	return (1);
161 }
162