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