Switch to array_t and feed the filters from config.
[apps/pfixtools.git] / postlicyd / config.c
index e165e81..2d818f2 100644 (file)
 #include "config.h"
 
 struct config_t {
-    filter_t *filters;
-    int filters_len;
-    int filters_size;
-
+    A(filter_t)        filters;
+    A(filter_params_t) params;
     int entry_point;
 };
 
@@ -55,20 +53,20 @@ static inline config_t *config_new(void)
 void config_delete(config_t **config)
 {
     if (*config) {
-        for (int i = 0 ; i < (*config)->filters_len ; ++i) {
-            filter_wipe((*config)->filters + i);
-        }
-        p_delete(&(*config)->filters);
+        array_deep_wipe((*config)->filters, filter_wipe);
+        array_deep_wipe((*config)->params, filter_params_wipe);
+        p_delete(config);
     }
 }
 
 config_t *config_read(const char *file)
 {
     config_t *config;
-    //filter_t *filter = NULL;
+    filter_t filter;
     file_map_t map;
     const char *p;
     int line = 0;
+    const char *linep;
 
     char key[BUFSIZ];
     char value[BUFSIZ];
@@ -79,45 +77,54 @@ config_t *config_read(const char *file)
     }
 
     config = config_new();
-    p = map.map;
+    linep = p = map.map;
 
 #define READ_ERROR(Fmt, ...)                                                   \
