print.c 5.5 KB

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