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