libxl: add support for yajl 2.x
authorRoger Pau Monne <roger.pau@entel.upc.edu>
Tue, 31 Jan 2012 16:48:06 +0000 (16:48 +0000)
committerRoger Pau Monne <roger.pau@entel.upc.edu>
Tue, 31 Jan 2012 16:48:06 +0000 (16:48 +0000)
This patch adds support for yajl versions 2.x, while retaining 1.x
compatibility. All the needed ifdefs can be found in libxl_json.h.

Signed-off-by: Roger Pau Monne <roger.pau@entel.upc.edu>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
Config.mk
tools/check/check_yajl_lib [deleted file]
tools/libxl/Makefile
tools/libxl/libxl_json.c
tools/libxl/libxl_json.h
tools/libxl/libxl_qmp.c

index 90a77286a169d421a7fd6fb92a360b47502e7bac..c66664600375a77401fe8b0a02cc03dc06355753 100644 (file)
--- a/Config.mk
+++ b/Config.mk
@@ -186,6 +186,11 @@ CONFIG_LIBICONV   := $(shell export OS="`uname -s`"; \
                        . $(XEN_ROOT)/tools/check/funcs.sh; \
                        has_lib libiconv.so && echo 'y' || echo 'n')
 
+CONFIG_YAJL_VERSION := $(shell export OS="`uname -s`"; \
+                       export CHECK_INCLUDES="$(CHECK_INCLUDES)"; \
+                       . $(XEN_ROOT)/tools/check/funcs.sh; \
+                       has_header yajl/yajl_version.h && echo 'y' || echo 'n')
+
 # Enable XSM security module (by default, Flask).
 XSM_ENABLE ?= n
 FLASK_ENABLE ?= $(XSM_ENABLE)
diff --git a/tools/check/check_yajl_lib b/tools/check/check_yajl_lib
deleted file mode 100755 (executable)
index a0f6c02..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/bin/sh
-# CHECK-BUILD CHECK-INSTALL
-
-. ./funcs.sh
-
-has_lib libyajl.so.1 || fail "can't find libyajl.so.1 version 1"
index d0b6fda27789b39e5ef7a423859c51c8e71fea6b..113b7c2e50aac66cd4f143b1529af1092de47045 100644 (file)
@@ -19,6 +19,10 @@ ifeq ($(CONFIG_Linux),y)
 LIBUUID_LIBS += -luuid
 endif
 
+ifeq ($(CONFIG_YAJL_VERSION),y)
+CFLAGS += -DHAVE_YAJL_VERSION
+endif
+
 LIBXL_LIBS =
 LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(UTIL_LIBS) $(LIBUUID_LIBS)
 
index 6ff2910806ec26b5626a047cfd4c66aabaea90bc..345894b000567e8394f3a728dcea4cd5610159f9 100644 (file)
@@ -517,7 +517,7 @@ static bool is_decimal(const char *s, unsigned len)
     return false;
 }
 
