1/**************************************************************** 2Copyright (C) Lucent Technologies 1997 3All Rights Reserved 4 5Permission to use, copy, modify, and distribute this software and 6its documentation for any purpose and without fee is hereby 7granted, provided that the above copyright notice appear in all 8copies and that both that the copyright notice and this 9permission notice and warranty disclaimer appear in supporting 10documentation, and that the name Lucent Technologies or any of 11its entities not be used in advertising or publicity pertaining 12to distribution of the software without specific, written prior 13permission. 14 15LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 17IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 18SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 20IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 21ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 22THIS SOFTWARE. 23****************************************************************/ 24 25const char *version = "version 20070501"; 26 27#define DEBUG 28#include <stdio.h> 29#include <ctype.h> 30#include <locale.h> 31#include <stdlib.h> 32#include <string.h> 33#include <signal.h> 34#include "awk.h" 35#include "ytab.h" 36 37#ifdef __APPLE__ 38#include "get_compat.h" 39#else 40#define COMPAT_MODE(func, mode) 1 41#endif 42 43extern char **environ; 44extern int nfields; 45 46int dbg = 0; 47char *cmdname; /* gets argv[0] for error messages */ 48extern FILE *yyin; /* lex input file */ 49char *lexprog; /* points to program argument if it exists */ 50extern int errorflag; /* non-zero if any syntax errors; set by yyerror */ 51int compile_time = 2; /* for error printing: */ 52 /* 2 = cmdline, 1 = compile, 0 = running */ 53 54#define MAX_PFILE 20 /* max number of -f's */ 55 56char *pfile[MAX_PFILE]; /* program filenames from -f's */ 57int npfile = 0; /* number of filenames */ 58int curpfile = 0; /* current filename */ 59 60int safe = 0; /* 1 => "safe" mode */ 61int Unix2003_compat; 62 63int main(int argc, char *argv[]) 64{ 65 const char *fs = NULL; 66 67 setlocale(LC_CTYPE, ""); 68 setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */ 69 cmdname = argv[0]; 70 if (argc == 1) { 71 fprintf(stderr, 72 "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n", 73 cmdname); 74 exit(1); 75 } 76 Unix2003_compat = COMPAT_MODE("bin/awk", "unix2003"); 77 signal(SIGFPE, fpecatch); 78 yyin = NULL; 79 symtab = makesymtab(NSYMTAB/NSYMTAB); 80 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') { 81 if (strcmp(argv[1],"-version") == 0 || strcmp(argv[1],"--version") == 0) { 82 printf("awk %s\n", version); 83 exit(0); 84 break; 85 } 86 if (strncmp(argv[1], "--", 2) == 0) { /* explicit end of args */ 87 argc--; 88 argv++; 89 break; 90 } 91 switch (argv[1][1]) { 92 case 's': 93 if (strcmp(argv[1], "-safe") == 0) 94 safe = 1; 95 break; 96 case 'f': /* next argument is program filename */ 97 argc--; 98 argv++; 99 if (argc <= 1) 100 FATAL("no program filename"); 101 if (npfile >= MAX_PFILE - 1) 102 FATAL("too many -f options"); 103 pfile[npfile++] = argv[1]; 104 break; 105 case 'F': /* set field separator */ 106 if (argv[1][2] != 0) { /* arg is -Fsomething */ 107 if (argv[1][2] == 't' && argv[1][3] == 0) /* wart: t=>\t */ 108 fs = "\t"; 109 else if (argv[1][2] != 0) 110 fs = &argv[1][2]; 111 } else { /* arg is -F something */ 112 argc--; argv++; 113 if (argc > 1 && argv[1][0] == 't' && argv[1][1] == 0) /* wart: t=>\t */ 114 fs = "\t"; 115 else if (argc > 1 && argv[1][0] != 0) 116 fs = &argv[1][0]; 117 } 118 if (fs == NULL || *fs == '\0') 119 WARNING("field separator FS is empty"); 120 break; 121 case 'v': /* -v a=1 to be done NOW. one -v for each */ 122 if (argv[1][2] == '\0' && --argc > 1 && isclvar((++argv)[1])) 123 setclvar(argv[1]); 124 else 125 FATAL("invalid -v option"); 126 break; 127 case 'd': 128 dbg = atoi(&argv[1][2]); 129 if (dbg == 0) 130 dbg = 1; 131 printf("awk %s\n", version); 132 break; 133 default: 134 WARNING("unknown option %s ignored", argv[1]); 135 break; 136 } 137 argc--; 138 argv++; 139 } 140 /* argv[1] is now the first argument */ 141 if (npfile == 0) { /* no -f; first argument is program */ 142 if (argc <= 1) { 143 if (dbg) 144 exit(0); 145 FATAL("no program given"); 146 } 147 dprintf( ("program = |%s|\n", argv[1]) ); 148 lexprog = argv[1]; 149 argc--; 150 argv++; 151 } 152 recinit(recsize); 153 syminit(); 154 compile_time = 1; 155 argv[0] = cmdname; /* put prog name at front of arglist */ 156 dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) ); 157 arginit(argc, argv); 158 if (!safe) 159 envinit(environ); 160 yyparse(); 161 setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */ 162 if (fs) 163 *FS = qstring(fs, '\0'); 164 dprintf( ("errorflag=%d\n", errorflag) ); 165 if (errorflag == 0) { 166 compile_time = 0; 167 run(winner); 168 } else 169 bracecheck(); 170 return(errorflag); 171} 172 173int pgetc(void) /* get 1 character from awk program */ 174{ 175 int c; 176 177 for (;;) { 178 if (yyin == NULL) { 179 if (curpfile >= npfile) 180 return EOF; 181 if (strcmp(pfile[curpfile], "-") == 0) 182 yyin = stdin; 183 else if ((yyin = fopen(pfile[curpfile], "r")) == NULL) 184 FATAL("can't open file %s", pfile[curpfile]); 185 lineno = 1; 186 } 187 if ((c = getc(yyin)) != EOF) 188 return c; 189 if (yyin != stdin) 190 fclose(yyin); 191 yyin = NULL; 192 curpfile++; 193 } 194} 195 196char *cursource(void) /* current source file name */ 197{ 198 if (npfile > 0) 199 return pfile[curpfile]; 200 else 201 return NULL; 202} 203