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
580148899SSurya Prakki * Common Development and Distribution License (the "License").
680148899SSurya Prakki * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
211ee2e5faSnakanon
227c478bd9Sstevel@tonic-gate /*
2380148899SSurya Prakki * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
241ee2e5faSnakanon * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
261ee2e5faSnakanon
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include <libintl.h>
357c478bd9Sstevel@tonic-gate #include <stdarg.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <values.h>
387c478bd9Sstevel@tonic-gate #include <langinfo.h>
397c478bd9Sstevel@tonic-gate #include "awk.h"
407c478bd9Sstevel@tonic-gate #include "y.tab.h"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate char *version = "version Oct 11, 1989";
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate int dbg = 0;
457c478bd9Sstevel@tonic-gate uchar *cmdname; /* gets argv[0] for error messages */
467c478bd9Sstevel@tonic-gate uchar *lexprog; /* points to program argument if it exists */
477c478bd9Sstevel@tonic-gate int compile_time = 2; /* for error printing: */
487c478bd9Sstevel@tonic-gate /* 2 = cmdline, 1 = compile, 0 = running */
497c478bd9Sstevel@tonic-gate char radixpoint = '.';
507c478bd9Sstevel@tonic-gate
511ee2e5faSnakanon static uchar **pfile = NULL; /* program filenames from -f's */
521ee2e5faSnakanon static int npfile = 0; /* number of filenames */
531ee2e5faSnakanon static int curpfile = 0; /* current filename */
541ee2e5faSnakanon
551ee2e5faSnakanon int
main(int argc,char * argv[],char * envp[])561ee2e5faSnakanon main(int argc, char *argv[], char *envp[])
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate uchar *fs = NULL;
597c478bd9Sstevel@tonic-gate char *nl_radix;
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * At this point, numbers are still scanned as in
627c478bd9Sstevel@tonic-gate * the POSIX locale.
637c478bd9Sstevel@tonic-gate * (POSIX.2, volume 2, P867, L4742-4757)
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
667c478bd9Sstevel@tonic-gate (void) setlocale(LC_NUMERIC, "C");
677c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
687c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
697c478bd9Sstevel@tonic-gate #endif
707c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
711ee2e5faSnakanon cmdname = (uchar *)argv[0];
727c478bd9Sstevel@tonic-gate if (argc == 1) {
731ee2e5faSnakanon (void) fprintf(stderr, gettext(
747c478bd9Sstevel@tonic-gate "Usage: %s [-f programfile | 'program'] [-Ffieldsep] "
757c478bd9Sstevel@tonic-gate "[-v var=value] [files]\n"), cmdname);
767c478bd9Sstevel@tonic-gate exit(1);
777c478bd9Sstevel@tonic-gate }
781ee2e5faSnakanon (void) signal(SIGFPE, fpecatch);
797c478bd9Sstevel@tonic-gate yyin = NULL;
807c478bd9Sstevel@tonic-gate syminit();
817c478bd9Sstevel@tonic-gate while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
827c478bd9Sstevel@tonic-gate if (strcmp(argv[1], "--") == 0) {
837c478bd9Sstevel@tonic-gate /* explicit end of args */
847c478bd9Sstevel@tonic-gate argc--;
857c478bd9Sstevel@tonic-gate argv++;
867c478bd9Sstevel@tonic-gate break;
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate switch (argv[1][1]) {
897c478bd9Sstevel@tonic-gate case 'f': /* next argument is program filename */
907c478bd9Sstevel@tonic-gate argc--;
917c478bd9Sstevel@tonic-gate argv++;
927c478bd9Sstevel@tonic-gate if (argc <= 1)
937c478bd9Sstevel@tonic-gate ERROR "no program filename" FATAL;
941ee2e5faSnakanon pfile = realloc(pfile, sizeof (uchar *) * (npfile + 1));
951ee2e5faSnakanon if (pfile == NULL)
961ee2e5faSnakanon ERROR "out of space in main" FATAL;
971ee2e5faSnakanon pfile[npfile++] = (uchar *)argv[1];
987c478bd9Sstevel@tonic-gate break;
997c478bd9Sstevel@tonic-gate case 'F': /* set field separator */
1007c478bd9Sstevel@tonic-gate if (argv[1][2] != 0) { /* arg is -Fsomething */
1017c478bd9Sstevel@tonic-gate /* wart: t=>\t */
1027c478bd9Sstevel@tonic-gate if (argv[1][2] == 't' && argv[1][3] == 0)
1037c478bd9Sstevel@tonic-gate fs = (uchar *) "\t";
1047c478bd9Sstevel@tonic-gate else if (argv[1][2] != 0)
1051ee2e5faSnakanon fs = (uchar *)&argv[1][2];
1067c478bd9Sstevel@tonic-gate } else { /* arg is -F something */
1077c478bd9Sstevel@tonic-gate argc--; argv++;
1087c478bd9Sstevel@tonic-gate if (argc > 1) {
1097c478bd9Sstevel@tonic-gate /* wart: t=>\t */
1107c478bd9Sstevel@tonic-gate if (argv[1][0] == 't' &&
1117c478bd9Sstevel@tonic-gate argv[1][1] == 0)
1127c478bd9Sstevel@tonic-gate fs = (uchar *) "\t";
1137c478bd9Sstevel@tonic-gate else if (argv[1][0] != 0)
1141ee2e5faSnakanon fs = (uchar *)&argv[1][0];
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate if (fs == NULL || *fs == '\0')
1187c478bd9Sstevel@tonic-gate ERROR "field separator FS is empty" WARNING;
1197c478bd9Sstevel@tonic-gate break;
1207c478bd9Sstevel@tonic-gate case 'v': /* -v a=1 to be done NOW. one -v for each */
1217c478bd9Sstevel@tonic-gate if (argv[1][2] == '\0' && --argc > 1 &&
1221ee2e5faSnakanon isclvar((uchar *)(++argv)[1]))
1231ee2e5faSnakanon setclvar((uchar *)argv[1]);
1247c478bd9Sstevel@tonic-gate break;
1257c478bd9Sstevel@tonic-gate case 'd':
1267c478bd9Sstevel@tonic-gate dbg = atoi(&argv[1][2]);
1277c478bd9Sstevel@tonic-gate if (dbg == 0)
1287c478bd9Sstevel@tonic-gate dbg = 1;
1291ee2e5faSnakanon (void) printf("awk %s\n", version);
1307c478bd9Sstevel@tonic-gate break;
1317c478bd9Sstevel@tonic-gate default:
1327c478bd9Sstevel@tonic-gate ERROR "unknown option %s ignored", argv[1] WARNING;
1337c478bd9Sstevel@tonic-gate break;
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate argc--;
1367c478bd9Sstevel@tonic-gate argv++;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate /* argv[1] is now the first argument */
1397c478bd9Sstevel@tonic-gate if (npfile == 0) { /* no -f; first argument is program */
140*0717e03dSYuri Pankov if (argc <= 1) {
141*0717e03dSYuri Pankov if (dbg)
142*0717e03dSYuri Pankov exit(0);
1437c478bd9Sstevel@tonic-gate ERROR "no program given" FATAL;
144*0717e03dSYuri Pankov }
1457c478bd9Sstevel@tonic-gate dprintf(("program = |%s|\n", argv[1]));
1461ee2e5faSnakanon lexprog = (uchar *)argv[1];
1477c478bd9Sstevel@tonic-gate argc--;
1487c478bd9Sstevel@tonic-gate argv++;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate compile_time = 1;
1511ee2e5faSnakanon argv[0] = (char *)cmdname; /* put prog name at front of arglist */
1527c478bd9Sstevel@tonic-gate dprintf(("argc=%d, argv[0]=%s\n", argc, argv[0]));
1531ee2e5faSnakanon arginit(argc, (uchar **)argv);
1541ee2e5faSnakanon envinit((uchar **)envp);
15580148899SSurya Prakki (void) yyparse();
1567c478bd9Sstevel@tonic-gate if (fs)
1571ee2e5faSnakanon *FS = qstring(fs, '\0');
1587c478bd9Sstevel@tonic-gate dprintf(("errorflag=%d\n", errorflag));
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate * done parsing, so now activate the LC_NUMERIC
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1637c478bd9Sstevel@tonic-gate nl_radix = nl_langinfo(RADIXCHAR);
1647c478bd9Sstevel@tonic-gate if (nl_radix)
1657c478bd9Sstevel@tonic-gate radixpoint = *nl_radix;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if (errorflag == 0) {
1687c478bd9Sstevel@tonic-gate compile_time = 0;
1697c478bd9Sstevel@tonic-gate run(winner);
1707c478bd9Sstevel@tonic-gate } else
1717c478bd9Sstevel@tonic-gate bracecheck();
1721ee2e5faSnakanon return (errorflag);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1751ee2e5faSnakanon int
pgetc(void)1761ee2e5faSnakanon pgetc(void) /* get program character */
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate int c;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate for (;;) {
1817c478bd9Sstevel@tonic-gate if (yyin == NULL) {
1827c478bd9Sstevel@tonic-gate if (curpfile >= npfile)
1837c478bd9Sstevel@tonic-gate return (EOF);
1847c478bd9Sstevel@tonic-gate yyin = (strcmp((char *)pfile[curpfile], "-") == 0) ?
1857c478bd9Sstevel@tonic-gate stdin : fopen((char *)pfile[curpfile], "r");
1861ee2e5faSnakanon if (yyin == NULL) {
1877c478bd9Sstevel@tonic-gate ERROR "can't open file %s",
1887c478bd9Sstevel@tonic-gate pfile[curpfile] FATAL;
1897c478bd9Sstevel@tonic-gate }
1901ee2e5faSnakanon }
1917c478bd9Sstevel@tonic-gate if ((c = getc(yyin)) != EOF)
1927c478bd9Sstevel@tonic-gate return (c);
1931ee2e5faSnakanon (void) fclose(yyin);
1947c478bd9Sstevel@tonic-gate yyin = NULL;
1957c478bd9Sstevel@tonic-gate curpfile++;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate }
198