misc.c 5.0 KB

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