/* Text file parser */
-static const byte *name_parse_fb;
+static const char *name_parse_fb;
static struct fastbuf *parse_fb;
static uns line_num;
#define MAX_LINE 4096
-static byte line_buf[MAX_LINE];
-static byte *line = line_buf;
+static char line_buf[MAX_LINE];
+static char *line = line_buf;
#include "lib/bbuf.h"
static bb_t copy_buf;
static uns ends_by_brace; // the line is ended by "{"
static int
-get_line(byte **msg)
+get_line(char **msg)
{
int err = bgets_nodie(parse_fb, line_buf, MAX_LINE);
line_num++;
}
static void
-append(byte *start, byte *end)
+append(char *start, char *end)
{
uns len = end - start;
bb_grow(©_buf, copied + len + 1);
copy_buf.ptr[copied-1] = 0;
}
-static byte *
+static char *
get_word(uns is_command_name)
{
- byte *msg;
+ char *msg;
if (*line == '\'') {
line++;
while (1) {
- byte *start = line;
+ char *start = line;
while (*line && *line != '\'')
line++;
append(start, line);
break;
copy_buf.ptr[copied-1] = '\n';
if (!get_line(&msg))
- return msg ? : (byte*) "Unterminated apostrophe word at the end";
+ return msg ? : "Unterminated apostrophe word at the end";
}
line++;
line++;
uns start_copy = copied;
while (1) {
- byte *start = line;
+ char *start = line;
uns escape = 0;
while (*line) {
if (*line == '"' && !escape)
else // merge two lines
copied -= 2;
if (!get_line(&msg))
- return msg ? : (byte*) "Unterminated quoted word at the end";
+ return msg ? : "Unterminated quoted word at the end";
}
line++;
- byte *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
+ char *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
uns l = strlen(tmp);
bb_grow(©_buf, start_copy + l + 1);
strcpy(copy_buf.ptr + start_copy, tmp);
} else {
// promised that *line is non-null and non-blank
- byte *start = line;
+ char *start = line;
while (*line && !Cblank(*line)
&& *line != '{' && *line != '}' && *line != ';'
&& (*line != '=' || !is_command_name))
return NULL;
}
-static byte *
-get_token(uns is_command_name, byte **err)
+static char *
+get_token(uns is_command_name, char **err)
{
*err = NULL;
while (1) {
return NULL;
}
if (!*line || *line == '#')
- msg(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb ? : (byte*) "", line_num);
+ msg(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb ? : "", line_num);
} else {
split_grow(&word_buf, words+1);
uns start = copied;
}
}
-static byte *
+static char *
split_command(void)
{
words = copied = ends_by_brace = 0;
- byte *msg, *start_word;
+ char *msg, *start_word;
if (!(start_word = get_token(1, &msg)))
return msg;
if (*start_word == '{') // only one opening brace
/* Parsing multiple files */
-static byte *
-parse_fastbuf(const byte *name_fb, struct fastbuf *fb, uns depth)
+static char *
+parse_fastbuf(const char *name_fb, struct fastbuf *fb, uns depth)
{
- byte *err;
+ char *err;
name_parse_fb = name_fb;
parse_fb = fb;
line_num = 0;
goto error;
if (!words)
return NULL;
- byte *name = copy_buf.ptr + word_buf.ptr[0];
- byte *pars[words-1];
+ char *name = copy_buf.ptr + word_buf.ptr[0];
+ char *pars[words-1];
for (uns i=1; i<words; i++)
pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
if (!strcasecmp(name, "include"))
continue;
}
enum cf_operation op;
- byte *c = strchr(name, ':');
+ char *c = strchr(name, ':');
if (!c)
op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
else {
#ifndef DEFAULT_CONFIG
#define DEFAULT_CONFIG NULL
#endif
-byte *cf_def_file = DEFAULT_CONFIG;
+char *cf_def_file = DEFAULT_CONFIG;
+
+#ifndef ENV_VAR_CONFIG
+#define ENV_VAR_CONFIG NULL
+#endif
+char *cf_env_file = ENV_VAR_CONFIG;
static uns postpone_commit; // only for cf_getopt()
static uns everything_committed; // after the 1st load, this flag is set on
}
static int
-load_file(const byte *file)
+load_file(const char *file)
{
cf_init_stack();
struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
msg(L_ERROR, "Cannot open %s: %m", file);
return 1;
}
- byte *err_msg = parse_fastbuf(file, fb, 0);
+ char *err_msg = parse_fastbuf(file, fb, 0);
bclose(fb);
int err = !!err_msg || done_stack();
if (!err)
}
static int
-load_string(const byte *string)
+load_string(const char *string)
{
cf_init_stack();
struct fastbuf fb;
fbbuf_init_read(&fb, (byte *)string, strlen(string), 0);
- byte *msg = parse_fastbuf(NULL, &fb, 0);
+ char *msg = parse_fastbuf(NULL, &fb, 0);
return !!msg || done_stack();
}
/* Safe loading and reloading */
int
-cf_reload(const byte *file)
+cf_reload(const char *file)
{
cf_journal_swap();
struct cf_journal_item *oldj = cf_journal_new_transaction(1);
}
int
-cf_load(const byte *file)
+cf_load(const char *file)
{
struct cf_journal_item *oldj = cf_journal_new_transaction(1);
int err = load_file(file);
}
int
-cf_set(const byte *string)
+cf_set(const char *string)
{
struct cf_journal_item *oldj = cf_journal_new_transaction(0);
int err = load_string(string);
load_default(void)
{
if (cf_def_file)
- if (cf_load(cf_def_file))
- die("Cannot load default config %s", cf_def_file);
+ {
+ char *env;
+ if (cf_env_file && (env = getenv(cf_env_file)))
+ {
+ if (cf_load(env))
+ die("Cannot load config file %s", env);
+ }
+ else if (cf_load(cf_def_file))
+ die("Cannot load default config %s", cf_def_file);
+ }
}
static void