fsck: provide a function to parse fsck message IDs
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 22 Jun 2015 15:25:14 +0000 (17:25 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Jun 2015 19:53:25 +0000 (12:53 -0700)
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 <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
fsck.c
diff --git a/fsck.c b/fsck.c
index ab246185e8f0d80e218b61fd88b74469cbb3f9ea..30c1a19aa583a4093fa6b9e2d2f6a6a7842e0b00 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -63,15 +63,46 @@ 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), NULL, FSCK_##msg_type },
 static struct {
+       const char *id_string;
+       const char *downcased;
        int msg_type;
 } msg_id_info[FSCK_MSG_MAX + 1] = {
        FOREACH_MSG_ID(MSG_ID)
-       { -1 }
+       { NULL, NULL, -1 }
 };
 #undef MSG_ID
 
+static int parse_msg_id(const char *text)
+{
+       int i;
+
+       if (!msg_id_info[0].downcased) {
+               /* convert id_string to lower case, without underscores. */
+               for (i = 0; i < FSCK_MSG_MAX; i++) {
+                       const char *p = msg_id_info[i].id_string;
+                       int len = strlen(p);
+                       char *q = xmalloc(len);
+
+                       msg_id_info[i].downcased = q;
+                       while (*p)
+                               if (*p == '_')
+                                       p++;
+                               else
+                                       *(q)++ = tolower(*(p)++);
+                       *q = '\0';
+               }
+       }
+
+       for (i = 0; i < FSCK_MSG_MAX; i++)
+               if (!strcmp(text, msg_id_info[i].downcased))
+                       return i;
+
+       return -1;
+}
+
 static int fsck_msg_type(enum fsck_msg_id msg_id,
        struct fsck_options *options)
 {