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