ambrop7 пре 13 година
родитељ
комит
577e83fa9c

+ 1 - 0
blog_channels.txt

@@ -120,3 +120,4 @@ ncd_file 4
 ncd_netmask 4
 ncd_implode 4
 ncd_call2 4
+ncd_assert 4

+ 4 - 0
generated/blog_channel_ncd_assert.h

@@ -0,0 +1,4 @@
+#ifdef BLOG_CURRENT_CHANNEL
+#undef BLOG_CURRENT_CHANNEL
+#endif
+#define BLOG_CURRENT_CHANNEL BLOG_CHANNEL_ncd_assert

+ 2 - 1
generated/blog_channels_defines.h

@@ -120,4 +120,5 @@
 #define BLOG_CHANNEL_ncd_netmask 119
 #define BLOG_CHANNEL_ncd_implode 120
 #define BLOG_CHANNEL_ncd_call2 121
-#define BLOG_NUM_CHANNELS 122
+#define BLOG_CHANNEL_ncd_assert 122
+#define BLOG_NUM_CHANNELS 123

+ 1 - 0
generated/blog_channels_list.h

@@ -120,3 +120,4 @@
 {.name = "ncd_netmask", .loglevel = 4},
 {.name = "ncd_implode", .loglevel = 4},
 {.name = "ncd_call2", .loglevel = 4},
+{.name = "ncd_assert", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -141,6 +141,7 @@ add_executable(badvpn-ncd
     modules/netmask.c
     modules/implode.c
     modules/call2.c
+    modules/assert.c
     ${NCD_ADDITIONAL_SOURCES}
 )
 target_link_libraries(badvpn-ncd system flow flowextra dhcpclient arpprobe ncdvalue ncdval ncdvalcompat ncdvaluegenerator ncdvalueparser ncdconfigparser udevmonitor ncdinterfacemonitor ncdrequest)

+ 86 - 0
ncd/modules/assert.c

@@ -0,0 +1,86 @@
+/**
+ * @file assert.c
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ * 
+ * @section LICENSE
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the
+ *    names of its contributors may be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * 
+ * @section DESCRIPTION
+ * 
+ * Synopsis:
+ *   assert(string cond)
+ * 
+ * Description:
+ *   If 'cond' is equal to the string "true", does nothing.
+ *   Otherwise, logs an error and initiates interpreter termination with exit code 1,
+ *   i.e. it is equivalent to calling exit("1").
+ */
+
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_assert.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+static void func_new (NCDModuleInst *i)
+{
+    // check arguments
+    NCDValRef cond_arg;
+    if (!NCDVal_ListRead(i->args, 1, &cond_arg)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
+    }
+    if (!NCDVal_IsString(cond_arg)) {
+        ModuleLog(i, BLOG_ERROR, "wrong type");
+        goto fail0;
+    }
+    
+    // signal up
+    NCDModuleInst_Backend_Up(i);
+    
+    // if failed, initiate exit (before up!)
+    if (!NCDVal_StringEquals(cond_arg, "true")) {
+        ModuleLog(i, BLOG_ERROR, "assertion failed");
+        NCDModuleInst_Backend_InterpExit(i, 1);
+    }
+    
+    return;
+    
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "assert",
+        .func_new = func_new
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_assert = {
+    .modules = modules
+};

+ 2 - 0
ncd/modules/modules.h

@@ -101,6 +101,7 @@ extern const struct NCDModuleGroup ncdmodule_file;
 extern const struct NCDModuleGroup ncdmodule_netmask;
 extern const struct NCDModuleGroup ncdmodule_implode;
 extern const struct NCDModuleGroup ncdmodule_call2;
+extern const struct NCDModuleGroup ncdmodule_assert;
 
 static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_var,
@@ -170,6 +171,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_netmask,
     &ncdmodule_implode,
     &ncdmodule_call2,
+    &ncdmodule_assert,
     NULL
 };