Annotation of researchv10no/cmd/grap/non-ansi/misc.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <string.h>
                      3: #include "grap.h"
                      4: #include "y.tab.h"
                      5: 
                      6: int    nnum    = 0;    /* number of saved numbers */
                      7: double num[MAXNUM];
                      8: 
                      9: int    just;           /* current justification mode (RJUST, etc.) */
                     10: int    sizeop;         /* current optional operator for size change */
                     11: double sizexpr;        /* current size change expression */
                     12: 
                     13: savenum(n, f)  /* save f in num[n] */
                     14:        int n;
                     15:        double f;
                     16: {
                     17:        num[n] = f;
                     18:        nnum = n+1;
                     19:        if (nnum >= MAXNUM)
                     20:                ERROR "too many numbers" WARNING;
                     21: }
                     22: 
                     23: setjust(j)
                     24: {
                     25:        just |= j;
                     26: }
                     27: 
                     28: setsize(op, expr)
                     29:        int op;
                     30:        double expr;
                     31: {
                     32:        sizeop = op;
                     33:        sizexpr = expr;
                     34: }
                     35: 
                     36: char *tostring(s)
                     37:        register char *s;
                     38: {
                     39:        register char *p;
                     40: 
                     41:        p = malloc(strlen(s)+1);
                     42:        if (p == NULL)
                     43:                ERROR "out of space in tostring on %s", s FATAL;
                     44:        strcpy(p, s);
                     45:        return(p);
                     46: }
                     47: 
                     48: range(pt)      /* update the range for point pt */
                     49:        Point pt;
                     50: {
                     51:        Obj *p = pt.obj;
                     52: 
                     53:        if (!(p->coord & XFLAG)) {
                     54:                if (pt.x > p->pt1.x)
                     55:                        p->pt1.x = pt.x;
                     56:                if (pt.x < p->pt.x)
                     57:                        p->pt.x = pt.x;
                     58:        }
                     59:        if (!(p->coord & YFLAG)) {
                     60:                if (pt.y > p->pt1.y)
                     61:                        p->pt1.y = pt.y;
                     62:                if (pt.y < p->pt.y)
                     63:                        p->pt.y = pt.y;
                     64:        }
                     65: }
                     66: 
                     67: halfrange(p, side, val)        /* record max and min for one direction */
                     68:        Obj *p;
                     69:        int side;
                     70:        double val;
                     71: {
                     72:        if (!(p->coord&XFLAG) && (side == LEFT || side == RIGHT)) {
                     73:                if (val < p->pt.y)
                     74:                        p->pt.y = val;
                     75:                if (val > p->pt1.y)
                     76:                        p->pt1.y = val;
                     77:        } else if (!(p->coord&YFLAG) && (side == TOP || side == BOT)) {
                     78:                if (val < p->pt.x)
                     79:                        p->pt.x = val;
                     80:                if (val > p->pt1.x)
                     81:                        p->pt1.x = val;
                     82:        }
                     83: }
                     84: 
                     85: 
                     86: Obj *lookup(s, inst)   /* find s in objlist, install if inst */
                     87:        char *s;
                     88:        int inst;
                     89: {
                     90:        Obj *p;
                     91:        int found = 0;
                     92: 
                     93:        for (p = objlist; p; p = p->next)
                     94:                if (strcmp(s, p->name) == 0) {
                     95:                        found = 1;
                     96:                        break;
                     97:                }
                     98:        if (p == NULL && inst != 0) {
                     99:                p = (Obj *) calloc(1, sizeof(Obj));
                    100:                if (p == NULL)
                    101:                        ERROR "out of space in lookup" FATAL;
                    102:                p->name = tostring(s);
                    103:                p->type = NAME;
                    104:                p->pt = ptmax;
                    105:                p->pt1 = ptmin;
                    106:                p->fval = 0.0;
                    107:                p->next = objlist;
                    108:                objlist = p;
                    109:        }
                    110:        dprintf("lookup(%s,%d) = %d\n", s, inst, found);
                    111:        return p;
                    112: }
                    113: 
                    114: double getvar(p)       /* return value of variable */
                    115:        Obj *p;
                    116: {
                    117:        return p->fval;
                    118: }
                    119: 
                    120: double setvar(p, f)    /* set value of variable to f */
                    121:        Obj *p;
                    122:        double f;
                    123: {
                    124:        if (strcmp(p->name, "pointsize") == 0) {        /* kludge */
                    125:                pointsize = f;
                    126:                ps_set = 1;
                    127:        }
                    128:        p->type = VARNAME;
                    129:        return p->fval = f;
                    130: }
                    131: 
                    132: Point makepoint(s, x, y)       /* make a Point */
                    133:        Obj *s;
                    134:        double x, y;
                    135: {
                    136:        Point p;
                    137:        
                    138:        dprintf("makepoint: %s, %g,%g\n", s->name, x, y);
                    139:        p.obj = s;
                    140:        p.x = x;
                    141:        p.y = y;
                    142:        return p;
                    143: }
                    144: 
                    145: Attr *makefattr(type, fval)    /* set double in attribute */
                    146:        int type;
                    147:        double fval;
                    148: {
                    149:        return makeattr(type, fval, (char *) 0, 0, 0);
                    150: }
                    151: 
                    152: Attr *makesattr(s)             /* make an Attr cell containing s */
                    153:        char *s;
                    154: {
                    155:        Attr *ap = makeattr(STRING, sizexpr, s, just, sizeop);
                    156:        just = sizeop = 0;
                    157:        sizexpr = 0.0;
                    158:        return ap;
                    159: }
                    160: 
                    161: Attr *makeattr(type, fval, sval, just, op)
                    162:        int type;
                    163:        double fval;
                    164:        char *sval;
                    165:        int just, op;
                    166: {
                    167:        Attr *a;
                    168: 
                    169:        a = (Attr *) malloc(sizeof(Attr));
                    170:        if (a == NULL)
                    171:                ERROR "out of space in makeattr" FATAL;
                    172:        a->type = type;
                    173:        a->fval = fval;
                    174:        a->sval = sval;
                    175:        a->just = just;
                    176:        a->op = op;
                    177:        a->next = NULL;
                    178:        return a;
                    179: }
                    180: 
                    181: Attr *addattr(a1, ap)  /* add attr ap to end of list a1 */
                    182:        Attr *a1, *ap;
                    183: {
                    184:        Attr *p;
                    185: 
                    186:        if (a1 == 0)
                    187:                return ap;
                    188:        if (ap == 0)
                    189:                return a1;
                    190:        for (p = a1; p->next; p = p->next)
                    191:                ;
                    192:        p->next = ap;
                    193:        return a1;
                    194: }
                    195: 
                    196: freeattr(ap)   /* free an attribute list */
                    197:        Attr *ap;
                    198: {
                    199:        Attr *p;
                    200: 
                    201:        while (ap) {
                    202:                p = ap->next;   /* save next */
                    203:                if (ap->sval)
                    204:                        free(ap->sval);
                    205:                free((char *) ap);
                    206:                ap = p;
                    207:        }
                    208: }
                    209: 
                    210: char *slprint(stringlist)      /* print strings from stringlist */
                    211:        Attr *stringlist;
                    212: {
                    213:        int ntext, n, last_op, last_just;
                    214:        double last_fval;
                    215:        static char buf[1000];
                    216:        Attr *ap;
                    217: 
                    218:        buf[0] = '\0';
                    219:        last_op = last_just = 0;
                    220:        last_fval = 0.0;
                    221:        for (ntext = 0, ap = stringlist; ap != NULL; ap = ap->next)
                    222:                ntext++;
                    223:        sprintf(buf, "box invis wid 0 ht %d*textht", ntext);
                    224:        n = strlen(buf);
                    225:        for (ap = stringlist; ap != NULL; ap = ap->next) {
                    226:                if (ap->op == 0) {      /* propagate last value */
                    227:                        ap->op = last_op;
                    228:                        ap->fval = last_fval;
                    229:                } else {
                    230:                        last_op = ap->op;
                    231:                        last_fval = ap->fval;
                    232:                }
                    233:                sprintf(buf+n, " \"%s\"", ps_set || ap->op ? sizeit(ap) : ap->sval);
                    234:                if (ap->just)
                    235:                        last_just = ap->just;
                    236:                if (last_just)
                    237:                        strcat(buf+n, juststr(last_just));
                    238:                n = strlen(buf);
                    239:        }
                    240:        return buf;     /* watch it:  static */
                    241: }
                    242: 
                    243: char *juststr(j)       /* convert RJUST, etc., into string */
                    244:        int j;
                    245: {
                    246:        static char buf[50];
                    247: 
                    248:        buf[0] = '\0';
                    249:        if (j & RJUST)
                    250:                strcat(buf, " rjust");
                    251:        if (j & LJUST)
                    252:                strcat(buf, " ljust");
                    253:        if (j & ABOVE)
                    254:                strcat(buf, " above");
                    255:        if (j & BELOW)
                    256:                strcat(buf, " below");
                    257:        return buf;     /* watch it:  static */
                    258: }
                    259: 
                    260: char *sprntf(s, ap)    /* sprintf(s, attrlist ap) */
                    261:        char *s;
                    262:        Attr *ap;
                    263: {
                    264:        char buf[500];
                    265:        int n;
                    266:        Attr *p;
                    267: 
                    268:        for (n = 0, p = ap; p; p = p->next)
                    269:                n++;
                    270:        switch (n) {
                    271:        case 0:
                    272:                return s;
                    273:        case 1:
                    274:                sprintf(buf, s, ap->fval);
                    275:                break;
                    276:        case 2:
                    277:                sprintf(buf, s, ap->fval, ap->next->fval);
                    278:                break;
                    279:        case 3:
                    280:                sprintf(buf, s, ap->fval, ap->next->fval, ap->next->next->fval);
                    281:                break;
                    282:        case 5:
                    283:                ERROR "too many expressions in sprintf" WARNING;
                    284:        case 4:
                    285:                sprintf(buf, s, ap->fval, ap->next->fval, ap->next->next->fval, ap->next->next->next->fval);
                    286:                break;
                    287:        }
                    288:        free(s);
                    289:        return tostring(buf);
                    290: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.