1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/* $NetBSD$ */
#include <ctype.h>
#include "ipf.h"
typedef struct variable {
struct variable *v_next;
char *v_name;
char *v_value;
} variable_t;
static variable_t *vtop = NULL;
static variable_t *find_var __P((char *));
static char *expand_string __P((char *, int));
static variable_t *find_var(name)
char *name;
{
variable_t *v;
for (v = vtop; v != NULL; v = v->v_next)
if (!strcmp(name, v->v_name))
return v;
return NULL;
}
char *get_variable(string, after, line)
char *string, **after;
int line;
{
char c, *s, *t, *value;
variable_t *v;
s = string;
if (*s == '{') {
s++;
for (t = s; *t != '\0'; t++)
if (*t == '}')
break;
if (*t == '\0') {
fprintf(stderr, "%d: { without }\n", line);
return NULL;
}
} else if (ISALPHA(*s)) {
for (t = s + 1; *t != '\0'; t++)
if (!ISALPHA(*t) && !ISDIGIT(*t) && (*t != '_'))
break;
} else {
fprintf(stderr, "%d: variables cannot start with '%c'\n",
line, *s);
return NULL;
}
if (after != NULL)
*after = t;
c = *t;
*t = '\0';
v = find_var(s);
*t = c;
if (v == NULL) {
fprintf(stderr, "%d: unknown variable '%s'\n", line, s);
return NULL;
}
s = strdup(v->v_value);
value = expand_string(s, line);
if (value != s)
free(s);
return value;
}
static char *expand_string(oldstring, line)
char *oldstring;
int line;
{
char c, *s, *p1, *p2, *p3, *newstring, *value;
int len;
p3 = NULL;
newstring = oldstring;
for (s = oldstring; *s != '\0'; s++)
if (*s == '$') {
*s = '\0';
s++;
switch (*s)
{
case '$' :
bcopy(s, s - 1, strlen(s));
break;
default :
c = *s;
if (c == '\0')
return newstring;
value = get_variable(s, &p3, line);
if (value == NULL)
return NULL;
p2 = expand_string(value, line);
if (p2 == NULL)
return NULL;
len = strlen(newstring) + strlen(p2);
if (p3 != NULL) {
if (c == '{' && *p3 == '}')
p3++;
len += strlen(p3);
}
p1 = malloc(len + 1);
if (p1 == NULL)
return NULL;
*(s - 1) = '\0';
strcpy(p1, newstring);
strcat(p1, p2);
if (p3 != NULL)
strcat(p1, p3);
s = p1 + len - strlen(p3) - 1;
if (newstring != oldstring)
free(newstring);
newstring = p1;
break;
}
}
return newstring;
}
void set_variable(name, value)
char *name;
char *value;
{
variable_t *v;
int len;
if (name == NULL || value == NULL || *name == '\0')
return;
v = find_var(name);
if (v != NULL) {
free(v->v_value);
v->v_value = strdup(value);
return;
}
len = strlen(value);
if ((*value == '"' && value[len - 1] == '"') ||
(*value == '\'' && value[len - 1] == '\'')) {
value[len - 1] = '\0';
value++;
len -=2;
}
v = (variable_t *)malloc(sizeof(*v));
if (v == NULL)
return;
v->v_name = strdup(name);
v->v_value = strdup(value);
v->v_next = vtop;
vtop = v;
}
|