From 9fd75d76cc383eaa4c01e57293bfecbb76ee3d8a Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 21 Jun 2010 18:08:53 +0200 Subject: [PATCH] libmount: add mnt_tab_set_parser_errcb() Signed-off-by: Karel Zak --- shlibs/mount/src/mount.h.in | 3 ++- shlibs/mount/src/mount.sym | 1 + shlibs/mount/src/mountP.h | 3 +++ shlibs/mount/src/tab_parse.c | 39 +++++++++++++++++++++++++++--------- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index cd9159cc..b1d66074 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -266,7 +266,8 @@ extern int mnt_fs_print_debug(mnt_fs *ent, FILE *file); /* tab-parse.c */ extern mnt_tab *mnt_new_tab_from_file(const char *filename); extern int mnt_tab_parse_file(mnt_tab *tb); -extern char *mnt_tab_strerror(mnt_tab *tb, char *buf, size_t buflen); +extern int mnt_tab_set_parser_errcb(mnt_tab *tb, + int (*cb)(mnt_tab *tb, const char *filename, int line, int flag)); extern int mnt_tab_get_nerrs(mnt_tab *tb); /* tab.c */ diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index 819be0f1..f6035e4b 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -124,6 +124,7 @@ global: mnt_tab_parse_file; mnt_tab_remove_fs; mnt_tab_set_cache; + mnt_tab_set_parser_errcb; mnt_tab_set_iter; mnt_tab_strerror; mnt_tab_update_file; diff --git a/shlibs/mount/src/mountP.h b/shlibs/mount/src/mountP.h index 4a99bb4e..63997544 100644 --- a/shlibs/mount/src/mountP.h +++ b/shlibs/mount/src/mountP.h @@ -175,6 +175,9 @@ struct _mnt_tab { mnt_cache *cache; /* canonicalized paths/tags cache */ + int (*errcb)(mnt_tab *tb, const char *filename, + int line, int flag); + struct list_head ents; /* list of entries (mentry) */ }; diff --git a/shlibs/mount/src/tab_parse.c b/shlibs/mount/src/tab_parse.c index 680e1fc1..0c4059da 100644 --- a/shlibs/mount/src/tab_parse.c +++ b/shlibs/mount/src/tab_parse.c @@ -354,6 +354,9 @@ static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs) return 0; err: + if (tb->errcb) + return tb->errcb(tb, NULL, tb->nlines, 0); + /* we don't report parse errors to caller; caller has to check * errors by mnt_tab_get_nerrs() or internaly by MNT_ENTRY_ERR flag */ @@ -374,22 +377,19 @@ err: * int rc; * * rc = mnt_tab_parse_file(tb); - * if (rc) { - * if (mnt_tab_get_nerrs(tb)) { / * parse error * / - * mnt_tab_strerror(tb, buf, sizeof(buf)); - * fprintf(stderr, "%s: %s\n", progname, buf); - * } else - * perror(mnt_tab_get_name(tb)); / * system error * / - * } else + * if (rc) + * perror(mnt_tab_get_name(tb)); / * system error * / + * else * mnt_fprintf_tab(tb, stdout, NULL); * * mnt_free_tab(tb); * * * - * Returns: 0 on success and -1 in case of error. The parse errors is possible - * to detect by mnt_tab_get_nerrs() and error message is possible to create by - * mnt_tab_strerror(). + * The libmount parser ignores broken (with syntax error) lines, these lines are + * reported to caller by errcb() function (see mnt_tab_set_parser_errcb()). + * + * Returns: 0 on success, -1 in case of error. */ int mnt_tab_parse_file(mnt_tab *tb) { @@ -457,6 +457,25 @@ mnt_tab *mnt_new_tab_from_file(const char *filename) return tb; } +/** + * mnt_tab_set_parser_errcb: + * @tab: pointer to table + * @cb: pointer to callback function + * + * The error callback function is called by table parser (mnt_tab_parse_file()) + * in case of sytax error. If the callback function does not return zero then + * parsing is aborted. + * + * Returns: 0 on success or -1 in case of error. + */ +int mnt_tab_set_parser_errcb(mnt_tab *tb, + int (*cb)(mnt_tab *tb, const char *filename, int line, int flag)) +{ + assert(tb); + tb->errcb = cb; + return 0; +} + /** * mnt_tab_get_nerrs: * @tb: pointer to table -- 2.39.5