snort3: clean up ucode usage
[feed/packages.git] / net / snort3 / files / main.uc
index 4f2a63ca881a99877ae7add2a731b75f0f20c28d..8e33f9e5d29ed5311e5bba3f6289c010b8690b86 100644 (file)
@@ -30,15 +30,46 @@ function wrn(fmt, ...args) {
 
 function rpad(str, fill, len)
 {
-    str = rtrim(str) + ' ';
-    while (length(str) < len) {
-        str += fill;
-    }
-    return str;
+       str = rtrim(str) + ' ';
+       while (length(str) < len) {
+               str += fill;
+       }
+       return str;
 }
 
 //------------------------------------------------------------------------------
 
+const ConfigItem = {
+       contains: function(value) {
+               // Check if the value is contained in the listed values,
+               // depending on the item type.
+               switch (this.type) {
+               case "enum":
+                       return value in this.values;
+               case "range":
+                       return value >= this.values[0] && value <= this.values[1];
+               default:
+                       return true;
+               }
+       },
+
+       allowed: function() {
+               // Show a pretty version of the possible values, for error messages.
+               switch (this.type) {
+               case "enum":
+                       return "one of [" + join(", ", this.values) + "]";
+               case "range":
+                       return `${this.values[0]} <= x <= ${this.values[1]}`;
+               case "path":
+                       return "a path string";
+               case "str":
+                       return "a string";
+               default:
+                       return "???";
+               }
+       },
+};
+
 function config_item(type, values, def) {
        // If no default value is provided explicity, then values[0] is used as default.
        if (! type in [ "enum", "range", "path", "str" ]) {
@@ -49,42 +80,13 @@ function config_item(type, values, def) {
                wrn(`A 'range' type item must have exactly 2 values in ascending order.`);
                return;
        }
-       // Maybe check paths for existence???
+       // Maybe check 'path' values for existence???
                
-       return {
+       return proto({
                type:     type,
                values:   values,
                default:  def ?? values[0],
-
-               contains: function(value) {
-                       // Check if the value is contained in the listed values,
-                       // depending on the item type.
-                       switch (this.type) {
-                       case "enum":
-                               return value in this.values;
-                       case "range":
-                               return value >= this.values[0] && value <= this.values[1];
-                       default:
-                               return true;
-                       }
-               },
-
-               allowed: function() {
-                       // Show a pretty version of the possible values, for error messages.
-                       switch (this.type) {
-                       case "enum":
-                               return "one of [" + join(", ", this.values) + "]";
-                       case "range":
-                               return `${this.values[0]} <= x <= ${this.values[1]}`;
-                       case "path":
-                               return "a path string";
-                       case "str":
-                               return "a string";
-                       default:
-                               return "???";
-                       }
-               },
-       }
+       }, ConfigItem);
 };
 
 const snort_config = {