Path: icaen!news.uiowa.edu!uunet!dziuxsolim.rutgers.edu!pilot.njin.net!not-for-mail From: comp-sources-apple2@pilot.njin.net Newsgroups: comp.sources.apple2 Subject: v001SRC096: AWK -- 16-bit Port of AT&T AWK (GNO) 03/06 Date: 1 Jan 1995 17:39:18 -0500 Organization: Rutgers University Lines: 1308 Sender: jac@pilot.njin.net Approved: jac@pilot.njin.net Distribution: world Message-ID: <3e7aum$d61@pilot.njin.net> NNTP-Posting-Host: pilot.njin.net Submitted-By: Jawaid Bazyar (bazyar@netcom.com) Posting-number: Volume 1, Source 96 Archive-Name: gno/util/awk.03 Architecture: 2gs,UNIX Version-Number: 1.00 =makefile.gno -o/b.a: b.c awk.h proto.h y.tab.h - compile b.c keep=o/b - -o/main.a: main.c awk.h proto.h y.tab.h - compile main.c keep=o/main - -o/maketab.a: maketab.c awk.h proto.h y.tab.h - compile maketab.c keep=o/maketab - -o/parse.a: parse.c awk.h proto.h y.tab.h - compile parse.c keep=o/parse - -o/lib.a: lib.c awk.h proto.h y.tab.h - compile lib.c keep=o/lib - -o/run.a: run.c awk.h proto.h y.tab.h - compile run.c keep=o/run - -o/tran.a: tran.c awk.h proto.h y.tab.h - compile tran.c keep=o/tran - -o/y.tab.a: y.tab.c awk.h proto.h - compile y.tab.c keep=o/y.tab - -o/lex.yy.a: lex.yy.c awk.h proto.h - compile lex.yy.c keep=o/lex.yy - -o/proctab.a: proctab.c awk.h proto.h - compile proctab.c keep=o/proctab - -awk: o/main.root o/b.a o/parse.a o/lib.a o/run.a o/tran.a o/y.tab.a o/lex.yy.a o/proctab.a - link o/main o/b o/parse o/lib o/run o/tran o/y.tab o/lex.yy o/proctab keep=awk =maketab.c -/**************************************************************** -Copyright (C) AT&T 1993 -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of AT&T or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL AT&T OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. -****************************************************************/ - -/* - * this program makes the table to link function names - * and type indices that is used by execute() in run.c. - * it finds the indices in y.tab.h, produced by yacc. - */ - -#include -#include -#include -#include "awk.h" -#include "y.tab.h" - -struct xx -{ int token; - char *name; - char *pname; -} proc[] = { - { PROGRAM, "program", NULL }, - { BOR, "boolop", " || " }, - { AND, "boolop", " && " }, - { NOT, "boolop", " !" }, - { NE, "relop", " != " }, - { EQ, "relop", " == " }, - { LE, "relop", " <= " }, - { LT, "relop", " < " }, - { GE, "relop", " >= " }, - { GT, "relop", " > " }, - { ARRAY, "array", NULL }, - { INDIRECT, "indirect", "$(" }, - { SUBSTR, "substr", "substr" }, - { SUB, "sub", "sub" }, - { GSUB, "gsub", "gsub" }, - { INDEX, "sindex", "sindex" }, - { SPRINTF, "asprintf", "sprintf " }, - { ADD, "arith", " + " }, - { MINUS, "arith", " - " }, - { MULT, "arith", " * " }, - { DIVIDE, "arith", " / " }, - { MOD, "arith", " % " }, - { UMINUS, "arith", " -" }, - { POWER, "arith", " **" }, - { PREINCR, "incrdecr", "++" }, - { POSTINCR, "incrdecr", "++" }, - { PREDECR, "incrdecr", "--" }, - { POSTDECR, "incrdecr", "--" }, - { CAT, "cat", " " }, - { PASTAT, "pastat", NULL }, - { PASTAT2, "dopa2", NULL }, - { MATCH, "matchop", " ~ " }, - { NOTMATCH, "matchop", " !~ " }, - { MATCHFCN, "matchop", "matchop" }, - { INTEST, "intest", "intest" }, - { PRINTF, "aprintf", "printf" }, - { PRINT, "printstat", "print" }, - { CLOSE, "closefile", "closefile" }, - { DELETE, "adelete", "adelete" }, - { SPLIT, "split", "split" }, - { ASSIGN, "assign", " = " }, - { ADDEQ, "assign", " += " }, - { SUBEQ, "assign", " -= " }, - { MULTEQ, "assign", " *= " }, - { DIVEQ, "assign", " /= " }, - { MODEQ, "assign", " %= " }, - { POWEQ, "assign", " ^= " }, - { CONDEXPR, "condexpr", " ?: " }, - { IF, "ifstat", "if(" }, - { WHILE, "whilestat", "while(" }, - { FOR, "forstat", "for(" }, - { DO, "dostat", "do" }, - { IN, "instat", "instat" }, - { NEXT, "jump", "next" }, - { EXIT, "jump", "exit" }, - { BREAK, "jump", "break" }, - { CONTINUE, "jump", "continue" }, - { RETURN, "jump", "ret" }, - { BLTIN, "bltin", "bltin" }, - { CALL, "call", "call" }, - { ARG, "arg", "arg" }, - { VARNF, "getnf", "NF" }, - { GETLINE, "getline", "getline" }, - { 0, "", "" }, -}; - -#define SIZE (LASTTOKEN - FIRSTTOKEN + 1) -char *table[SIZE]; -char *names[SIZE]; - -int main(int argc, char **argv) -{ - struct xx *p; - int i, n, tok; - char c; - FILE *fp; - char buf[200], name[200], def[200]; - - printf("#include \n"); - printf("#include \"awk.h\"\n"); - printf("#include \"y.tab.h\"\n\n"); - for (i = SIZE; --i >= 0; ) - names[i] = ""; - - if ((fp = fopen("y.tab.h", "r")) == NULL) { - fprintf(stderr, "maketab can't open y.tab.h!\n"); - exit(1); - } - printf("static uchar *printname[%d] = {\n", SIZE); - i = 0; - while (fgets(buf, sizeof buf, fp) != NULL) { - n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok); - if (c != '#' || n != 4 && strcmp(def,"define") != 0) /* not a valid #define */ - continue; - if (tok < FIRSTTOKEN || tok > LASTTOKEN) { - fprintf(stderr, "maketab funny token %d %s\n", tok, buf); - exit(1); - } - names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1); - strcpy(names[tok-FIRSTTOKEN], name); - printf("\t(uchar *) \"%s\",\t/* %d */\n", name, tok); - i++; - } - printf("};\n\n"); - - for (p=proc; p->token!=0; p++) - table[p->token-FIRSTTOKEN] = p->name; - printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE); - for (i=0; i LASTTOKEN) {\n"); - printf(" sprintf(buf, \"token %%d\", n);\n"); - printf(" return buf;\n"); - printf(" }\n"); - printf(" return printname[n-FIRSTTOKEN];\n"); - printf("}\n"); - return 0; -} =parse.c -/**************************************************************** -Copyright (C) AT&T 1993 -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of AT&T or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL AT&T OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. -****************************************************************/ - -#define DEBUG -#include -#include -#include -#include "awk.h" -#include "y.tab.h" - -Node *nodealloc(int n) -{ - Node *x; - - x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *)); - if (x == NULL) - ERROR "out of space in nodealloc" FATAL; - x->nnext = NULL; - x->lineno = lineno; - return(x); -} - -Node *exptostat(Node *a) -{ - a->ntype = NSTAT; - return(a); -} - -Node *node1(int a, Node *b) -{ - Node *x; - - x = nodealloc(1); - x->nobj = a; - x->narg[0]=b; - return(x); -} - -Node *node2(int a, Node *b, Node *c) -{ - Node *x; - - x = nodealloc(2); - x->nobj = a; - x->narg[0] = b; - x->narg[1] = c; - return(x); -} - -Node *node3(int a, Node *b, Node *c, Node *d) -{ - Node *x; - - x = nodealloc(3); - x->nobj = a; - x->narg[0] = b; - x->narg[1] = c; - x->narg[2] = d; - return(x); -} - -Node *node4(int a, Node *b, Node *c, Node *d, Node *e) -{ - Node *x; - - x = nodealloc(4); - x->nobj = a; - x->narg[0] = b; - x->narg[1] = c; - x->narg[2] = d; - x->narg[3] = e; - return(x); -} - -Node *stat1(int a, Node *b) -{ - Node *x; - - x = node1(a,b); - x->ntype = NSTAT; - return(x); -} - -Node *stat2(int a, Node *b, Node *c) -{ - Node *x; - - x = node2(a,b,c); - x->ntype = NSTAT; - return(x); -} - -Node *stat3(int a, Node *b, Node *c, Node *d) -{ - Node *x; - - x = node3(a,b,c,d); - x->ntype = NSTAT; - return(x); -} - -Node *stat4(int a, Node *b, Node *c, Node *d, Node *e) -{ - Node *x; - - x = node4(a,b,c,d,e); - x->ntype = NSTAT; - return(x); -} - -Node *op1(int a, Node *b) -{ - Node *x; - - x = node1(a,b); - x->ntype = NEXPR; - return(x); -} - -Node *op2(int a, Node *b, Node *c) -{ - Node *x; - - x = node2(a,b,c); - x->ntype = NEXPR; - return(x); -} - -Node *op3(int a, Node *b, Node *c, Node *d) -{ - Node *x; - - x = node3(a,b,c,d); - x->ntype = NEXPR; - return(x); -} - -Node *op4(int a, Node *b, Node *c, Node *d, Node *e) -{ - Node *x; - - x = node4(a,b,c,d,e); - x->ntype = NEXPR; - return(x); -} - -Node *valtonode(Cell *a, int b) -{ - Node *x; - - a->ctype = OCELL; - a->csub = b; - x = node1(0, (Node *) a); - x->ntype = NVALUE; - return(x); -} - -Node *rectonode(void) /* make $0 into a Node */ -{ - return valtonode(recloc, CFLD); -} - -Node *makearr(Node *p) -{ - Cell *cp; - - if (isvalue(p)) { - cp = (Cell *) (p->narg[0]); - if (isfunc(cp)) - ERROR "%s is a function, not an array", cp->nval SYNTAX; - else if (!isarr(cp)) { - xfree(cp->sval); - cp->sval = (uchar *) makesymtab(NSYMTAB); - cp->tval = ARR; - } - } - return p; -} - -Node *pa2stat(Node *a, Node *b, Node *c) /* pat, pat {...} */ -{ - Node *x; - - x = node4(PASTAT2, a, b, c, (Node *) paircnt); - paircnt++; - x->ntype = NSTAT; - return(x); -} - -Node *linkum(Node *a, Node *b) -{ - Node *c; - - if (errorflag) /* don't link things that are wrong */ - return a; - if (a == NULL) - return(b); - else if (b == NULL) - return(a); - for (c = a; c->nnext != NULL; c = c->nnext) - ; - c->nnext = b; - return(a); -} - -void defn(Cell *v, Node *vl, Node *st) /* turn on FCN bit in definition, */ -{ /* body of function, arglist */ - Node *p; - int n; - - if (isarr(v)) { - ERROR "`%s' is an array name and a function name", v->nval SYNTAX; - return; - } - v->tval = FCN; - v->sval = (uchar *) st; - n = 0; /* count arguments */ - for (p = vl; p; p = p->nnext) - n++; - v->fval = n; - dprintf( ("defining func %s (%d args)\n", v->nval, (int)n) ); -} - -int isarg(uchar *s) /* is s in argument list for current function? */ -{ /* return -1 if not, otherwise arg # */ - extern Node *arglist; - Node *p = arglist; - int n; - - for (n = 0; p != 0; p = p->nnext, n++) - if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0) - return n; - return -1; -} =proto.h -/**************************************************************** -Copyright (C) AT&T 1993 -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of AT&T or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL AT&T OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. -****************************************************************/ - -/*extern int yywrap(void);*/ -extern void setfname(Cell *); -extern int constnode(Node *); -extern uchar *strnode(Node *); -extern Node *notnull(Node *); -extern int yyparse(void); - -extern int yylex(void); -extern void startreg(void); -extern int input(void); -extern void unput(int); -extern void unputstr(char *); -extern int yylook(void); -extern int yyback(int *, int); -extern int yyinput(void); - -extern fa *makedfa(uchar *, int); -extern fa *mkdfa(uchar *, int); -extern int makeinit(fa *, int); -extern void penter(Node *); -extern void freetr(Node *); -extern int hexstr(char **); -extern int quoted(char **); -extern uchar *cclenter(uchar *); -extern void overflo(uchar *); -extern void cfoll(fa *, Node *); -extern int first(Node *); -extern void follow(Node *); -extern int member(int, uchar *); -extern int match(fa *, uchar *); -extern int pmatch(fa *, uchar *); -extern int nematch(fa *, uchar *); -extern Node *reparse(uchar *); -extern Node *regexp(void); -extern Node *primary(void); -extern Node *concat(Node *); -extern Node *alt(Node *); -extern Node *unary(Node *); -extern int relex(void); -extern int cgoto(fa *, int, int); -extern void freefa(fa *); - -extern int main(int, uchar **); -extern int pgetc(void); - -extern Node *nodealloc(int); -extern Node *exptostat(Node *); -extern Node *node1(int, Node *); -extern Node *node2(int, Node *, Node *); -extern Node *node3(int, Node *, Node *, Node *); -extern Node *node4(int, Node *, Node *, Node *, Node *); -extern Node *stat3(int, Node *, Node *, Node *); -extern Node *op2(int, Node *, Node *); -extern Node *op1(int, Node *); -extern Node *stat1(int, Node *); -extern Node *op3(int, Node *, Node *, Node *); -extern Node *op4(int, Node *, Node *, Node *, Node *); -extern Node *stat2(int, Node *, Node *); -extern Node *stat4(int, Node *, Node *, Node *, Node *); -extern Node *valtonode(Cell *, int); -extern Node *rectonode(void); -extern Node *makearr(Node *); -extern Node *pa2stat(Node *, Node *, Node *); -extern Node *linkum(Node *, Node *); -extern void defn(Cell *, Node *, Node *); -extern int isarg(uchar *); -extern uchar *tokname(int); -extern Cell *(*proctab[])(Node **, int); - -extern void syminit(void); -extern void arginit(int, uchar **); -extern void envinit(uchar **); -extern Array *makesymtab(int); -extern void freesymtab(Cell *); -extern void freeelem(Cell *, uchar *); -extern Cell *setsymtab(uchar *, uchar *, double, unsigned, Array *); -extern int hash(uchar *, int); -extern void rehash(Array *); -extern Cell *lookup(uchar *, Array *); -extern double setfval(Cell *, double); -extern void funnyvar(Cell *, char *); -extern uchar *setsval(Cell *, uchar *); -extern double r_getfval(Cell *); -extern uchar *r_getsval(Cell *); -extern uchar *tostring(uchar *); -extern uchar *qstring(uchar *, int); - -extern void recinit(unsigned int); -extern void initgetrec(void); -extern int getrec(uchar *); -extern int readrec(uchar *buf, int bufsize, FILE *inf); -extern uchar *getargv(int); -extern void setclvar(uchar *); -extern void fldbld(void); -extern void cleanfld(int, int); -extern void newfld(int); -extern int refldbld(uchar *, uchar *); -extern void recbld(void); -extern Cell *fieldadr(int); -extern void yyerror(char *); -extern void fpecatch(int); -extern void bracecheck(void); -extern void bcheck2(int, int, int); -extern void error(int, char *); -extern void eprint(void); -extern void bclass(int); -extern double errcheck(double, uchar *); -extern int isclvar(uchar *); -extern int isnumber(uchar *); - -extern void run(Node *); -extern Cell *r_execute(Node *); -extern Cell *program(Node **, int); -extern Cell *call(Node **, int); -extern Cell *copycell(Cell *); -extern Cell *arg(Node **, int); -extern Cell *jump(Node **, int); -extern Cell *getline(Node **, int); -extern Cell *getnf(Node **, int); -extern Cell *array(Node **, int); -extern Cell *adelete(Node **, int); -extern Cell *intest(Node **, int); -extern Cell *matchop(Node **, int); -extern Cell *boolop(Node **, int); -extern Cell *relop(Node **, int); -extern void tfree(Cell *); -extern Cell *gettemp(void); -extern Cell *field(Node **, int); -extern Cell *indirect(Node **, int); -extern Cell *substr(Node **, int); -extern Cell *sindex(Node **, int); -extern int format(uchar *, int, uchar *, Node *); -extern Cell *asprintf(Node **, int); -extern Cell *aprintf(Node **, int); -extern Cell *arith(Node **, int); -extern double ipow(double, int); -extern Cell *incrdecr(Node **, int); -extern Cell *assign(Node **, int); -extern Cell *cat(Node **, int); -extern Cell *pastat(Node **, int); -extern Cell *dopa2(Node **, int); -extern Cell *split(Node **, int); -extern Cell *condexpr(Node **, int); -extern Cell *ifstat(Node **, int); -extern Cell *whilestat(Node **, int); -extern Cell *dostat(Node **, int); -extern Cell *forstat(Node **, int); -extern Cell *instat(Node **, int); -extern Cell *bltin(Node **, int); -extern Cell *printstat(Node **, int); -extern Cell *nullproc(Node **, int); -extern FILE *redirect(int, Node *); -extern FILE *openfile(int, uchar *); -extern uchar *filename(FILE *); -extern Cell *closefile(Node **, int); -extern void closeall(void); -extern Cell *sub(Node **, int); -extern Cell *gsub(Node **, int); - -extern FILE *popen(const char *, const char *); -extern int pclose(FILE *); =awk.h -/**************************************************************** -Copyright (C) AT&T 1993 -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of AT&T or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL AT&T OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. -****************************************************************/ - -#ifdef __ORCAC__ -#pragma optimize 15 -#pragma lint -1 -#endif - -typedef double Awkfloat; -typedef /*unsigned*/ char uchar; - -#define xfree(a) { if ((a) != 0l) { free((void *) a); a = 0l; } } - -/*#define DEBUG*/ -#undef DEBUG -#ifdef DEBUG - /* uses have to be doubly parenthesized */ -# define dprintf(x) if (dbg) printf x -#else -# define dprintf(x) -#endif - -extern char errbuf[200]; -#define ERROR sprintf(errbuf, -#define FATAL ), error(1, errbuf) -#define WARNING ), error(0, errbuf) -#define SYNTAX ), yyerror(errbuf) - -extern int compile_time; /* 1 if compiling, 0 if running */ - -#define RECSIZE (3 * 1024) /* sets limit on records, fields, etc., etc. */ -extern int recsize; /* variable version */ - -extern uchar **FS; -extern uchar **RS; -extern uchar **ORS; -extern uchar **OFS; -extern uchar **OFMT; -extern Awkfloat *NR; -extern Awkfloat *FNR; -extern Awkfloat *NF; -extern uchar **FILENAME; -extern uchar **SUBSEP; -extern Awkfloat *RSTART; -extern Awkfloat *RLENGTH; - -extern uchar *record; /* points to $0 */ -extern int lineno; /* line number in awk program */ -extern int errorflag; /* 1 if error has occurred */ -extern int donefld; /* 1 if record broken into fields */ -extern int donerec; /* 1 if record is valid (no fld has changed */ - -extern int dbg; - -#define CBUFLEN 400 -extern uchar cbuf[CBUFLEN]; /* miscellaneous character collection */ - -extern uchar *patbeg; /* beginning of pattern matched */ -extern int patlen; /* length of pattern matched. set in b.c */ - -/* Cell: all information about a variable or constant */ - -typedef struct Cell { - uchar ctype; /* OCELL, OBOOL, OJUMP, etc. */ - uchar csub; /* CCON, CTEMP, CFLD, etc. */ - uchar *nval; /* name, for variables only */ - uchar *sval; /* string value */ - Awkfloat fval; /* value as number */ - unsigned tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */ - struct Cell *cnext; /* ptr to next if chained */ -} Cell; - -typedef struct { /* symbol table array */ - int nelem; /* elements in table right now */ - int size; /* size of tab */ - Cell **tab; /* hash table pointers */ -} Array; - -#define NSYMTAB 50 /* initial size of a symbol table */ -extern Array *symtab; - -extern Cell *recloc; /* location of input record */ -extern Cell *nrloc; /* NR */ -extern Cell *fnrloc; /* FNR */ -extern Cell *nfloc; /* NF */ -extern Cell *rstartloc; /* RSTART */ -extern Cell *rlengthloc; /* RLENGTH */ - -/* Cell.tval values: */ -#define NUM 01 /* number value is valid */ -#define STR 02 /* string value is valid */ -#define DONTFREE 04 /* string space is not freeable */ -#define CON 010 /* this is a constant */ -#define ARR 020 /* this is an array */ -#define FCN 040 /* this is a function name */ -#define FLD 0100 /* this is a field $1, $2, ... */ -#define REC 0200 /* this is $0 */ - - -/* function types */ -#define FLENGTH 1 -#define FSQRT 2 -#define FEXP 3 -#define FLOG 4 -#define FINT 5 -#define FSYSTEM 6 -#define FRAND 7 -#define FSRAND 8 -#define FSIN 9 -#define FCOS 10 -#define FATAN 11 -#define FTOUPPER 12 -#define FTOLOWER 13 -#define FFLUSH 14 - -/* Node: parse tree is made of nodes, with Cell's at bottom */ - -typedef struct Node { - int ntype; - struct Node *nnext; - int lineno; - int nobj; - struct Node *narg[1]; /* variable: actual size set by calling malloc */ -} Node; - -#define NIL ((Node *) 0) - -extern Node *winner; -extern Node *nullstat; -extern Node *nullnode; - -/* ctypes */ -#define OCELL 1 -#define OBOOL 2 -#define OJUMP 3 - -/* Cell subtypes: csub */ -#define CFREE 7 -#define CCOPY 6 -#define CCON 5 -#define CTEMP 4 -#define CNAME 3 -#define CVAR 2 -#define CFLD 1 - -/* bool subtypes */ -#define BTRUE 11 -#define BFALSE 12 - -/* jump subtypes */ -#define JEXIT 21 -#define JNEXT 22 -#define JBREAK 23 -#define JCONT 24 -#define JRET 25 - -/* node types */ -#define NVALUE 1 -#define NSTAT 2 -#define NEXPR 3 -#define NFIELD 4 - - -extern int pairstack[], paircnt; - -#define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc) -#define isvalue(n) ((n)->ntype == NVALUE) -#define isexpr(n) ((n)->ntype == NEXPR) -#define isjump(n) ((n)->ctype == OJUMP) -#define isexit(n) ((n)->csub == JEXIT) -#define isbreak(n) ((n)->csub == JBREAK) -#define iscont(n) ((n)->csub == JCONT) -#define isnext(n) ((n)->csub == JNEXT) -#define isret(n) ((n)->csub == JRET) -#define isstr(n) ((n)->tval & STR) -#define isnum(n) ((n)->tval & NUM) -#define isarr(n) ((n)->tval & ARR) -#define isfunc(n) ((n)->tval & FCN) -#define istrue(n) ((n)->csub == BTRUE) -#define istemp(n) ((n)->csub == CTEMP) -#define isargument(n) ((n)->nobj == ARG) -#define freeable(p) (!((p)->tval & DONTFREE)) - -/* structures used by regular expression matching machinery, mostly b.c: */ - -#define NCHARS (256+1) /* 256 handles 8-bit chars; 128 does 7-bit */ - /* watch out in match(), etc. */ -#define NSTATES 32 - -typedef struct rrow { - int ltype; - long lval; /* because Al stores a pointer in it! */ - int *lfollow; -} rrow; - -typedef struct fa { - uchar *restr; - int anchor; - int use; - uchar gototab[NSTATES][NCHARS]; - int *posns[NSTATES]; - uchar out[NSTATES]; - int initstat; - int curstat; - int accept; - int reset; - struct rrow re[1]; -} fa; - - -#include "proto.h" =tran.c -/**************************************************************** -Copyright (C) AT&T 1993 -All Rights Reserved - -Permission to use, copy, modify, and distribute this software and -its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of AT&T or any of its entities -not be used in advertising or publicity pertaining to -distribution of the software without specific, written prior -permission. - -AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. -IN NO EVENT SHALL AT&T OR ANY OF ITS ENTITIES BE LIABLE FOR ANY -SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. -****************************************************************/ - -#ifdef __ORCAC__ -segment "tran"; -#endif - -#define DEBUG -#include -#include -#include -#include -#include -#include "awk.h" -#include "y.tab.h" - -#define FULLTAB 2 /* rehash when table gets this x full */ -#define GROWTAB 4 /* grow table by this factor */ - -Array *symtab; /* main symbol table */ - -uchar **FS; /* initial field sep */ -uchar **RS; /* initial record sep */ -uchar **OFS; /* output field sep */ -uchar **ORS; /* output record sep */ -uchar **OFMT; /* output format for numbers */ -uchar **CONVFMT; /* format for conversions in getsval */ -Awkfloat *NF; /* number of fields in current record */ -Awkfloat *NR; /* number of current record */ -Awkfloat *FNR; /* number of current record in current file */ -uchar **FILENAME; /* current filename argument */ -Awkfloat *ARGC; /* number of arguments from command line */ -uchar **SUBSEP; /* subscript separator for a[i,j,k]; default \034 */ -Awkfloat *RSTART; /* start of re matched with ~; origin 1 (!) */ -Awkfloat *RLENGTH; /* length of same */ - -Cell *recloc; /* location of record */ -Cell *nrloc; /* NR */ -Cell *nfloc; /* NF */ -Cell *fnrloc; /* FNR */ -Array *ARGVtab; /* symbol table containing ARGV[...] */ -Array *ENVtab; /* symbol table containing ENVIRON[...] */ -Cell *rstartloc; /* RSTART */ -Cell *rlengthloc; /* RLENGTH */ -Cell *symtabloc; /* SYMTAB */ - -Cell *nullloc; /* a guaranteed empty cell */ -Node *nullnode; /* zero&null, converted into a node for comparisons */ - -extern Cell *fldtab; - -void syminit(void) /* initialize symbol table with builtin vars */ -{ - setsymtab("0", "0", 0.0, NUM|STR|CON|DONTFREE, symtab); - /* this is used for if(x)... tests: */ - nullloc = setsymtab("$zero&null", "", 0.0, NUM|STR|CON|DONTFREE, symtab); - nullnode = valtonode(nullloc, CCON); - - /* recloc = setsymtab("$0", record, 0.0, REC|STR|DONTFREE, symtab); */ - /* has been done elsewhere */ - recloc = &fldtab[0]; - FS = &setsymtab("FS", " ", 0.0, STR|DONTFREE, symtab)->sval; - RS = &setsymtab("RS", "\n", 0.0, STR|DONTFREE, symtab)->sval; - OFS = &setsymtab("OFS", " ", 0.0, STR|DONTFREE, symtab)->sval; - ORS = &setsymtab("ORS", "\n", 0.0, STR|DONTFREE, symtab)->sval; - OFMT = &setsymtab("OFMT", "%.6g", 0.0, STR|DONTFREE, symtab)->sval; - CONVFMT = &setsymtab("CONVFMT", "%.6g", 0.0, STR|DONTFREE, symtab)->sval; - FILENAME = &setsymtab("FILENAME", "", 0.0, STR|DONTFREE, symtab)->sval; - nfloc = setsymtab("NF", "", 0.0, NUM, symtab); - NF = &nfloc->fval; - nrloc = setsymtab("NR", "", 0.0, NUM, symtab); - NR = &nrloc->fval; - fnrloc = setsymtab("FNR", "", 0.0, NUM, symtab); - FNR = &fnrloc->fval; - SUBSEP = &setsymtab("SUBSEP", "\034", 0.0, STR|DONTFREE, symtab)->sval; - rstartloc = setsymtab("RSTART", "", 0.0, NUM, symtab); - RSTART = &rstartloc->fval; - rlengthloc = setsymtab("RLENGTH", "", 0.0, NUM, symtab); - RLENGTH = &rlengthloc->fval; - symtabloc = setsymtab("SYMTAB", "", 0.0, ARR, symtab); - symtabloc->sval = (uchar *) symtab; -} - -void arginit(int ac, uchar **av) /* set up ARGV and ARGC */ -{ - Cell *cp; - int i; - uchar temp[5]; - - ARGC = &setsymtab("ARGC", "", (Awkfloat) ac, NUM, symtab)->fval; - cp = setsymtab("ARGV", "", 0.0, ARR, symtab); - ARGVtab = makesymtab(NSYMTAB); /* could be (int) ARGC as well */ - cp->sval = (uchar *) ARGVtab; - for (i = 0; i < ac; i++) { - sprintf((char *)temp, "%d", i); - if (isnumber(*av)) - setsymtab(temp, *av, atof(*av), STR|NUM, ARGVtab); - else - setsymtab(temp, *av, 0.0, STR, ARGVtab); - av++; - } -} - -void envinit(uchar **envp) /* set up ENVIRON variable */ -{ - Cell *cp; - uchar *p; - - cp = setsymtab("ENVIRON", "", 0.0, ARR, symtab); - ENVtab = makesymtab(NSYMTAB); - cp->sval = (uchar *) ENVtab; - for ( ; *envp; envp++) { - if ((p = (uchar *) strchr((char *) *envp, '=')) == NULL) - continue; - *p++ = 0; /* split into two strings at = */ - if (isnumber(p)) - setsymtab(*envp, p, atof(p), STR|NUM, ENVtab); - else - setsymtab(*envp, p, 0.0, STR, ENVtab); - p[-1] = '='; /* restore in case env is passed down to a shell */ - } -} - -Array *makesymtab(int n) /* make a new symbol table */ -{ - Array *ap; - Cell **tp; - - ap = (Array *) malloc(sizeof(Array)); - tp = (Cell **) calloc(n, sizeof(Cell *)); - if (ap == NULL || tp == NULL) - ERROR "out of space in makesymtab" FATAL; - ap->nelem = 0; - ap->size = n; - ap->tab = tp; - return(ap); -} - -void freesymtab(Cell *ap) /* free a symbol table */ -{ - Cell *cp, *temp; - Array *tp; - int i; - - if (!isarr(ap)) - return; - tp = (Array *) ap->sval; - if (tp == NULL) - return; - for (i = 0; i < tp->size; i++) { - for (cp = tp->tab[i]; cp != NULL; cp = temp) { - xfree(cp->nval); - if (freeable(cp)) - xfree(cp->sval); - temp = cp->cnext; /* avoids freeing then using */ - free((char *) cp); - } - } - free((char *) (tp->tab)); - free((char *) tp); -} - -void freeelem(Cell *ap, uchar *s) /* free elem s from ap (i.e., ap["s"] */ -{ - Array *tp; - Cell *p, *prev = NULL; - int h; - - tp = (Array *) ap->sval; - h = hash(s, tp->size); - for (p = tp->tab[h]; p != NULL; prev = p, p = p->cnext) - if (strcmp((char *) s, (char *) p->nval) == 0) { - if (prev == NULL) /* 1st one */ - tp->tab[h] = p->cnext; - else /* middle somewhere */ - prev->cnext = p->cnext; - if (freeable(p)) - xfree(p->sval); - free(p->nval); - free((char *) p); - tp->nelem--; - return; - } -} - -Cell *setsymtab(uchar *n, uchar *s, double f, unsigned t, Array *tp) -{ - register int h; - register Cell *p; - - if (n != NULL && (p = lookup(n, tp)) != NULL) { - dprintf( ("setsymtab found %o: n=%s s=\"%s\" f=%g t=%o\n", - (int)p, p->nval, p->sval, p->fval, (int)p->tval) ); - return(p); - } - p = (Cell *) malloc(sizeof(Cell)); - if (p == NULL) - ERROR "out of space for symbol table at %s", n FATAL; - p->nval = tostring(n); - p->sval = s ? tostring(s) : tostring(""); - p->fval = f; - p->tval = t; - tp->nelem++; - if (tp->nelem > FULLTAB * tp->size) - rehash(tp); - h = hash(n, tp->size); - p->cnext = tp->tab[h]; - tp->tab[h] = p; - dprintf( ("setsymtab set %o: n=%s s=\"%s\" f=%g t=%o\n", - (int)p, p->nval, p->sval, p->fval, (int)p->tval) ); - return(p); -} - -int hash(uchar *s, int n) /* form hash value for string s */ -{ - register unsigned long hashval; - - for (hashval = 0l; *s != '\0'; s++) - hashval = (*s + 31 * hashval); - return (int)(hashval % n); -} - -void rehash(Array *tp) /* rehash items in small table into big one */ -{ - int i, nh, nsz; - Cell *cp, *op, **np; - - nsz = GROWTAB * tp->size; - np = (Cell **) calloc(nsz, sizeof(Cell *)); - if (np == NULL) /* can't do it, but can keep running. */ - return; /* someone else will run out later. */ - for (i = 0; i < tp->size; i++) { - for (cp = tp->tab[i]; cp; cp = op) { - op = cp->cnext; - nh = hash(cp->nval, nsz); - cp->cnext = np[nh]; - np[nh] = cp; - } - } - free((char *) (tp->tab)); - tp->tab = np; - tp->size = nsz; -} - -Cell *lookup(uchar *s, Array *tp) /* look for s in tp */ -{ - register Cell *p, *prev = NULL; - int h; - - h = hash(s, tp->size); - for (p = tp->tab[h]; p != NULL; prev = p, p = p->cnext) - if (strcmp((char *) s, (char *) p->nval) == 0) - return(p); /* found it */ - return(NULL); /* not found */ -} - -Awkfloat setfval(Cell *vp, Awkfloat f) /* set float val of a Cell */ -{ - if ((vp->tval & (NUM | STR)) == 0) - funnyvar(vp, "assign to"); - if (vp->tval & FLD) { - donerec = 0; /* mark $0 invalid */ - if (vp-fldtab > *NF) - newfld(vp-fldtab); - dprintf( ("setting field %ld to %g\n", (long)(vp-fldtab), f) ); - } else if (vp->tval & REC) { - donefld = 0; /* mark $1... invalid */ - donerec = 1; - } - vp->tval &= ~STR; /* mark string invalid */ - vp->tval |= NUM; /* mark number ok */ - dprintf( ("setfval %o: %s = %g, t=%o\n", (int)vp, vp->nval, f, (int)vp->tval) ); - return vp->fval = f; -} - -void funnyvar(Cell *vp, char *rw) -{ - if (vp->tval & ARR) - ERROR "can't %s %s; it's an array name.", rw, vp->nval FATAL; - if (vp->tval & FCN) - ERROR "can't %s %s; it's a function.", rw, vp->nval FATAL; - ERROR "funny variable %o: n=%s s=\"%s\" f=%g t=%o", - vp, vp->nval, vp->sval, vp->fval, vp->tval WARNING; -} - -uchar *setsval(Cell *vp, uchar *s) /* set string val of a Cell */ -{ - if ((vp->tval & (NUM | STR)) == 0) - funnyvar(vp, "assign to"); - if (vp->tval & FLD) { - donerec = 0; /* mark $0 invalid */ - if (vp-fldtab > *NF) - newfld(vp-fldtab); - dprintf( ("setting field %ld to %s\n", (long)(vp-fldtab), s) ); - } else if (vp->tval & REC) { - donefld = 0; /* mark $1... invalid */ - donerec = 1; - } - vp->tval &= ~NUM; - vp->tval |= STR; - if (freeable(vp)) - xfree(vp->sval); - vp->tval &= ~DONTFREE; - dprintf( ("setsval %o: %s = \"%s\", t=%o\n", (int)vp, vp->nval, s, (int)vp->tval) ); - return(vp->sval = tostring(s)); -} - -Awkfloat r_getfval(Cell *vp) /* get float val of a Cell */ -{ - if ((vp->tval & (NUM | STR)) == 0) - funnyvar(vp, "read value of"); - if ((vp->tval & FLD) && donefld == 0) - fldbld(); - else if ((vp->tval & REC) && donerec == 0) - recbld(); - if (!isnum(vp)) { /* not a number */ - vp->fval = atof(vp->sval); /* best guess */ - if (isnumber(vp->sval) && !(vp->tval&CON)) - vp->tval |= NUM; /* make NUM only sparingly */ - } - dprintf( ("getfval %o: %s = %g, t=%o\n", (int)vp, vp->nval, vp->fval, (int)vp->tval) ); - return(vp->fval); -} - -uchar *r_getsval(Cell *vp) /* get string val of a Cell */ -{ - uchar s[100]; - int dtemp; - - if ((vp->tval & (NUM | STR)) == 0) - funnyvar(vp, "read value of"); - if ((vp->tval & FLD) && donefld == 0) - fldbld(); - else if ((vp->tval & REC) && donerec == 0) - recbld(); - if ((vp->tval & STR) == 0) { - if (!(vp->tval&DONTFREE)) - xfree(vp->sval); - if (modf(vp->fval, &dtemp) == 0) /* it's integral */ - sprintf((char *)s, "%.20g", vp->fval); - else - sprintf((char *)s, (char *)*CONVFMT, vp->fval); - vp->sval = tostring(s); - vp->tval &= ~DONTFREE; - vp->tval |= STR; - } - dprintf( ("getsval %o: %s = \"%s\", t=%o\n", (int)vp, vp->nval, vp->sval, (int) vp->tval) ); - return(vp->sval); -} - -uchar *tostring(uchar *s) /* make a copy of string s */ -{ - register uchar *p; - - p = (uchar *) malloc(strlen((char *) s)+1); - if (p == NULL) - ERROR "out of space in tostring on %s", s FATAL; - strcpy((char *) p, (char *) s); - return(p); -} - -uchar *qstring(uchar *s, int delim) /* collect string up to next delim */ -{ - uchar *q; - int c, n; - - for (q = cbuf; (c = *s) != delim; s++) { - if (q >= cbuf + CBUFLEN - 1) - ERROR "string %.10s... too long", cbuf SYNTAX; - else if (c == '\n') - ERROR "newline in string %.10s...", cbuf SYNTAX; - else if (c != '\\') - *q++ = c; - else /* \something */ - switch (c = *++s) { - case '\\': *q++ = '\\'; break; - case 'n': *q++ = '\n'; break; - case 't': *q++ = '\t'; break; - case 'b': *q++ = '\b'; break; - case 'f': *q++ = '\f'; break; - case 'r': *q++ = '\r'; break; - default: - if (!isdigit(c)) { - *q++ = c; - break; - } - n = c - '0'; - if (isdigit(s[1])) { - n = 8 * n + *++s - '0'; - if (isdigit(s[1])) - n = 8 * n + *++s - '0'; - } - *q++ = n; - break; - } - } - *q = '\0'; - return cbuf; -} + END OF ARCHIVE