[PATCH] Use json_object_object_get_ex if is available

Colin King colin.king at canonical.com
Thu May 1 11:40:44 UTC 2014


From: Colin Ian King <colin.king at canonical.com>

fwts fails to build on utopic because json_object_object_get has
been deprecated.  Check if json_object_object_get_ex is available
at config time and if so, use this.  This fix ensures fwts is
backwardly compatible so we can still build it on older releases.

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 configure.ac                       |  1 +
 src/acpi/syntaxcheck/syntaxcheck.c | 36 +++++++++++++++++++++++++++++++-----
 src/lib/include/fwts.h             |  2 ++
 src/lib/src/fwts_klog.c            | 31 ++++++++++++++++++++++++-------
 src/utilities/kernelscan.c         | 33 ++++++++++++++++++++++++++++++---
 5 files changed, 88 insertions(+), 15 deletions(-)

diff --git a/configure.ac b/configure.ac
index ec29f84..fc1ea03 100644
--- a/configure.ac
+++ b/configure.ac
@@ -10,6 +10,7 @@
 	  AC_C_INLINE
 	  AM_PROG_CC_C_O
 	  AC_SEARCH_LIBS([json_object_from_file], [json json-c], [], [ AC_MSG_ERROR([no available json library]) ])
+	  AC_SEARCH_LIBS([json_object_object_get_ex], [json json-c], [ AC_DEFINE([JSON_HAS_GET_EX], [1], [Define if we have json_object_object_get_ex])], [])
   	  AC_CHECK_FUNCS([localtime_r])
 	  AC_CHECK_FUNCS([dup2])
 	  AC_CHECK_FUNCS([getcwd])
diff --git a/src/acpi/syntaxcheck/syntaxcheck.c b/src/acpi/syntaxcheck/syntaxcheck.c
index 1e91ffc..a6dd583 100644
--- a/src/acpi/syntaxcheck/syntaxcheck.c
+++ b/src/acpi/syntaxcheck/syntaxcheck.c
@@ -342,12 +342,18 @@ static int syntaxcheck_load_advice(fwts_framework *fw)
 		fwts_log_error(fw, "Cannot load objects table from %s.", json_data_path);
 		return FWTS_ERROR;
 	}
-
-        syntaxcheck_table = json_object_object_get(syntaxcheck_objs, "erroradvice");
-        if (FWTS_JSON_ERROR(syntaxcheck_table)) {
+#if JSON_HAS_GET_EX
+        if (!json_object_object_get_ex(syntaxcheck_objs, "erroradvice", &syntaxcheck_table)) {
                 fwts_log_error(fw, "Cannot fetch syntaxcheck table from %s.", json_data_path);
                 goto fail_put;
         }
+#else
+	syntaxcheck_table = json_object_object_get(syntaxcheck_objs, "erroradvice");
+	if (FWTS_JSON_ERROR(syntaxcheck_table)) {
+                fwts_log_error(fw, "Cannot fetch syntaxcheck table from %s.", json_data_path);
+                goto fail_put;
+        }
+#endif
 
 	n = json_object_array_length(syntaxcheck_table);
 
@@ -355,6 +361,9 @@ static int syntaxcheck_load_advice(fwts_framework *fw)
 	for (i = 0; i < n; i++) {
 		const char *advice, *id_str;
 		json_object *obj;
+#if JSON_HAS_GET_EX
+		json_object *obj_str;
+#endif
 		int j;
 
 		obj = json_object_array_get_idx(syntaxcheck_table, i);
@@ -362,14 +371,31 @@ static int syntaxcheck_load_advice(fwts_framework *fw)
 			fwts_log_error(fw, "Cannot fetch %d item from syntaxcheck table.", i);
 			break;
 		}
+
+#if JSON_HAS_GET_EX
+		if (!json_object_object_get_ex(obj, "advice", &obj_str)) {
+			fwts_log_error(fw, "Cannot fetch advice object from item %d.", i);
+			break;
+		}
+		advice = json_object_get_string(obj_str);
+#else
 		advice = json_object_get_string(json_object_object_get(obj, "advice"));
+#endif
 		if (FWTS_JSON_ERROR(advice)) {
-                	fwts_log_error(fw, "Cannot fetch advice from item %d.", i);
+			fwts_log_error(fw, "Cannot fetch advice string from item %d.", i);
+			break;
+		}
+#if JSON_HAS_GET_EX
+		if (!json_object_object_get_ex(obj, "id", &obj_str)) {
+			fwts_log_error(fw, "Cannot fetch ID object from item %d.", i);
 			break;
 		}
+		id_str = json_object_get_string(obj_str);
+#else
 		id_str = json_object_get_string(json_object_object_get(obj, "id"));
+#endif
 		if (FWTS_JSON_ERROR(id_str)) {
-                	fwts_log_error(fw, "Cannot fetch ID from item %d.", i);
+			fwts_log_error(fw, "Cannot fetch ID string from item %d.", i);
 			break;
 		}
 
diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h
index b8b0602..84075f7 100644
--- a/src/lib/include/fwts.h
+++ b/src/lib/include/fwts.h
@@ -20,6 +20,8 @@
 #ifndef __FWTS_H__
 #define __FWTS_H__
 
+#include "config.h"
+
 #if defined(__x86_64__) || defined(__x86_64) || defined(__i386__) || defined(__i386)
 #define FWTS_ARCH_INTEL	1
 #endif
diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c
index cfe7dcf..ae6e396 100644
--- a/src/lib/src/fwts_klog.c
+++ b/src/lib/src/fwts_klog.c
@@ -336,15 +336,25 @@ static const char *fwts_json_str(
 	bool log_error)
 {
 	const char *str;
-
+#if JSON_HAS_GET_EX
+	json_object *str_obj;
+
+	if (!json_object_object_get_ex(obj, key, &str_obj))
+		goto nullobj;
+	str = json_object_get_string(str_obj);
+	if (FWTS_JSON_ERROR(str))
+		goto nullobj;
+#else
 	str = json_object_get_string(json_object_object_get(obj, key));
-	if (FWTS_JSON_ERROR(str)) {
-		if (log_error)
-			fwts_log_error(fw, "Cannot fetch %s val from item %d, table %s.",
-				key, index, table);
-		return NULL;
-	}
+	if (FWTS_JSON_ERROR(str))
+		goto nullobj;
+#endif
 	return str;
+nullobj:
+	if (log_error)
+		fwts_log_error(fw, "Cannot fetch %s val from item %d, table %s.",
+			key, index, table);
+	return NULL;
 }
 
 static int fwts_klog_check(fwts_framework *fw,
@@ -380,11 +390,18 @@ static int fwts_klog_check(fwts_framework *fw,
 		return FWTS_ERROR;
 	}
 
+#if JSON_HAS_GET_EX
+	if (!json_object_object_get_ex(klog_objs, table, &klog_table)) {
+		fwts_log_error(fw, "Cannot fetch klog table object '%s' from %s.", table, json_data_path);
+		goto fail_put;
+	}
+#else
 	klog_table = json_object_object_get(klog_objs, table);
 	if (FWTS_JSON_ERROR(klog_table)) {
 		fwts_log_error(fw, "Cannot fetch klog table object '%s' from %s.", table, json_data_path);
 		goto fail_put;
 	}
+#endif
 
 	n = json_object_array_length(klog_table);
 
diff --git a/src/utilities/kernelscan.c b/src/utilities/kernelscan.c
index 5409ca6..1b793bb 100644
--- a/src/utilities/kernelscan.c
+++ b/src/utilities/kernelscan.c
@@ -26,6 +26,7 @@
 
 #include <pcre.h>
 #include <json/json.h>
+#include "config.h"
 
 #define PARSER_OK		0
 #define PARSER_COMMENT_FOUND	1
@@ -248,11 +249,18 @@ static klog_pattern *klog_load(const char *table)
 		exit(EXIT_FAILURE);
 	}
 
+#if JSON_HAS_GET_EX
+	if (!json_object_object_get_ex(klog_objs, table, &klog_table)) {
+		fprintf(stderr, "Cannot fetch klog table object from %s.\n", table);
+		exit(EXIT_FAILURE);
+	}
+#else
 	klog_table = json_object_object_get(klog_objs, table);
 	if (JSON_ERROR(klog_table)) {
 		fprintf(stderr, "Cannot fetch klog table object from %s.\n", table);
 		exit(EXIT_FAILURE);
 	}
+#endif
 
 	n = json_object_array_length(klog_table);
 
@@ -265,9 +273,12 @@ static klog_pattern *klog_load(const char *table)
 	/* Now fetch json objects and compile regex */
 	for (i = 0; i < n; i++) {
 		const char *error;
-		char *str;
+		const char *str;
 		int erroffset;
 		json_object *obj;
+#if JSON_HAS_GET_EX
+		json_object *obj_str;
+#endif
 
 		obj = json_object_array_get_idx(klog_table, i);
 		if (JSON_ERROR(obj)) {
@@ -275,16 +286,32 @@ static klog_pattern *klog_load(const char *table)
 			exit(EXIT_FAILURE);
 		}
 
+#if JSON_HAS_GET_EX
+		if (!json_object_object_get_ex(obj, "compare_mode", &obj_str)) {
+			fprintf(stderr, "Cannot fetch compare_mode object, item %d from table %s.\n", i, table);
+			exit(EXIT_FAILURE);
+		}
+		str = json_object_get_string(obj_str);
+#else
 		str = (char*)json_object_get_string(json_object_object_get(obj, "compare_mode"));
+#endif
 		if (JSON_ERROR(str)) {
-			fprintf(stderr, "Cannot fetch compare_mode  object, item %d from table %s.\n", i, table);
+			fprintf(stderr, "Cannot fetch compare_mode string, item %d from table %s.\n", i, table);
 			exit(EXIT_FAILURE);
 		}
 		patterns[i].cm = klog_compare_mode_str_to_val(str);
 
+#if JSON_HAS_GET_EX
+		if (!json_object_object_get_ex(obj, "pattern", &obj_str)) {
+			fprintf(stderr, "Cannot fetch pattern object, item %d from table %s.\n", i, table);
+			exit(EXIT_FAILURE);
+		}
+		str = json_object_get_string(obj_str);
+#else
 		str = (char*)json_object_get_string(json_object_object_get(obj, "pattern"));
+#endif
 		if (JSON_ERROR(str)) {
-			fprintf(stderr, "Cannot fetch pattern object, item %d from table %s.\n", i, table);
+			fprintf(stderr, "Cannot fetch pattern string, item %d from table %s.\n", i, table);
 			exit(EXIT_FAILURE);
 		}
 		patterns[i].pattern = strdup(str);
-- 
2.0.0.rc0




More information about the fwts-devel mailing list