# 2.11BSD#431 - /usr/src/bin/awk/tran.c

##### Functions

    checkval          X
    freesymtab        X
    getfval           X
    getsval           X
    hash              X
    lookup            X
    makesymtab        X
    setfval           X
    setsval           X
    setsymtab         X
    syminit           X
    tostring          X
    yfree             X
    ymalloc           X

##### Variables

    FILENAME          X
    FS                X
    OFMT              X
    OFS               X
    ORS               X
    RS                X
    nfloc             X
    nrloc             X
    recloc            X
    symtab            X

       1: #ifndef lint
       2: static char sccsid[] = "@(#)tran.c	4.4 12/9/83";
       3: #endif
       4:
       5: #include "stdio.h"
       6: #include "awk.def"
       7: #include "awk.h"
       8:
       9: cell *symtab[MAXSYM];   /* symbol table pointers */
      10:
      11: char    **FS;   /* initial field sep */
      12: char    **RS;   /* initial record sep */
      13: char    **OFS;  /* output field sep */
      14: char    **ORS;  /* output record sep */
      15: char    **OFMT; /*output format for numbers*/
      16: awkfloat *NF;   /* number of fields in current record */
      17: awkfloat *NR;   /* number of current record */
      18: char    **FILENAME; /* current filename argument */
      19:
      20: cell    *recloc;    /* location of record */
      21: cell    *nrloc;     /* NR */
      22: cell    *nfloc;     /* NF */
      23:
      24: syminit()
      25: {
      26:     setsymtab("0", tostring("0"), 0.0, NUM|STR|CON|FLD, symtab);
      27:     /* this one is used for if(x)... tests: */
      28:     setsymtab("$zero&null", tostring(""), 0.0, NUM|STR|CON|FLD, symtab);
      29:     recloc = setsymtab("$record", record, 0.0, STR|FLD, symtab);
      30:     dprintf("recloc %o lookup %o\n", recloc, lookup("$record", symtab, 0), NULL);
      31:     FS = &setsymtab("FS", tostring(" "), 0.0, STR|FLD, symtab)->sval;
      32:     RS = &setsymtab("RS", tostring("\n"), 0.0, STR|FLD, symtab)->sval;
      33:     OFS = &setsymtab("OFS", tostring(" "), 0.0, STR|FLD, symtab)->sval;
      34:     ORS = &setsymtab("ORS", tostring("\n"), 0.0, STR|FLD, symtab)->sval;
      35:     OFMT = &setsymtab("OFMT", tostring("%.6g"), 0.0, STR|FLD, symtab)->sval;
      36:     FILENAME = &setsymtab("FILENAME", EMPTY, 0.0, STR|FLD, symtab)->sval;
      37:     nfloc = setsymtab("NF", EMPTY, 0.0, NUM, symtab);
      38:     NF = &nfloc->fval;
      39:     nrloc = setsymtab("NR", EMPTY, 0.0, NUM, symtab);
      40:     NR = &nrloc->fval;
      41: }
      42:
      43: cell **makesymtab()
      44: {
      45:     int i;
      46:     cell **cp;
      47:
      48:     cp = (cell **) malloc(MAXSYM * sizeof(cell *));
      49:     if (cp == NULL)
      50:         error(FATAL, "out of space in makesymtab");
      51:     for (i = 0; i < MAXSYM; i++)
      52:         cp[i] = 0;
      53:     return(cp);
      54: }
      55:
      56: freesymtab(ap)  /* free symbol table */
      57: cell *ap;
      58: {
      59:     cell *cp, **tp;
      60:     int i;
      61:
      62:     if (!(ap->tval & ARR))
      63:         return;
      64:     tp = (cell **) ap->sval;
      65:     for (i = 0; i < MAXSYM; i++) {
      66:         for (cp = tp[i]; cp != NULL; cp = cp->nextval) {
      67:             strfree(cp->nval);
      68:             strfree(cp->sval);
      69:             free(cp);
      70:         }
      71:     }
      72:     xfree(tp);
      73: }
      74:
      75: cell *setsymtab(n, s, f, t, tab)
      76: char *n, *s;
      77: awkfloat f;
      78: unsigned t;
      79: cell **tab;
      80: {
      81:     register h;
      82:     register cell *p;
      83:     cell *lookup();
      84:
      85:     if (n != NULL && (p = lookup(n, tab, 0)) != NULL) {
      86:         if (s != EMPTY ) xfree(s); /* careful here */
      87:         dprintf("setsymtab found %o: %s", p, p->nval, NULL);
      88:         dprintf(" %s %g %o\n", p->sval, p->fval, p->tval);
      89:         return(p);
      90:     }
      91:     p = (cell *) malloc(sizeof(cell));
      92:     if (p == NULL)
      93:         error(FATAL, "symbol table overflow at %s", n);
      94:     p->nval = tostring(n);
      95:     p->sval = s;
      96:     p->fval = f;
      97:     p->tval = t;
      98:     h = hash(n);
      99:     p->nextval = tab[h];
     100:     tab[h] = p;
     101:     dprintf("setsymtab set %o: %s", p, p->nval, NULL);
     102:     dprintf(" %s %g %o\n", p->sval, p->fval, p->tval);
     103:     return(p);
     104: }
     105:
     106: hash(s) /* form hash value for string s */
     107: register unsigned char *s;
     108: {
     109:     register int hashval;
     110:
     111:     for (hashval = 0; *s != '\0'; )
     112:         hashval += *s++;
     113:     return(hashval % MAXSYM);
     114: }
     115:
     116: cell *lookup(s, tab, flag)  /* look for s in tab, flag must match*/
     117: register char *s;
     118: cell **tab;
     119: {
     120:     register cell *p;
     121:
     122:     for (p = tab[hash(s)]; p != NULL; p = p->nextval)
     123:         if (strcmp(s, p->nval) == 0 &&
     124:             (flag == 0 || flag == p->tval))
     125:             return(p);  /* found it */
     126:     return(NULL);   /* not found */
     127: }
     128:
     129: awkfloat setfval(vp, f)
     130: register cell *vp;
     131: awkfloat f;
     132: {
     133:     dprintf("setfval: %o %g\n", vp, f, NULL);
     134:     checkval(vp);
     135:     if (vp == recloc)
     136:         error(FATAL, "can't set $0");
     137:     vp->tval &= ~STR;   /* mark string invalid */
     138:     vp->tval |= NUM;    /* mark number ok */
     139:     if ((vp->tval & FLD) && isnull(vp->nval))
     140:         donerec = 0;
     141:     return(vp->fval = f);
     142: }
     143:
     144: char *setsval(vp, s)
     145: register cell *vp;
     146: char *s;
     147: {
     148:     dprintf("setsval: %o %s\n", vp, s, NULL);
     149:     checkval(vp);
     150:     if (vp == recloc)
     151:         error(FATAL, "can't set $0");
     152:     vp->tval &= ~NUM;
     153:     vp->tval |= STR;
     154:     if ((vp->tval & FLD) && isnull(vp->nval))
     155:         donerec = 0;
     156:     if (!(vp->tval&FLD))
     157:         strfree(vp->sval);
     158:     vp->tval &= ~FLD;
     159:     return(vp->sval = tostring(s));
     160: }
     161:
     162: awkfloat getfval(vp)
     163: register cell *vp;
     164: {
     165:
     166:     if (vp->sval == record && donerec == 0)
     167:         recbld();
     168:     dprintf("getfval: %o", vp, NULL, NULL);
     169:     checkval(vp);
     170:     if ((vp->tval & NUM) == 0) {
     171:         /* the problem is to make non-numeric things */
     172:         /* have unlikely numeric variables, so that */
     173:         /* $1 == $2 comparisons sort of make sense when */
     174:         /* one or the other is numeric */
     175:         if (isnumber(vp->sval)) {
     176:             vp->fval = atof(vp->sval);
     177:             if (!(vp->tval & CON))  /* don't change type of a constant */
     178:                 vp->tval |= NUM;
     179:         }
     180:         else
     181:             vp->fval = 0.0; /* not a very good idea */
     182:     }
     183:     dprintf("  %g\n", vp->fval, NULL, NULL);
     184:     return(vp->fval);
     185: }
     186:
     187: char *getsval(vp)
     188: register cell *vp;
     189: {
     190:     char s[100];
     191:
     192:     if (vp->sval == record && donerec == 0)
     193:         recbld();
     194:     dprintf("getsval: %o", vp, NULL, NULL);
     195:     checkval(vp);
     196:     if ((vp->tval & STR) == 0) {
     197:         if (!(vp->tval&FLD))
     198:             strfree(vp->sval);
     199:         if ((long)vp->fval==vp->fval)
     200:             sprintf(s, "%.20g", vp->fval);
     201:         else
     202:             sprintf(s, *OFMT, vp->fval);
     203:         vp->sval = tostring(s);
     204:         vp->tval &= ~FLD;
     205:         vp->tval |= STR;
     206:     }
     207:     dprintf("  %s\n", vp->sval, NULL, NULL);
     208:     return(vp->sval);
     209: }
     210:
     211: checkval(vp)
     212: register cell *vp;
     213: {
     214:     if (vp->tval & ARR)
     215:         error(FATAL, "illegal reference to array %s", vp->nval);
     216:     if ((vp->tval & (NUM | STR)) == 0)
     217:         error(FATAL, "funny variable %o: %s %s %g %o", vp, vp->nval,
     218:             vp->sval, vp->fval, vp->tval);
     219: }
     220:
     221: char *tostring(s)
     222: register char *s;
     223: {
     224:     register char *p;
     225:
     226:     if (s==NULL){
     227:         p = malloc(1);
     228:         if (p == NULL)
     229:             error(FATAL, "out of space in tostring on %s", s);
     230:         *p = '\0';
     231:     } else {
     232:         p = malloc(strlen(s)+1);
     233:         if (p == NULL)
     234:             error(FATAL, "out of space in tostring on %s", s);
     235:         strcpy(p, s);
     236:     }
     237:     return(p);
     238: }
     239: #ifndef yfree
     240: yfree(a) char *a;
     241: {
     242:     printf("%o\n", a);
     243:     free(a);
     244: }
     245: #endif
     246: #ifdef malloc
     247: #undef malloc
     248: char *ymalloc(u) unsigned u;
     249: {   char *p;
     250:     p = malloc(u);
     251:     printf("%o %o\n", u, p);
     252:     return(p);
     253: }
     254: #endif

