Annotation of researchv9/cmd/grap/misc.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include "grap.h"
                      3: #include "y.tab.h"
                      4: 
                      5: int    nnum    = 0;    /* number of saved numbers */
                      6: double num[MAXNUM];
                      7: 
                      8: int    just;           /* current justification mode (RJUST, etc.) */
                      9: int    sizeop;         /* current optional operator for size change */
                     10: double sizexpr;        /* current size change expression */
                     11: 
                     12: savenum(n, f)  /* save f in num[n] */
                     13:        int n;
                     14:        double f;
                     15: {
                     16:        num[n] = f;
                     17:        nnum = n+1;
                     18:        if (nnum >= MAXNUM)
                     19:                yyerror("too many numbers");
                     20: }
                     21: 
                     22: setjust(j)
                     23: {
                     24:        just |= j;
                     25: }
                     26: 
                     27: setsize(op, expr)
                     28:        int op;
                     29:        double expr;
                     30: {
                     31:        sizeop = op;
                     32:        sizexpr = expr;
                     33: }
                     34: 
                     35: char *tostring(s)
                     36:        register char *s;
                     37: {
                     38:        register char *p;
                     39: 
                     40:        p = malloc(strlen(s)+1);
                     41:        if (p == NULL)
                     42:                fatal("out of space in tostring on %s", s);
                     43:        strcpy(p, s);
                     44:        return(p);
                     45: }
                     46: 
                     47: range(pt)      /* update the range for point pt */
                     48:        Point pt;
                     49: {
                     50:        Obj *p = pt.obj;
                     51: 
                     52:        if (!(p->coord & XFLAG)) {
                     53:                if (pt.x > p->pt1.x)
                     54:                        p->pt1.x = pt.x;
                     55:                if (pt.x < p->pt.x)
                     56:                        p->pt.x = pt.x;
                     57:        }
                     58:        if (!(p->coord & YFLAG)) {
                     59:                if (pt.y > p->pt1.y)
                     60:                        p->pt1.y = pt.y;
                     61:                if (pt.y < p->pt.y)
                     62:                        p->pt.y = pt.y;
                     63:        }
                     64: }
                     65: 
                     66: halfrange(p, side, val)        /* record max and min for one direction */
                     67:        Obj *p;
                     68:        int side;
                     69:        double val;
                     70: {
                     71:        if (!(p->coord&XFLAG) && (side == LEFT || side == RIGHT)) {
                     72:                if (val < p->pt.y)
                     73:                        p->pt.y = val;
                     74:                if (val > p->pt1.y)
                     75:                        p->pt1.y = val;
                     76:        } else if (!(p->coord&YFLAG) && (side == TOP || side == BOT)) {
                     77:                if (val < p->pt.x)
                     78:                        p->pt.x = val;
                     79:                if (val > p->pt1.x)
                     80:                        p->pt1.x = val;
                     81:        }
                     82: }
                     83: 
                     84: 
                     85: Obj *lookup(s, inst)   /* find s in objlist, install if inst */
                     86:        char *s;
                     87:        int inst;
                     88: {
                     89:        Obj *p;
                     90:        int found = 0;
                     91: 
                     92:        for (p = objlist; p; p = p->next)
                     93:                if (strcmp(s, p->name) == 0) {
                     94:                        found = 1;
                     95:                        break;
                     96:                }
                     97:        if (p == NULL && inst != 0) {
                     98:                p = (Obj *) calloc(1, sizeof(Obj));
                     99:                if (p == NULL)
                    100:                        fatal("out of space in lookup");
                    101:                p->name = tostring(s);
                    102:                p->type = NAME;
                    103:                p->pt = ptmax;
                    104:                p->pt1 = ptmin;
                    105:                p->fval = 0.0;
                    106:                p->next = objlist;
                    107:                objlist = p;
                    108:        }
                    109:        dprintf("lookup(%s,%d) = %d\n", s, inst, found);
                    110:        return p;
                    111: }
                    112: 
                    113: double getvar(p)       /* return value of variable */
                    114:        Obj *p;
                    115: {
                    116:        return p->fval;
                    117: }
                    118: 
                    119: double setvar(p, f)    /* set value of variable to f */
                    120:        Obj *p;
                    121:        double f;
                    122: {
                    123:        if (strcmp(p->name, "pointsize") == 0) {        /* kludge */
                    124:                pointsize = f;
                    125:                ps_set = 1;
                    126:        }
                    127:        p->type = VARNAME;
                    128:        return p->fval = f;
                    129: }
                    130: 
                    131: Point makepoint(s, x, y)       /* make a Point */
                    132:        Obj *s;
                    133:        double x, y;
                    134: {
                    135:        Point p;
                    136:        
                    137:        dprintf("makepoint: %s, %g,%g\n", s->name, x, y);
                    138:        p.obj = s;
                    139:        p.x = x;
                    140:        p.y = y;
                    141:        return p;
                    142: }
                    143: 
                    144: Attr *makefattr(type, fval)    /* set double in attribute */
                    145:        int type;
                    146:        double fval;
                    147: {
                    148:        return makeattr(type, fval, (char *) 0, 0, 0);
                    149: }
                    150: 
                    151: Attr *makesattr(s, a)          /* make an Attr cell containing s */
                    152:        char *s;
                    153:        int a;  /* fake */
                    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:                fatal("out of space in makeattr");
                    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(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:                yyerror("too many expressions in sprintf");
                    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.