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