#### Defined functions

checkval defined in line 211; used 4 times

- in line 134, 149, 169, 195

freesymtab defined in line 56; used 1 times

- in /usr/src/bin/awk/run.c line 622

getfval defined in line 162; used 15 times

- in /usr/src/bin/awk/awk.def line 54
- in /usr/src/bin/awk/run.c line 255, 274, 282, 384, 419-423(2), 464, 498-499(2), 771, 799-805(4)

getsval defined in line 187; used 18 times

- in /usr/src/bin/awk/awk.def line 55
- in /usr/src/bin/awk/lib.c line 182
- in /usr/src/bin/awk/run.c line 128, 150, 202(2), 270, 306-309(2), 388, 405, 537-538(2), 612, 618, 797, 823, 859

hash defined in line 106; used 2 times

- in line 98, 122

lookup defined in line 116; used 11 times

- in line 30, 83-85(2)
- in /usr/src/bin/awk/awk.def line 42
- in /usr/src/bin/awk/awk.g.y line 75, 110-113(2), 169, 179
- in /usr/src/bin/awk/lib.c line 167
- in /usr/src/bin/awk/parse.c line 148

makesymtab defined in line 43; used 3 times

- in /usr/src/bin/awk/awk.def line 42
- in /usr/src/bin/awk/run.c line 134, 626

