From 23436e27dfb5a82854d7fc762e48eea5b755aee5 Mon Sep 17 00:00:00 2001
From: Anthony Liguori <aliguori@us.ibm.com>
Date: Mon, 5 Mar 2012 17:43:41 -0500
Subject: [PATCH 14/98] json-parser: propagate error from parser

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
(cherry picked from commit ef749d07e717ca38b7d8b525c81d6b04db5f16ed)

Conflicts:

	Makefile
	Makefile.objs
Signed-off-by: Jeff Cody <jcody@redhat.com>
Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 Makefile      |   14 +++++++-------
 Makefile.objs |    4 ++--
 json-parser.c |   19 ++++++++++++++++---
 json-parser.h |    2 ++
 qerror.h      |    3 +++
 5 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/Makefile b/Makefile
index 1e62e13..6daa0ee 100644
--- a/Makefile
+++ b/Makefile
@@ -167,7 +167,7 @@ trace-dtrace.o: trace-dtrace.dtrace $(GENERATED_HEADERS)
 qemu-img.o: qemu-img-cmds.h
 qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o: $(GENERATED_HEADERS)
 
-TOOLS_OBJ=qemu-tool.o $(shared-obj-y) $(trace-obj-y)
+TOOLS_OBJ=qemu-tool.o qerror.o $(shared-obj-y) $(trace-obj-y)
 
 qemu-img$(EXESUF): qemu-img.o $(TOOLS_OBJ)
 
@@ -178,12 +178,12 @@ qemu-io$(EXESUF): qemu-io.o cmd.o $(TOOLS_OBJ)
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@,"  GEN   $@")
 
-check-qint: check-qint.o qint.o qemu-malloc.o
-check-qstring: check-qstring.o qstring.o qemu-malloc.o
-check-qdict: check-qdict.o qdict.o qfloat.o qint.o qstring.o qbool.o qemu-malloc.o qlist.o
-check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o
-check-qfloat: check-qfloat.o qfloat.o qemu-malloc.o
-check-qjson: check-qjson.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o qjson.o json-streamer.o json-lexer.o json-parser.o qemu-malloc.o
+check-qint: check-qint.o qint.o qemu-malloc.o qemu-tool.o
+check-qstring: check-qstring.o qstring.o qemu-malloc.o qemu-tool.o
+check-qdict: check-qdict.o qdict.o qfloat.o qint.o qstring.o qbool.o qemu-malloc.o qlist.o qemu-tool.o
+check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o qemu-tool.o
+check-qfloat: check-qfloat.o qfloat.o qemu-malloc.o qemu-tool.o
+check-qjson: check-qjson.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o qjson.o json-streamer.o json-lexer.o json-parser.o qemu-malloc.o error.o qerror.o qemu-error.o qemu-tool.o
 
 QEMULIBS=libhw32 libhw64 libuser
 
diff --git a/Makefile.objs b/Makefile.objs
index 6d6244c..4ff5ba3 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -2,7 +2,7 @@
 # QObject
 qobject-obj-y = qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o
 qobject-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o
-qobject-obj-y += qerror.o
+qobject-obj-y += qerror.o error.o
 
 #######################################################################
 # coroutines
@@ -63,7 +63,7 @@ shared-obj-y = qemu-error.o $(block-obj-y) $(qobject-obj-y)
 
 common-obj-y = $(shared-obj-y)
 common-obj-y += qemu-thread.o
-common-obj-y += blockdev.o error.o
+common-obj-y += blockdev.o
 common-obj-y += $(net-obj-y)
 common-obj-y += readline.o console.o cursor.o
 
diff --git a/json-parser.c b/json-parser.c
index f48f109..6665a13 100644
--- a/json-parser.c
+++ b/json-parser.c
@@ -22,9 +22,11 @@
 #include "qbool.h"
 #include "json-parser.h"
 #include "json-lexer.h"
+#include "qerror.h"
 
 typedef struct JSONParserContext
 {
+    Error *err;
 } JSONParserContext;
 
 #define BUG_ON(cond) assert(!(cond))
@@ -94,11 +96,15 @@ static int token_is_escape(QObject *obj, const char *value)
 static void parse_error(JSONParserContext *ctxt, QObject *token, const char *msg, ...)
 {
     va_list ap;
+    char message[1024];
     va_start(ap, msg);
-    fprintf(stderr, "parse error: ");
-    vfprintf(stderr, msg, ap);
-    fprintf(stderr, "\n");
+    vsnprintf(message, sizeof(message), msg, ap);
     va_end(ap);
+    if (ctxt->err) {
+        error_free(ctxt->err);
+        ctxt->err = NULL;
+    }
+    error_set(&ctxt->err, QERR_JSON_PARSE_ERROR, message);
 }
 
 /**
@@ -566,6 +572,11 @@ static QObject *parse_value(JSONParserContext *ctxt, QList **tokens, va_list *ap
 
 QObject *json_parser_parse(QList *tokens, va_list *ap)
 {
+    return json_parser_parse_err(tokens, ap, NULL);
+}
+
+QObject *json_parser_parse_err(QList *tokens, va_list *ap, Error **errp)
+{
     JSONParserContext ctxt = {};
     QList *working = qlist_copy(tokens);
     QObject *result;
@@ -574,5 +585,7 @@ QObject *json_parser_parse(QList *tokens, va_list *ap)
 
     QDECREF(working);
 
+    error_propagate(errp, ctxt.err);
+
     return result;
 }
diff --git a/json-parser.h b/json-parser.h
index 97f43f6..8f2b5ec 100644
--- a/json-parser.h
+++ b/json-parser.h
@@ -16,7 +16,9 @@
 
 #include "qemu-common.h"
 #include "qlist.h"
+#include "error.h"
 
 QObject *json_parser_parse(QList *tokens, va_list *ap);
+QObject *json_parser_parse_err(QList *tokens, va_list *ap, Error **errp);
 
 #endif
diff --git a/qerror.h b/qerror.h
index 3a9c000..42e7207 100644
--- a/qerror.h
+++ b/qerror.h
@@ -126,6 +126,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_JSON_PARSING \
     "{ 'class': 'JSONParsing', 'data': {} }"
 
+#define QERR_JSON_PARSE_ERROR \
+    "{ 'class': 'JSONParseError', 'data': { 'message': %s } }"
+
 #define QERR_KVM_MISSING_CAP \
     "{ 'class': 'KVMMissingCap', 'data': { 'capability': %s, 'feature': %s } }"
 
-- 
1.7.7.6

