10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*11053SSurya.Prakki@Sun.COM * Common Development and Distribution License (the "License").
6*11053SSurya.Prakki@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21289Snakanon
220Sstevel@tonic-gate /*
23*11053SSurya.Prakki@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24289Snakanon * Use is subject to license terms.
250Sstevel@tonic-gate */
26289Snakanon
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <locale.h>
340Sstevel@tonic-gate #include <libintl.h>
350Sstevel@tonic-gate #include <stdarg.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <values.h>
380Sstevel@tonic-gate #include <langinfo.h>
390Sstevel@tonic-gate #include "awk.h"
400Sstevel@tonic-gate #include "y.tab.h"
410Sstevel@tonic-gate
420Sstevel@tonic-gate char *version = "version Oct 11, 1989";
430Sstevel@tonic-gate
440Sstevel@tonic-gate int dbg = 0;
450Sstevel@tonic-gate uchar *cmdname; /* gets argv[0] for error messages */
460Sstevel@tonic-gate uchar *lexprog; /* points to program argument if it exists */
470Sstevel@tonic-gate int compile_time = 2; /* for error printing: */
480Sstevel@tonic-gate /* 2 = cmdline, 1 = compile, 0 = running */
490Sstevel@tonic-gate char radixpoint = '.';
500Sstevel@tonic-gate
51289Snakanon static uchar **pfile = NULL; /* program filenames from -f's */
52289Snakanon static int npfile = 0; /* number of filenames */
53289Snakanon static int curpfile = 0; /* current filename */
54289Snakanon
55289Snakanon int
main(int argc,char * argv[],char * envp[])56289Snakanon main(int argc, char *argv[], char *envp[])
570Sstevel@tonic-gate {
580Sstevel@tonic-gate uchar *fs = NULL;
590Sstevel@tonic-gate char *nl_radix;
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * At this point, numbers are still scanned as in
620Sstevel@tonic-gate * the POSIX locale.
630Sstevel@tonic-gate * (POSIX.2, volume 2, P867, L4742-4757)
640Sstevel@tonic-gate */
650Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
660Sstevel@tonic-gate (void) setlocale(LC_NUMERIC, "C");
670Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
680Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
71289Snakanon cmdname = (uchar *)argv[0];
720Sstevel@tonic-gate if (argc == 1) {
73289Snakanon (void) fprintf(stderr, gettext(
74289Snakanon "Usage: %s [-f programfile | 'program'] [-Ffieldsep] "
75289Snakanon "[-v var=value] [files]\n"), cmdname);
760Sstevel@tonic-gate exit(1);
770Sstevel@tonic-gate }
78289Snakanon (void) signal(SIGFPE, fpecatch);
790Sstevel@tonic-gate yyin = NULL;
800Sstevel@tonic-gate syminit();
810Sstevel@tonic-gate while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
820Sstevel@tonic-gate if (strcmp(argv[1], "--") == 0) {
830Sstevel@tonic-gate /* explicit end of args */
840Sstevel@tonic-gate argc--;
850Sstevel@tonic-gate argv++;
860Sstevel@tonic-gate break;
870Sstevel@tonic-gate }
880Sstevel@tonic-gate switch (argv[1][1]) {
890Sstevel@tonic-gate case 'f': /* next argument is program filename */
900Sstevel@tonic-gate argc--;
910Sstevel@tonic-gate argv++;
920Sstevel@tonic-gate if (argc <= 1)
930Sstevel@tonic-gate ERROR "no program filename" FATAL;
94289Snakanon pfile = realloc(pfile, sizeof (uchar *) * (npfile + 1));
95289Snakanon if (pfile == NULL)
96289Snakanon ERROR "out of space in main" FATAL;
97289Snakanon pfile[npfile++] = (uchar *)argv[1];
980Sstevel@tonic-gate break;
990Sstevel@tonic-gate case 'F': /* set field separator */
1000Sstevel@tonic-gate if (argv[1][2] != 0) { /* arg is -Fsomething */
1010Sstevel@tonic-gate /* wart: t=>\t */
1020Sstevel@tonic-gate if (argv[1][2] == 't' && argv[1][3] == 0)
1030Sstevel@tonic-gate fs = (uchar *) "\t";
1040Sstevel@tonic-gate else if (argv[1][2] != 0)
105289Snakanon fs = (uchar *)&argv[1][2];
1060Sstevel@tonic-gate } else { /* arg is -F something */
1070Sstevel@tonic-gate argc--; argv++;
1080Sstevel@tonic-gate if (argc > 1) {
1090Sstevel@tonic-gate /* wart: t=>\t */
1100Sstevel@tonic-gate if (argv[1][0] == 't' &&
111289Snakanon argv[1][1] == 0)
1120Sstevel@tonic-gate fs = (uchar *) "\t";
1130Sstevel@tonic-gate else if (argv[1][0] != 0)
114289Snakanon fs = (uchar *)&argv[1][0];
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate if (fs == NULL || *fs == '\0')
1180Sstevel@tonic-gate ERROR "field separator FS is empty" WARNING;
1190Sstevel@tonic-gate break;
1200Sstevel@tonic-gate case 'v': /* -v a=1 to be done NOW. one -v for each */
1210Sstevel@tonic-gate if (argv[1][2] == '\0' && --argc > 1 &&
122289Snakanon isclvar((uchar *)(++argv)[1]))
123289Snakanon setclvar((uchar *)argv[1]);
1240Sstevel@tonic-gate break;
1250Sstevel@tonic-gate case 'd':
1260Sstevel@tonic-gate dbg = atoi(&argv[1][2]);
1270Sstevel@tonic-gate if (dbg == 0)
1280Sstevel@tonic-gate dbg = 1;
129289Snakanon (void) printf("awk %s\n", version);
1300Sstevel@tonic-gate break;
1310Sstevel@tonic-gate default:
1320Sstevel@tonic-gate ERROR "unknown option %s ignored", argv[1] WARNING;
1330Sstevel@tonic-gate break;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate argc--;
1360Sstevel@tonic-gate argv++;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate /* argv[1] is now the first argument */
1390Sstevel@tonic-gate if (npfile == 0) { /* no -f; first argument is program */
1400Sstevel@tonic-gate if (argc <= 1)
1410Sstevel@tonic-gate ERROR "no program given" FATAL;
1420Sstevel@tonic-gate dprintf(("program = |%s|\n", argv[1]));
143289Snakanon lexprog = (uchar *)argv[1];
1440Sstevel@tonic-gate argc--;
1450Sstevel@tonic-gate argv++;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate compile_time = 1;
148289Snakanon argv[0] = (char *)cmdname; /* put prog name at front of arglist */
1490Sstevel@tonic-gate dprintf(("argc=%d, argv[0]=%s\n", argc, argv[0]));
150289Snakanon arginit(argc, (uchar **)argv);
151289Snakanon envinit((uchar **)envp);
152*11053SSurya.Prakki@Sun.COM (void) yyparse();
1530Sstevel@tonic-gate if (fs)
154289Snakanon *FS = qstring(fs, '\0');
1550Sstevel@tonic-gate dprintf(("errorflag=%d\n", errorflag));
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate * done parsing, so now activate the LC_NUMERIC
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1600Sstevel@tonic-gate nl_radix = nl_langinfo(RADIXCHAR);
1610Sstevel@tonic-gate if (nl_radix)
1620Sstevel@tonic-gate radixpoint = *nl_radix;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate if (errorflag == 0) {
1650Sstevel@tonic-gate compile_time = 0;
1660Sstevel@tonic-gate run(winner);
1670Sstevel@tonic-gate } else
1680Sstevel@tonic-gate bracecheck();
169289Snakanon return (errorflag);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
172289Snakanon int
pgetc(void)173289Snakanon pgetc(void) /* get program character */
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate int c;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate for (;;) {
1780Sstevel@tonic-gate if (yyin == NULL) {
1790Sstevel@tonic-gate if (curpfile >= npfile)
1800Sstevel@tonic-gate return (EOF);
181289Snakanon yyin = (strcmp((char *)pfile[curpfile], "-") == 0) ?
182289Snakanon stdin : fopen((char *)pfile[curpfile], "r");
183289Snakanon if (yyin == NULL) {
1840Sstevel@tonic-gate ERROR "can't open file %s",
185289Snakanon pfile[curpfile] FATAL;
186289Snakanon }
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate if ((c = getc(yyin)) != EOF)
1890Sstevel@tonic-gate return (c);
190289Snakanon (void) fclose(yyin);
1910Sstevel@tonic-gate yyin = NULL;
1920Sstevel@tonic-gate curpfile++;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate }
195