misc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <string.h>
  11. #include <stdlib.h>
  12. #include "grap.h"
  13. #include "y.tab.h"
  14. int nnum = 0; /* number of saved numbers */
  15. double num[MAXNUM];
  16. int just; /* current justification mode (RJUST, etc.) */
  17. int sizeop; /* current optional operator for size change */
  18. double sizexpr; /* current size change expression */
  19. void savenum(int n, double f) /* save f in num[n] */
  20. {
  21. num[n] = f;
  22. nnum = n+1;
  23. if (nnum >= MAXNUM)
  24. ERROR "too many numbers" WARNING;
  25. }
  26. void setjust(int j)
  27. {
  28. just |= j;
  29. }
  30. void setsize(int op, double expr)
  31. {
  32. sizeop = op;
  33. sizexpr = expr;
  34. }
  35. char *tostring(char *s)
  36. {
  37. register char *p;
  38. p = malloc(strlen(s)+1);
  39. if (p == NULL)
  40. ERROR "out of space in tostring on %s", s FATAL;
  41. strcpy(p, s);
  42. return(p);
  43. }
  44. void range(Point pt) /* update the range for point pt */
  45. {
  46. Obj *p = pt.obj;
  47. if (!(p->coord & XFLAG)) {
  48. if (pt.x > p->pt1.x)
  49. p->pt1.x = pt.x;
  50. if (pt.x < p->pt.x)
  51. p->pt.x = pt.x;
  52. }
  53. if (!(p->coord & YFLAG)) {
  54. if (pt.y > p->pt1.y)
  55. p->pt1.y = pt.y;
  56. if (pt.y < p->pt.y)
  57. p->pt.y = pt.y;
  58. }
  59. }
  60. void halfrange(Obj *p, int side, double val) /* record max and min for one direction */
  61. {
  62. if (!(p->coord&XFLAG) && (side == LEFT || side == RIGHT)) {
  63. if (val < p->pt.y)
  64. p->pt.y = val;
  65. if (val > p->pt1.y)
  66. p->pt1.y = val;
  67. } else if (!(p->coord&YFLAG) && (side == TOP || side == BOT)) {
  68. if (val < p->pt.x)
  69. p->pt.x = val;
  70. if (val > p->pt1.x)
  71. p->pt1.x = val;
  72. }
  73. }
  74. Obj *lookup(char *s, int inst) /* find s in objlist, install if inst */
  75. {
  76. Obj *p;
  77. int found = 0;
  78. for (p = objlist; p; p = p->next){
  79. if (strcmp(s, p->name) == 0) {
  80. found = 1;
  81. break;
  82. }
  83. }
  84. if (p == NULL && inst != 0) {
  85. p = (Obj *) calloc(1, sizeof(Obj));
  86. if (p == NULL)
  87. ERROR "out of space in lookup" FATAL;
  88. p->name = tostring(s);
  89. p->type = NAME;
  90. p->pt = ptmax;
  91. p->pt1 = ptmin;
  92. p->fval = 0.0;
  93. p->next = objlist;
  94. objlist = p;
  95. }
  96. dprintf("lookup(%s,%d) = %d\n", s, inst, found);
  97. return p;
  98. }
  99. double getvar(Obj *p) /* return value of variable */
  100. {
  101. return p->fval;
  102. }
  103. double setvar(Obj *p, double f) /* set value of variable to f */
  104. {
  105. if (strcmp(p->name, "pointsize") == 0) { /* kludge */
  106. pointsize = f;
  107. ps_set = 1;
  108. }
  109. p->type = VARNAME;
  110. return p->fval = f;
  111. }
  112. Point makepoint(Obj *s, double x, double y) /* make a Point */
  113. {
  114. Point p;
  115. dprintf("makepoint: %s, %g,%g\n", s->name, x, y);
  116. p.obj = s;
  117. p.x = x;
  118. p.y = y;
  119. return p;
  120. }
  121. Attr *makefattr(int type, double fval) /* set double in attribute */
  122. {
  123. return makeattr(type, fval, (char *) 0, 0, 0);
  124. }
  125. Attr *makesattr(char *s) /* make an Attr cell containing s */
  126. {
  127. Attr *ap = makeattr(STRING, sizexpr, s, just, sizeop);
  128. just = sizeop = 0;
  129. sizexpr = 0.0;
  130. return ap;
  131. }
  132. Attr *makeattr(int type, double fval, char *sval, int just, int op)
  133. {
  134. Attr *a;
  135. a = (Attr *) malloc(sizeof(Attr));
  136. if (a == NULL)
  137. ERROR "out of space in makeattr" FATAL;
  138. a->type = type;
  139. a->fval = fval;
  140. a->sval = sval;
  141. a->just = just;
  142. a->op = op;
  143. a->next = NULL;
  144. return a;
  145. }
  146. Attr *addattr(Attr *a1, Attr *ap) /* add attr ap to end of list a1 */
  147. {
  148. Attr *p;
  149. if (a1 == 0)
  150. return ap;
  151. if (ap == 0)
  152. return a1;
  153. for (p = a1; p->next; p = p->next)
  154. ;
  155. p->next = ap;
  156. return a1;
  157. }
  158. void freeattr(Attr *ap) /* free an attribute list */
  159. {
  160. Attr *p;
  161. while (ap) {
  162. p = ap->next; /* save next */
  163. if (ap->sval)
  164. free(ap->sval);
  165. free((char *) ap);
  166. ap = p;
  167. }
  168. }
  169. char *slprint(Attr *stringlist) /* print strings from stringlist */
  170. {
  171. int ntext, n, last_op, last_just;
  172. double last_fval;
  173. static char buf[1000];
  174. Attr *ap;
  175. buf[0] = '\0';
  176. last_op = last_just = 0;
  177. last_fval = 0.0;
  178. for (ntext = 0, ap = stringlist; ap != NULL; ap = ap->next)
  179. ntext++;
  180. sprintf(buf, "box invis wid 0 ht %d*textht", ntext);
  181. n = strlen(buf);
  182. for (ap = stringlist; ap != NULL; ap = ap->next) {
  183. if (ap->op == 0) { /* propagate last value */
  184. ap->op = last_op;
  185. ap->fval = last_fval;
  186. } else {
  187. last_op = ap->op;
  188. last_fval = ap->fval;
  189. }
  190. sprintf(buf+n, " \"%s\"", ps_set || ap->op ? sizeit(ap) : ap->sval);
  191. if (ap->just)
  192. last_just = ap->just;
  193. if (last_just)
  194. strcat(buf+n, juststr(last_just));
  195. n = strlen(buf);
  196. }
  197. return buf; /* watch it: static */
  198. }
  199. char *juststr(int j) /* convert RJUST, etc., into string */
  200. {
  201. static char buf[50];
  202. buf[0] = '\0';
  203. if (j & RJUST)
  204. strcat(buf, " rjust");
  205. if (j & LJUST)
  206. strcat(buf, " ljust");
  207. if (j & ABOVE)
  208. strcat(buf, " above");
  209. if (j & BELOW)
  210. strcat(buf, " below");
  211. return buf; /* watch it: static */
  212. }
  213. char *sprntf(char *s, Attr *ap) /* sprintf(s, attrlist ap) */
  214. {
  215. char buf[500];
  216. int n;
  217. Attr *p;
  218. for (n = 0, p = ap; p; p = p->next)
  219. n++;
  220. switch (n) {
  221. case 0:
  222. return s;
  223. case 1:
  224. sprintf(buf, s, ap->fval);
  225. break;
  226. case 2:
  227. sprintf(buf, s, ap->fval, ap->next->fval);
  228. break;
  229. case 3:
  230. sprintf(buf, s, ap->fval, ap->next->fval, ap->next->next->fval);
  231. break;
  232. case 5:
  233. ERROR "too many expressions in sprintf" WARNING;
  234. case 4:
  235. sprintf(buf, s, ap->fval, ap->next->fval, ap->next->next->fval, ap->next->next->next->fval);
  236. break;
  237. }
  238. free(s);
  239. return tostring(buf);
  240. }