]> mj.ucw.cz Git - libucw.git/blobdiff - lib/conf-input.c
Cleaned up the fastbuf interfaces.
[libucw.git] / lib / conf-input.c
index 786725bda7b455fb613924dda12a08473049d4a7..0272de21eb27722c68a1704cdb862d80640136c3 100644 (file)
@@ -43,11 +43,14 @@ static uns words;
 static uns ends_by_brace;              // the line is ended by "{"
 
 static int
 static uns ends_by_brace;              // the line is ended by "{"
 
 static int
-get_line(void)
+get_line(byte **msg)
 {
 {
-  if (!bgets(parse_fb, line_buf, MAX_LINE))
-    return 0;
+  int err = bgets_nodie(parse_fb, line_buf, MAX_LINE);
   line_num++;
   line_num++;
+  if (err <= 0) {
+    *msg = err < 0 ? "Line too long" : NULL;
+    return 0;
+  }
   line = line_buf;
   while (Cblank(*line))
     line++;
   line = line_buf;
   while (Cblank(*line))
     line++;
@@ -64,12 +67,10 @@ append(byte *start, byte *end)
   copy_buf.ptr[copied-1] = 0;
 }
 
   copy_buf.ptr[copied-1] = 0;
 }
 
-#define        CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
-  // these characters separate words like blanks
-
 static byte *
 get_word(uns is_command_name)
 {
 static byte *
 get_word(uns is_command_name)
 {
+  byte *msg;
   if (*line == '\'') {
     line++;
     while (1) {
   if (*line == '\'') {
     line++;
     while (1) {
@@ -80,8 +81,8 @@ get_word(uns is_command_name)
       if (*line)
        break;
       copy_buf.ptr[copied-1] = '\n';
       if (*line)
        break;
       copy_buf.ptr[copied-1] = '\n';
-      if (!get_line())
-       return "Unterminated apostrophe word at the end";
+      if (!get_line(&msg))
+       return msg ? : (byte*) "Unterminated apostrophe word at the end";
     }
     line++;
 
     }
     line++;
 
@@ -107,8 +108,8 @@ get_word(uns is_command_name)
        copy_buf.ptr[copied-1] = '\n';
       else // merge two lines
        copied -= 2;
        copy_buf.ptr[copied-1] = '\n';
       else // merge two lines
        copied -= 2;
-      if (!get_line())
-       return "Unterminated quoted word at the end";
+      if (!get_line(&msg))
+       return msg ? : (byte*) "Unterminated quoted word at the end";
     }
     line++;
 
     }
     line++;
 
@@ -121,7 +122,8 @@ get_word(uns is_command_name)
   } else {
     // promised that *line is non-null and non-blank
     byte *start = line;
   } else {
     // promised that *line is non-null and non-blank
     byte *start = line;
-    while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
+    while (*line && !Cblank(*line)
+       && *line != '{' && *line != '}' && *line != ';'
        && (*line != '=' || !is_command_name))
       line++;
     if (*line == '=') {                                // nice for setting from a command-line
        && (*line != '=' || !is_command_name))
       line++;
     if (*line == '=') {                                // nice for setting from a command-line
@@ -144,19 +146,20 @@ get_token(uns is_command_name, byte **msg)
   *msg = NULL;
   while (1) {
     if (!*line || *line == '#') {
   *msg = NULL;
   while (1) {
     if (!*line || *line == '#') {
-      if (!is_command_name || !get_line())
+      if (!is_command_name || !get_line(msg))
        return NULL;
     } else if (*line == ';') {
       *msg = get_word(0);
       if (!is_command_name || *msg)
        return NULL;
     } else if (*line == '\\' && !line[1]) {
        return NULL;
     } else if (*line == ';') {
       *msg = get_word(0);
       if (!is_command_name || *msg)
        return NULL;
     } else if (*line == '\\' && !line[1]) {
-      if (!get_line()) {
-       *msg = "Last line ends by a backslash";
+      if (!get_line(msg)) {
+       if (!*msg)
+         *msg = "Last line ends by a backslash";
        return NULL;
       }
       if (!*line || *line == '#')
        return NULL;
       }
       if (!*line || *line == '#')
-       log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb, line_num);
+       log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb ? : (byte*) "", line_num);
     } else {
       split_grow(&word_buf, words+1);
       uns start = copied;
     } else {
       split_grow(&word_buf, words+1);
       uns start = copied;
@@ -244,7 +247,11 @@ parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
       switch (Clocase(*c)) {
        case 's': op = OP_SET; break;
        case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
       switch (Clocase(*c)) {
        case 's': op = OP_SET; break;
        case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
-       case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
+       case 'a': switch (Clocase(c[1])) {
+                   case 'p': op = OP_APPEND; break;
+                   case 'f': op = OP_AFTER; break;
+                   default: op = OP_ALL;
+                 }; break;
        case 'p': op = OP_PREPEND; break;
        case 'r': op = OP_REMOVE; break;
        case 'e': op = OP_EDIT; break;
        case 'p': op = OP_PREPEND; break;
        case 'r': op = OP_REMOVE; break;
        case 'e': op = OP_EDIT; break;
@@ -263,7 +270,12 @@ parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
       goto error;
   }
 error:
       goto error;
   }
 error:
-  log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
+  if (name_fb)
+    log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
+  else if (line_num == 1)
+    log(L_ERROR, "Manual setting of configuration: %s", msg);
+  else
+    log(L_ERROR, "Manual setting of configuration, line %d: %s", line_num, msg);
   return "included from here";
 }
 
   return "included from here";
 }
 
@@ -310,7 +322,7 @@ load_string(byte *string)
   cf_init_stack();
   struct fastbuf fb;
   fbbuf_init_read(&fb, string, strlen(string), 0);
   cf_init_stack();
   struct fastbuf fb;
   fbbuf_init_read(&fb, string, strlen(string), 0);
-  byte *msg = parse_fastbuf("memory string", &fb, 0);
+  byte *msg = parse_fastbuf(NULL, &fb, 0);
   return !!msg || done_stack();
 }
 
   return !!msg || done_stack();
 }