|
|
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 != NULL) { ! 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 : frame_wid/2; ! 79: yfac = dy > 0 ? frame_ht/dy : frame_ht/2; ! 80: ! 81: fprintf(tfd, "define xy_%s @ ", p->name); ! 82: if (dx > 0) ! 83: fprintf(tfd, "\t(($1)-(%g))*%g", pt.x, xfac); ! 84: else ! 85: fprintf(tfd, "\t%g", xfac); ! 86: if (dy > 0) ! 87: fprintf(tfd, ", (($2)-(%g))*%g @\n", pt.y, yfac); ! 88: else ! 89: fprintf(tfd, ", %g @\n", yfac); ! 90: fprintf(tfd, "define x_%s @ ", p->name); ! 91: if (dx > 0) ! 92: fprintf(tfd, "\t(($1)-(%g))*%g @\n", pt.x, xfac); ! 93: else ! 94: fprintf(tfd, "\t%g @\n", xfac); ! 95: fprintf(tfd, "define y_%s @ ", p->name); ! 96: if (dy > 0) ! 97: fprintf(tfd, "\t(($1)-(%g))*%g @\n", pt.y, yfac); ! 98: else ! 99: fprintf(tfd, "\t%g @\n", yfac); ! 100: } ! 101: } ! 102: if (codegen) ! 103: frame(); ! 104: if (codegen && autoticks && dfp) ! 105: do_autoticks(dfp); ! 106: ! 107: if ((fd = fopen(tempfile, "r")) != NULL) { ! 108: while ((c = getc(fd)) != EOF) ! 109: putc(c, tfd); ! 110: fclose(fd); ! 111: } ! 112: tfd = NULL; ! 113: } ! 114: ! 115: endstat() /* clean up after each statement */ ! 116: { ! 117: extern int just, sizeop, tick_dir; ! 118: extern double sizexpr, lab_up, lab_rt; ! 119: ! 120: just = sizeop = 0; ! 121: lab_up = lab_rt = 0.0; ! 122: sizexpr = 0.0; ! 123: nnum = 0; ! 124: ntick = 0; ! 125: tside = 0; ! 126: tick_dir = OUT; ! 127: ticklen = TICKLEN; ! 128: } ! 129: ! 130: graph(s) /* graph statement */ ! 131: char *s; ! 132: { ! 133: char *p, *os; ! 134: int c; ! 135: ! 136: if (codegen) { ! 137: fprintf(stdout, "%s: [\n", graphname); ! 138: print(); /* pump out previous graph */ ! 139: fprintf(stdout, "\n] %s\n", graphpos); ! 140: reset(); ! 141: } ! 142: if (s) { ! 143: dprintf("into graph with <%s>\n", s); ! 144: opentemp(); ! 145: os = s; ! 146: while ((c = *s) == ' ' || c == '\t') ! 147: s++; ! 148: if (c == '\0') ! 149: yyerror("no name on graph statement"); ! 150: if (!isupper(s[0])) ! 151: yyerror("graph name %s must be capitalized", s); ! 152: for (p=graphname; (c = *s) != ' ' && c != '\t' && c != '\0'; ) ! 153: *p++ = *s++; ! 154: *p = '\0'; ! 155: strcpy(graphpos, s); ! 156: dprintf("graphname = <%s>, graphpos = <%s>\n", graphname, graphpos); ! 157: free(os); ! 158: } ! 159: } ! 160: ! 161: setup() /* done at each .G1 */ ! 162: { ! 163: static int firstG1 = 0; ! 164: ! 165: reset(); ! 166: opentemp(); ! 167: frame_ht = frame_wid = -1; /* reset in frame() */ ! 168: ticklen = getvar(lookup("ticklen", 0)); ! 169: if (firstG1++ == 0) ! 170: do_first(); ! 171: codegen = synerr = 0; ! 172: strcpy(graphname, "Graph"); ! 173: strcpy(graphpos, ""); ! 174: } ! 175: ! 176: do_first() /* done at first .G1: definitions, etc. */ ! 177: { ! 178: extern int lib; ! 179: extern char *lib_defines; ! 180: static char buf[50], buf1[50]; /* static because pbstr uses them */ ! 181: ! 182: sprintf(buf, "define pid /%d/\n", getpid()); ! 183: pbstr(buf); ! 184: if (lib != 0) { ! 185: if (access(lib_defines, 4) == 0) { ! 186: sprintf(buf1, "copy \"%s\"\n", lib_defines); ! 187: pbstr(buf1); ! 188: } else { ! 189: fprintf(stderr, "grap warning: can't open %s\n", lib_defines); ! 190: } ! 191: } ! 192: } ! 193: ! 194: reset() /* done at each "graph ..." statement */ ! 195: { ! 196: Obj *p, *np, *deflist; ! 197: extern int tlist, toffside, autodir; ! 198: ! 199: curr_coord = dflt_coord; ! 200: ncoord = auto_x = 0; ! 201: autoticks = LEFT|BOT; ! 202: autodir = 0; ! 203: tside = tlist = toffside = 0; ! 204: tick_dir = OUT; ! 205: margin = MARGIN; ! 206: deflist = NULL; ! 207: for (p = objlist; p; p = np) { ! 208: np = p->next; ! 209: if (p->type == DEFNAME || p->type == VARNAME) { ! 210: p->next = deflist; ! 211: deflist = p; ! 212: } else { ! 213: free(p->name); ! 214: freeattr(p->attr); ! 215: free(p); ! 216: } ! 217: } ! 218: objlist = deflist; ! 219: } ! 220: ! 221: opentemp() ! 222: { ! 223: if (tfd != NULL) ! 224: fclose(tfd); ! 225: if ((tfd = fopen(tempfile, "w")) == NULL) { ! 226: fprintf(stderr, "grap: can't open %s\n", tempfile); ! 227: exit(1); ! 228: } ! 229: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.