1*271018d0Smillert /* $OpenBSD: main.c,v 1.7 1999/04/20 17:31:30 millert Exp $ */ 26ab05f83Stholo /**************************************************************** 307edfa4aSkstailey Copyright (C) Lucent Technologies 1997 46ab05f83Stholo All Rights Reserved 56ab05f83Stholo 66ab05f83Stholo Permission to use, copy, modify, and distribute this software and 76ab05f83Stholo its documentation for any purpose and without fee is hereby 86ab05f83Stholo granted, provided that the above copyright notice appear in all 96ab05f83Stholo copies and that both that the copyright notice and this 106ab05f83Stholo permission notice and warranty disclaimer appear in supporting 1107edfa4aSkstailey documentation, and that the name Lucent Technologies or any of 1207edfa4aSkstailey its entities not be used in advertising or publicity pertaining 1307edfa4aSkstailey to distribution of the software without specific, written prior 1407edfa4aSkstailey permission. 156ab05f83Stholo 1607edfa4aSkstailey LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 1707edfa4aSkstailey INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 1807edfa4aSkstailey IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 1907edfa4aSkstailey SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 2007edfa4aSkstailey WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 2107edfa4aSkstailey IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 2207edfa4aSkstailey ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 2307edfa4aSkstailey THIS SOFTWARE. 246ab05f83Stholo ****************************************************************/ 256ab05f83Stholo 26*271018d0Smillert char *version = "version 19990416"; 276ab05f83Stholo 286ab05f83Stholo #define DEBUG 296ab05f83Stholo #include <stdio.h> 306ab05f83Stholo #include <ctype.h> 31b2698ba9Smillert #include <locale.h> 326ab05f83Stholo #include <stdlib.h> 336ab05f83Stholo #include <string.h> 346ab05f83Stholo #include <signal.h> 356ab05f83Stholo #include "awk.h" 3607edfa4aSkstailey #include "ytab.h" 376ab05f83Stholo 386ab05f83Stholo extern char **environ; 396ab05f83Stholo extern int nfields; 40a4fa8700Smillert extern char *__progname; 416ab05f83Stholo 426ab05f83Stholo int dbg = 0; 436ab05f83Stholo char *cmdname; /* gets argv[0] for error messages */ 446ab05f83Stholo extern FILE *yyin; /* lex input file */ 456ab05f83Stholo char *lexprog; /* points to program argument if it exists */ 466ab05f83Stholo extern int errorflag; /* non-zero if any syntax errors; set by yyerror */ 476ab05f83Stholo int compile_time = 2; /* for error printing: */ 486ab05f83Stholo /* 2 = cmdline, 1 = compile, 0 = running */ 496ab05f83Stholo 506ab05f83Stholo char *pfile[20]; /* program filenames from -f's */ 516ab05f83Stholo int npfile = 0; /* number of filenames */ 526ab05f83Stholo int curpfile = 0; /* current filename */ 536ab05f83Stholo 5407edfa4aSkstailey int safe = 0; /* 1 => "safe" mode */ 5507edfa4aSkstailey 566ab05f83Stholo int main(int argc, char *argv[]) 576ab05f83Stholo { 586ab05f83Stholo char *fs = NULL, *marg; 596ab05f83Stholo int temp; 606ab05f83Stholo 6128f4856aStholo setlocale(LC_ALL, ""); 6228f4856aStholo 63a4fa8700Smillert cmdname = __progname; 646ab05f83Stholo if (argc == 1) { 6555c82563Sangelos fprintf(stderr, "Usage: %s [-f programfile | 'program'] [-Ffieldsep] [-v var=value] [-safe] [-mrn] [-mfn] [files]\n", cmdname); 666ab05f83Stholo exit(1); 676ab05f83Stholo } 686ab05f83Stholo signal(SIGFPE, fpecatch); 696ab05f83Stholo yyin = NULL; 706ab05f83Stholo symtab = makesymtab(NSYMTAB); 716ab05f83Stholo while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') { 7207edfa4aSkstailey if (strcmp(argv[1], "--") == 0) { /* explicit end of args */ 736ab05f83Stholo argc--; 746ab05f83Stholo argv++; 756ab05f83Stholo break; 766ab05f83Stholo } 776ab05f83Stholo switch (argv[1][1]) { 7807edfa4aSkstailey case 's': 7907edfa4aSkstailey if (strcmp(argv[1], "-safe") == 0) 8007edfa4aSkstailey safe = 1; 8107edfa4aSkstailey break; 826ab05f83Stholo case 'f': /* next argument is program filename */ 836ab05f83Stholo argc--; 846ab05f83Stholo argv++; 856ab05f83Stholo if (argc <= 1) 866ab05f83Stholo ERROR "no program filename" FATAL; 876ab05f83Stholo pfile[npfile++] = argv[1]; 886ab05f83Stholo break; 896ab05f83Stholo case 'F': /* set field separator */ 906ab05f83Stholo if (argv[1][2] != 0) { /* arg is -Fsomething */ 916ab05f83Stholo if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */ 9207edfa4aSkstailey fs = "\t"; 936ab05f83Stholo else if (argv[1][2] != 0) 946ab05f83Stholo fs = &argv[1][2]; 956ab05f83Stholo } else { /* arg is -F something */ 966ab05f83Stholo argc--; argv++; 976ab05f83Stholo if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */ 9807edfa4aSkstailey fs = "\t"; 996ab05f83Stholo else if (argc > 1 && argv[1][0] != 0) 1006ab05f83Stholo fs = &argv[1][0]; 1016ab05f83Stholo } 1026ab05f83Stholo if (fs == NULL || *fs == '\0') 1036ab05f83Stholo ERROR "field separator FS is empty" WARNING; 1046ab05f83Stholo break; 1056ab05f83Stholo case 'v': /* -v a=1 to be done NOW. one -v for each */ 1066ab05f83Stholo if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1])) 1076ab05f83Stholo setclvar(argv[1]); 1086ab05f83Stholo break; 1096ab05f83Stholo case 'm': /* more memory: -mr=record, -mf=fields */ 11007edfa4aSkstailey /* no longer needed */ 1116ab05f83Stholo marg = argv[1]; 1126ab05f83Stholo if (argv[1][3]) 1136ab05f83Stholo temp = atoi(&argv[1][3]); 1146ab05f83Stholo else { 1156ab05f83Stholo argv++; argc--; 1166ab05f83Stholo temp = atoi(&argv[1][0]); 1176ab05f83Stholo } 1186ab05f83Stholo switch (marg[2]) { 1196ab05f83Stholo case 'r': recsize = temp; break; 1206ab05f83Stholo case 'f': nfields = temp; break; 1216ab05f83Stholo default: ERROR "unknown option %s\n", marg FATAL; 1226ab05f83Stholo } 1236ab05f83Stholo break; 1246ab05f83Stholo case 'd': 1256ab05f83Stholo dbg = atoi(&argv[1][2]); 1266ab05f83Stholo if (dbg == 0) 1276ab05f83Stholo dbg = 1; 1286ab05f83Stholo printf("awk %s\n", version); 1296ab05f83Stholo break; 130a4fa8700Smillert case 'V': /* added for exptools "standard" */ 131a4fa8700Smillert printf("awk %s\n", version); 132a4fa8700Smillert exit(0); 133a4fa8700Smillert break; 1346ab05f83Stholo default: 1356ab05f83Stholo ERROR "unknown option %s ignored", argv[1] WARNING; 1366ab05f83Stholo break; 1376ab05f83Stholo } 1386ab05f83Stholo argc--; 1396ab05f83Stholo argv++; 1406ab05f83Stholo } 1416ab05f83Stholo /* argv[1] is now the first argument */ 1426ab05f83Stholo if (npfile == 0) { /* no -f; first argument is program */ 1436ab05f83Stholo if (argc <= 1) { 1446ab05f83Stholo if (dbg) 1456ab05f83Stholo exit(0); 1466ab05f83Stholo ERROR "no program given" FATAL; 1476ab05f83Stholo } 1486ab05f83Stholo dprintf( ("program = |%s|\n", argv[1]) ); 1496ab05f83Stholo lexprog = argv[1]; 1506ab05f83Stholo argc--; 1516ab05f83Stholo argv++; 1526ab05f83Stholo } 1536ab05f83Stholo recinit(recsize); 1546ab05f83Stholo syminit(); 1556ab05f83Stholo compile_time = 1; 1566ab05f83Stholo argv[0] = cmdname; /* put prog name at front of arglist */ 1576ab05f83Stholo dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) ); 1586ab05f83Stholo arginit(argc, argv); 15907edfa4aSkstailey if (!safe) 1606ab05f83Stholo envinit(environ); 1616ab05f83Stholo yyparse(); 1626ab05f83Stholo if (fs) 16307edfa4aSkstailey *FS = qstring(fs, '\0'); 1646ab05f83Stholo dprintf( ("errorflag=%d\n", errorflag) ); 1656ab05f83Stholo if (errorflag == 0) { 1666ab05f83Stholo compile_time = 0; 1676ab05f83Stholo run(winner); 1686ab05f83Stholo } else 1696ab05f83Stholo bracecheck(); 1706ab05f83Stholo return(errorflag); 1716ab05f83Stholo } 1726ab05f83Stholo 1736ab05f83Stholo int pgetc(void) /* get 1 character from awk program */ 1746ab05f83Stholo { 1756ab05f83Stholo int c; 1766ab05f83Stholo 1776ab05f83Stholo for (;;) { 1786ab05f83Stholo if (yyin == NULL) { 1796ab05f83Stholo if (curpfile >= npfile) 1806ab05f83Stholo return EOF; 18107edfa4aSkstailey if (strcmp(pfile[curpfile], "-") == 0) 1826ab05f83Stholo yyin = stdin; 18307edfa4aSkstailey else if ((yyin = fopen(pfile[curpfile], "r")) == NULL) 1846ab05f83Stholo ERROR "can't open file %s", pfile[curpfile] FATAL; 185*271018d0Smillert lineno = 1; 1866ab05f83Stholo } 1876ab05f83Stholo if ((c = getc(yyin)) != EOF) 1886ab05f83Stholo return c; 1896ab05f83Stholo if (yyin != stdin) 1906ab05f83Stholo fclose(yyin); 1916ab05f83Stholo yyin = NULL; 1926ab05f83Stholo curpfile++; 1936ab05f83Stholo } 1946ab05f83Stholo } 195*271018d0Smillert 196*271018d0Smillert char *cursource(void) /* current source file name */ 197*271018d0Smillert { 198*271018d0Smillert if (npfile > 0) 199*271018d0Smillert return pfile[curpfile]; 200*271018d0Smillert else 201*271018d0Smillert return NULL; 202*271018d0Smillert } 203