ambrop7 пре 15 година
родитељ
комит
0ca7a2623d

+ 1 - 0
blog_channels.txt

@@ -14,6 +14,7 @@ ncd_if 4
 ncd_strcmp 4
 ncd_logical 4
 ncd_sleep 4
+ncd_print 4
 ncd_ip_in_network 4
 ncd_run 4
 ncd_net_backend_waitdevice 4

+ 4 - 0
generated/blog_channel_ncd_print.h

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

+ 42 - 41
generated/blog_channels_defines.h

@@ -14,44 +14,45 @@
 #define BLOG_CHANNEL_ncd_strcmp 13
 #define BLOG_CHANNEL_ncd_logical 14
 #define BLOG_CHANNEL_ncd_sleep 15
-#define BLOG_CHANNEL_ncd_ip_in_network 16
-#define BLOG_CHANNEL_ncd_run 17
-#define BLOG_CHANNEL_ncd_net_backend_waitdevice 18
-#define BLOG_CHANNEL_ncd_net_backend_waitlink 19
-#define BLOG_CHANNEL_ncd_net_backend_badvpn 20
-#define BLOG_CHANNEL_ncd_net_backend_wpa_supplicant 21
-#define BLOG_CHANNEL_ncd_net_backend_rfkill 22
-#define BLOG_CHANNEL_ncd_net_up 23
-#define BLOG_CHANNEL_ncd_net_dns 24
-#define BLOG_CHANNEL_ncd_net_iptables 25
-#define BLOG_CHANNEL_ncd_net_ipv4_addr 26
-#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 27
-#define BLOG_CHANNEL_ncd_net_ipv4_route 28
-#define BLOG_CHANNEL_StreamPeerIO 29
-#define BLOG_CHANNEL_DatagramPeerIO 30
-#define BLOG_CHANNEL_BReactor 31
-#define BLOG_CHANNEL_BSignal 32
-#define BLOG_CHANNEL_FragmentProtoAssembler 33
-#define BLOG_CHANNEL_BPredicate 34
-#define BLOG_CHANNEL_ServerConnection 35
-#define BLOG_CHANNEL_Listener 36
-#define BLOG_CHANNEL_DataProto 37
-#define BLOG_CHANNEL_FrameDecider 38
-#define BLOG_CHANNEL_BSocksClient 39
-#define BLOG_CHANNEL_BDHCPClientCore 40
-#define BLOG_CHANNEL_BDHCPClient 41
-#define BLOG_CHANNEL_NCDIfConfig 42
-#define BLOG_CHANNEL_BUnixSignal 43
-#define BLOG_CHANNEL_BProcess 44
-#define BLOG_CHANNEL_StreamSocketSink 45
-#define BLOG_CHANNEL_StreamSocketSource 46
-#define BLOG_CHANNEL_DatagramSocketSink 47
-#define BLOG_CHANNEL_DatagramSocketSource 48
-#define BLOG_CHANNEL_PRStreamSink 49
-#define BLOG_CHANNEL_PRStreamSource 50
-#define BLOG_CHANNEL_BSocketPRFileDesc 51
-#define BLOG_CHANNEL_PacketProtoDecoder 52
-#define BLOG_CHANNEL_DPRelay 53
-#define BLOG_CHANNEL_BThreadWork 54
-#define BLOG_CHANNEL_DPReceive 55
-#define BLOG_NUM_CHANNELS 56
+#define BLOG_CHANNEL_ncd_print 16
+#define BLOG_CHANNEL_ncd_ip_in_network 17
+#define BLOG_CHANNEL_ncd_run 18
+#define BLOG_CHANNEL_ncd_net_backend_waitdevice 19
+#define BLOG_CHANNEL_ncd_net_backend_waitlink 20
+#define BLOG_CHANNEL_ncd_net_backend_badvpn 21
+#define BLOG_CHANNEL_ncd_net_backend_wpa_supplicant 22
+#define BLOG_CHANNEL_ncd_net_backend_rfkill 23
+#define BLOG_CHANNEL_ncd_net_up 24
+#define BLOG_CHANNEL_ncd_net_dns 25
+#define BLOG_CHANNEL_ncd_net_iptables 26
+#define BLOG_CHANNEL_ncd_net_ipv4_addr 27
+#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 28
+#define BLOG_CHANNEL_ncd_net_ipv4_route 29
+#define BLOG_CHANNEL_StreamPeerIO 30
+#define BLOG_CHANNEL_DatagramPeerIO 31
+#define BLOG_CHANNEL_BReactor 32
+#define BLOG_CHANNEL_BSignal 33
+#define BLOG_CHANNEL_FragmentProtoAssembler 34
+#define BLOG_CHANNEL_BPredicate 35
+#define BLOG_CHANNEL_ServerConnection 36
+#define BLOG_CHANNEL_Listener 37
+#define BLOG_CHANNEL_DataProto 38
+#define BLOG_CHANNEL_FrameDecider 39
+#define BLOG_CHANNEL_BSocksClient 40
+#define BLOG_CHANNEL_BDHCPClientCore 41
+#define BLOG_CHANNEL_BDHCPClient 42
+#define BLOG_CHANNEL_NCDIfConfig 43
+#define BLOG_CHANNEL_BUnixSignal 44
+#define BLOG_CHANNEL_BProcess 45
+#define BLOG_CHANNEL_StreamSocketSink 46
+#define BLOG_CHANNEL_StreamSocketSource 47
+#define BLOG_CHANNEL_DatagramSocketSink 48
+#define BLOG_CHANNEL_DatagramSocketSource 49
+#define BLOG_CHANNEL_PRStreamSink 50
+#define BLOG_CHANNEL_PRStreamSource 51
+#define BLOG_CHANNEL_BSocketPRFileDesc 52
+#define BLOG_CHANNEL_PacketProtoDecoder 53
+#define BLOG_CHANNEL_DPRelay 54
+#define BLOG_CHANNEL_BThreadWork 55
+#define BLOG_CHANNEL_DPReceive 56
+#define BLOG_NUM_CHANNELS 57

