main.c (8109B)
1 /* *************************************************************** 2 * copyright (c) lucent technologies 1997 3 * all rights reserved 4 * 5 * permission to use, copy, modify, and distribute this software and 6 * its documentation for any purpose and without fee is hereby 7 * granted, provided that the above copyright notice appear in all 8 * copies and that both that the copyright notice and this 9 * permission notice and warranty disclaimer appear in supporting 10 * documentation, and that the name lucent technologies or any of 11 * its entities not be used in advertising or publicity pertaining 12 * to distribution of the software without specific, written prior 13 * permission 14 * 15 * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 17 * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 20 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 21 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 22 * THIS SOFTWARE 23 * ************************************************************** */ 24 25 const char *version = "version 20260426"; 26 27 #define DEBUG 28 #if FEATURE_AWK_RANDOM 29 #ifndef _DEFAULT_SOURCE 30 #define _DEFAULT_SOURCE 31 #endif 32 #endif 33 #include "awk.h" 34 #include <ctype.h> 35 #include <locale.h> 36 #include <signal.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 41 extern char **environ; 42 extern int nfields; 43 44 int dbg = 0; 45 Awkfloat srand_seed = 1; 46 char *cmdname; /* gets argv[0] for error messages */ 47 extern FILE *yyin; /* lex input file */ 48 char *lexprog; /* points to program argument if it exists */ 49 extern int errorflag; /* non-zero if any syntax errors; set by yyerror */ 50 enum compile_states compile_time = ERROR_PRINTING; 51 52 static char **pfile; /* program filenames from -f's */ 53 static size_t maxpfile; /* max program filename */ 54 static size_t npfile; /* number of filenames */ 55 static size_t curpfile; /* current filename */ 56 57 bool CSV = false; /* true for csv input */ 58 59 bool safe = false; /* true => "safe" mode */ 60 61 size_t awk_mb_cur_max = 1; 62 63 static noreturn void 64 fpecatch( 65 int n 66 #ifdef SA_SIGINFO 67 , 68 siginfo_t *si, 69 void *uc 70 #endif 71 ) 72 { 73 (void)n; 74 #ifdef SA_SIGINFO 75 (void)uc; 76 #endif 77 #ifdef SA_SIGINFO 78 const char *mesg = NULL; 79 80 switch (si->si_code) { 81 case FPE_INTDIV: 82 mesg = "Integer divide by zero"; 83 break; 84 case FPE_INTOVF: 85 mesg = "Integer overflow"; 86 break; 87 case FPE_FLTDIV: 88 mesg = "Floating point divide by zero"; 89 break; 90 case FPE_FLTOVF: 91 mesg = "Floating point overflow"; 92 break; 93 case FPE_FLTUND: 94 mesg = "Floating point underflow"; 95 break; 96 case FPE_FLTRES: 97 mesg = "Floating point inexact result"; 98 break; 99 case FPE_FLTINV: 100 mesg = "Invalid Floating point operation"; 101 break; 102 case FPE_FLTSUB: 103 mesg = "Subscript out of range"; 104 break; 105 case 0: 106 default: 107 mesg = "Unknown error"; 108 break; 109 } 110 #endif 111 FATAL( 112 "floating point exception" 113 #ifdef SA_SIGINFO 114 ": %s", 115 mesg 116 #endif 117 ); 118 } 119 120 /* can this work with recursive calls? i dont think so 121 * void segvcatch(int n) 122 * { 123 * fatal("segfault. do you have an unbounded recursive call?", n); 124 * } 125 */ 126 127 static const char * 128 setfs(char *p) 129 { 130 /* wart: t=>\t */ 131 if (p[0] == 't' && p[1] == '\0') 132 return "\t"; 133 return p; 134 } 135 136 static char * 137 getarg(int *argc, char ***argv, const char *msg) 138 { 139 if ((*argv)[1][2] != '\0') { /* arg is -fsomething */ 140 return &(*argv)[1][2]; 141 } else { /* arg is -f something */ 142 (*argc)--; 143 (*argv)++; 144 if (*argc <= 1) 145 FATAL("%s", msg); 146 return (*argv)[1]; 147 } 148 } 149 150 int 151 main(int argc, char *argv[]) 152 { 153 const char *fs = NULL; 154 char *fn, *vn; 155 156 setlocale(LC_CTYPE, ""); 157 setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */ 158 awk_mb_cur_max = MB_CUR_MAX; 159 cmdname = argv[0]; 160 if (argc == 1) { 161 fprintf( 162 stderr, 163 "usage: %s [-F fs | --csv] [-v var=value] [-f progfile " 164 "| 'prog'] [file ...]\n", 165 cmdname 166 ); 167 exit(1); 168 } 169 #ifdef SA_SIGINFO 170 { 171 struct sigaction sa; 172 sa.sa_sigaction = fpecatch; 173 sa.sa_flags = SA_SIGINFO; 174 sigemptyset(&sa.sa_mask); 175 (void)sigaction(SIGFPE, &sa, NULL); 176 } 177 #else 178 (void)signal(SIGFPE, fpecatch); 179 #endif 180 /* signal(sigsegv, segvcatch); experiment */ 181 182 /* set and keep track of the random seed */ 183 srand_seed = 1; 184 srandom((unsigned long)srand_seed); 185 186 yyin = NULL; 187 symtab = makesymtab(NSYMTAB / NSYMTAB); 188 while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') { 189 if (strcmp(argv[1], "-version") == 0 || strcmp(argv[1], "--version") == 0) { 190 printf("awk %s\n", version); 191 return 0; 192 } 193 if (strcmp(argv[1], "--") == 0) { /* explicit end of args */ 194 argc--; 195 argv++; 196 break; 197 } 198 if (strcmp(argv[1], "--csv") == 0) { /* turn on csv input processing */ 199 CSV = true; 200 argc--; 201 argv++; 202 continue; 203 } 204 switch (argv[1][1]) { 205 case 's': 206 if (strcmp(argv[1], "-safe") == 0) 207 safe = true; 208 break; 209 case 'f': /* next argument is program filename */ 210 fn = getarg(&argc, &argv, "no program filename"); 211 if (npfile >= maxpfile) { 212 maxpfile += 20; 213 pfile = (char **)realloc(pfile, maxpfile * sizeof(*pfile)); 214 if (pfile == NULL) 215 FATAL( 216 "error allocating space for -f " 217 "options" 218 ); 219 } 220 pfile[npfile++] = fn; 221 break; 222 case 'F': /* set field separator */ 223 fs = setfs(getarg(&argc, &argv, "no field separator")); 224 break; 225 case 'v': /* -v a=1 to be done NOW. one -v for each */ 226 vn = getarg(&argc, &argv, "no variable name"); 227 if (isclvar(vn)) 228 setclvar(vn); 229 else 230 FATAL("invalid -v option argument: %s", vn); 231 break; 232 case 'd': 233 dbg = atoi(&argv[1][2]); 234 if (dbg == 0) 235 dbg = 1; 236 printf("awk %s\n", version); 237 break; 238 default: 239 WARNING("unknown option %s ignored", argv[1]); 240 break; 241 } 242 argc--; 243 argv++; 244 } 245 246 if (CSV && (fs != NULL || lookup("FS", symtab) != NULL)) 247 WARNING("danger: don't set FS when --csv is in effect"); 248 249 /* argv[1] is now the first argument */ 250 if (npfile == 0) { /* no -f; first argument is program */ 251 if (argc <= 1) { 252 if (dbg) 253 exit(0); 254 FATAL("no program given"); 255 } 256 DPRINTF("program = |%s|\n", argv[1]); 257 lexprog = argv[1]; 258 argc--; 259 argv++; 260 } 261 recinit(recsize); 262 syminit(); 263 compile_time = COMPILING; 264 argv[0] = cmdname; /* put prog name at front of arglist */ 265 DPRINTF("argc=%d, argv[0]=%s\n", argc, argv[0]); 266 arginit(argc, argv); 267 if (!safe) 268 envinit(environ); 269 yyparse(); 270 #if 0 271 /* doing this would comply with POSIX, but is not compatible with 272 * other awks and with what most users expect. so comment it out */ 273 setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */ 274 #endif 275 if (fs) 276 *FS = qstring(fs, '\0'); 277 DPRINTF("errorflag=%d\n", errorflag); 278 if (errorflag == 0) { 279 compile_time = RUNNING; 280 run(winner); 281 } else 282 bracecheck(); 283 return (errorflag); 284 } 285 286 int 287 pgetc(void) /* get 1 character from awk program */ 288 { 289 int c; 290 291 for (;;) { 292 if (yyin == NULL) { 293 if (curpfile >= npfile) 294 return EOF; 295 if (strcmp(pfile[curpfile], "-") == 0) 296 yyin = stdin; 297 else if ((yyin = fopen(pfile[curpfile], "r")) == NULL) 298 FATAL("can't open file %s", pfile[curpfile]); 299 lineno = 1; 300 } 301 if ((c = getc(yyin)) != EOF) 302 return c; 303 if (yyin != stdin) 304 fclose(yyin); 305 yyin = NULL; 306 curpfile++; 307 } 308 } 309 310 char * 311 cursource(void) /* current source file name */ 312 { 313 if (npfile > 0) 314 return pfile[curpfile < npfile ? curpfile : curpfile - 1]; 315 else 316 return NULL; 317 }