setfval defined in line 129; used 12 times

- in /usr/src/bin/awk/awk.def line 54
- in /usr/src/bin/awk/lib.c line 167
- in /usr/src/bin/awk/run.c line 106, 317-321(2), 453, 467-472(3), 494, 525, 810

setsval defined in line 144; used 6 times

- in /usr/src/bin/awk/awk.def line 55
- in /usr/src/bin/awk/lib.c line 108
- in /usr/src/bin/awk/run.c line 295, 487-492(2), 745

setsymtab defined in line 75; used 18 times

- in line 26-39(11)
- in /usr/src/bin/awk/awk.def line 42
- in /usr/src/bin/awk/lib.c line 107
- in /usr/src/bin/awk/run.c line 136, 644-646(2), 661-663(2)

syminit defined in line 24; used 1 times

- in /usr/src/bin/awk/main.c line 27

tostring defined in line 221; used 19 times

- in line 26-35(7), 94, 159, 203
- in /usr/src/bin/awk/awk.def line 56
- in /usr/src/bin/awk/b.c line 140
- in /usr/src/bin/awk/lib.c line 107
- in /usr/src/bin/awk/run.c line 136, 644-646(2), 661-663(2), 876

yfree defined in line 240; never used
ymalloc defined in line 248; never used

#### Defined variables

