ambrop7 15 лет назад
Родитель
Сommit
2db4d1a01b

+ 1 - 0
blog_channels.txt

@@ -8,6 +8,7 @@ ncd_list 4
 ncd_depend 4
 ncd_multidepend 4
 ncd_concat 4
+ncd_concatv 4
 ncd_concatlist 4
 ncd_if 4
 ncd_strcmp 4

+ 4 - 0
generated/blog_channel_ncd_concatv.h

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

+ 47 - 46
generated/blog_channels_defines.h

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

+ 1 - 0
generated/blog_channels_list.h

@@ -8,6 +8,7 @@
 {.name = "ncd_depend", .loglevel = 4},
 {.name = "ncd_multidepend", .loglevel = 4},
 {.name = "ncd_concat", .loglevel = 4},
+{.name = "ncd_concatv", .loglevel = 4},
 {.name = "ncd_concatlist", .loglevel = 4},
 {.name = "ncd_if", .loglevel = 4},
 {.name = "ncd_strcmp", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -18,6 +18,7 @@ add_executable(badvpn-ncd
     modules/depend.c
     modules/multidepend.c
     modules/concat.c
+    modules/concatv.c
     modules/concatlist.c
     modules/if.c
     modules/strcmp.c

+ 152 - 0
ncd/modules/concatv.c

@@ -0,0 +1,152 @@
+/**
+ * @file concatv.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
+ * 
+ * String concatenation module.
+ * 
+ * Synopsis: concatv(list(string) strings)
+ * Variables:
+ *   string (empty) - elements of strings concatenated
+ */
+
+#include <stdlib.h>
+
+#include <misc/expstring.h>
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_concatv.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct instance {
+    NCDModuleInst *i;
+    char *string;
+};
+
+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;
+    
+    // read arguments
+    NCDValue *strings_arg;
+    if (!NCDValue_ListRead(o->i->args, 1, &strings_arg)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    if (NCDValue_Type(strings_arg) != NCDVALUE_LIST) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong type");
+        goto fail1;
+    }
+    
+    // init string
+    ExpString s;
+    if (!ExpString_Init(&s)) {
+        ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
+        goto fail1;
+    }
+    
+    // append arguments
+    NCDValue *arg = NCDValue_ListFirst(strings_arg);
+    while (arg) {
+        if (NCDValue_Type(arg) != NCDVALUE_STRING) {
+            ModuleLog(i, BLOG_ERROR, "wrong type");
+            goto fail2;
+        }
+        
+        if (!ExpString_Append(&s, NCDValue_StringValue(arg))) {
+            ModuleLog(i, BLOG_ERROR, "ExpString_Append failed");
+            goto fail2;
+        }
+        
+        arg = NCDValue_ListNext(strings_arg, arg);
+    }
+    
+    // set string
+    o->string = ExpString_Get(&s);
+    
+    // signal up
+    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    
+    return;
+    
+fail2:
+    ExpString_Free(&s);
+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 string
+    free(o->string);
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static int func_getvar (void *vo, const char *name, NCDValue *out)
+{
+    struct instance *o = vo;
+    
+    if (!strcmp(name, "")) {
+        if (!NCDValue_InitString(out, o->string)) {
+            ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
+            return 0;
+        }
+        
+        return 1;
+    }
+    
+    return 0;
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "concatv",
+        .func_new = func_new,
+        .func_die = func_die,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_concatv = {
+    .modules = modules
+};

+ 2 - 0
ncd/modules/modules.h

@@ -32,6 +32,7 @@ extern const struct NCDModuleGroup ncdmodule_list;
 extern const struct NCDModuleGroup ncdmodule_depend;
 extern const struct NCDModuleGroup ncdmodule_multidepend;
 extern const struct NCDModuleGroup ncdmodule_concat;
+extern const struct NCDModuleGroup ncdmodule_concatv;
 extern const struct NCDModuleGroup ncdmodule_concatlist;
 extern const struct NCDModuleGroup ncdmodule_if;
 extern const struct NCDModuleGroup ncdmodule_strcmp;
@@ -59,6 +60,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_depend,
     &ncdmodule_multidepend,
     &ncdmodule_concat,
+    &ncdmodule_concatv,
     &ncdmodule_concatlist,
     &ncdmodule_if,
     &ncdmodule_strcmp,