4 * Copyright (C) 2002-2004 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Id: var.c,v 1.4.2.3 2006/06/16 17:21:18 darrenr Exp
15 typedef struct variable
{
16 struct variable
*v_next
;
21 static variable_t
*vtop
= NULL
;
23 static variable_t
*find_var
__P((char *));
24 static char *expand_string
__P((char *, int));
27 static variable_t
*find_var(name
)
32 for (v
= vtop
; v
!= NULL
; v
= v
->v_next
)
33 if (!strcmp(name
, v
->v_name
))
39 char *get_variable(string
, after
, line
)
40 char *string
, **after
;
43 char c
, *s
, *t
, *value
;
50 for (t
= s
; *t
!= '\0'; t
++)
54 fprintf(stderr
, "%d: { without }\n", line
);
57 } else if (ISALPHA(*s
)) {
58 for (t
= s
+ 1; *t
!= '\0'; t
++)
59 if (!ISALPHA(*t
) && !ISDIGIT(*t
) && (*t
!= '_'))
62 fprintf(stderr
, "%d: variables cannot start with '%c'\n",
74 fprintf(stderr
, "%d: unknown variable '%s'\n", line
, s
);
78 s
= strdup(v
->v_value
);
79 value
= expand_string(s
, line
);
86 static char *expand_string(oldstring
, line
)
90 char c
, *s
, *p1
, *p2
, *p3
, *newstring
, *value
;
94 newstring
= oldstring
;
96 for (s
= oldstring
; *s
!= '\0'; s
++)
104 bcopy(s
, s
- 1, strlen(s
));
111 value
= get_variable(s
, &p3
, line
);
115 p2
= expand_string(value
, line
);
119 len
= strlen(newstring
) + strlen(p2
);
121 if (c
== '{' && *p3
== '}')
125 p1
= malloc(len
+ 1);
130 strcpy(p1
, newstring
);
135 s
= p1
+ len
- strlen(p3
) - 1;
136 if (newstring
!= oldstring
)
146 void set_variable(name
, value
)
153 if (name
== NULL
|| value
== NULL
|| *name
== '\0')
159 v
->v_value
= strdup(value
);
165 if ((*value
== '"' && value
[len
- 1] == '"') ||
166 (*value
== '\'' && value
[len
- 1] == '\'')) {
167 value
[len
- 1] = '\0';
172 v
= (variable_t
*)malloc(sizeof(*v
));
175 v
->v_name
= strdup(name
);
176 v
->v_value
= strdup(value
);