commit 836a2edc972102f05b1d6d7700b3ed8a8afcc5b8 from: Omar Polo date: Fri Jun 14 10:44:00 2024 UTC mailcap: extract the FILE parsing routine and export it so that we can write tests for this module. commit - dcdea5d144e2eb2dc27299e73c0713a2d03e44cd commit + 836a2edc972102f05b1d6d7700b3ed8a8afcc5b8 blob - 5aca129f239d116940caa8c04508e86a789c5af6 blob + e0836997564c3b42ef6c0e2113046348b23a353a --- mailcap.c +++ mailcap.c @@ -32,9 +32,6 @@ #include "mailcap.h" #define DEFAULT_MAILCAP_ENTRY "*/*; xdg-open %s ; needsterminal" - -#define MAILCAP_NEEDSTERMINAL 0x1 -#define MAILCAP_COPIOUSOUTPUT 0x2 #define str_unappend(ch) if (sps.off > 0 && (ch) != EOF) { sps.off--; } @@ -411,14 +408,29 @@ mailcap_by_mimetype(const char *mt) void init_mailcap(void) { - FILE *f = NULL; + FILE *f; + char *copy; + + if ((f = find_mailcap_file()) != NULL) { + mailcap_parse(f); + fclose(f); + } + + if ((copy = strdup(DEFAULT_MAILCAP_ENTRY)) == NULL) + errx(1, "strdup"); + + /* Our own entry won't error. */ + (void)parse_mailcap_line(copy); + free(copy); +} + +void +mailcap_parse(FILE *f) +{ const char delims[3] = {'\\', '\\', '\0'}; char *buf, *copy; size_t line = 0; - if ((f = find_mailcap_file()) == NULL) - goto add_default; - while ((buf = fparseln(f, NULL, &line, delims, 0)) != NULL) { memset(&sps, 0, sizeof sps); copy = buf; @@ -436,15 +448,6 @@ init_mailcap(void) } free(copy); } - fclose(f); - -add_default: - if ((copy = strdup(DEFAULT_MAILCAP_ENTRY)) == NULL) - errx(1, "strdup"); - - /* Our own entry won't error. */ - (void)parse_mailcap_line(copy); - free(copy); } struct mailcap * blob - 51e987db50d309bea75d1b0b472d9626b44d51ed blob + de52a7d3e8da2b96e5fc3b13d4ab111b85282edb --- mailcap.h +++ mailcap.h @@ -22,6 +22,9 @@ struct mailcap { char *cmd; char **cmd_argv; int cmd_argc; + +#define MAILCAP_NEEDSTERMINAL 0x1 +#define MAILCAP_COPIOUSOUTPUT 0x2 int flags; TAILQ_ENTRY(mailcap) mailcaps; @@ -32,6 +35,7 @@ extern TAILQ_HEAD(mailcaplist, mailcap) mailcaps; extern struct mailcaplist mailcaps; void init_mailcap(void); +void mailcap_parse(FILE *); struct mailcap *mailcap_cmd_from_mimetype(char *, char *); #endif