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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 /* 27 * Copyright (C) Lucent Technologies 1997 28 * All Rights Reserved 29 * 30 * Permission to use, copy, modify, and distribute this software and 31 * its documentation for any purpose and without fee is hereby 32 * granted, provided that the above copyright notice appear in all 33 * copies and that both that the copyright notice and this 34 * permission notice and warranty disclaimer appear in supporting 35 * documentation, and that the name Lucent Technologies or any of 36 * its entities not be used in advertising or publicity pertaining 37 * to distribution of the software without specific, written prior 38 * permission. 39 * 40 * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 41 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 42 * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 43 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 44 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 45 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 46 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 47 * THIS SOFTWARE. 48 */ 49 50 #ifndef AWK_H 51 #define AWK_H 52 53 #include <sys/types.h> 54 #include <assert.h> 55 #include <ctype.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <libintl.h> 60 #include <limits.h> 61 62 typedef double Awkfloat; 63 typedef unsigned char uchar; 64 65 #define xfree(a) { if ((a) != NULL) { free((void *)(a)); (a) = NULL; } } 66 67 #define DEBUG 68 #ifdef DEBUG 69 /* uses have to be doubly parenthesized */ 70 #define dprintf(x) if (dbg) (void) printf x 71 #else 72 #define dprintf(x) 73 #endif 74 75 extern int compile_time; /* 1 if compiling, 0 if running */ 76 extern int safe; /* 0 => unsafe, 1 => safe */ 77 78 #define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */ 79 80 extern uchar **FS; 81 extern uchar **RS; 82 extern uchar **ORS; 83 extern uchar **OFS; 84 extern uchar **OFMT; 85 extern Awkfloat *NR; 86 extern Awkfloat *FNR; 87 extern Awkfloat *NF; 88 extern uchar **FILENAME; 89 extern uchar **SUBSEP; 90 extern Awkfloat *RSTART; 91 extern Awkfloat *RLENGTH; 92 93 extern uchar *record; 94 extern size_t record_size; 95 extern int errorflag; 96 extern int donefld; /* 1 if record broken into fields */ 97 extern int donerec; /* 1 if record is valid (no fld has changed */ 98 99 extern uchar *patbeg; /* beginning of pattern matched */ 100 extern int patlen; /* length. set in b.c */ 101 102 /* Cell: all information about a variable or constant */ 103 104 typedef struct Cell { 105 uchar ctype; /* OCELL, OBOOL, OJUMP, etc. */ 106 uchar csub; /* CCON, CTEMP, CFLD, etc. */ 107 uchar *nval; /* name, for variables only */ 108 uchar *sval; /* string value */ 109 Awkfloat fval; /* value as number */ 110 int tval; 111 /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */ 112 struct Cell *cnext; /* ptr to next if chained */ 113 } Cell; 114 115 typedef struct Array { /* symbol table array */ 116 int nelem; /* elements in table right now */ 117 int size; /* size of tab */ 118 Cell **tab; /* hash table pointers */ 119 } Array; 120 121 #define NSYMTAB 50 /* initial size of a symbol table */ 122 extern Array *symtab, *makesymtab(int); 123 extern Cell *setsymtab(const uchar *, const uchar *, Awkfloat, 124 unsigned int, Array *); 125 extern Cell *lookup(const uchar *, Array *); 126 127 extern Cell *recloc; /* location of input record */ 128 extern Cell *nrloc; /* NR */ 129 extern Cell *fnrloc; /* FNR */ 130 extern Cell *nfloc; /* NF */ 131 extern Cell *rstartloc; /* RSTART */ 132 extern Cell *rlengthloc; /* RLENGTH */ 133 134 /* Cell.tval values: */ 135 #define NUM 01 /* number value is valid */ 136 #define STR 02 /* string value is valid */ 137 #define DONTFREE 04 /* string space is not freeable */ 138 #define CON 010 /* this is a constant */ 139 #define ARR 020 /* this is an array */ 140 #define FCN 040 /* this is a function name */ 141 #define FLD 0100 /* this is a field $1, $2, ... */ 142 #define REC 0200 /* this is $0 */ 143 144 145 extern Awkfloat setfval(Cell *, Awkfloat), getfval(Cell *), r_getfval(Cell *); 146 extern uchar *setsval(Cell *, const uchar *); 147 extern uchar *getsval(Cell *); 148 extern uchar *tostring(const uchar *); 149 extern uchar *tokname(int); 150 extern uchar *qstring(const uchar *, int); 151 152 /* function types */ 153 #define FLENGTH 1 154 #define FSQRT 2 155 #define FEXP 3 156 #define FLOG 4 157 #define FINT 5 158 #define FSYSTEM 6 159 #define FRAND 7 160 #define FSRAND 8 161 #define FSIN 9 162 #define FCOS 10 163 #define FATAN 11 164 #define FTOUPPER 12 165 #define FTOLOWER 13 166 #define FFLUSH 14 167 168 /* Node: parse tree is made of nodes, with Cell's at bottom */ 169 170 typedef struct Node { 171 int ntype; 172 struct Node *nnext; 173 off_t lineno; 174 int nobj; 175 struct Node *narg[1]; 176 /* variable: actual size set by calling malloc */ 177 } Node; 178 179 #define NIL ((Node *)0) 180 181 extern Node *winner; 182 extern Node *nullstat; 183 extern Node *nullnode; 184 185 /* ctypes */ 186 #define OCELL 1 187 #define OBOOL 2 188 #define OJUMP 3 189 190 /* Cell subtypes: csub */ 191 #define CFREE 7 192 #define CCOPY 6 193 #define CCON 5 194 #define CTEMP 4 195 #define CNAME 3 196 #define CVAR 2 197 #define CFLD 1 198 #define CUNK 0 199 200 /* bool subtypes */ 201 #define BTRUE 11 202 #define BFALSE 12 203 204 /* jump subtypes */ 205 #define JEXIT 21 206 #define JNEXT 22 207 #define JBREAK 23 208 #define JCONT 24 209 #define JRET 25 210 #define JNEXTFILE 26 211 212 /* node types */ 213 #define NVALUE 1 214 #define NSTAT 2 215 #define NEXPR 3 216 #define NFIELD 4 217 218 extern Cell *(*proctab[])(Node **, int); 219 extern Cell *nullproc(Node **, int); 220 extern int pairstack[], paircnt; 221 222 extern int pgetc(void); 223 extern Node *stat1(int, Node *); 224 extern Node *stat2(int, Node *, Node *); 225 extern Node *stat3(int, Node *, Node *, Node *); 226 extern Node *stat4(int, Node *, Node *, Node *, Node *); 227 extern Node *pa2stat(Node *, Node *, Node *); 228 extern Node *op1(int, Node *); 229 extern Node *op2(int, Node *, Node *); 230 extern Node *op3(int, Node *, Node *, Node *); 231 extern Node *op4(int, Node *, Node *, Node *, Node *); 232 extern Node *linkum(Node *, Node *); 233 extern Node *celltonode(Cell *, int); 234 extern Node *rectonode(void), *exptostat(Node *); 235 extern Node *makearr(Node *); 236 extern Node *itonp(int); 237 238 #define notlegal(n) \ 239 (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc) 240 #define isvalue(n) ((n)->ntype == NVALUE) 241 #define isexpr(n) ((n)->ntype == NEXPR) 242 #define isjump(n) ((n)->ctype == OJUMP) 243 #define isexit(n) ((n)->csub == JEXIT) 244 #define isbreak(n) ((n)->csub == JBREAK) 245 #define iscont(n) ((n)->csub == JCONT) 246 #define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE) 247 #define isret(n) ((n)->csub == JRET) 248 #define isrec(n) ((n)->tval & REC) 249 #define isfld(n) ((n)->tval & FLD) 250 #define isstr(n) ((n)->tval & STR) 251 #define isnum(n) ((n)->tval & NUM) 252 #define isarr(n) ((n)->tval & ARR) 253 #define isfcn(n) ((n)->tval & FCN) 254 #define istrue(n) ((n)->csub == BTRUE) 255 #define istemp(n) ((n)->csub == CTEMP) 256 #define isargument(n) ((n)->nobj == ARG) 257 /* #define freeable(p) (!((p)->tval & DONTFREE)) */ 258 #define freeable(p) (((p)->tval & (STR|DONTFREE)) == STR) 259 260 #define NCHARS (256+3) /* 256 handles 8-bit chars; 128 does 7-bit */ 261 /* watch out in match(), etc. */ 262 #define NSTATES 32 263 264 typedef struct rrow { 265 long ltype; /* long avoids pointer warnings on 64-bit */ 266 union { 267 int i; 268 Node *np; 269 uchar *up; 270 } lval; /* because Al stores a pointer in it! */ 271 int *lfollow; 272 } rrow; 273 274 typedef struct fa { 275 uchar gototab[NSTATES][NCHARS]; 276 uchar out[NSTATES]; 277 uchar *restr; 278 int *posns[NSTATES]; 279 int anchor; 280 int use; 281 int initstat; 282 int curstat; 283 int accept; 284 int reset; 285 struct rrow re[1]; /* variable: actual size set by calling malloc */ 286 } fa; 287 288 /* b.c */ 289 extern fa *makedfa(const uchar *, int); 290 extern int nematch(fa *, const uchar *); 291 extern int match(fa *, const uchar *); 292 extern int pmatch(fa *, const uchar *); 293 294 /* lex.c */ 295 extern void unput(int); 296 extern void unputstr(const char *); 297 extern void startreg(void); 298 299 /* lib.c */ 300 extern void recinit(unsigned int); 301 extern void growfldtab(int); 302 extern int isclvar(const uchar *); 303 extern int is_number(const uchar *); 304 extern void setclvar(uchar *); 305 extern int readrec(uchar **, size_t *, FILE *); 306 extern void bracecheck(void); 307 extern void syminit(void); 308 extern void yyerror(char *); 309 extern void fldbld(void); 310 extern void recbld(void); 311 extern int getrec(uchar **, size_t *, int); 312 extern Cell *fieldadr(int); 313 extern void newfld(int); 314 extern Cell *getfld(int); 315 extern int fldidx(Cell *); 316 extern void SYNTAX(const char *, ...); 317 extern void FATAL(const char *, ...) __attribute__((__noreturn__)); 318 extern void WARNING(const char *, ...); 319 extern void error(void); 320 extern double errcheck(double, const char *); 321 extern void fpecatch(int); 322 extern void nextfile(void); 323 324 extern int donefld; 325 extern int donerec; 326 extern uchar *record; 327 extern size_t record_size; 328 329 /* main.c */ 330 extern int dbg; 331 extern uchar *cmdname; 332 extern uchar *lexprog; 333 extern int compile_time; 334 extern char radixpoint; 335 extern uchar *cursource(void); 336 337 /* tran.c */ 338 extern void syminit(void); 339 extern void arginit(int, uchar **); 340 extern void envinit(uchar **); 341 extern void freesymtab(Cell *); 342 extern void freeelem(Cell *, const uchar *); 343 extern void funnyvar(Cell *, char *); 344 extern int hash(const uchar *, int); 345 extern Awkfloat *ARGC; 346 347 /* run.c */ 348 extern int adjbuf(uchar **, size_t *, int, int, uchar **, const char *); 349 extern void run(Node *); 350 351 extern int paircnt; 352 extern Node *winner; 353 354 #ifndef input 355 extern int input(void); 356 #endif 357 extern int yyparse(void); 358 extern FILE *yyin; 359 extern off_t lineno; 360 361 /* parse.c */ 362 extern int ptoi(void *); 363 extern int isarg(const uchar *); 364 extern void defn(Cell *, Node *, Node *); 365 366 /* proc */ 367 extern Cell *nullproc(Node **, int); 368 extern Cell *program(Node **, int); 369 extern Cell *boolop(Node **, int); 370 extern Cell *relop(Node **, int); 371 extern Cell *array(Node **, int); 372 extern Cell *indirect(Node **, int); 373 extern Cell *substr(Node **, int); 374 extern Cell *sub(Node **, int); 375 extern Cell *gsub(Node **, int); 376 extern Cell *sindex(Node **, int); 377 extern Cell *awksprintf(Node **, int); 378 extern Cell *arith(Node **, int); 379 extern Cell *incrdecr(Node **, int); 380 extern Cell *cat(Node **, int); 381 extern Cell *pastat(Node **, int); 382 extern Cell *dopa2(Node **, int); 383 extern Cell *matchop(Node **, int); 384 extern Cell *intest(Node **, int); 385 extern Cell *awkprintf(Node **, int); 386 extern Cell *printstat(Node **, int); 387 extern Cell *closefile(Node **, int); 388 extern Cell *awkdelete(Node **, int); 389 extern Cell *split(Node **, int); 390 extern Cell *assign(Node **, int); 391 extern Cell *condexpr(Node **, int); 392 extern Cell *ifstat(Node **, int); 393 extern Cell *whilestat(Node **, int); 394 extern Cell *forstat(Node **, int); 395 extern Cell *dostat(Node **, int); 396 extern Cell *instat(Node **, int); 397 extern Cell *jump(Node **, int); 398 extern Cell *bltin(Node **, int); 399 extern Cell *call(Node **, int); 400 extern Cell *arg(Node **, int); 401 extern Cell *getnf(Node **, int); 402 extern Cell *awkgetline(Node **, int); 403 404 #endif /* AWK_H */