|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <ctype.h> ! 3: #include "grap.h" ! 4: #include "y.tab.h" ! 5: ! 6: double margin = MARGIN; /* extra space around edges */ ! 7: extern double frame_ht, frame_wid, ticklen; ! 8: ! 9: char graphname[50] = "Graph"; ! 10: char graphpos[200] = ""; ! 11: ! 12: print() /* arrange final output */ ! 13: { ! 14: FILE *fd; ! 15: Obj *p, *dfp, *setauto(); ! 16: int c; ! 17: static int firstG1 = 0; ! 18: double dx, dy, xfac, yfac; ! 19: extern double pow(); ! 20: ! 21: if (tfd != stdout) { ! 22: fclose(tfd); /* end the temp file */ ! 23: tfd = stdout; ! 24: } ! 25: ! 26: if ((p=lookup("margin",0)) != NULL) ! 27: margin = p->fval; ! 28: if (frame_ht < 0) /* wasn't set explicitly, so use default */ ! 29: frame_ht = getvar(lookup("frameht", 0)); ! 30: if (frame_wid < 0) ! 31: frame_wid = getvar(lookup("framewid", 0)); ! 32: dfp = NULL; ! 33: for (p = objlist; p; p = p->next) { ! 34: dprintf("print: name = <%s>, type = %d\n", p->name, p->type); ! 35: if (p->type == NAME) { ! 36: Point pt, pt1; ! 37: pt = p->pt; ! 38: pt1 = p->pt1; ! 39: fprintf(tfd, "\t# %s %g .. %g, %g .. %g\n", ! 40: p->name, pt.x, pt1.x, pt.y, pt1.y); ! 41: if (p->log & XFLAG) { ! 42: if (pt.x <= 0.0) ! 43: fatal("can't take log of x coord %g", pt.x); ! 44: logit(pt.x); ! 45: logit(pt1.x); ! 46: } ! 47: if (p->log & YFLAG) { ! 48: if (pt.y <= 0.0) ! 49: fatal("can't take log of y coord %g", pt.y); ! 50: logit(pt.y); ! 51: logit(pt1.y); ! 52: } ! 53: if (!(p->coord & XFLAG)) { ! 54: dx = pt1.x - pt.x; ! 55: pt.x -= margin * dx; ! 56: pt1.x += margin * dx; ! 57: } ! 58: if (!(p->coord & YFLAG)) { ! 59: dy = pt1.y - pt.y; ! 60: pt.y -= margin * dy; ! 61: pt1.y += margin * dy; ! 62: } ! 63: if (autoticks && strcmp(p->name, dflt_coord) == 0) { ! 64: p->pt = pt; ! 65: p->pt1 = pt1; ! 66: if (p->log & XFLAG) { ! 67: p->pt.x = pow(10.0, pt.x); ! 68: p->pt1.x = pow(10.0, pt1.x); ! 69: } ! 70: if (p->log & YFLAG) { ! 71: p->pt.y = pow(10.0, pt.y); ! 72: p->pt1.y = pow(10.0, pt1.y); ! 73: } ! 74: dfp = setauto(p); ! 75: } ! 76: dx = pt1.x - pt.x; ! 77: dy = pt1.y - pt.y; ! 78: xfac = dx > 0 ? frame_wid/dx : 0; ! 79: yfac = dy > 0 ? frame_ht/dy : 0; ! 80: ! 81: fprintf(tfd, "define xy_%s @ ", p->name); ! 82: fprintf(tfd, "\t(($1)-(%g))*%g", pt.x, xfac); ! 83: fprintf(tfd, ", (($2)-(%g))*%g @\n", pt.y, yfac); ! 84: fprintf(tfd, "define x_%s @ ", p->name); ! 85: fprintf(tfd, "\t(($1)-(%g))*%g @\n", pt.x, xfac); ! 86: fprintf(tfd, "define y_%s @ ", p->name); ! 87: fprintf(tfd, "\t(($1)-(%g))*%g @\n", pt.y, yfac); ! 88: } ! 89: } ! 90: if (codegen) ! 91: frame(); ! 92: if (codegen && autoticks && dfp) ! 93: do_autoticks(dfp); ! 94: ! 95: if ((fd = fopen(tempfile, "r")) != NULL) { ! 96: while ((c = getc(fd)) != EOF) ! 97: putc(c, tfd); ! 98: fclose(fd); ! 99: } ! 100: tfd = NULL; ! 101: } ! 102: ! 103: endstat() /* clean up after each statement */ ! 104: { ! 105: extern int just, sizeop, tick_dir; ! 106: extern double sizexpr, lab_up, lab_rt; ! 107: ! 108: just = sizeop = 0; ! 109: lab_up = lab_rt = 0.0; ! 110: sizexpr = 0.0; ! 111: nnum = 0; ! 112: ntick = 0; ! 113: tside = 0; ! 114: tick_dir = OUT; ! 115: ticklen = TICKLEN; ! 116: } ! 117: ! 118: graph(s) /* graph statement */ ! 119: char *s; ! 120: { ! 121: char *p, *os; ! 122: int c; ! 123: ! 124: if (codegen) { ! 125: fprintf(stdout, "%s: [\n", graphname); ! 126: print(); /* pump out previous graph */ ! 127: fprintf(stdout, "\n] %s\n", graphpos); ! 128: reset(); ! 129: } ! 130: if (s) { ! 131: dprintf("into graph with <%s>\n", s); ! 132: os = s; ! 133: while ((c = *s) == ' ' || c == '\t') ! 134: s++; ! 135: if (c == '\0') ! 136: yyerror("no name on graph statement"); ! 137: if (!isupper(s[0])) ! 138: yyerror("graph name %s must be capitalized", s); ! 139: for (p=graphname; (c = *s) != ' ' && c != '\t' && c != '\0'; ) ! 140: *p++ = *s++; ! 141: *p = '\0'; ! 142: strcpy(graphpos, s); ! 143: dprintf("graphname = <%s>, graphpos = <%s>\n", graphname, graphpos); ! 144: free(os); ! 145: } ! 146: } ! 147: ! 148: setup() /* done at each .G1 */ ! 149: { ! 150: static int firstG1 = 0; ! 151: ! 152: reset(); ! 153: frame_ht = frame_wid = -1; /* reset in frame() */ ! 154: ticklen = getvar(lookup("ticklen", 0)); ! 155: if (firstG1++ == 0) ! 156: do_first(); ! 157: codegen = synerr = 0; ! 158: strcpy(graphname, "Graph"); ! 159: strcpy(graphpos, ""); ! 160: } ! 161: ! 162: do_first() /* done at first .G1: definitions, etc. */ ! 163: { ! 164: extern int lib; ! 165: extern char *lib_defines; ! 166: static char buf[50], buf1[50]; /* static because pbstr uses them */ ! 167: ! 168: sprintf(buf, "define pid /%d/\n", getpid()); ! 169: pbstr(buf); ! 170: if (lib != 0) { ! 171: if (access(lib_defines, 4) == 0) { ! 172: sprintf(buf1, "copy \"%s\"\n", lib_defines); ! 173: pbstr(buf1); ! 174: } else { ! 175: fprintf(stderr, "grap warning: can't open %s\n", lib_defines); ! 176: } ! 177: } ! 178: } ! 179: ! 180: reset() /* done at each "graph ..." statement */ ! 181: { ! 182: Obj *p, *np, *deflist; ! 183: ! 184: curr_coord = dflt_coord; ! 185: ncoord = auto_x = 0; ! 186: autoticks = LEFT|BOT; ! 187: margin = MARGIN; ! 188: deflist = NULL; ! 189: for (p = objlist; p; p = np) { ! 190: np = p->next; ! 191: if (p->type == DEFNAME || p->type == VARNAME) { ! 192: p->next = deflist; ! 193: deflist = p; ! 194: } else { ! 195: free(p->name); ! 196: freeattr(p->attr); ! 197: free(p); ! 198: } ! 199: } ! 200: objlist = deflist; ! 201: if (tfd != stdout && (tfd = fopen(tempfile, "w")) == NULL) { ! 202: fprintf(stderr, "grap: can't open %s\n", tempfile); ! 203: exit(1); ! 204: } ! 205: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.