devutf: add unicode aliases for accented characters
[troff.git] / pic / symtab.c
blob879075c6854b3083ccc2f4f09e530bbc0151a8c5
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include "pic.h"
6 #include "y.tab.h"
8 YYSTYPE getvar(char *s) /* return value of variable s (usually pointer) */
10 struct symtab *p;
11 static YYSTYPE bug;
13 p = lookup(s);
14 if (p == NULL) {
15 if (islower(s[0]))
16 ERROR "no such variable as %s", s WARNING;
17 else
18 ERROR "no such place as %s", s WARNING;
19 return(bug);
21 return(p->s_val);
24 double getfval(char *s) /* return float value of variable s */
26 YYSTYPE y;
28 y = getvar(s);
29 return y.f;
32 void setfval(char *s, double f) /* set variable s to f */
34 struct symtab *p;
36 if ((p = lookup(s)) != NULL)
37 p->s_val.f = f;
40 struct symtab *makevar(char *s, int t, YYSTYPE v) /* make variable named s in table */
41 /* assumes s is static or from tostring */
43 struct symtab *p;
45 for (p = stack[nstack].p_symtab; p != NULL; p = p->s_next)
46 if (strcmp(s, p->s_name) == 0)
47 break;
48 if (p == NULL) { /* it's a new one */
49 p = (struct symtab *) malloc(sizeof(struct symtab));
50 if (p == NULL)
51 ERROR "out of symtab space with %s", s FATAL;
52 p->s_next = stack[nstack].p_symtab;
53 stack[nstack].p_symtab = p; /* stick it at front */
55 p->s_name = s;
56 p->s_type = t;
57 p->s_val = v;
58 return(p);
61 struct symtab *lookup(char *s) /* find s in symtab */
63 int i;
64 struct symtab *p;
66 for (i = nstack; i >= 0; i--) /* look in each active symtab */
67 for (p = stack[i].p_symtab; p != NULL; p = p->s_next)
68 if (strcmp(s, p->s_name) == 0)
69 return(p);
70 return(NULL);
73 void freesymtab(struct symtab *p) /* free space used by symtab at p */
75 struct symtab *q;
77 for ( ; p != NULL; p = q) {
78 q = p->s_next;
79 free(p->s_name); /* assumes done with tostring */
80 free((char *)p);
84 void freedef(char *s) /* free definition for string s */
86 struct symtab *p, *q, *op;
88 for (p = op = q = stack[nstack].p_symtab; p != NULL; p = p->s_next) {
89 if (strcmp(s, p->s_name) == 0) { /* got it */
90 if (p->s_type != DEFNAME)
91 break;
92 if (p == op) /* 1st elem */
93 stack[nstack].p_symtab = p->s_next;
94 else
95 q->s_next = p->s_next;
96 free(p->s_name);
97 free(p->s_val.p);
98 free((char *)p);
99 return;
101 q = p;
103 /* ERROR "%s is not defined at this point", s WARNING; */