FILENAME defined in line 18; used 4 times

- in line 36
- in /usr/src/bin/awk/lib.c line 56
- in /usr/src/bin/awk/main.c line 81
- in /usr/src/bin/awk/tran.c line 36

FS defined in line 11; used 9 times

- in line 31
- in /usr/src/bin/awk/lib.c line 43, 120, 188-190(2)
- in /usr/src/bin/awk/main.c line 44-46(2)
- in /usr/src/bin/awk/run.c line 615
- in /usr/src/bin/awk/tran.c line 31

OFMT defined in line 15; used 4 times

- in line 35, 202
- in /usr/src/bin/awk/tran.c line 35, 202

OFS defined in line 13; used 4 times

- in line 33
- in /usr/src/bin/awk/lib.c line 185
- in /usr/src/bin/awk/run.c line 828
- in /usr/src/bin/awk/tran.c line 33

ORS defined in line 14; used 3 times

- in line 34
- in /usr/src/bin/awk/run.c line 826
- in /usr/src/bin/awk/tran.c line 34

RS defined in line 12; used 5 times

- in line 32
- in /usr/src/bin/awk/lib.c line 43, 63-68(2)
- in /usr/src/bin/awk/tran.c line 32

nfloc defined in line 22; used 4 times

- in line 37-38(2)
- in /usr/src/bin/awk/tran.c line 37-38(2)

nrloc defined in line 21; used 7 times

- in line 39-40(2)
- in /usr/src/bin/awk/lib.c line 83-85(3)
- in /usr/src/bin/awk/tran.c line 39-40(2)

recloc defined in line 20; used 13 times

- in line 29-30(2), 135, 150
- in /usr/src/bin/awk/lib.c line 81-82(2), 188-190(3)
- in /usr/src/bin/awk/tran.c line 29-30(2), 135, 150

sccsid defined in line 2; never used
symtab defined in line 9; used 32 times

- in line 26-39(12)
- in /usr/src/bin/awk/awk.g.y line 75, 110-113(2), 169, 179
- in /usr/src/bin/awk/lib.c line 107, 167
- in /usr/src/bin/awk/parse.c line 148
- in /usr/src/bin/awk/tran.c line 26-39(12)

Last modified: 1987-02-08 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1811 |