Kaynağa Gözat

ncd: remove NCDValCompat

ambrop7 13 yıl önce
ebeveyn
işleme
9ce50b23fb

+ 1 - 1
examples/CMakeLists.txt

@@ -30,7 +30,7 @@ if (BUILD_NCD)
     target_link_libraries(ncd_tokenizer_test ncdtokenizer)
 
     add_executable(ncd_parser_test ncd_parser_test.c)
-    target_link_libraries(ncd_parser_test ncdconfigparser ncdvaluegenerator ncdsugar ncdvalcompat)
+    target_link_libraries(ncd_parser_test ncdconfigparser ncdvaluegenerator ncdsugar)
 
     add_executable(ncd_value_parser_test ncd_value_parser_test.c)
     target_link_libraries(ncd_value_parser_test ncdvalueparser ncdvaluegenerator)

+ 118 - 14
examples/ncd_parser_test.c

@@ -31,15 +31,124 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <inttypes.h>
 
 #include <misc/debug.h>
+#include <misc/expstring.h>
 #include <base/BLog.h>
 #include <ncd/NCDConfigParser.h>
 #include <ncd/NCDValueGenerator.h>
 #include <ncd/NCDSugar.h>
-#include <ncd/NCDValCompat.h>
 
-int error;
+static int generate_val (NCDValue *value, ExpString *out_str)
+{
+    switch (NCDValue_Type(value)) {
+        case NCDVALUE_STRING: {
+            const char *str = NCDValue_StringValue(value);
+            size_t len = NCDValue_StringLength(value);
+            
+            if (!ExpString_AppendChar(out_str, '"')) {
+                goto fail;
+            }
+            
+            for (size_t i = 0; i < len; i++) {
+                if (str[i] == '\0') {
+                    char buf[5];
+                    snprintf(buf, sizeof(buf), "\\x%02"PRIx8, (uint8_t)str[i]);
+                    
+                    if (!ExpString_Append(out_str, buf)) {
+                        goto fail;
+                    }
+                    
+                    continue;
+                }
+                
+                if (str[i] == '"' || str[i] == '\\') {
+                    if (!ExpString_AppendChar(out_str, '\\')) {
+                        goto fail;
+                    }
+                }
+                
+                if (!ExpString_AppendChar(out_str, str[i])) {
+                    goto fail;
+                }
+            }
+            
+            if (!ExpString_AppendChar(out_str, '"')) {
+                goto fail;
+            }
+        } break;
+        
+        case NCDVALUE_LIST: {
+            if (!ExpString_AppendChar(out_str, '{')) {
+                goto fail;
+            }
+            
+            int is_first = 1;
+            
+            for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
+                if (!is_first) {
+                    if (!ExpString_Append(out_str, ", ")) {
+                        goto fail;
+                    }
+                }
+                
+                if (!generate_val(e, out_str)) {
+                    goto fail;
+                }
+                
+                is_first = 0;
+            }
+            
+            if (!ExpString_AppendChar(out_str, '}')) {
+                goto fail;
+            }
+        } break;
+        
+        case NCDVALUE_MAP: {
+            if (!ExpString_AppendChar(out_str, '[')) {
+                goto fail;
+            }
+            
+            int is_first = 1;
+            
+            for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
+                NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
+                
+                if (!is_first) {
+                    if (!ExpString_Append(out_str, ", ")) {
+                        goto fail;
+                    }
+                }
+                
+                if (!generate_val(ekey, out_str)) {
+                    goto fail;
+                }
+                
+                if (!ExpString_AppendChar(out_str, ':')) {
+                    goto fail;
+                }
+                
+                if (!generate_val(eval, out_str)) {
+                    goto fail;
+                }
+                
+                is_first = 0;
+            }
+            
+            if (!ExpString_AppendChar(out_str, ']')) {
+                goto fail;
+            }
+        } break;
+        
+        default: ASSERT(0);
+    }
+    
+    return 1;
+    
+fail:
+    return 0;
+}
 
 static void print_indent (unsigned int indent)
 {
@@ -51,26 +160,21 @@ static void print_indent (unsigned int indent)
 
 static void print_value (NCDValue *v, unsigned int indent)
 {
-    NCDValMem mem;
-    NCDValMem_Init(&mem);
-    
-    NCDValRef vref;
-    if (!NCDValCompat_ValueToVal(v, &mem, &vref)) {
-        DEBUG("NCDValCompat_ValueToVal failed");
+    ExpString estr;
+    if (!ExpString_Init(&estr)) {
+        DEBUG("ExpString_Init failed");
         exit(1);
     }
     
-    char *str = NCDValGenerator_Generate(vref);
-    if (!str) {
-        DEBUG("NCDValGenerator_Generate failed");
+    if (!generate_val(v, &estr)) {
+        DEBUG("generate_val failed");
         exit(1);
     }
     
     print_indent(indent);
-    printf("%s\n", str);
+    printf("%s\n", ExpString_Get(&estr));
     
-    free(str);
-    NCDValMem_Free(&mem);
+    ExpString_Free(&estr);
 }
 
 static void print_block (NCDBlock *block, unsigned int indent)

+ 1 - 6
ncd/CMakeLists.txt

@@ -31,11 +31,6 @@ add_library(ncdval
 )
 target_link_libraries(ncdval base)
 
-add_library(ncdvalcompat
-    NCDValCompat.c
-)
-target_link_libraries(ncdvalcompat ncdast ncdval)
-
 add_library(ncdvaluegenerator
     NCDValueGenerator.c
 )
@@ -155,7 +150,7 @@ add_executable(badvpn-ncd
     ${NCD_ADDITIONAL_SOURCES}
 )
 target_link_libraries(badvpn-ncd
-    system flow flowextra dhcpclient arpprobe ncdval ncdvalcompat ncdvaluegenerator
+    system flow flowextra dhcpclient arpprobe ncdval ncdvaluegenerator
     ncdvalueparser ncdconfigparser ncdsugar udevmonitor ncdinterfacemonitor ncdrequest
     badvpn_random
 )

+ 0 - 161
ncd/NCDValCompat.c

@@ -1,161 +0,0 @@
-/**
- * @file NCDValCompat.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.
- */
-
-#include "NCDValCompat.h"
-
-int NCDValCompat_ValueToVal (NCDValue *value, NCDValMem *mem, NCDValRef *out)
-{
-    ASSERT((NCDValue_Type(value), 1))
-    ASSERT(mem)
-    ASSERT(out)
-    
-    switch (NCDValue_Type(value)) {
-        case NCDVALUE_STRING: {
-            *out = NCDVal_NewStringBin(mem, (const uint8_t *)NCDValue_StringValue(value), NCDValue_StringLength(value));
-            if (NCDVal_IsInvalid(*out)) {
-                goto fail;
-            }
-        } break;
-        
-        case NCDVALUE_LIST: {
-            *out = NCDVal_NewList(mem, NCDValue_ListCount(value));
-            if (NCDVal_IsInvalid(*out)) {
-                goto fail;
-            }
-            
-            for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
-                NCDValRef vval;
-                if (!NCDValCompat_ValueToVal(e, mem, &vval)) {
-                    goto fail;
-                }
-                
-                NCDVal_ListAppend(*out, vval);
-            }
-        } break;
-        
-        case NCDVALUE_MAP: {
-            *out = NCDVal_NewMap(mem, NCDValue_MapCount(value));
-            if (NCDVal_IsInvalid(*out)) {
-                goto fail;
-            }
-            
-            for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
-                NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
-                
-                NCDValRef vkey;
-                NCDValRef vval;
-                if (!NCDValCompat_ValueToVal(ekey, mem, &vkey) || !NCDValCompat_ValueToVal(eval, mem, &vval)) {
-                    goto fail;
-                }
-                
-                int res = NCDVal_MapInsert(*out, vkey, vval);
-                ASSERT_EXECUTE(res)
-            }
-        } break;
-        
-        default:
-            goto fail;
-    }
-    
-    return 1;
-    
-fail:
-    return 0;
-}
-
-int NCDValCompat_ValToValue (NCDValRef value, NCDValue *out)
-{
-    ASSERT(!NCDVal_IsInvalid(value))
-    ASSERT(out)
-    
-    switch (NCDVal_Type(value)) {
-        case NCDVAL_STRING: {
-            if (!NCDValue_InitStringBin(out, (const uint8_t *)NCDVal_StringValue(value), NCDVal_StringLength(value))) {
-                goto fail0;
-            }
-        } break;
-        
-        case NCDVAL_LIST: {
-            NCDValue_InitList(out);
-            
-            size_t count = NCDVal_ListCount(value);
-            
-            for (size_t j = 0; j < count; j++) {
-                NCDValRef velem = NCDVal_ListGet(value, j);
-                
-                NCDValue elem;
-                if (!NCDValCompat_ValToValue(velem, &elem)) {
-                    goto fail1;
-                }
-                
-                if (!NCDValue_ListAppend(out, elem)) {
-                    NCDValue_Free(&elem);
-                    goto fail1;
-                }
-            }
-        } break;
-        
-        case NCDVAL_MAP: {
-            NCDValue_InitMap(out);
-            
-            for (NCDValMapElem e = NCDVal_MapFirst(value); !NCDVal_MapElemInvalid(e); e = NCDVal_MapNext(value, e)) {
-                NCDValRef vkey = NCDVal_MapElemKey(value, e);
-                NCDValRef vval = NCDVal_MapElemVal(value, e);
-                
-                NCDValue key;
-                if (!NCDValCompat_ValToValue(vkey, &key)) {
-                    goto fail1;
-                }
-                
-                NCDValue val;
-                if (!NCDValCompat_ValToValue(vval, &val)) {
-                    NCDValue_Free(&key);
-                    goto fail1;
-                }
-                
-                if (!NCDValue_MapInsert(out, key, val)) {
-                    NCDValue_Free(&key);
-                    NCDValue_Free(&val);
-                    goto fail1;
-                }
-            }
-        } break;
-        
-        default:
-            ASSERT(0);
-            return 0;
-    }
-    
-    return 1;
-    
-fail1:
-    NCDValue_Free(out);
-fail0:
-    return 0;
-}

+ 0 - 56
ncd/NCDValCompat.h

@@ -1,56 +0,0 @@
-/**
- * @file NCDValCompat.h
- * @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.
- */
-
-#ifndef BADVPN_NCDVALCOMPAT_H
-#define BADVPN_NCDVALCOMPAT_H
-
-#include <misc/debug.h>
-#include <ncd/NCDAst.h>
-#include <ncd/NCDVal.h>
-
-/**
- * Converts an old NCDValue-form value into the new NCDVal-form.
- * 
- * @param value input value
- * @param mem value memory to use
- * @param out on success, a non-invalid converted value will be returned here
- * @return 1 on success, 0 on failure
- */
-int NCDValCompat_ValueToVal (NCDValue *value, NCDValMem *mem, NCDValRef *out) WARN_UNUSED;
-
-/**
- * Converts a new NCDVal-form value into the old NCDValue-form.
- * 
- * @param value non-invalid input value
- * @param out on success, the converted value will be returned here
- * @return 1 on success, 0 on failure
- */
-int NCDValCompat_ValToValue (NCDValRef value, NCDValue *out) WARN_UNUSED;
-
-#endif

+ 0 - 1
ncd/modules/sys_request_client.c

@@ -92,7 +92,6 @@
 #include <system/BAddr.h>
 #include <ncd/NCDModule.h>
 #include <ncd/NCDRequestClient.h>
-#include <ncd/NCDValCompat.h>
 
 #include <generated/blog_channel_ncd_sys_request_client.h>