Check configuration for internal loops.
[apps/pfixtools.git] / postlicyd / filter.h
index a113727..e8319d3 100644 (file)
@@ -70,10 +70,15 @@ typedef struct filter_t {
     void *data;
 
     A(filter_param_t) params;
+
+    /* Internal: to check the filter tree structure.
+     */
+    unsigned safe   :1;
+    unsigned seen   :1;
 } filter_t;
 ARRAY(filter_t)
 
-#define FILTER_INIT { NULL, FTK_UNKNOWN, ARRAY_INIT, NULL, ARRAY_INIT }
+#define FILTER_INIT { NULL, FTK_UNKNOWN, ARRAY_INIT, NULL, ARRAY_INIT, false, false }
 #define CHECK_FILTER(Filter)                                                   \
     assert(Filter != FTK_UNKNOWN && Filter != FTK_count                        \
            && "Unknown filter type")
@@ -147,6 +152,9 @@ static inline int filter_find_with_name(A(filter_t) *array, const char *name)
 __attribute__((nonnull(1,2)))
 bool filter_update_references(filter_t *filter, A(filter_t) *array);
 
+__attribute__((nonnull(1)))
+bool filter_check_safety(A(filter_t) *array);
+
 __attribute__((nonnull(1)))
 static inline void filter_hook_wipe(filter_hook_t *hook)
 {
@@ -166,4 +174,43 @@ __attribute__((nonnull(1,2)))
 const filter_hook_t *filter_run(const filter_t *filter, const query_t *query);
 
 
+/* Helpers
+ */
+
+#define FILTER_PARAM_PARSE_STRING(Param, Dest)                                 \
+    case ATK_ ## Param: {                                                      \
+        (Dest) = param->value;                                                 \
+    } break
+
+#define FILTER_PARAM_PARSE_INT(Param, Dest)                                    \
+    case ATK_ ## Param: {                                                      \
+        char *next;                                                            \
+        (Dest) = strtol(param->value, &next, 10);                              \
+        PARSE_CHECK(!*next, "invalid %s value %s", atokens[ATK_ ## Param],     \
+                    param->value);                                             \
+     } break
+
+#define FILTER_PARAM_PARSE_BOOLEAN(Param, Dest)                                \
+    case ATK_ ## Param: {                                                      \
+        if (param->value[0] == '1' && param->value[1] == '\0') {               \
+            (Dest) = true;                                                     \
+        } else if (param->value[0] == '0' && param->value[1] == '\0') {        \
+            (Dest) = false;                                                    \
+        } else if (ascii_tolower(param->value[0]) == 't') {                    \
+            (Dest) = ascii_tolower(param->value[1]) == 'r'                     \
+                  && ascii_tolower(param->value[2]) == 'u'                     \
+                  && ascii_tolower(param->value[3]) == 'e'                     \
+                  && !param->value[4];                                         \
+        } else if (ascii_tolower(param->value[0]) == 'f') {                    \
+            (Dest) = ascii_tolower(param->value[1]) == 'a'                     \
+                  && ascii_tolower(param->value[2]) == 'l'                     \
+                  && ascii_tolower(param->value[3]) == 's'                     \
+                  && ascii_tolower(param->value[4]) == 'e'                     \
+                  && !param->value[5];                                         \
+        } else {                                                               \
+            PARSE_CHECK(false, "invalid %s value %s", atokens[ATK_ ## Param],  \
+                        param->value);                                         \
+        }                                                                      \
+    } break
+
 #endif