1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23 /* All Rights Reserved */ 24 25 /* 26 * Copyright (c) 1996, 2001 by Sun Microsystems, Inc. 27 * All rights reserved. 28 */ 29 30 #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 2.13 */ 31 32 #include <sys/types.h> 33 #include <limits.h> 34 35 typedef double Awkfloat; 36 typedef unsigned char uchar; 37 38 #define xfree(a) { if ((a) != NULL) { free(a); a = NULL; } } 39 40 #define DEBUG 41 #ifdef DEBUG 42 /* uses have to be doubly parenthesized */ 43 # define dprintf(x) if (dbg) printf x 44 #else 45 # define dprintf(x) 46 #endif 47 48 extern char errbuf[200]; 49 #define ERROR sprintf(errbuf, 50 #define FATAL ), error(1, errbuf) 51 #define WARNING ), error(0, errbuf) 52 #define SYNTAX ), yyerror(errbuf) 53 54 extern int compile_time; /* 1 if compiling, 0 if running */ 55 56 /* The standards (SUSV2) requires that Record size be atleast LINE_MAX. 57 * LINE_MAX is standard variable defined in limits.h. 58 * Though nawk is not standards compliant, we let RECSIZE 59 * grow with LINE_MAX instead of magic number 1024. 60 */ 61 #define RECSIZE (3 * LINE_MAX) /* sets limit on records, fields, etc., etc. */ 62 63 #define MAXFLD 500 64 65 extern uchar **FS; 66 extern uchar **RS; 67 extern uchar **ORS; 68 extern uchar **OFS; 69 extern uchar **OFMT; 70 extern Awkfloat *NR; 71 extern Awkfloat *FNR; 72 extern Awkfloat *NF; 73 extern uchar **FILENAME; 74 extern uchar **SUBSEP; 75 extern Awkfloat *RSTART; 76 extern Awkfloat *RLENGTH; 77 78 extern uchar *record; 79 extern int dbg; 80 extern off_t lineno; 81 extern int errorflag; 82 extern int donefld; /* 1 if record broken into fields */ 83 extern int donerec; /* 1 if record is valid (no fld has changed */ 84 85 extern uchar cbuf[RECSIZE]; /* miscellaneous character collection */ 86 87 extern uchar *patbeg; /* beginning of pattern matched */ 88 extern int patlen; /* length. set in b.c */ 89 90 /* Cell: all information about a variable or constant */ 91 92 typedef struct Cell { 93 uchar ctype; /* OCELL, OBOOL, OJUMP, etc. */ 94 uchar csub; /* CCON, CTEMP, CFLD, etc. */ 95 uchar *nval; /* name, for variables only */ 96 uchar *sval; /* string value */ 97 Awkfloat fval; /* value as number */ 98 unsigned tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */ 99 struct Cell *cnext; /* ptr to next if chained */ 100 } Cell; 101 102 typedef struct { /* symbol table array */ 103 int nelem; /* elements in table right now */ 104 int size; /* size of tab */ 105 Cell **tab; /* hash table pointers */ 106 } Array; 107 108 #define NSYMTAB 50 /* initial size of a symbol table */ 109 extern Array *symtab, *makesymtab(); 110 extern Cell *setsymtab(), *lookup(); 111 112 extern Cell *recloc; /* location of input record */ 113 extern Cell *nrloc; /* NR */ 114 extern Cell *fnrloc; /* FNR */ 115 extern Cell *nfloc; /* NF */ 116 extern Cell *rstartloc; /* RSTART */ 117 extern Cell *rlengthloc; /* RLENGTH */ 118 119 /* Cell.tval values: */ 120 #define NUM 01 /* number value is valid */ 121 #define STR 02 /* string value is valid */ 122 #define DONTFREE 04 /* string space is not freeable */ 123 #define CON 010 /* this is a constant */ 124 #define ARR 020 /* this is an array */ 125 #define FCN 040 /* this is a function name */ 126 #define FLD 0100 /* this is a field $1, $2, ... */ 127 #define REC 0200 /* this is $0 */ 128 129 #define freeable(p) (!((p)->tval & DONTFREE)) 130 131 Awkfloat setfval(), getfval(); 132 uchar *setsval(), *getsval(); 133 uchar *tostring(), *tokname(), *qstring(); 134 135 double log(), sqrt(), exp(), atof(); 136 137 /* function types */ 138 #define FLENGTH 1 139 #define FSQRT 2 140 #define FEXP 3 141 #define FLOG 4 142 #define FINT 5 143 #define FSYSTEM 6 144 #define FRAND 7 145 #define FSRAND 8 146 #define FSIN 9 147 #define FCOS 10 148 #define FATAN 11 149 #define FTOUPPER 12 150 #define FTOLOWER 13 151 152 /* Node: parse tree is made of nodes, with Cell's at bottom */ 153 154 typedef struct Node { 155 int ntype; 156 struct Node *nnext; 157 off_t lineno; 158 int nobj; 159 struct Node *narg[1]; /* variable: actual size set by calling malloc */ 160 } Node; 161 162 #define NIL ((Node *) 0) 163 164 extern Node *winner; 165 extern Node *nullstat; 166 extern Node *nullnode; 167 168 /* ctypes */ 169 #define OCELL 1 170 #define OBOOL 2 171 #define OJUMP 3 172 173 /* Cell subtypes: csub */ 174 #define CFREE 7 175 #define CCOPY 6 176 #define CCON 5 177 #define CTEMP 4 178 #define CNAME 3 179 #define CVAR 2 180 #define CFLD 1 181 182 /* bool subtypes */ 183 #define BTRUE 11 184 #define BFALSE 12 185 186 /* jump subtypes */ 187 #define JEXIT 21 188 #define JNEXT 22 189 #define JBREAK 23 190 #define JCONT 24 191 #define JRET 25 192 193 /* node types */ 194 #define NVALUE 1 195 #define NSTAT 2 196 #define NEXPR 3 197 #define NFIELD 4 198 199 extern Cell *(*proctab[])(); 200 extern Cell *nullproc(); 201 extern int pairstack[], paircnt; 202 extern Cell *fieldadr(); 203 204 extern Node *stat1(), *stat2(), *stat3(), *stat4(), *pa2stat(); 205 extern Node *op1(), *op2(), *op3(), *op4(); 206 extern Node *linkum(), *valtonode(), *rectonode(), *exptostat(); 207 extern Node *makearr(); 208 209 #define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc) 210 #define isvalue(n) ((n)->ntype == NVALUE) 211 #define isexpr(n) ((n)->ntype == NEXPR) 212 #define isjump(n) ((n)->ctype == OJUMP) 213 #define isexit(n) ((n)->csub == JEXIT) 214 #define isbreak(n) ((n)->csub == JBREAK) 215 #define iscont(n) ((n)->csub == JCONT) 216 #define isnext(n) ((n)->csub == JNEXT) 217 #define isret(n) ((n)->csub == JRET) 218 #define isstr(n) ((n)->tval & STR) 219 #define isnum(n) ((n)->tval & NUM) 220 #define isarr(n) ((n)->tval & ARR) 221 #define isfunc(n) ((n)->tval & FCN) 222 #define istrue(n) ((n)->csub == BTRUE) 223 #define istemp(n) ((n)->csub == CTEMP) 224 225 #define NCHARS (256+1) 226 #define NSTATES 32 227 228 typedef struct rrow { 229 int ltype; 230 int lval; 231 int *lfollow; 232 } rrow; 233 234 typedef struct fa { 235 uchar *restr; 236 int anchor; 237 int use; 238 uchar gototab[NSTATES][NCHARS]; 239 int *posns[NSTATES]; 240 uchar out[NSTATES]; 241 int initstat; 242 int curstat; 243 int accept; 244 int reset; 245 struct rrow re[1]; 246 } fa; 247 248 extern fa *makedfa(); 249