Deleted Added
full compact
run.c (90902) run.c (107806)
1/****************************************************************
2Copyright (C) Lucent Technologies 1997
3All Rights Reserved
4
5Permission to use, copy, modify, and distribute this software and
6its documentation for any purpose and without fee is hereby
7granted, provided that the above copyright notice appear in all
8copies and that both that the copyright notice and this

--- 76 unchanged lines hidden (view full) ---

85static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM };
86Cell *jret = &retcell;
87static Cell tempcell ={ OCELL, CTEMP, 0, "", 0.0, NUM|STR|DONTFREE };
88
89Node *curnode = NULL; /* the node being executed, for debugging */
90
91/* buffer memory management */
92int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr,
1/****************************************************************
2Copyright (C) Lucent Technologies 1997
3All Rights Reserved
4
5Permission to use, copy, modify, and distribute this software and
6its documentation for any purpose and without fee is hereby
7granted, provided that the above copyright notice appear in all
8copies and that both that the copyright notice and this

--- 76 unchanged lines hidden (view full) ---

85static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM };
86Cell *jret = &retcell;
87static Cell tempcell ={ OCELL, CTEMP, 0, "", 0.0, NUM|STR|DONTFREE };
88
89Node *curnode = NULL; /* the node being executed, for debugging */
90
91/* buffer memory management */
92int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr,
93 char *whatrtn)
93 const char *whatrtn)
94/* pbuf: address of pointer to buffer being managed
95 * psiz: address of buffer size variable
96 * minlen: minimum length of buffer needed
97 * quantum: buffer size quantum
98 * pbptr: address of movable pointer into buffer, or 0 if none
99 * whatrtn: name of the calling routine if failure should cause fatal error
100 *
101 * return 0 for realloc failure, !=0 for success

--- 140 unchanged lines hidden (view full) ---

242 s, ncall, ndef);
243 if (ncall + ndef > NARGS)
244 FATAL("function %s has %d arguments, limit %d", s, ncall+ndef, NARGS);
245 for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */
246 dprintf( ("evaluate args[%d], fp=%d:\n", i, (int) (fp-frame)) );
247 y = execute(x);
248 oargs[i] = y;
249 dprintf( ("args[%d]: %s %f <%s>, t=%o\n",
94/* pbuf: address of pointer to buffer being managed
95 * psiz: address of buffer size variable
96 * minlen: minimum length of buffer needed
97 * quantum: buffer size quantum
98 * pbptr: address of movable pointer into buffer, or 0 if none
99 * whatrtn: name of the calling routine if failure should cause fatal error
100 *
101 * return 0 for realloc failure, !=0 for success

--- 140 unchanged lines hidden (view full) ---

242 s, ncall, ndef);
243 if (ncall + ndef > NARGS)
244 FATAL("function %s has %d arguments, limit %d", s, ncall+ndef, NARGS);
245 for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */
246 dprintf( ("evaluate args[%d], fp=%d:\n", i, (int) (fp-frame)) );
247 y = execute(x);
248 oargs[i] = y;
249 dprintf( ("args[%d]: %s %f <%s>, t=%o\n",
250 i, y->nval, y->fval, isarr(y) ? "(array)" : y->sval, y->tval) );
250 i, NN(y->nval), y->fval, isarr(y) ? "(array)" : NN(y->sval), y->tval) );
251 if (isfcn(y))
252 FATAL("can't use function %s as argument in %s", y->nval, s);
253 if (isarr(y))
254 args[i] = y; /* arrays by ref */
255 else
256 args[i] = copycell(y);
257 tempfree(y);
258 }

--- 199 unchanged lines hidden (view full) ---

458 if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, 0))
459 FATAL("out of memory for %s[%s...]", x->nval, buf);
460 strcat(buf, s);
461 if (np->nnext)
462 strcat(buf, *SUBSEP);
463 tempfree(y);
464 }
465 if (!isarr(x)) {
251 if (isfcn(y))
252 FATAL("can't use function %s as argument in %s", y->nval, s);
253 if (isarr(y))
254 args[i] = y; /* arrays by ref */
255 else
256 args[i] = copycell(y);
257 tempfree(y);
258 }

--- 199 unchanged lines hidden (view full) ---

458 if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, 0))
459 FATAL("out of memory for %s[%s...]", x->nval, buf);
460 strcat(buf, s);
461 if (np->nnext)
462 strcat(buf, *SUBSEP);
463 tempfree(y);
464 }
465 if (!isarr(x)) {
466 dprintf( ("making %s into an array\n", x->nval) );
466 dprintf( ("making %s into an array\n", NN(x->nval)) );
467 if (freeable(x))
468 xfree(x->sval);
469 x->tval &= ~(STR|NUM|DONTFREE);
470 x->tval |= ARR;
471 x->sval = (char *) makesymtab(NSYMTAB);
472 }
473 z = setsymtab(buf, "", 0.0, STR|NUM, (Array *) x->sval);
474 z->ctype = OCELL;

--- 84 unchanged lines hidden (view full) ---

559
560
561Cell *matchop(Node **a, int n) /* ~ and match() */
562{
563 Cell *x, *y;
564 char *s, *t;
565 int i;
566 fa *pfa;
467 if (freeable(x))
468 xfree(x->sval);
469 x->tval &= ~(STR|NUM|DONTFREE);
470 x->tval |= ARR;
471 x->sval = (char *) makesymtab(NSYMTAB);
472 }
473 z = setsymtab(buf, "", 0.0, STR|NUM, (Array *) x->sval);
474 z->ctype = OCELL;

--- 84 unchanged lines hidden (view full) ---

559
560
561Cell *matchop(Node **a, int n) /* ~ and match() */
562{
563 Cell *x, *y;
564 char *s, *t;
565 int i;
566 fa *pfa;
567 int (*mf)(fa *, char *) = match, mode = 0;
567 int (*mf)(fa *, const char *) = match, mode = 0;
568
569 if (n == MATCHFCN) {
570 mf = pmatch;
571 mode = 1;
572 }
573 x = execute(a[1]); /* a[1] = target text */
574 s = getsval(x);
575 if (a[0] == 0) /* a[1] == 0: already-compiled reg expr */

--- 88 unchanged lines hidden (view full) ---

664 FATAL("unknown relational operator %d", n);
665 }
666 return 0; /*NOTREACHED*/
667}
668
669void tfree(Cell *a) /* free a tempcell */
670{
671 if (freeable(a)) {
568
569 if (n == MATCHFCN) {
570 mf = pmatch;
571 mode = 1;
572 }
573 x = execute(a[1]); /* a[1] = target text */
574 s = getsval(x);
575 if (a[0] == 0) /* a[1] == 0: already-compiled reg expr */

--- 88 unchanged lines hidden (view full) ---

664 FATAL("unknown relational operator %d", n);
665 }
666 return 0; /*NOTREACHED*/
667}
668
669void tfree(Cell *a) /* free a tempcell */
670{
671 if (freeable(a)) {
672 dprintf( ("freeing %s %s %o\n", a->nval, a->sval, a->tval) );
672 dprintf( ("freeing %s %s %o\n", NN(a->nval), NN(a->sval), a->tval) );
673 xfree(a->sval);
674 }
675 if (a == tmps)
676 FATAL("tempcell list is curdled");
677 a->cnext = tmps;
678 tmps = a;
679}
680

--- 104 unchanged lines hidden (view full) ---

785 tempfree(x);
786 tempfree(y);
787 setfval(z, v);
788 return(z);
789}
790
791#define MAXNUMSIZE 50
792
673 xfree(a->sval);
674 }
675 if (a == tmps)
676 FATAL("tempcell list is curdled");
677 a->cnext = tmps;
678 tmps = a;
679}
680

--- 104 unchanged lines hidden (view full) ---