-static int json_callback_number(void *opaque, const char *s, unsigned int len)
+static int json_callback_number(void *opaque, const char *s, libxl_yajl_length len)
 {
     libxl__yajl_ctx *ctx = opaque;
     libxl__json_object *obj = NULL;
@@ -574,7 +574,7 @@ out:
 }
 
 static int json_callback_string(void *opaque, const unsigned char *str,
-                                unsigned int len)
+                                libxl_yajl_length len)
 {
     libxl__yajl_ctx *ctx = opaque;
     char *t = NULL;
@@ -607,7 +607,7 @@ static int json_callback_string(void *opaque, const unsigned char *str,
 }
 
 static int json_callback_map_key(void *opaque, const unsigned char *str,
-                                 unsigned int len)
+                                 libxl_yajl_length len)
 {
     libxl__yajl_ctx *ctx = opaque;
     char *t = NULL;
@@ -770,17 +770,13 @@ libxl__json_object *libxl__json_parse(libxl__gc *gc, const char *s)
     DEBUG_GEN_ALLOC(&yajl_ctx);
 
     if (yajl_ctx.hand == NULL) {
-        yajl_parser_config cfg = {
-            .allowComments = 1,
-            .checkUTF8 = 1,
-        };
-        yajl_ctx.hand = yajl_alloc(&callbacks, &cfg, NULL, &yajl_ctx);
+        yajl_ctx.hand = libxl__yajl_alloc(&callbacks, NULL, &yajl_ctx);
     }
     status = yajl_parse(yajl_ctx.hand, (const unsigned char *)s, strlen(s));
     if (status != yajl_status_ok)
         goto out;
 
-    status = yajl_parse_complete(yajl_ctx.hand);
+    status = yajl_complete_parse(yajl_ctx.hand);
     if (status != yajl_status_ok)
         goto out;
 
@@ -832,14 +828,13 @@ static const char *yajl_gen_status_to_string(yajl_gen_status s)
 char *libxl__object_to_json(libxl_ctx *ctx, const char *type,
                             libxl__gen_json_callback gen, void *p)
 {
-    yajl_gen_config conf = { 1, "    " };
     const unsigned char *buf;
     char *ret = NULL;
-    unsigned int len = 0;
+    libxl_yajl_length len = 0;
     yajl_gen_status s;
     yajl_gen hand;
 
-    hand = yajl_gen_alloc(&conf, NULL);
+    hand = libxl__yajl_gen_alloc(NULL);
     if (!hand)
         return NULL;
 
index 720f6df10f7acaf503c56b2a5e9fd443c9ac7a5a..898dec79e75b2a7e80b1902ebc8eeb854d0857dc 100644 (file)
 #define LIBXL_JSON_H
 
 #include <yajl/yajl_gen.h>
+#include <yajl/yajl_parse.h>
+
+#ifdef HAVE_YAJL_VERSION
+#  include <yajl/yajl_version.h>
+#endif
 
 #include <_libxl_types_json.h>
 
+/* YAJL version check */
+#if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
+#  define HAVE_YAJL_V2 1
+#endif
+
+#ifdef HAVE_YAJL_V2
+
+typedef size_t libxl_yajl_length;
+
+static inline yajl_handle libxl__yajl_alloc(const yajl_callbacks *callbacks,
+                                            yajl_alloc_funcs *allocFuncs,
+                                            void *ctx)
+{
+    return yajl_alloc(callbacks, allocFuncs, ctx);
+}
+
+static inline yajl_gen libxl__yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
+{
+    return yajl_gen_alloc(allocFuncs);
+}
+
+#else /* !HAVE_YAJL_V2 */
+
+#define yajl_complete_parse yajl_parse_complete
+
+typedef unsigned int libxl_yajl_length;
+
+static inline yajl_handle libxl__yajl_alloc(const yajl_callbacks *callbacks,
+                                            const yajl_alloc_funcs *allocFuncs,
+                                            void *ctx)
+{
+    yajl_parser_config cfg = {
+        .allowComments = 1,
+        .checkUTF8 = 1,
+    };
+    return yajl_alloc(callbacks, &cfg, allocFuncs, ctx);
+}
+
+static inline yajl_gen libxl__yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
+{
+    yajl_gen_config conf = { 1, "    " };
+    return yajl_gen_alloc(&conf, allocFuncs);
+}
+
+#endif /* !HAVE_YAJL_V2 */
+
 #endif /* LIBXL_JSON_H */
index a43838d85a664183dd0c0b073e417e0775e1f653..e0642e31b5980c6ad48b84a5b27ef11b20c340be 100644 (file)
@@ -454,15 +454,15 @@ static char *qmp_send_prepare(libxl__gc *gc, libxl__qmp_handler *qmp,
                               qmp_callback_t callback, void *opaque,
                               qmp_request_context *context)
 {
-    yajl_gen_config conf = { 0, NULL };
     const unsigned char *buf = NULL;
     char *ret = NULL;
-    unsigned int len = 0;
+    libxl_yajl_length len = 0;
     yajl_gen_status s;
     yajl_gen hand;
     callback_id_pair *elm = NULL;
 
-    hand = yajl_gen_alloc(&conf, NULL);
+    hand = libxl__yajl_gen_alloc(NULL);
+
     if (!hand) {
         return NULL;
     }