7 typedef struct variable {
8 struct variable *v_next;
13 static variable_t *vtop = NULL;
15 static variable_t *find_var __P((char *));
16 static char *expand_string __P((char *, int));
19 static variable_t *find_var(name)
24 for (v = vtop; v != NULL; v = v->v_next)
25 if (!strcmp(name, v->v_name))
31 char *get_variable(string, after, line)
32 char *string, **after;
35 char c, *s, *t, *value;
42 for (t = s; *t != '\0'; t++)
46 fprintf(stderr, "%d: { without }\n", line);
49 } else if (ISALPHA(*s)) {
50 for (t = s + 1; *t != '\0'; t++)
51 if (!ISALPHA(*t) && !ISDIGIT(*t) && (*t != '_'))
54 fprintf(stderr, "%d: variables cannot start with '%c'\n",
66 fprintf(stderr, "%d: unknown variable '%s'\n", line, s);
70 s = strdup(v->v_value);
71 value = expand_string(s, line);
78 static char *expand_string(oldstring, line)
82 char c, *s, *p1, *p2, *p3, *newstring, *value;
86 newstring = oldstring;
88 for (s = oldstring; *s != '\0'; s++)
96 bcopy(s, s - 1, strlen(s));
103 value = get_variable(s, &p3, line);
107 p2 = expand_string(value, line);
111 len = strlen(newstring) + strlen(p2);
113 if (c == '{' && *p3 == '}')
117 p1 = malloc(len + 1);
122 strcpy(p1, newstring);
127 s = p1 + len - strlen(p3) - 1;
128 if (newstring != oldstring)
138 void set_variable(name, value)
145 if (name == NULL || value == NULL || *name == '\0')
151 v->v_value = strdup(value);
157 if ((*value == '"' && value[len - 1] == '"') ||
158 (*value == '\'' && value[len - 1] == '\'')) {
159 value[len - 1] = '\0';
164 v = (variable_t *)malloc(sizeof(*v));
167 v->v_name = strdup(name);
168 v->v_value = strdup(value);