From ff28b38daf32c81eab01238826b067432d93a8fb Mon Sep 17 00:00:00 2001 From: rbbrnc Date: Tue, 5 Apr 2011 16:01:04 +0200 Subject: [PATCH] Integrate test_header in test_view_mail and various coding style modifications --- src/network/test_pop3.c | 2 +- src/utils/.gitignore | 1 - src/utils/Makefile | 4 -- src/utils/rfc822.c | 111 +++++++++++++++++++++++---------------------- src/utils/test_header.c | 84 ---------------------------------- src/utils/test_view_mail.c | 61 ++++++++++++++++++------- 6 files changed, 101 insertions(+), 162 deletions(-) delete mode 100644 src/utils/test_header.c diff --git a/src/network/test_pop3.c b/src/network/test_pop3.c index c10d4b7..cbc5d7a 100644 --- a/src/network/test_pop3.c +++ b/src/network/test_pop3.c @@ -41,7 +41,7 @@ int main(int argc, char **argv) while ((opt = getopt(argc, argv, "u:h:p:P:sr")) != -1) { switch (opt) { - case 'h' : + case 'h': hostname = optarg; break; case 'p': diff --git a/src/utils/.gitignore b/src/utils/.gitignore index c3fea0d..806131f 100644 --- a/src/utils/.gitignore +++ b/src/utils/.gitignore @@ -1,6 +1,5 @@ # Generated test files test_mime -test_header test_view_mail test_list test_list_x11 diff --git a/src/utils/Makefile b/src/utils/Makefile index a2c07e0..ed8700e 100644 --- a/src/utils/Makefile +++ b/src/utils/Makefile @@ -55,10 +55,6 @@ WIDGETS_OBJS = \ all: $(TARGETS) -test_header: $(COMMON_OBJS) test_header.o - @echo " LD $@" - $(CC) $^ -o $@ $(LDFLAGS) - test_header_osd: $(COMMON_OBJS) $(MD5_UTILS_OBJS) $(OSD_OBJS) test_header_osd.o @echo " LD $@" $(CC) $^ -o $@ $(LDFLAGS) -lXft -lpng diff --git a/src/utils/rfc822.c b/src/utils/rfc822.c index f4a8252..2643247 100644 --- a/src/utils/rfc822.c +++ b/src/utils/rfc822.c @@ -39,8 +39,6 @@ int rfc822_check_header_end(const char *line) * Date Time Functions *========================================================================== */ - - time_t rfc822_parse_date(char *date_string) { struct tm tm; @@ -140,7 +138,9 @@ time_t rfc822_parse_date(char *date_string) } /* Now looking at hms */ - /* For now, forget this. The searching will be vague enough that nearest day is good enough. */ + /* For now, forget this. + * The searching will be vague enough that nearest day is good enough. + */ #warning RFC822 TIME NOT PARSED tm.tm_hour = 0; tm.tm_min = 0; @@ -156,9 +156,6 @@ tough_cheese: /* ** Copyright 1998 - 1999 Double Precision, Inc. ** See COPYING for distribution information. -*/ - -/* ** $Id: rfc822_parsedt.c,v 1.4 2002/05/21 16:02:19 mrsam Exp $ */ #include @@ -167,18 +164,17 @@ tough_cheese: #include /* -** time_t rfc822_parsedate(const char *p) -** -** p - contents of the Date: header, attempt to parse it into a time_t. -** -** returns - time_t, or 0 if the date cannot be parsed -*/ - + * time_t rfc822_parsedate(const char *p) + * + * p - contents of the Date: header, attempt to parse it into a time_t. + * + * returns - time_t, or 0 if the date cannot be parsed + */ static unsigned parsedig(const char **p) { unsigned i = 0; - while (isdigit((int)(unsigned char)**p)) { + while (isdigit((int)(unsigned char) **p)) { i = i*10 + **p - '0'; ++*p; } @@ -198,23 +194,23 @@ static const char * const mnames[13] = { #define leap(y) ( \ ((y) % 400) == 0 || \ - (((y) % 4) == 0 && (y) % 100) ) + (((y) % 4) == 0 && (y) % 100)) -static unsigned mlength[]={31,28,31,30,31,30,31,31,30,31,30,31}; -#define mdays(m,y) ( (m) != 2 ? mlength[(m)-1] : leap(y) ? 29:28) +static unsigned mlength[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +#define mdays(m, y) ((m) != 2 ? mlength[(m) - 1] : leap(y) ? 29 : 28) static const char * const zonenames[] = { - "UT","GMT", - "EST","EDT", - "CST","CDT", - "MST","MDT", - "PST","PDT", + "UT", "GMT", + "EST", "EDT", + "CST", "CDT", + "MST", "MDT", + "PST", "PDT", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", NULL}; -#define ZH(n) ( (n) * 60 * 60 ) +#define ZH(n) ((n) * 60 * 60) static int zoneoffset[] = { 0, 0, @@ -231,13 +227,13 @@ static unsigned parsekey(const char **mon, const char * const *ary) { unsigned m, j; - for (m=0; ary[m]; m++) { - for (j=0; ary[m][j]; j++) + for (m = 0; ary[m]; m++) { + for (j = 0; ary[m][j]; j++) if (tolower(ary[m][j]) != tolower((*mon)[j])) break; if (!ary[m][j]) { *mon += j; - return (m+1); + return m + 1; } } return 0; @@ -245,9 +241,11 @@ static unsigned parsekey(const char **mon, const char * const *ary) static int parsetime(const char **t) { - unsigned h,m,s=0; + unsigned h; + unsigned m; + unsigned s = 0; - if (!isdigit((int)(unsigned char)**t)) + if (!isdigit((int)(unsigned char) **t)) return -1; h = parsedig(t); @@ -257,26 +255,29 @@ static int parsetime(const char **t) if (**t != ':') return -1; ++*t; - if (!isdigit((int)(unsigned char)**t)) + if (!isdigit((int)(unsigned char) **t)) return -1; m = parsedig(t); if (**t == ':') { ++*t; - if (!isdigit((int)(unsigned char)**t)) + if (!isdigit((int)(unsigned char) **t)) return -1; - s=parsedig(t); + + s = parsedig(t); } if (m > 59 || s > 59) return -1; - return (h * 60 * 60 + m * 60 + s); + return h * 60 * 60 + m * 60 + s; } time_t rfc822_parsedt(const char *rfcdt) { - unsigned day=0, mon=0, year; + unsigned day = 0; + unsigned mon = 0; + unsigned year; int secs; int offset; time_t t; @@ -289,33 +290,33 @@ time_t rfc822_parsedt(const char *rfcdt) while (!day || !mon) { if (!*rfcdt) return 0; - if (isalpha((int)(unsigned char)*rfcdt)) { + if (isalpha((int)(unsigned char) *rfcdt)) { if (mon) return 0; - mon=parsekey(&rfcdt, mnames); + mon = parsekey(&rfcdt, mnames); if (!mon) - while (*rfcdt && isalpha((int)(unsigned char)*rfcdt)) + while (*rfcdt && isalpha((int)(unsigned char) *rfcdt)) ++rfcdt; continue; } - if (isdigit((int)(unsigned char)*rfcdt)) { + if (isdigit((int)(unsigned char) *rfcdt)) { if (day) - return (0); + return 0; day = parsedig(&rfcdt); if (!day) - return (0); + return 0; continue; } ++rfcdt; } - while (*rfcdt && isspace((int)(unsigned char)*rfcdt)) + while (*rfcdt && isspace((int)(unsigned char) *rfcdt)) ++rfcdt; - if (!isdigit((int)(unsigned char)*rfcdt)) + if (!isdigit((int)(unsigned char) *rfcdt)) return 0; year = parsedig(&rfcdt); @@ -325,13 +326,13 @@ time_t rfc822_parsedt(const char *rfcdt) if (year < 100) year += 1900; - while (*rfcdt && isspace((int)(unsigned char)*rfcdt)) + while (*rfcdt && isspace((int)(unsigned char) *rfcdt)) ++rfcdt; - if (day == 0 || mon == 0 || mon > 12 || day > mdays(mon,year)) + if (day == 0 || mon == 0 || mon > 12 || day > mdays(mon, year)) return 0; - secs=parsetime(&rfcdt); + secs = parsetime(&rfcdt); if (secs < 0) return 0; @@ -345,18 +346,18 @@ time_t rfc822_parsedt(const char *rfcdt) ++rfcdt; } - if (isalpha((int)(unsigned char)*rfcdt)) { + if (isalpha((int)(unsigned char) *rfcdt)) { int n = parsekey(&rfcdt, zonenames); if (n > 0) - offset= zoneoffset[n-1]; + offset = zoneoffset[n-1]; } else { - int sign=1; + int sign = 1; unsigned n; switch (*rfcdt) { case '-': - sign= -1; + sign = -1; case '+': ++rfcdt; } @@ -366,7 +367,7 @@ time_t rfc822_parsedt(const char *rfcdt) if (n > 2359 || (n % 100) > 59) { n = 0; } - offset = sign * ( (n % 100) * 60 + n / 100 * 60 * 60); + offset = sign * ((n % 100) * 60 + n / 100 * 60 * 60); } } @@ -376,12 +377,12 @@ time_t rfc822_parsedt(const char *rfcdt) if (year > 9999) return 0; - t=0; - for (y=1970; y= 4) { y += 3; - t += ( 365*3+366 ) * 24 * 60 * 60; + t += (365 * 3 + 366) * 24 * 60 * 60; continue; } t += 24 * 60 * 60; @@ -389,10 +390,10 @@ time_t rfc822_parsedt(const char *rfcdt) t += 365 * 24 * 60 * 60; } - for (y=1; y < mon; y++) + for (y = 1; y < mon; y++) t += mdays(y, year) * 24 * 60 * 60; - return ( t + (day-1) * 24 * 60 * 60 + secs - offset ); + return t + (day-1) * 24 * 60 * 60 + secs - offset; } const char *rfc822_mkdt(time_t t) diff --git a/src/utils/test_header.c b/src/utils/test_header.c deleted file mode 100644 index 30a0864..0000000 --- a/src/utils/test_header.c +++ /dev/null @@ -1,84 +0,0 @@ -#define _BSD_SOURCE - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "string_utils.h" -#include "file_utils.h" -#include "rfc5322.h" -#include "rfc822.h" - -void parse_file(const char *filename) -{ - FILE *fh; - - char *crlf = NULL; - - time_t dt; - char buf[1000]; - char from[1000]; - char subject[1000]; /* 998 + CR + LF */ - - fh = fopen(filename, "r"); - if (!fh) - return; - - fprintf(stderr, "-------------------------------------------\n"); - fprintf(stderr, "Test file: %s\n", filename); - fprintf(stderr, "-------------------------------------------\n"); - - /* Scan all lines */ - while (fgets(buf, sizeof(buf), fh)) { - if (!strncmp(buf, "From:", 5)) { - rfc5322_extract_address(from, buf, sizeof(from)); - crlf = strrchr(buf, '\n'); - if (crlf) - *crlf = '\0'; - - fprintf(stderr, "%s --> \"%s\"\n", buf , from); - } else if (!strncmp(buf, "Subject:", 8)) { - rfc5322_extract_subject(subject, buf, sizeof(subject)); - fprintf(stderr, "%s --> \"%s\"\n", buf, subject); - } else if (!strncmp(buf, "Date:", 5)) { -#if 0 - dt = rfc822_parse_date(buf); - fprintf(stderr, "%s --> \"%s\"\n", buf, ctime(&dt)); -#else - dt = rfc822_parsedt(buf); - fprintf(stderr, "%s --> \"%s\"\n", buf, rfc822_mkdt(dt)); -#endif - } else { -#if 0 - printf("[SKIP] ------- %s", buf); -#endif - } - } - - fclose(fh); -} - -int main(int argc, char **argv) -{ - const char *file; - - if (argc < 2) { - return -1; - } - - file = argv[1]; - - if (file_type(file)) - return -1; - - parse_file(file); - - return 0; -} - diff --git a/src/utils/test_view_mail.c b/src/utils/test_view_mail.c index 0dfbecd..c70477b 100644 --- a/src/utils/test_view_mail.c +++ b/src/utils/test_view_mail.c @@ -19,25 +19,23 @@ #include "mime.h" #include "mail.h" - - -void plain_body(char *line) +static void plain_body(char *line) { printf("%s", line); } -void qp_body(char *line) +static void qp_body(char *line) { - char buf[1000]; + char buf[1024]; - rfc2047_decode_quoted_printable(buf, line, 999); + rfc2047_decode_quoted_printable(buf, line, sizeof(buf) - 1); printf("%s", buf); } -void parse_mail_body(FILE *fh, struct mail_header *hdr) +static void parse_mail_body(FILE *fh, struct mail_header *hdr) { - char buf[1000]; + char buf[1024]; void (*decode_body)(char *line); switch (hdr->mime.transfer_encoding) { @@ -56,10 +54,10 @@ void parse_mail_body(FILE *fh, struct mail_header *hdr) } } -void parse_mail_header(FILE *fh, struct mail_header *hdr) +static void parse_mail_header(FILE *fh, struct mail_header *hdr) { char *crlf = NULL; - char buf[1000]; + char buf[1024]; /* Scan all lines */ while (fgets(buf, sizeof(buf), fh)) { @@ -89,7 +87,7 @@ void parse_mail_header(FILE *fh, struct mail_header *hdr) hdr->body_offset = ftell(fh); } -void init_mail_header(struct mail_header *hdr) +static void init_mail_header(struct mail_header *hdr) { memset(hdr->from, 0, sizeof(hdr->from)); memset(hdr->subject, 0, sizeof(hdr->subject)); @@ -102,18 +100,45 @@ void init_mail_header(struct mail_header *hdr) hdr->mime.transfer_encoding = MIME_ENCODING_UNKNOWN; } +void usage(const char *appname) +{ + fprintf(stderr, "Usage:\n%s [-h] -f \n", appname); + fprintf(stderr, "Parameters:\n"); + fprintf(stderr, " -h Parse header only\n" + " -f Parse file\n"); +} + int main(int argc, char **argv) { FILE *fh; struct mail_header mail_hdr; - const char *file; - if (argc < 2) { + int opt; + + char *file = NULL; + int hdr_only = 0; + + + while ((opt = getopt(argc, argv, "f:h")) != -1) { + switch (opt) { + case 'f': + file = optarg; + break; + case 'h': + hdr_only = 1; + break; + default: + fprintf(stderr, "Unknown parameter.\n"); + usage(argv[0]); + return -1; + } + } + + if (file == NULL) { + usage(argv[0]); return -1; } - file = argv[1]; - if (file_type(file)) return -1; @@ -137,8 +162,10 @@ int main(int argc, char **argv) fprintf(stderr, "-------------------------------------------\n\n"); - /* Message Body */ - parse_mail_body(fh, &mail_hdr); + if (!hdr_only) { + /* Message Body */ + parse_mail_body(fh, &mail_hdr); + } fclose(fh); -- 2.11.4.GIT