[PATCH v5 03/19] fsck: Provide a function to parse fsck message IDs
From: Johannes Schindelin <hidden>
Date: 2016-06-15 23:05:23
Subsystem:
the rest · Maintainer:
Linus Torvalds
These functions will be used in the next commits to allow the user to ask fsck to handle specific problems differently, e.g. demoting certain errors to warnings. The upcoming `fsck_set_msg_types()` function has to handle partial strings because we would like to be able to parse, say, 'missingemail=warn,missingtaggerentry=warn' command line parameters (which will be passed by receive-pack to index-pack and unpack-objects). To make the parsing robust, we generate strings from the enum keys, and using these keys, we match up strings without dashes case-insensitively to the corresponding enum values. Signed-off-by: Johannes Schindelin <redacted> --- fsck.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-)
diff --git a/fsck.c b/fsck.c
index ed0bfc3..4595c7f 100644
--- a/fsck.c
+++ b/fsck.c@@ -63,15 +63,41 @@ enum fsck_msg_id { }; #undef MSG_ID -#define MSG_ID(id, msg_type) { FSCK_##msg_type }, +#define STR(x) #x +#define MSG_ID(id, msg_type) { STR(id), FSCK_##msg_type }, static struct { + const char *id_string; int msg_type; } msg_id_info[FSCK_MSG_MAX + 1] = { FOREACH_MSG_ID(MSG_ID) - { -1 } + { NULL, -1 } }; #undef MSG_ID +static int parse_msg_id(const char *text, int len) +{ + int i, j; + + if (len < 0) + len = strlen(text); + + for (i = 0; i < FSCK_MSG_MAX; i++) { + const char *key = msg_id_info[i].id_string; + /* match id_string case-insensitively, without underscores. */ + for (j = 0; j < len; j++) { + char c = *(key++); + if (c == '_') + c = *(key++); + if (toupper(text[j]) != c) + break; + } + if (j == len && !*key) + return i; + } + + return -1; +} + static int fsck_msg_type(enum fsck_msg_id msg_id, struct fsck_options *options) {
--
2.3.1.windows.1.9.g8c01ab4