BSD 4_4 release
[unix-history] / usr / src / old / awk / tran.c
index 781439b..f548fc1 100644 (file)
@@ -1,4 +1,15 @@
-/*     tran.c  4.1     82/05/07        */
+/*-
+ * Copyright (c) 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This module is believed to contain source code proprietary to AT&T.
+ * Use and redistribution is subject to the Berkeley Software License
+ * Agreement and your Software Agreement with AT&T (Western Electric).
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)tran.c     4.7 (Berkeley) 5/26/93";
+#endif /* not lint */
 
 #include "stdio.h"
 #include "awk.def"
 
 #include "stdio.h"
 #include "awk.def"
@@ -31,10 +42,10 @@ syminit()
        OFS = &setsymtab("OFS", tostring(" "), 0.0, STR|FLD, symtab)->sval;
        ORS = &setsymtab("ORS", tostring("\n"), 0.0, STR|FLD, symtab)->sval;
        OFMT = &setsymtab("OFMT", tostring("%.6g"), 0.0, STR|FLD, symtab)->sval;
        OFS = &setsymtab("OFS", tostring(" "), 0.0, STR|FLD, symtab)->sval;
        ORS = &setsymtab("ORS", tostring("\n"), 0.0, STR|FLD, symtab)->sval;
        OFMT = &setsymtab("OFMT", tostring("%.6g"), 0.0, STR|FLD, symtab)->sval;
-       FILENAME = &setsymtab("FILENAME", NULL, 0.0, STR|FLD, symtab)->sval;
-       nfloc = setsymtab("NF", NULL, 0.0, NUM, symtab);
+       FILENAME = &setsymtab("FILENAME", EMPTY, 0.0, STR|FLD, symtab)->sval;
+       nfloc = setsymtab("NF", EMPTY, 0.0, NUM, symtab);
        NF = &nfloc->fval;
        NF = &nfloc->fval;
-       nrloc = setsymtab("NR", NULL, 0.0, NUM, symtab);
+       nrloc = setsymtab("NR", EMPTY, 0.0, NUM, symtab);
        NR = &nrloc->fval;
 }
 
        NR = &nrloc->fval;
 }
 
@@ -62,8 +73,8 @@ cell *ap;
        tp = (cell **) ap->sval;
        for (i = 0; i < MAXSYM; i++) {
                for (cp = tp[i]; cp != NULL; cp = cp->nextval) {
        tp = (cell **) ap->sval;
        for (i = 0; i < MAXSYM; i++) {
                for (cp = tp[i]; cp != NULL; cp = cp->nextval) {
-                       xfree(cp->nval);
-                       xfree(cp->sval);
+                       strfree(cp->nval);
+                       strfree(cp->sval);
                        free(cp);
                }
        }
                        free(cp);
                }
        }
@@ -81,7 +92,7 @@ cell **tab;
        cell *lookup();
 
        if (n != NULL && (p = lookup(n, tab, 0)) != NULL) {
        cell *lookup();
 
        if (n != NULL && (p = lookup(n, tab, 0)) != NULL) {
-               xfree(s);
+               if (s != EMPTY ) xfree(s); /* careful here */
                dprintf("setsymtab found %o: %s", p, p->nval, NULL);
                dprintf(" %s %g %o\n", p->sval, p->fval, p->tval);
                return(p);
                dprintf("setsymtab found %o: %s", p, p->nval, NULL);
                dprintf(" %s %g %o\n", p->sval, p->fval, p->tval);
                return(p);
@@ -134,7 +145,7 @@ awkfloat f;
                error(FATAL, "can't set $0");
        vp->tval &= ~STR;       /* mark string invalid */
        vp->tval |= NUM;        /* mark number ok */
                error(FATAL, "can't set $0");
        vp->tval &= ~STR;       /* mark string invalid */
        vp->tval |= NUM;        /* mark number ok */
-       if ((vp->tval & FLD) && vp->nval == 0)
+       if ((vp->tval & FLD) && isnull(vp->nval))
                donerec = 0;
        return(vp->fval = f);
 }
                donerec = 0;
        return(vp->fval = f);
 }
@@ -149,10 +160,10 @@ char *s;
                error(FATAL, "can't set $0");
        vp->tval &= ~NUM;
        vp->tval |= STR;
                error(FATAL, "can't set $0");
        vp->tval &= ~NUM;
        vp->tval |= STR;
-       if ((vp->tval & FLD) && vp->nval == 0)
+       if ((vp->tval & FLD) && isnull(vp->nval))
                donerec = 0;
        if (!(vp->tval&FLD))
                donerec = 0;
        if (!(vp->tval&FLD))
-               xfree(vp->sval);
+               strfree(vp->sval);
        vp->tval &= ~FLD;
        return(vp->sval = tostring(s));
 }
        vp->tval &= ~FLD;
        return(vp->sval = tostring(s));
 }
@@ -170,7 +181,7 @@ register cell *vp;
                /* have unlikely numeric variables, so that */
                /* $1 == $2 comparisons sort of make sense when */
                /* one or the other is numeric */
                /* have unlikely numeric variables, so that */
                /* $1 == $2 comparisons sort of make sense when */
                /* one or the other is numeric */
-               if (isnumber(vp->sval)) {
+               if (isanumber(vp->sval)) {
                        vp->fval = atof(vp->sval);
                        if (!(vp->tval & CON))  /* don't change type of a constant */
                                vp->tval |= NUM;
                        vp->fval = atof(vp->sval);
                        if (!(vp->tval & CON))  /* don't change type of a constant */
                                vp->tval |= NUM;
@@ -193,11 +204,11 @@ register cell *vp;
        checkval(vp);
        if ((vp->tval & STR) == 0) {
                if (!(vp->tval&FLD))
        checkval(vp);
        if ((vp->tval & STR) == 0) {
                if (!(vp->tval&FLD))
-                       xfree(vp->sval);
+                       strfree(vp->sval);
                if ((long)vp->fval==vp->fval)
                if ((long)vp->fval==vp->fval)
-                       sprintf(s, "%.20g", vp->fval);
+                       (void)sprintf(s, "%.20g", vp->fval);
                else
                else
-                       sprintf(s, *OFMT, vp->fval);
+                       (void)sprintf(s, *OFMT, vp->fval);
                vp->sval = tostring(s);
                vp->tval &= ~FLD;
                vp->tval |= STR;
                vp->sval = tostring(s);
                vp->tval &= ~FLD;
                vp->tval |= STR;
@@ -221,10 +232,17 @@ register char *s;
 {
        register char *p;
 
 {
        register char *p;
 
-       p = malloc(strlen(s)+1);
-       if (p == NULL)
-               error(FATAL, "out of space in tostring on %s", s);
-       strcpy(p, s);
+       if (s==NULL){
+               p = malloc(1);
+               if (p == NULL)
+                       error(FATAL, "out of space in tostring on %s", s);
+               *p = '\0';
+       } else {
+               p = malloc(strlen(s)+1);
+               if (p == NULL)
+                       error(FATAL, "out of space in tostring on %s", s);
+               strcpy(p, s);
+       }
        return(p);
 }
 #ifndef yfree
        return(p);
 }
 #ifndef yfree