From 3dfd663c98d1f7586e95ce5d82a92c9059ab46c1 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 21 Dec 2019 07:41:14 -0500 Subject: Clean up mboxrd code --- imbox.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'imbox.c') diff --git a/imbox.c b/imbox.c index 22bb36a..5ab23c1 100644 --- a/imbox.c +++ b/imbox.c @@ -55,35 +55,34 @@ static void printLines(const char *lines) { } static void mboxrd(const char *headers, const char *body) { - static regex_t fromRegex; - compile(&fromRegex, "^From: .*<([^>]+)>"); +#define MATCH(str, match) \ + (int)((match).rm_eo - (match).rm_so), &(str)[(match).rm_so] + static regex_t fromRegex, dateRegex; + compile(&fromRegex, "^From: .*<([^>]+)>"); regmatch_t from[2]; int error = regexec(&fromRegex, headers, 2, from, 0); if (error) errx(EX_DATAERR, "missing From header"); - printf( - "From %.*s ", - (int)(from[1].rm_eo - from[1].rm_so), &headers[from[1].rm_so] - ); + printf("From %.*s ", MATCH(headers, from[1])); - static regex_t dateRegex; + // Day, Date Month Year Time -> Day Month Date Time Year compile(&dateRegex, "^Date: (...), (..) (...) (....) (.{8})"); - regmatch_t date[6]; error = regexec(&dateRegex, headers, 6, date, 0); if (error) errx(EX_DATAERR, "missing Date header"); printf( "%.*s %.*s %.*s %.*s %.*s\n", - (int)(date[1].rm_eo - date[1].rm_so), &headers[date[1].rm_so], - (int)(date[3].rm_eo - date[3].rm_so), &headers[date[3].rm_so], - (int)(date[2].rm_eo - date[2].rm_so), &headers[date[2].rm_so], - (int)(date[5].rm_eo - date[5].rm_so), &headers[date[5].rm_so], - (int)(date[4].rm_eo - date[4].rm_so), &headers[date[4].rm_so] + MATCH(headers, date[1]), + MATCH(headers, date[3]), + MATCH(headers, date[2]), + MATCH(headers, date[5]), + MATCH(headers, date[4]) ); printLines(headers); printLines(body); printf("\n"); +#undef MATCH } static bool verbose; -- cgit 1.4.1