Skip to content

Commit f417eed

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c99ba49 commit f417eed

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

fsck.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,46 @@ enum fsck_msg_id {
6363
};
6464
#undef MSG_ID
6565

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 },
6768
static struct {
69+
const char *id_string;
70+
const char *downcased;
6871
int msg_type;
6972
} msg_id_info[FSCK_MSG_MAX + 1] = {
7073
FOREACH_MSG_ID(MSG_ID)
71-
{ -1 }
74+
{ NULL, NULL, -1 }
7275
};
7376
#undef MSG_ID
7477

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+
75106
static int fsck_msg_type(enum fsck_msg_id msg_id,
76107
struct fsck_options *options)
77108
{

0 commit comments

Comments
 (0)