--- var_test.c 2001/11/14 15:55:09 1.11
+++ var_test.c 2001/11/20 11:41:54 1.12
@@ -3,27 +3,28 @@
#include <errno.h>
#include "var.h"
+struct variable {
+ const char *name;
+ const char *data;
+};
+
static int env_lookup(void *context,
const char *varname, size_t name_len,
const char **data, size_t *data_len,
size_t *buffer_size)
{
- char tmp[256];
-
- if (name_len > sizeof(tmp) - 1) {
- /* Callback can't expand variable names longer than
- sizeof(tmp) characters. */
+ const struct variable* vars = context;
+ size_t i;
- return VAR_ERR_CALLBACK;
+ for (i = 0; vars[i].name; ++i) {
+ if (strncmp(varname, vars[i].name, name_len) == 0) {
+ *data = vars[i].data;
+ *data_len = strlen(*data);
+ *buffer_size = 0;
+ return 1;
+ }
}
- memcpy(tmp, varname, name_len);
- tmp[name_len] = '\0';
- *data = getenv(tmp);
- if (*data == NULL)
- return 0;
- *data_len = strlen(*data);
- *buffer_size = 0;
- return 1;
+ return 0;
}
struct test_case {
@@ -33,6 +34,16 @@
int main(int argc, char **argv)
{
+ const struct variable vars[] = {
+ { "HOME", "/home/regression-tests" },
+ { "OSTYPE", "regression-os" },
+ { "TERM", "regression-term" },
+ { "FOO", "os" },
+ { "BAR", "type" },
+ { "EMPTY", "" },
+ { NULL, NULL }
+ };
+
const struct test_case tests[] = {
{"$HOME", "/home/regression-tests"},
{"${FOO}", "os"},
@@ -94,16 +105,6 @@
size_t i;
char buffer[1024];
- if (setenv("HOME", "/home/regression-tests", 1) != 0 ||
- setenv("OSTYPE", "regression-os", 1) != 0 ||
- setenv("TERM", "regression-term", 1) != 0 ||
- setenv("FOO", "os", 1) != 0 ||
- setenv("BAR", "type", 1) != 0 || setenv("EMPTY", "", 1) != 0) {
- printf("Failed to set the environment: %s.\n", strerror(errno));
- return 1;
- }
- unsetenv("UNDEFINED");
-
for (i = 0; i < sizeof(tests) / sizeof(struct test_case); ++i) {
#ifdef DEBUG
printf("Test case #%02d: Original input is '%s'.\n", i,
@@ -119,7 +120,7 @@
printf("Test case #%02d: Unescaped input is '%s'.\n", i, buffer);
#endif
rc = var_expand(buffer, strlen(buffer), &tmp, &tmp_len,
- &env_lookup, NULL, NULL, 0);
+ &env_lookup, (void *)vars, NULL, 0);
if (rc != VAR_OK) {
printf ("Test case #%d: var_expand() \
failed with return code %d.\n", i, rc);
|