Blob


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