-    syslog(LOG_ERR, "config file %s:%d: " Fmt, file, line, ##__VA_ARGS__)
-#define ADD_IN_BUFFER(Buffer, Len, Char)                                       \
-    if ((Len) >= BUFSIZ - 1) {                                                 \
-        READ_ERROR("unreasonnable long line");                                 \
+    do {                                                                       \
+        syslog(LOG_ERR, "config file %s:%d:%d: " Fmt, file, line + 1,          \
+               p - linep + 1, ##__VA_ARGS__);                                  \
         goto error;                                                            \
-    }                                                                          \
-    (Buffer)[(Len)++] = (Char);                                                \
-    (Buffer)[(Len)]   = '\0';
-
+    } while (0)
+#define ADD_IN_BUFFER(Buffer, Len, Char)                                       \
+    do {                                                                       \
+        if ((Len) >= BUFSIZ - 1) {                                             \
+            READ_ERROR("unreasonnable long line");                             \
+        }                                                                      \
+        (Buffer)[(Len)++] = (Char);                                            \
+        (Buffer)[(Len)]   = '\0';                                              \
+    } while (0)
 #define READ_NEXT(OnEOF)                                                       \
-    if (++p >= map.end) {                                                      \
-        OnEOF;                                                                 \
-    }
-#define READ_BLANK(OnEOF)                                                      \
-    while (isblank(*p)) {                                                      \
+    do {                                                                       \
         if (*p == '\n') {                                                      \
             ++line;                                                            \
+            linep = p + 1;                                                     \
         }                                                                      \
-        READ_NEXT(OnEOF);                                                      \
-    }
+        if (++p >= map.end) {                                                  \
+            OnEOF;                                                             \
+        }                                                                      \
+    } while (0)
+#define READ_BLANK(OnEOF)                                                      \
+    do {                                                                       \
+        bool in_comment = false;                                               \
+        while (in_comment || isspace(*p) || *p == '#') {                       \
+            if (*p == '\n') {                                                  \
+                in_comment = false;                                            \
+            } else if (*p == '#') {                                            \
+                in_comment = true;                                             \
+            }                                                                  \
+            READ_NEXT(OnEOF);                                                  \
+        }                                                                      \
+    } while (0)
 #define READ_TOKEN(Name, Buffer, Len)                                          \
     do {                                                                       \
         (Len) = 0;                                                             \
         (Buffer)[0] = '\0';                                                    \
         if (!isalpha(*p)) {                                                    \
             READ_ERROR("invalid %s, unexpected character '%c'", Name, *p);     \
-            goto error;                                                        \
         }                                                                      \
         do {                                                                   \
             ADD_IN_BUFFER(Buffer, Len, *p);                                    \
-            if ((Len) >= BUFSIZ - 1) {                                         \
-                READ_ERROR("unreasonnable long token");                        \
-                goto error;                                                    \
-            }                                                                  \
-            (Buffer)[(Len)++] = *p;                                            \
-            READ_NEXT(goto badeof)                                             \
+            READ_NEXT(goto badeof);                                            \
         } while (isalnum(*p) || *p == '_');                                    \
     } while (0)
 #define READ_STRING(Name, Buffer, Len, OnEOF)                                  \
@@ -131,7 +138,6 @@ config_t *config_read(const char *file)
                 while (true) {                                                 \
                     if (*p == '\n') {                                          \
                         READ_ERROR("string must not contain EOL");             \
-                        goto error;                                            \
                     } else if (escaped) {                                      \
                         ADD_IN_BUFFER(Buffer, Len, *p);                        \
                         escaped = false;                                       \
@@ -149,12 +155,10 @@ config_t *config_read(const char *file)
             }                                                                  \
             if (*p != ';') {                                                   \
                 READ_ERROR("%s must end with a ';'", Name);                    \
-                goto error;                                                    \
             }                                                                  \
         } else {                                                               \
             bool escaped = false;                                              \
-            READ_NEXT(goto badeof);                                            \
-            while (*p != ';' && isascii(*p) && isprint(*p)) {                  \
+            while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
                 if (escaped) {                                                 \
                     if (*p == '\r' || *p == '\n') {                            \
                         READ_BLANK(goto badeof);                               \
@@ -176,7 +180,7 @@ config_t *config_read(const char *file)
                 ADD_IN_BUFFER(Buffer, Len, '\\');                              \
             }                                                                  \
         }                                                                      \
-        READ_NEXT(OnEOF)                                                       \
+        READ_NEXT(OnEOF);                                                      \
     } while(0)
 
 
@@ -193,14 +197,13 @@ read_section:
     READ_BLANK(goto badeof);
     switch (*p) {
       case '=':
-        READ_NEXT(goto badeof)
+        READ_NEXT(goto badeof);
         goto read_param_value;
       case '{':
-        READ_NEXT(goto badeof)
+        READ_NEXT(goto badeof);
         goto read_filter;
       default:
         READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
-        goto error;
     }
 
 read_param_value:
@@ -211,18 +214,41 @@ read_param_value:
     goto read_section;
 
 read_filter:
-    /* TODO: Create a filter with the given name.
-     */
+    filter_init(&filter);
+    filter_set_name(&filter, key, key_len);
     READ_BLANK(goto badeof);
     while (*p != '}') {
         READ_TOKEN("filter parameter name", key, key_len);
         READ_BLANK(goto badeof);
+        if (*p != '=') {
+            READ_ERROR("invalid character '%c', expected '='", *p);
+        }
+        READ_NEXT(goto badeof);
+        READ_BLANK(goto badeof);
         READ_STRING("filter parameter value", value, value_len, goto badeof);
-        /* TODO: Insert parameter in the filter.
-         */
+        READ_BLANK(goto badeof);
+        if (strcmp(key, "type") == 0) {
+            if (!filter_set_type(&filter, value, value_len)) {
+                READ_ERROR("unknow filter type (%s) for filter %s",
+                           value, filter.name);
+            }
+        } else if (key_len > 3 && strncmp(key, "on_", 3) == 0) {
+            if (!filter_add_hook(&filter, key + 3, key_len - 3,
+                                 value, value_len)) {
+                READ_ERROR("hook %s not supported by filter %s",
+                           key + 3, filter.name);
+            }
+        } else {
+            if (!filter_add_param(&filter, key, key_len, value, value_len)) {
+                goto error;
+            }
+        }
     }
-    /* TODO: Check the filter.
-     */
+    READ_NEXT(;);
+    if (!filter_build(&filter)) {
+        READ_ERROR("invalid filter %s", filter.name);
+    }
+    array_add(config->filters, filter);
     goto read_section;
 
 ok: