version 1.72, 2009/10/28 16:38:18 |
version 1.73, 2009/11/20 03:24:07 |
|
|
percent_expand(const char *string, ...) |
percent_expand(const char *string, ...) |
{ |
{ |
#define EXPAND_MAX_KEYS 16 |
#define EXPAND_MAX_KEYS 16 |
|
u_int num_keys, i, j; |
struct { |
struct { |
const char *key; |
const char *key; |
const char *repl; |
const char *repl; |
} keys[EXPAND_MAX_KEYS]; |
} keys[EXPAND_MAX_KEYS]; |
u_int num_keys, i, j; |
|
char buf[4096]; |
char buf[4096]; |
va_list ap; |
va_list ap; |
|
|
|
|
break; |
break; |
keys[num_keys].repl = va_arg(ap, char *); |
keys[num_keys].repl = va_arg(ap, char *); |
if (keys[num_keys].repl == NULL) |
if (keys[num_keys].repl == NULL) |
fatal("percent_expand: NULL replacement"); |
fatal("%s: NULL replacement", __func__); |
} |
} |
|
if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL) |
|
fatal("%s: too many keys", __func__); |
va_end(ap); |
va_end(ap); |
|
|
if (num_keys >= EXPAND_MAX_KEYS) |
|
fatal("percent_expand: too many keys"); |
|
|
|
/* Expand string */ |
/* Expand string */ |
*buf = '\0'; |
*buf = '\0'; |
for (i = 0; *string != '\0'; string++) { |
for (i = 0; *string != '\0'; string++) { |
|
|
append: |
append: |
buf[i++] = *string; |
buf[i++] = *string; |
if (i >= sizeof(buf)) |
if (i >= sizeof(buf)) |
fatal("percent_expand: string too long"); |
fatal("%s: string too long", __func__); |
buf[i] = '\0'; |
buf[i] = '\0'; |
continue; |
continue; |
} |
} |
string++; |
string++; |
|
/* %% case */ |
if (*string == '%') |
if (*string == '%') |
goto append; |
goto append; |
for (j = 0; j < num_keys; j++) { |
for (j = 0; j < num_keys; j++) { |
if (strchr(keys[j].key, *string) != NULL) { |
if (strchr(keys[j].key, *string) != NULL) { |
i = strlcat(buf, keys[j].repl, sizeof(buf)); |
i = strlcat(buf, keys[j].repl, sizeof(buf)); |
if (i >= sizeof(buf)) |
if (i >= sizeof(buf)) |
fatal("percent_expand: string too long"); |
fatal("%s: string too long", __func__); |
break; |
break; |
} |
} |
} |
} |
if (j >= num_keys) |
if (j >= num_keys) |
fatal("percent_expand: unknown key %%%c", *string); |
fatal("%s: unknown key %%%c", __func__, *string); |
} |
} |
return (xstrdup(buf)); |
return (xstrdup(buf)); |
#undef EXPAND_MAX_KEYS |
#undef EXPAND_MAX_KEYS |