]> mj.ucw.cz Git - libucw.git/blobdiff - lib/conf-input.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / lib / conf-input.c
index 260d9f2a50cdf80d7a9d797d682a5dda78b27063..b890295d99742025c5fecbe69c89544770d6c86d 100644 (file)
 
 /* 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;
@@ -43,7 +43,7 @@ static uns words;
 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++;
@@ -58,7 +58,7 @@ get_line(byte **msg)
 }
 
 static void
-append(byte *start, byte *end)
+append(char *start, char *end)
 {
   uns len = end - start;
   bb_grow(&copy_buf, copied + len + 1);
@@ -67,14 +67,14 @@ append(byte *start, byte *end)
   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);
@@ -82,7 +82,7 @@ get_word(uns is_command_name)
        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++;
 
@@ -90,7 +90,7 @@ get_word(uns is_command_name)
     line++;
     uns start_copy = copied;
     while (1) {
-      byte *start = line;
+      char *start = line;
       uns escape = 0;
       while (*line) {
        if (*line == '"' && !escape)
@@ -109,11 +109,11 @@ get_word(uns is_command_name)
       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(&copy_buf, start_copy + l + 1);
     strcpy(copy_buf.ptr + start_copy, tmp);
@@ -121,7 +121,7 @@ get_word(uns is_command_name)
 
   } 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))
@@ -140,8 +140,8 @@ get_word(uns 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) {
@@ -159,7 +159,7 @@ get_token(uns is_command_name, byte **err)
        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;
@@ -170,11 +170,11 @@ get_token(uns is_command_name, byte **err)
   }
 }
 
-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
@@ -194,10 +194,10 @@ split_command(void)
 
 /* 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;
@@ -210,8 +210,8 @@ parse_fastbuf(const byte *name_fb, struct fastbuf *fb, uns depth)
       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"))
@@ -239,7 +239,7 @@ parse_fastbuf(const byte *name_fb, struct fastbuf *fb, uns depth)
       continue;
     }
     enum cf_operation op;
-    byte *c = strchr(name, ':');
+    char *c = strchr(name, ':');
     if (!c)
       op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
     else {
@@ -282,7 +282,12 @@ error:
 #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
@@ -300,7 +305,7 @@ done_stack(void)
 }
 
 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);
@@ -308,7 +313,7 @@ load_file(const byte *file)
     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)
@@ -317,19 +322,19 @@ load_file(const byte *file)
 }
 
 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);
@@ -351,7 +356,7 @@ cf_reload(const byte *file)
 }
 
 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);
@@ -363,7 +368,7 @@ cf_load(const byte *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);
@@ -380,8 +385,16 @@ static void
 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