+ 1 - 0
generated/blog_channels_list.h

@@ -14,6 +14,7 @@
 {.name = "ncd_strcmp", .loglevel = 4},
 {.name = "ncd_logical", .loglevel = 4},
 {.name = "ncd_sleep", .loglevel = 4},
+{.name = "ncd_print", .loglevel = 4},
 {.name = "ncd_ip_in_network", .loglevel = 4},
 {.name = "ncd_run", .loglevel = 4},
 {.name = "ncd_net_backend_waitdevice", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -24,6 +24,7 @@ add_executable(badvpn-ncd
     modules/strcmp.c
     modules/logical.c
     modules/sleep.c
+    modules/print.c
     modules/ip_in_network.c
     modules/run.c
     modules/net_backend_waitdevice.c

+ 2 - 0
ncd/modules/modules.h

@@ -38,6 +38,7 @@ extern const struct NCDModuleGroup ncdmodule_if;
 extern const struct NCDModuleGroup ncdmodule_strcmp;
 extern const struct NCDModuleGroup ncdmodule_logical;
 extern const struct NCDModuleGroup ncdmodule_sleep;
+extern const struct NCDModuleGroup ncdmodule_print;
 extern const struct NCDModuleGroup ncdmodule_ip_in_network;
 extern const struct NCDModuleGroup ncdmodule_run;
 extern const struct NCDModuleGroup ncdmodule_net_backend_waitdevice;
@@ -66,6 +67,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_strcmp,
     &ncdmodule_logical,
     &ncdmodule_sleep,
+    &ncdmodule_print,
     &ncdmodule_ip_in_network,
     &ncdmodule_run,
     &ncdmodule_net_backend_waitdevice,

+ 137 - 0
ncd/modules/print.c

@@ -0,0 +1,137 @@
+/**
+ * @file print.c
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ * 
+ * @section LICENSE
+ * 
+ * This file is part of BadVPN.
+ * 
+ * BadVPN is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ * 
+ * BadVPN is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * @section DESCRIPTION
+ * 
+ * Conditional module.
+ * 
+ * Synopsis: print(string str)
+ * Synopsis: println(string str)
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_print.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct instance {
+    NCDModuleInst *i;
+};
+
+static void func_new (NCDModuleInst *i)
+{
+    // allocate instance
+    struct instance *o = malloc(sizeof(*o));
+    if (!o) {
+        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
+        goto fail0;
+    }
+    NCDModuleInst_Backend_SetUser(i, o);
+    
+    // init arguments
+    o->i = i;
+    
+    // print
+    NCDValue *arg = NCDValue_ListFirst(o->i->args);
+    while (arg) {
+        if (NCDValue_Type(arg) != NCDVALUE_STRING) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong type");
+            goto fail1;
+        }
+        printf("%s", NCDValue_StringValue(arg));
+        arg = NCDValue_ListNext(o->i->args, arg);
+    }
+    
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static void func_new_ln (NCDModuleInst *i)
+{
+    // allocate instance
+    struct instance *o = malloc(sizeof(*o));
+    if (!o) {
+        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
+        goto fail0;
+    }
+    NCDModuleInst_Backend_SetUser(i, o);
+    
+    // init arguments
+    o->i = i;
+    
+    // print
+    NCDValue *arg = NCDValue_ListFirst(o->i->args);
+    while (arg) {
+        if (NCDValue_Type(arg) != NCDVALUE_STRING) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong type");
+            goto fail1;
+        }
+        printf("%s", NCDValue_StringValue(arg));
+        arg = NCDValue_ListNext(o->i->args, arg);
+    }
+    printf("\n");
+    
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static void func_die (void *vo)
+{
+    struct instance *o = vo;
+    NCDModuleInst *i = o->i;
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "print",
+        .func_new = func_new,
+        .func_die = func_die
+    }, {
+        .type = "println",
+        .func_new = func_new_ln,
+        .func_die = func_die
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_print = {
+    .modules = modules
+};