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