From a2bae081b6de82a3c3cb889328a5ca02735f5904 Mon Sep 17 00:00:00 2001 From: Michael Schroeder Date: Thu, 22 Nov 2018 12:53:20 +0100 Subject: [PATCH] Get rid of the xmlparser's error callback It's much simpler to just return an error from the parse call. --- ext/repo_appdata.c | 22 +++++++--------------- ext/repo_arch.c | 5 ++--- ext/repo_comps.c | 13 +++---------- ext/repo_content.c | 3 +-- ext/repo_cudf.c | 15 +++------------ ext/repo_deb.c | 4 ++-- ext/repo_deltainfoxml.c | 12 +++--------- ext/repo_helix.c | 14 +++----------- ext/repo_mdk.c | 15 ++++----------- ext/repo_products.c | 21 ++++++--------------- ext/repo_repomdxml.c | 13 +++---------- ext/repo_rpmdb.c | 7 +++---- ext/repo_rpmmd.c | 12 +++--------- ext/repo_updateinfoxml.c | 10 ++-------- ext/repo_zyppdb.c | 20 ++++++-------------- ext/solv_xmlparser.c | 39 +++++++++++++++++++++++++-------------- ext/solv_xmlparser.h | 12 ++++++++---- 17 files changed, 84 insertions(+), 153 deletions(-) diff --git a/ext/repo_appdata.c b/ext/repo_appdata.c index 03098e23..31749686 100644 --- a/ext/repo_appdata.c +++ b/ext/repo_appdata.c @@ -420,19 +420,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pool_debug(pd->pool, SOLV_ERROR, "repo_appdata: %s at line %u:%u\n", errstr, line, column); - pd->ret = -1; - if (pd->solvable) - { - repo_free_solvable(pd->repo, pd->solvable - pd->pool->solvables, 1); - pd->solvable = 0; - } -} - static int repo_add_appdata_fn(Repo *repo, FILE *fp, int flags, const char *filename, Queue *owners) { @@ -448,8 +435,13 @@ repo_add_appdata_fn(Repo *repo, FILE *fp, int flags, const char *filename, Queue pd.filename = filename; pd.owners = owners; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); - solv_xmlparser_parse(&pd.xmlp, fp); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + { + pool_debug(pd.pool, SOLV_ERROR, "repo_appdata: %s at line %u:%u\n", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); + pd.ret = -1; + pd.solvable = solvable_free(pd.solvable, 1); + } solv_xmlparser_free(&pd.xmlp); solv_free(pd.desktop_file); diff --git a/ext/repo_arch.c b/ext/repo_arch.c index a0c45ce4..698d5063 100644 --- a/ext/repo_arch.c +++ b/ext/repo_arch.c @@ -444,8 +444,7 @@ repo_add_arch_pkg(Repo *repo, const char *fn, int flags) if (s && !s->name) { pool_error(pool, -1, "%s: package has no name", fn); - repo_free_solvable(repo, s - pool->solvables, 1); - s = 0; + s = solvable_free(s, 1); } if (s) { @@ -728,7 +727,7 @@ finishsolvable(Repo *repo, Solvable *s) return; if (!s->name) { - repo_free_solvable(repo, s - pool->solvables, 1); + solvable_free(s, 1); return; } if (!s->arch) diff --git a/ext/repo_comps.c b/ext/repo_comps.c index 255ecb16..9400e1ea 100644 --- a/ext/repo_comps.c +++ b/ext/repo_comps.c @@ -206,14 +206,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pool_debug(pd->pool, SOLV_ERROR, "repo_comps: %s at line %u:%u\n", errstr, line, column); -} - - int repo_add_comps(Repo *repo, FILE *fp, int flags) { @@ -226,8 +218,9 @@ repo_add_comps(Repo *repo, FILE *fp, int flags) pd.repo = repo; pd.pool = repo->pool; pd.data = data; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); - solv_xmlparser_parse(&pd.xmlp, fp); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + pool_debug(pd.pool, SOLV_ERROR, "repo_comps: %s at line %u:%u\n", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); solv_xmlparser_free(&pd.xmlp); join_freemem(&pd.jd); diff --git a/ext/repo_content.c b/ext/repo_content.c index 68af2d5f..f361900e 100644 --- a/ext/repo_content.c +++ b/ext/repo_content.c @@ -517,8 +517,7 @@ repo_add_content(Repo *repo, FILE *fp, int flags) if (s && !s->name) { pool_debug(pool, SOLV_ERROR, "repo_content: 'content' incomplete, no product solvable created!\n"); - repo_free_solvable(repo, s - pool->solvables, 1); - s = 0; + s = solvable_free(s, 1); } if (s) { diff --git a/ext/repo_cudf.c b/ext/repo_cudf.c index 14ddcc94..64bb86f8 100644 --- a/ext/repo_cudf.c +++ b/ext/repo_cudf.c @@ -224,10 +224,7 @@ repo_add_cudf(Repo *repo, Repo *installedrepo, FILE *fp, Queue *job, int flags) if (!*buf) { if (s && !repo && !isinstalled) - { - repo_free_solvable(repo, s - pool->solvables, 1); - s = 0; - } + s = solvable_free(s, 1); if (s) finishpackage(pool, s, keep, job); s = 0; @@ -323,10 +320,7 @@ repo_add_cudf(Repo *repo, Repo *installedrepo, FILE *fp, Queue *job, int flags) { isinstalled = 1; if (!installedrepo) - { - repo_free_solvable(repo, s - pool->solvables, 1); - s = 0; - } + s = solvable_free(s, 1); else if (s->repo != installedrepo) { copysolvabledata(pool, s, installedrepo); @@ -371,10 +365,7 @@ repo_add_cudf(Repo *repo, Repo *installedrepo, FILE *fp, Queue *job, int flags) } } if (s && !repo && !isinstalled) - { - repo_free_solvable(repo, s - pool->solvables, 1); - s = 0; - } + s = solvable_free(s, 1); if (s) finishpackage(pool, s, keep, job); solv_free(buf); diff --git a/ext/repo_deb.c b/ext/repo_deb.c index 6af7f105..5dd79f4f 100644 --- a/ext/repo_deb.c +++ b/ext/repo_deb.c @@ -499,7 +499,7 @@ repo_add_debpackages(Repo *repo, FILE *fp, int flags) s = pool_id2solvable(pool, repo_add_solvable(repo)); control2solvable(s, data, buf); if (!s->name) - repo_free_solvable(repo, s - pool->solvables, 1); + s = solvable_free(s, 1); if (l > ll) memmove(buf, p + 1, l - ll); l -= ll; @@ -511,7 +511,7 @@ repo_add_debpackages(Repo *repo, FILE *fp, int flags) s = pool_id2solvable(pool, repo_add_solvable(repo)); control2solvable(s, data, buf); if (!s->name) - repo_free_solvable(repo, s - pool->solvables, 1); + s = solvable_free(s, 1); } solv_free(buf); if (!(flags & REPO_NO_INTERNALIZE)) diff --git a/ext/repo_deltainfoxml.c b/ext/repo_deltainfoxml.c index 4eb340f4..ad315da1 100644 --- a/ext/repo_deltainfoxml.c +++ b/ext/repo_deltainfoxml.c @@ -306,13 +306,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pd->ret = pool_error(pd->pool, -1, "repo_deltainfoxml: %s at line %u:%u", errstr, line, column); -} - int repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags) { @@ -327,8 +320,9 @@ repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags) pd.pool = pool; pd.repo = repo; pd.data = data; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); - solv_xmlparser_parse(&pd.xmlp, fp); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + pd.ret = pool_error(pd.pool, -1, "repo_deltainfoxml: %s at line %u:%u", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); solv_xmlparser_free(&pd.xmlp); /* now commit all handles */ diff --git a/ext/repo_helix.c b/ext/repo_helix.c index 3c2c6d1d..37359bbf 100644 --- a/ext/repo_helix.c +++ b/ext/repo_helix.c @@ -674,14 +674,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pd->ret = pool_error(pd->pool, -1, "%s at line %u", errstr, line); -} - - /*-------------------------------------------------------------------*/ /* @@ -706,14 +698,14 @@ repo_add_helix(Repo *repo, FILE *fp, int flags) pd.pool = pool; pd.repo = repo; pd.data = data; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); pd.evrspace = (char *)solv_malloc(256); pd.aevrspace = 256; pd.levrspace = 1; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); - solv_xmlparser_parse(&pd.xmlp, fp); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + pd.ret = pool_error(pd.pool, -1, "repo_helix: %s at line %u", pd.xmlp.errstr, pd.xmlp.line); solv_xmlparser_free(&pd.xmlp); solv_free(pd.evrspace); diff --git a/ext/repo_mdk.c b/ext/repo_mdk.c index 418bc61d..4d3e102b 100644 --- a/ext/repo_mdk.c +++ b/ext/repo_mdk.c @@ -230,7 +230,7 @@ repo_add_mdk(Repo *repo, FILE *fp, int flags) if (s) { pool_debug(pool, SOLV_ERROR, "unclosed package at EOF\n"); - repo_free_solvable(s->repo, s - pool->solvables, 1); + s = solvable_free(s, 1); } solv_free(buf); if (!(flags & REPO_NO_INTERNALIZE)) @@ -437,14 +437,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pool_debug(pd->pool, SOLV_ERROR, "%s at line %u:%u\n", errstr, line, column); -} - - int repo_add_mdk_info(Repo *repo, FILE *fp, int flags) { @@ -463,9 +455,10 @@ repo_add_mdk_info(Repo *repo, FILE *fp, int flags) pd.repo = repo; pd.pool = repo->pool; pd.data = data; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); pd.joinhash = joinhash_init(repo, &pd.joinhashmask); - solv_xmlparser_parse(&pd.xmlp, fp); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + pool_debug(pd.pool, SOLV_ERROR, "%s at line %u:%u\n", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); solv_xmlparser_free(&pd.xmlp); solv_free(pd.joinhash); if (!(flags & REPO_NO_INTERNALIZE)) diff --git a/ext/repo_products.c b/ext/repo_products.c index 154a909f..edf15bf7 100644 --- a/ext/repo_products.c +++ b/ext/repo_products.c @@ -291,19 +291,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pool_debug(pd->pool, SOLV_ERROR, "%s: %s at line %u:%u\n", pd->filename, errstr, line, column); - if (pd->solvable) - { - repo_free_solvable(pd->repo, pd->solvable - pd->pool->solvables, 1); - pd->solvable = 0; - } -} - - int repo_add_code11_products(Repo *repo, const char *dirpath, int flags) { @@ -318,7 +305,7 @@ repo_add_code11_products(Repo *repo, const char *dirpath, int flags) pd.pool = repo->pool; pd.data = data; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); if (flags & REPO_USE_ROOTDIR) dirpath = pool_prepend_rootdir(repo->pool, dirpath); @@ -358,7 +345,11 @@ repo_add_code11_products(Repo *repo, const char *dirpath, int flags) pd.ctime = (unsigned int)st.st_ctime; pd.filename = fullpath; pd.basename = entry->d_name; - solv_xmlparser_parse(&pd.xmlp, fp); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + { + pool_debug(pd.pool, SOLV_ERROR, "%s: %s at line %u:%u\n", pd.filename, pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); + pd.solvable = solvable_free(pd.solvable, 1); + } fclose(fp); } closedir(dir); diff --git a/ext/repo_repomdxml.c b/ext/repo_repomdxml.c index 760d481f..fd46272b 100644 --- a/ext/repo_repomdxml.c +++ b/ext/repo_repomdxml.c @@ -329,13 +329,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pd->ret = pool_error(pd->pool, -1, "repo_repomdxml: %s at line %u:%u", errstr, line, column); -} - int repo_add_repomdxml(Repo *repo, FILE *fp, int flags) { @@ -350,9 +343,9 @@ repo_add_repomdxml(Repo *repo, FILE *fp, int flags) pd.pool = pool; pd.repo = repo; pd.data = data; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); - - solv_xmlparser_parse(&pd.xmlp, fp); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + pd.ret = pool_error(pd.pool, -1, "repo_repomdxml: %s at line %u:%u", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); solv_xmlparser_free(&pd.xmlp); if (!(flags & REPO_NO_INTERNALIZE)) diff --git a/ext/repo_rpmdb.c b/ext/repo_rpmdb.c index a2d518f0..9acb4006 100644 --- a/ext/repo_rpmdb.c +++ b/ext/repo_rpmdb.c @@ -1661,9 +1661,8 @@ repo_add_rpmdb(Repo *repo, Repo *ref, int flags) if (s) { /* oops, could not reuse. free it instead */ - repo_free_solvable(repo, s - pool->solvables, 1); + s = solvable_free(s, 1); solvend--; - s = 0; } /* now sort all solvables in the new solvstart..solvend block */ if (solvend - solvstart > 1) @@ -2044,7 +2043,7 @@ repo_add_rpm(Repo *repo, const char *rpm, int flags) s = pool_id2solvable(pool, repo_add_solvable(repo)); if (!rpmhead2solv(pool, repo, data, s, state.rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID))) { - repo_free_solvable(repo, s - pool->solvables, 1); + s = solvable_free(s, 1); solv_chksum_free(chksumh, 0); headfree(state.rpmhead); return 0; @@ -2096,7 +2095,7 @@ repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags) s = pool_id2solvable(pool, repo_add_solvable(repo)); if (!rpmhead2solv(pool, repo, data, s, rpmhead, flags)) { - repo_free_solvable(repo, s - pool->solvables, 1); + s = solvable_free(s, 1); return 0; } if (!(flags & REPO_NO_INTERNALIZE)) diff --git a/ext/repo_rpmmd.c b/ext/repo_rpmmd.c index 9f49bd31..9bb50a05 100644 --- a/ext/repo_rpmmd.c +++ b/ext/repo_rpmmd.c @@ -1091,13 +1091,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pd->ret = pool_error(pd->pool, -1, "repo_rpmmd: %s at line %u:%u", errstr, line, column); -} - /*-----------------------------------------------*/ @@ -1135,8 +1128,9 @@ repo_add_rpmmd(Repo *repo, FILE *fp, const char *language, int flags) fill_cshash_from_repo(&pd); } - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); - solv_xmlparser_parse(&pd.xmlp, fp); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + pd.ret = pool_error(pool, -1, "repo_rpmmd: %s at line %u:%u", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); solv_xmlparser_free(&pd.xmlp); solv_free(pd.lastdirstr); diff --git a/ext/repo_updateinfoxml.c b/ext/repo_updateinfoxml.c index 7ba00625..b7c91043 100644 --- a/ext/repo_updateinfoxml.c +++ b/ext/repo_updateinfoxml.c @@ -427,13 +427,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pd->ret = pool_error(pd->pool, -1, "repo_updateinfoxml: %s at line %u:%u", errstr, line, column); -} - int repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags) { @@ -447,8 +440,9 @@ repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags) pd.pool = pool; pd.repo = repo; pd.data = data; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); solv_xmlparser_parse(&pd.xmlp, fp); + pd.ret = pool_error(pool, -1, "repo_updateinfoxml: %s at line %u:%u", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); solv_xmlparser_free(&pd.xmlp); join_freemem(&pd.jd); diff --git a/ext/repo_zyppdb.c b/ext/repo_zyppdb.c index d73a9002..f6e2bfa6 100644 --- a/ext/repo_zyppdb.c +++ b/ext/repo_zyppdb.c @@ -138,18 +138,6 @@ endElement(struct solv_xmlparser *xmlp, int state, char *content) } } -static void -errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) -{ - struct parsedata *pd = xmlp->userdata; - pool_debug(pd->pool, SOLV_ERROR, "repo_zyppdb: %s: %s at line %u:%u\n", pd->filename, errstr, line, column); - if (pd->solvable) - { - repo_free_solvable(pd->repo, pd->solvable - pd->pool->solvables, 1); - pd->solvable = 0; - } -} - /* * read all installed products @@ -172,7 +160,7 @@ repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags) pd.repo = repo; pd.pool = repo->pool; pd.data = data; - solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback); + solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement); if (flags & REPO_USE_ROOTDIR) dirpath = pool_prepend_rootdir(repo->pool, dirpath); @@ -190,7 +178,11 @@ repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags) continue; } pd.filename = entry->d_name; - solv_xmlparser_parse(&pd.xmlp, fp); + if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK) + { + pool_debug(pd.pool, SOLV_ERROR, "repo_zyppdb: %s: %s at line %u:%u\n", pd.filename, pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column); + pd.solvable = solvable_free(pd.solvable, 1); + } fclose(fp); } } diff --git a/ext/solv_xmlparser.c b/ext/solv_xmlparser.c index 170520ff..a23f4750 100644 --- a/ext/solv_xmlparser.c +++ b/ext/solv_xmlparser.c @@ -132,8 +132,7 @@ solv_xmlparser_init(struct solv_xmlparser *xmlp, struct solv_xmlparser_element *elements, void *userdata, void (*startelement)(struct solv_xmlparser *, int state, const char *name, const char **atts), - void (*endelement)(struct solv_xmlparser *, int state, char *content), - void (*errorhandler)(struct solv_xmlparser *, const char *errstr, unsigned int line, unsigned int column)) + void (*endelement)(struct solv_xmlparser *, int state, char *content)) { int i, nstates, nelements; struct solv_xmlparser_element *el; @@ -169,7 +168,6 @@ solv_xmlparser_init(struct solv_xmlparser *xmlp, xmlp->userdata = userdata; xmlp->startelement = startelement; xmlp->endelement = endelement; - xmlp->errorhandler = errorhandler; } void @@ -178,6 +176,16 @@ solv_xmlparser_free(struct solv_xmlparser *xmlp) xmlp->elementhelper = solv_free(xmlp->elementhelper); queue_free(&xmlp->elementq); xmlp->content = solv_free(xmlp->content); + xmlp->errstr = solv_free(xmlp->errstr); +} + +static void +set_error(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column) +{ + solv_free(xmlp->errstr); + xmlp->errstr = solv_strdup(errstr); + xmlp->line = line; + xmlp->column = column; } #ifdef WITH_LIBXML2 @@ -197,7 +205,8 @@ free_parser(struct solv_xmlparser *xmlp) xmlp->parser = 0; } -static xmlParserCtxtPtr create_parser_ctx(struct solv_xmlparser *xmlp, char *buf, int l) +static xmlParserCtxtPtr +create_parser_ctx(struct solv_xmlparser *xmlp, char *buf, int l) { xmlSAXHandler sax; memset(&sax, 0, sizeof(sax)); @@ -216,7 +225,7 @@ parse_block(struct solv_xmlparser *xmlp, char *buf, int l) xmlp->parser = create_parser_ctx(xmlp, buf, l2); if (!xmlp->parser) { - xmlp->errorhandler(xmlp, "could not create parser", 0, 0); + set_error(xmlp, "could not create parser", 0, 0); return 0; } buf += l2; @@ -227,7 +236,7 @@ parse_block(struct solv_xmlparser *xmlp, char *buf, int l) if (xmlParseChunk(xmlp->parser, buf, l, l == 0 ? 1 : 0)) { xmlErrorPtr err = xmlCtxtGetLastError(xmlp->parser); - xmlp->errorhandler(xmlp, err->message, err->line, err->int2); + set_error(xmlp, err->message, err->line, err->int2); return 0; } return 1; @@ -265,9 +274,7 @@ parse_block(struct solv_xmlparser *xmlp, char *buf, int l) { if (XML_Parse(xmlp->parser, buf, l, l == 0) == XML_STATUS_ERROR) { - unsigned int line = XML_GetCurrentLineNumber(xmlp->parser); - unsigned int column = XML_GetCurrentColumnNumber(xmlp->parser); - xmlp->errorhandler(xmlp, XML_ErrorString(XML_GetErrorCode(xmlp->parser)), line, column); + set_error(xmlp, XML_ErrorString(XML_GetErrorCode(xmlp->parser)), XML_GetCurrentLineNumber(xmlp->parser), XML_GetCurrentColumnNumber(xmlp->parser)); return 0; } return 1; @@ -281,11 +288,11 @@ solv_xmlparser_lineno(struct solv_xmlparser *xmlp) #endif -void +int solv_xmlparser_parse(struct solv_xmlparser *xmlp, FILE *fp) { char buf[8192]; - int l; + int l, ret = SOLV_XMLPARSER_OK; xmlp->state = 0; xmlp->unknowncnt = 0; @@ -295,16 +302,20 @@ solv_xmlparser_parse(struct solv_xmlparser *xmlp, FILE *fp) if (!create_parser(xmlp)) { - xmlp->errorhandler(xmlp, "could not create xml parser", 0, 0); - return; + set_error(xmlp, "could not create parser", 0, 0); + return SOLV_XMLPARSER_ERROR; } for (;;) { l = fread(buf, 1, sizeof(buf), fp); if (!parse_block(xmlp, buf, l) || !l) - break; + { + ret = SOLV_XMLPARSER_ERROR; + break; + } } free_parser(xmlp); + return ret; } char * diff --git a/ext/solv_xmlparser.h b/ext/solv_xmlparser.h index 9fb342f4..ced0571f 100644 --- a/ext/solv_xmlparser.h +++ b/ext/solv_xmlparser.h @@ -8,6 +8,9 @@ struct solv_xmlparser_element { struct solv_xmlparser { void *userdata; + char *errstr; + unsigned int line; + unsigned int column; int state; int docontent; @@ -24,12 +27,14 @@ struct solv_xmlparser { void (*startelement)(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts); void (*endelement)(struct solv_xmlparser *xmlp, int state, char *content); - void (*errorhandler)(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column); Id *elementhelper; void *parser; }; +#define SOLV_XMLPARSER_OK 0 +#define SOLV_XMLPARSER_ERROR -1 + static inline const char * solv_xmlparser_find_attr(const char *txt, const char **atts) { @@ -41,11 +46,10 @@ solv_xmlparser_find_attr(const char *txt, const char **atts) extern void solv_xmlparser_init(struct solv_xmlparser *xmlp, struct solv_xmlparser_element *elements, void *userdata, void (*startelement)(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts), - void (*endelement)(struct solv_xmlparser *xmlp, int state, char *content), - void (*errorhandler)(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column)); + void (*endelement)(struct solv_xmlparser *xmlp, int state, char *content)); extern void solv_xmlparser_free(struct solv_xmlparser *xmlp); -extern void solv_xmlparser_parse(struct solv_xmlparser *xmlp, FILE *fp); +extern int solv_xmlparser_parse(struct solv_xmlparser *xmlp, FILE *fp); unsigned int solv_xmlparser_lineno(struct solv_xmlparser *xmlp); char *solv_xmlparser_contentspace(struct solv_xmlparser *xmlp, int l); -- 2.47.2