On Tue Dec  1 11:58:47 EST 2009, fors...@terzarima.net wrote:
> i hadn't noticed the changed example.
> it's removing the cast, which probably isn't right, although
> in that particular case it's probably better
> that it should trap (because the result is wrong).
> what is the actual code in awk that's equivalent to that sequence?
> 

all these fault:

minooka; awk 'BEGIN {
        s = "01234567890";
        printf "%x\n", 4215866817.
        g = 1.                                  # delayed fpe
        printf "%08x", 4215866817.
        print substr(s, 4215866817., 1)
        s = substr(s, 1, 4215866817.)
        g= 1.                                   # delayed fpe
        printf utf(4215866817.); 
        g= 1.                                   # delayed fpe
}'

a (not-so-elegant) fix follows.

- erik

proto.h:92,98 - /n/dump/2009/1129/sys/src/cmd/awk/proto.h:92,97
  extern        void    funnyvar(Cell *, const char *);
  extern        char    *setsval(Cell *, const char *);
  extern        double  getfval(Cell *);
- extern        unsigned long   getival(Cell *, unsigned long, unsigned long);
  extern        char    *getsval(Cell *);
  extern        char    *getpssval(Cell *);     /* for print */
  extern        char    *tostring(const char *);
diff: can't stat /n/dump/2009/1129/sys/src/cmd/awk/trans.c
run.c:50,57 - /n/dump/2009/1129/sys/src/cmd/awk/run.c:50,55
  }
  */
  
- #define       Imax    ULONG_MAX
- 
  /* do we really need these? */
  /* #ifdef _NFILE */
  /* #ifndef FOPEN_MAX */
run.c:355,361 - /n/dump/2009/1129/sys/src/cmd/awk/run.c:353,359
        case EXIT:
                if (a[0] != NULL) {
                        y = execute(a[0]);
-                       errorflag = getfval(y) != 0.;
+                       errorflag = (int) getfval(y);
                        tempfree(y);
                }
                longjmp(env, 1);
run.c:752,765 - /n/dump/2009/1129/sys/src/cmd/awk/run.c:750,770
                setsval(x, "");
                return(x);
        }
-       m = getival(y, 1, k);   /* 1 <= m <= k */
+       m = (int) getfval(y);
+       if (m <= 0)
+               m = 1;
+       else if (m > k)
+               m = k;
        tempfree(y);
        if (a[2] != 0) {
-               n = getival(z, 0, k-m);
-                               /* n <= 0 <= k-m */
+               n = (int) getfval(z);
                tempfree(z);
        } else
                n = k - 1;
+       if (n < 0)
+               n = 0;
+       else if (n > k - m)
+               n = k - m;
           dprintf( ("substr: m=%d, n=%d, s=%s\n", m, n, s) );
        y = gettemp();
        temp = s[n+m-1];        /* with thanks to John Linderman */
run.c:804,810 - /n/dump/2009/1129/sys/src/cmd/awk/run.c:809,815
        char *p, *t;
        const char *os;
        Cell *x;
-       int flag = 0, n, ch;
+       int flag = 0, n;
        int fmtwd; /* format width */
        int fmtsz = recsize;
        char *buf = *pbuf;
run.c:838,844 - /n/dump/2009/1129/sys/src/cmd/awk/run.c:843,849
                        if (*s == '*') {
                                x = execute(a);
                                a = a->nnext;
-                               sprintf(t-1, "%d", fmtwd=getival(x, Imax, 
Imax));
+                               sprintf(t-1, "%d", fmtwd=(int) getfval(x));
                                if (fmtwd < 0)
                                        fmtwd = -fmtwd;
                                adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, 
&p, "format");
run.c:894,901 - /n/dump/2009/1129/sys/src/cmd/awk/run.c:900,907
                        sprintf(p, "%s", t);
                        break;
                case 'f':       sprintf(p, fmt, getfval(x)); break;
-               case 'd':       sprintf(p, fmt, getival(x, Imax, Imax)); break;
-               case 'u':       sprintf(p, fmt, getival(x, Imax, Imax)); break;
+               case 'd':       sprintf(p, fmt, (long) getfval(x)); break;
+               case 'u':       sprintf(p, fmt, (int) getfval(x)); break;
                case 's':
                        t = getsval(x);
                        n = strlen(t);
run.c:907,915 - /n/dump/2009/1129/sys/src/cmd/awk/run.c:913,920
                        break;
                case 'c':
                        if (isnum(x)) {
-                               ch = getival(x, Imax, Imax);
-                               if (ch)
-                                       sprintf(p, fmt, ch);
+                               if (getfval(x))
+                                       sprintf(p, fmt, (int) getfval(x));
                                else {
                                        *p++ = '\0'; /* explicit null byte */
                                        *p = '\0';   /* next output will start 
here */
  Awkfloat
  strtonum(Cell *x)
  {
run.c:1566,1579 - /n/dump/2009/1129/sys/src/cmd/awk/run.c:1583,1589
                        u = fflush(fp);
                break;
        case FUTF:
-               r = getival(x, Imax, Imax);
-               if(r < 0)
-                       r = -r;
-               if(r > Runemax){
-                       WARNING("utf argument out-of-range %g", getfval(x));
-                       r = Runeerror;
-               }
-               wc = r;
+               wc = (int)getfval(x);
                mbc[wctomb(mbc, wc)] = 0;
                tempfree(x);
                x = gettemp();


Reply via email to