From: chris mikkelson Date: Thu, 11 Mar 2010 22:03:29 +0000 (-0600) Subject: Make module initiation cascade to modules which may be used. Guard X-Git-Url: https://git.mikk.net/?a=commitdiff_plain;h=5c0e607e68e16ca60da0409423d2bc5e1bd2fe74;p=liburl Make module initiation cascade to modules which may be used. Guard initialization from multiple invocations. --- diff --git a/base64.c b/base64.c index 117dc39..6f16f5f 100644 --- a/base64.c +++ b/base64.c @@ -18,6 +18,10 @@ base64_init(void) { int i; const char *p; + static int initialized = 0; + + if (initialized++) return; + for (i = 0; i < 256; i++) i64[i] = -1; for (p = b64c, i = 0; *p; p++, i++) diff --git a/message.c b/message.c index 7e28acc..4b66878 100644 --- a/message.c +++ b/message.c @@ -20,6 +20,12 @@ static pcre *enc_re; static const char *bound_pat = "boundary\\s*=\\s*\"?([\\da-z'()+,-./:=?_ \r\n]+)\"?"; static pcre *bound_re; +static msgproc_module *text_module = &msgproc_text, + *html_module = &msgproc_html, + *multipart_module = &msgproc_multipart, + *quoted_module = &msgproc_quoted, + *base64_module = &msgproc_base64; + static void message_init(void) { @@ -37,13 +43,13 @@ message_init(void) type_re = pcre_compile(type_pat, options, &etxt, &epos, 0); enc_re = pcre_compile(enc_pat, options, &etxt, &epos, 0); bound_re = pcre_compile(bound_pat, options, &etxt, &epos, 0); + msgproc_module_init(text_module); + msgproc_module_init(html_module); + msgproc_module_init(multipart_module); + msgproc_module_init(quoted_module); + msgproc_module_init(base64_module); } -static msgproc_module *text_module = &msgproc_text, - *html_module = &msgproc_html, - *multipart_module = &msgproc_multipart, - *quoted_module = &msgproc_quoted, - *base64_module = &msgproc_base64; #define MESSAGE_MODULE_TEXT 0 #define MESSAGE_MODULE_HTML 1 @@ -54,13 +60,16 @@ static msgproc_module *text_module = &msgproc_text, static void message_module_set(int type, void *data, size_t size) { + msgproc_module *mpm; switch(type) { - case MESSAGE_MODULE_TEXT: text_module = data; break; - case MESSAGE_MODULE_HTML: html_module = data; break; - case MESSAGE_MODULE_MULTIPART: multipart_module = data; break; - case MESSAGE_MODULE_QUOTED: quoted_module = data; break; - case MESSAGE_MODULE_BASE64: base64_module = data; break; + case MESSAGE_MODULE_TEXT: mpm = text_module = data; break; + case MESSAGE_MODULE_HTML: mpm = html_module = data; break; + case MESSAGE_MODULE_MULTIPART: mpm = multipart_module = data; break; + case MESSAGE_MODULE_QUOTED: mpm = quoted_module = data; break; + case MESSAGE_MODULE_BASE64: mpm = base64_module = data; break; + default: return; } + msgproc_module_init(mpm); } #define HDRALLOC 512 diff --git a/multipart.c b/multipart.c index 22e836d..f2e4fef 100644 --- a/multipart.c +++ b/multipart.c @@ -21,6 +21,14 @@ struct multipart_state { int state; }; +static void +multipart_init(void) +{ + static int initialized = 0; + if (initialized++) return; + msgproc_module_init(nextmod); +} + static void multipart_start(msgproc *m) { @@ -36,6 +44,7 @@ setnextmod(int type, void *data, size_t size) { if (type != MULTIPART_MODULE_NEXTMOD) return; nextmod = (msgproc_module *)data; + msgproc_module_init(nextmod); } static void @@ -199,7 +208,7 @@ multipart_finish(msgproc *m) msgproc_module msgproc_multipart = { MSGPROC_MULTIPART, /* type */ - NULL, /* module init */ + multipart_init, /* module init */ setnextmod, /* set module parameter */ multipart_start, /* start module instance */ setboundary, /* set module instance parameter */ diff --git a/text.c b/text.c index c0b15d4..f0b1e0a 100644 --- a/text.c +++ b/text.c @@ -24,8 +24,7 @@ text_init(void) { const char *etxt; int epos; static int initialized = 0; - if (initialized) return; - initialized = 1; + if (initialized++) return; text_url_re = pcre_compile(text_url_pattern, PCRE_CASELESS, &etxt, &epos, 0); if (!text_url_re) {