fsck: provide a function to parse fsck message IDs

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>

Johannes Schindelin committed Jun 22, 2015 at 17:25 UTC f417eed8cde2e7def06091977f888c296cbeae00
1 file changed +33 -2
fsck.c
+33 -2
@@ -63,15 +63,46 @@ enum fsck_msg_id {
63 };
64 #undef MSG_ID
65
66 -#define MSG_ID(id, msg_type) { FSCK_##msg_type },
66 +#define STR(x) #x
67 +#define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type },
68 static struct {
69 + const char *id_string;
70 + const char *downcased;
71 int msg_type;
72 } msg_id_info[FSCK_MSG_MAX + 1] = {
73 FOREACH_MSG_ID(MSG_ID)
71 - { -1 }
74 + { NULL, NULL, -1 }
75 };
76 #undef MSG_ID
77
78 +static int parse_msg_id(const char *text)
79 +{
80 + int i;
81 +
82 + if (!msg_id_info[0].downcased) {
83 + /* convert id_string to lower case, without underscores. */
84 + for (i = 0; i < FSCK_MSG_MAX; i++) {
85 + const char *p = msg_id_info[i].id_string;
86 + int len = strlen(p);
87 + char *q = xmalloc(len);
88 +
89 + msg_id_info[i].downcased = q;
90 + while (*p)
91 + if (*p == '_')
92 + p++;
93 + else
94 + *(q)++ = tolower(*(p)++);
95 + *q = '\0';
96 + }
97 + }
98 +
99 + for (i = 0; i < FSCK_MSG_MAX; i++)
100 + if (!strcmp(text, msg_id_info[i].downcased))
101 + return i;
102 +
103 + return -1;
104 +}
105 +
106 static int fsck_msg_type(enum fsck_msg_id msg_id,
107 struct fsck_options *options)
108 {