785 tempfree(x);
786 tempfree(y);
787 setfval(z, v);
788 return(z);
789}
790
791#define MAXNUMSIZE 50
792
793int format(char **pbuf, int *pbufsize, char *s, Node *a) /* printf-like conversions */
793int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like conversions */
794{
795 char *fmt;
794{
795 char *fmt;
796 char *p, *t, *os;
796 char *p, *t;
797 const char *os;
797 Cell *x;
798 int flag = 0, n;
799 int fmtwd; /* format width */
800 int fmtsz = recsize;
801 char *buf = *pbuf;
802 int bufsize = *pbufsize;
803
804 os = s;

--- 34 unchanged lines hidden (view full) ---

839 }
840 *t = '\0';
841 if (fmtwd < 0)
842 fmtwd = -fmtwd;
843 adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
844
845 switch (*s) {
846 case 'f': case 'e': case 'g': case 'E': case 'G':
798 Cell *x;
799 int flag = 0, n;
800 int fmtwd; /* format width */
801 int fmtsz = recsize;
802 char *buf = *pbuf;
803 int bufsize = *pbufsize;
804
805 os = s;

--- 34 unchanged lines hidden (view full) ---

840 }
841 *t = '\0';
842 if (fmtwd < 0)
843 fmtwd = -fmtwd;
844 adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
845
846 switch (*s) {
847 case 'f': case 'e': case 'g': case 'E': case 'G':
847 flag = 1;
848 flag = 'f';
848 break;
849 case 'd': case 'i':
849 break;
850 case 'd': case 'i':
850 flag = 2;
851 flag = 'd';
851 if(*(s-1) == 'l') break;
852 *(t-1) = 'l';
853 *t = 'd';
854 *++t = '\0';
855 break;
856 case 'o': case 'x': case 'X': case 'u':
852 if(*(s-1) == 'l') break;
853 *(t-1) = 'l';
854 *t = 'd';
855 *++t = '\0';
856 break;
857 case 'o': case 'x': case 'X': case 'u':
857 flag = *(s-1) == 'l' ? 2 : 3;
858 flag = *(s-1) == 'l' ? 'd' : 'u';
858 break;
859 case 's':
859 break;
860 case 's':
860 flag = 4;
861 flag = 's';
861 break;
862 case 'c':
862 break;
863 case 'c':
863 flag = 5;
864 flag = 'c';
864 break;
865 default:
866 WARNING("weird printf conversion %s", fmt);
865 break;
866 default:
867 WARNING("weird printf conversion %s", fmt);
867 flag = 0;
868 flag = '?';
868 break;
869 }
870 if (a == NULL)
871 FATAL("not enough args in printf(%s)", os);
872 x = execute(a);
873 a = a->nnext;
874 n = MAXNUMSIZE;
875 if (fmtwd > n)
876 n = fmtwd;
877 adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format");
878 switch (flag) {
869 break;
870 }
871 if (a == NULL)
872 FATAL("not enough args in printf(%s)", os);
873 x = execute(a);
874 a = a->nnext;
875 n = MAXNUMSIZE;
876 if (fmtwd > n)
877 n = fmtwd;
878 adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format");
879 switch (flag) {
879 case 0: sprintf(p, "%s", fmt); /* unknown, so dump it too */
880 case '?': sprintf(p, "%s", fmt); /* unknown, so dump it too */
880 t = getsval(x);
881 n = strlen(t);
882 if (fmtwd > n)
883 n = fmtwd;
884 adjbuf(&buf, &bufsize, 1+strlen(p)+n+p-buf, recsize, &p, "format");
885 p += strlen(p);
886 sprintf(p, "%s", t);
887 break;
881 t = getsval(x);
882 n = strlen(t);
883 if (fmtwd > n)
884 n = fmtwd;
885 adjbuf(&buf, &bufsize, 1+strlen(p)+n+p-buf, recsize, &p, "format");
886 p += strlen(p);
887 sprintf(p, "%s", t);
888 break;
888 case 1: sprintf(p, fmt, getfval(x)); break;
889 case 2: sprintf(p, fmt, (long) getfval(x)); break;
890 case 3: sprintf(p, fmt, (int) getfval(x)); break;
891 case 4:
889 case 'f': sprintf(p, fmt, getfval(x)); break;
890 case 'd': sprintf(p, fmt, (long) getfval(x)); break;
891 case 'u': sprintf(p, fmt, (int) getfval(x)); break;
892 case 's':
892 t = getsval(x);
893 n = strlen(t);
894 if (fmtwd > n)
895 n = fmtwd;
896 if (!adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, 0))
897 FATAL("huge string/format (%d chars) in printf %.30s... ran format() out of memory", n, t);
898 sprintf(p, fmt, t);
899 break;
893 t = getsval(x);
894 n = strlen(t);
895 if (fmtwd > n)
896 n = fmtwd;
897 if (!adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, 0))
898 FATAL("huge string/format (%d chars) in printf %.30s... ran format() out of memory", n, t);
899 sprintf(p, fmt, t);
900 break;
900 case 5:
901 case 'c':
901 if (isnum(x)) {
902 if (getfval(x))
903 sprintf(p, fmt, (int) getfval(x));
902 if (isnum(x)) {
903 if (getfval(x))
904 sprintf(p, fmt, (int) getfval(x));
904 else
905 *p++ = '\0';
905 else {
906 *p++ = '\0'; /* explicit null byte */
907 *p = '\0'; /* next output will start here */
908 }
906 } else
907 sprintf(p, fmt, getsval(x)[0]);
908 break;
909 } else
910 sprintf(p, fmt, getsval(x)[0]);
911 break;
912 default:
913 FATAL("can't happen: bad conversion %c in format()", flag);
909 }
910 tempfree(x);
911 p += strlen(p);
912 s++;
913 }
914 *p = '\0';
915 free(fmt);
916 for ( ; a; a = a->nnext) /* evaluate any remaining args */

