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