--- 288 unchanged lines hidden (view full) ---

1205 fs = getsval(x);
1206 } else if (arg3type == REGEXPR)
1207 fs = "(regexpr)"; /* split(str,arr,/regexpr/) */
1208 else
1209 FATAL("illegal type of split");
1210 sep = *fs;
1211 ap = execute(a[1]); /* array name */
1212 freesymtab(ap);
914 }
915 tempfree(x);
916 p += strlen(p);
917 s++;
918 }
919 *p = '\0';
920 free(fmt);
921 for ( ; a; a = a->nnext) /* evaluate any remaining args */

--- 288 unchanged lines hidden (view full) ---

1210 fs = getsval(x);
1211 } else if (arg3type == REGEXPR)
1212 fs = "(regexpr)"; /* split(str,arr,/regexpr/) */
1213 else
1214 FATAL("illegal type of split");
1215 sep = *fs;
1216 ap = execute(a[1]); /* array name */
1217 freesymtab(ap);
1213 dprintf( ("split: s=|%s|, a=%s, sep=|%s|\n", s, ap->nval, fs) );
1218 dprintf( ("split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs) );
1214 ap->tval &= ~STR;
1215 ap->tval |= ARR;
1216 ap->sval = (char *) makesymtab(NSYMTAB);
1217
1218 n = 0;
1219 if ((*s != '\0' && strlen(fs) > 1) || arg3type == REGEXPR) { /* reg expr */
1220 fa *pfa;
1221 if (arg3type == REGEXPR) { /* it's ready already */

--- 277 unchanged lines hidden (view full) ---

1499 srand((unsigned int) u);
1500 break;
1501 case FTOUPPER:
1502 case FTOLOWER:
1503 buf = tostring(getsval(x));
1504 if (t == FTOUPPER) {
1505 for (p = buf; *p; p++)
1506 if (islower((uschar) *p))
1219 ap->tval &= ~STR;
1220 ap->tval |= ARR;
1221 ap->sval = (char *) makesymtab(NSYMTAB);
1222
1223 n = 0;
1224 if ((*s != '\0' && strlen(fs) > 1) || arg3type == REGEXPR) { /* reg expr */
1225 fa *pfa;
1226 if (arg3type == REGEXPR) { /* it's ready already */

--- 277 unchanged lines hidden (view full) ---

1504 srand((unsigned int) u);
1505 break;
1506 case FTOUPPER:
1507 case FTOLOWER:
1508 buf = tostring(getsval(x));
1509 if (t == FTOUPPER) {
1510 for (p = buf; *p; p++)
1511 if (islower((uschar) *p))
1507 *p = toupper(*p);
1512 *p = toupper((uschar)*p);
1508 } else {
1509 for (p = buf; *p; p++)
1510 if (isupper((uschar) *p))
1513 } else {
1514 for (p = buf; *p; p++)
1515 if (isupper((uschar) *p))
1511 *p = tolower(*p);
1516 *p = tolower((uschar)*p);
1512 }
1513 tempfree(x);
1514 x = gettemp();
1515 setsval(x, buf);
1516 free(buf);
1517 return x;
1518 case FFLUSH:
1519 if (isrec(x) || strlen(getsval(x)) == 0) {

--- 26 unchanged lines hidden (view full) ---

1546 FILE *fp;
1547
1548 if (a[1] == 0) /* a[1] is redirection operator, a[2] is file */
1549 fp = stdout;
1550 else
1551 fp = redirect(ptoi(a[1]), a[2]);
1552 for (x = a[0]; x != NULL; x = x->nnext) {
1553 y = execute(x);
1517 }
1518 tempfree(x);
1519 x = gettemp();
1520 setsval(x, buf);
1521 free(buf);
1522 return x;
1523 case FFLUSH:
1524 if (isrec(x) || strlen(getsval(x)) == 0) {

--- 26 unchanged lines hidden (view full) ---

1551 FILE *fp;
1552
1553 if (a[1] == 0) /* a[1] is redirection operator, a[2] is file */
1554 fp = stdout;
1555 else
1556 fp = redirect(ptoi(a[1]), a[2]);
1557 for (x = a[0]; x != NULL; x = x->nnext) {
1558 y = execute(x);
1554 fputs(getsval(y), fp);
1559 fputs(getpssval(y), fp);
1555 tempfree(y);
1556 if (x->nnext == NULL)
1557 fputs(*ORS, fp);
1558 else
1559 fputs(*OFS, fp);
1560 }
1561 if (a[1] != 0)
1562 fflush(fp);

--- 22 unchanged lines hidden (view full) ---

1585 if (fp == NULL)
1586 FATAL("can't open file %s", fname);
1587 tempfree(x);
1588 return fp;
1589}
1590
1591struct files {
1592 FILE *fp;
1560 tempfree(y);
1561 if (x->nnext == NULL)
1562 fputs(*ORS, fp);
1563 else
1564 fputs(*OFS, fp);
1565 }
1566 if (a[1] != 0)
1567 fflush(fp);

--- 22 unchanged lines hidden (view full) ---

1590 if (fp == NULL)
1591 FATAL("can't open file %s", fname);
1592 tempfree(x);
1593 return fp;
1594}
1595
1596struct files {
1597 FILE *fp;
1593 char *fname;
1598 const char *fname;
1594 int mode; /* '|', 'a', 'w' => LE/LT, GT */
1595} files[FOPEN_MAX] ={
1596 { NULL, "/dev/stdin", LT }, /* watch out: don't free this! */
1597 { NULL, "/dev/stdout", GT },
1598 { NULL, "/dev/stderr", GT }
1599};
1600
1601void stdinit(void) /* in case stdin, etc., are not constants */
1602{
1603 files[0].fp = stdin;
1604 files[1].fp = stdout;
1605 files[2].fp = stderr;
1606}
1607
1599 int mode; /* '|', 'a', 'w' => LE/LT, GT */
1600} files[FOPEN_MAX] ={
1601 { NULL, "/dev/stdin", LT }, /* watch out: don't free this! */
1602 { NULL, "/dev/stdout", GT },
1603 { NULL, "/dev/stderr", GT }
1604};
1605
1606void stdinit(void) /* in case stdin, etc., are not constants */
1607{
1608 files[0].fp = stdin;
1609 files[1].fp = stdout;
1610 files[2].fp = stderr;
1611}
1612
1608FILE *openfile(int a, char *us)
1613FILE *openfile(int a, const char *us)
1609{
1614{
1610 char *s = us;
1615 const char *s = us;
1611 int i, m;
1612 FILE *fp = 0;
1613
1614 if (*s == '\0')
1615 FATAL("null file name in print or getline");
1616 for (i=0; i < FOPEN_MAX; i++)
1617 if (files[i].fname && strcmp(s, files[i].fname) == 0) {
1618 if (a == files[i].mode || (a==APPEND && files[i].mode==GT))

--- 27 unchanged lines hidden (view full) ---

1646 if (fp != NULL) {
1647 files[i].fname = tostring(s);
1648 files[i].fp = fp;
1649 files[i].mode = m;
1650 }
1651 return fp;
1652}
1653
1616 int i, m;
1617 FILE *fp = 0;
1618
1619 if (*s == '\0')
1620 FATAL("null file name in print or getline");
1621 for (i=0; i < FOPEN_MAX; i++)
1622 if (files[i].fname && strcmp(s, files[i].fname) == 0) {
1623 if (a == files[i].mode || (a==APPEND && files[i].mode==GT))

--- 27 unchanged lines hidden (view full) ---

1651 if (fp != NULL) {
1652 files[i].fname = tostring(s);
1653 files[i].fp = fp;
1654 files[i].mode = m;
1655 }
1656 return fp;
1657}
1658
1654char *filename(FILE *fp)
1659const char *filename(FILE *fp)
1655{
1656 int i;
1657
1658 for (i = 0; i < FOPEN_MAX; i++)
1659 if (fp == files[i].fp)
1660 return files[i].fname;
1661 return "???";
1662}

--- 246 unchanged lines hidden ---
1660{
1661 int i;
1662
1663 for (i = 0; i < FOPEN_MAX; i++)
1664 if (fp == files[i].fp)
1665 return files[i].fname;
1666 return "???";
1667}

--- 246 unchanged lines hidden ---