mailinfo: move content/content_top to struct mailinfo

Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 14, 2015 at 17:43 UTC 8e919277e07c5056021e3ec746693e5cd0496a5b
1 file changed +26 -20
builtin/mailinfo.c
+26 -20
@@ -7,6 +7,8 @@
7 #include "utf8.h"
8 #include "strbuf.h"
9
10 +#define MAX_BOUNDARIES 5
11 +
12 struct mailinfo {
13 FILE *input;
14 FILE *output;
@@ -22,6 +24,8 @@ struct mailinfo {
24 int use_inbody_headers;
25 const char *metainfo_charset;
26
27 + struct strbuf *content[MAX_BOUNDARIES];
28 + struct strbuf **content_top;
29 struct strbuf charset;
30 char *message_id;
31 enum {
@@ -34,8 +38,6 @@ struct mailinfo {
38 struct strbuf **s_hdr_data;
39 };
40
37 -#define MAX_BOUNDARIES 5
38 -
41 static void cleanup_space(struct strbuf *sb)
42 {
43 size_t pos, cnt;
@@ -188,10 +190,6 @@ static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
190 return 1;
191 }
192
191 -static struct strbuf *content[MAX_BOUNDARIES];
192 -
193 -static struct strbuf **content_top = content;
194 -
193 static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
194 {
195 struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
@@ -199,11 +197,11 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
197
198 if (slurp_attr(line->buf, "boundary=", boundary)) {
199 strbuf_insert(boundary, 0, "--", 2);
202 - if (++content_top >= &content[MAX_BOUNDARIES]) {
200 + if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
201 fprintf(stderr, "Too many boundaries to handle\n");
202 exit(1);
203 }
206 - *content_top = boundary;
204 + *(mi->content_top) = boundary;
205 boundary = NULL;
206 }
207 slurp_attr(line->buf, "charset=", &mi->charset);
@@ -231,10 +229,12 @@ static void handle_content_transfer_encoding(struct mailinfo *mi,
229 mi->transfer_encoding = TE_DONTCARE;
230 }
231
234 -static int is_multipart_boundary(const struct strbuf *line)
232 +static int is_multipart_boundary(struct mailinfo *mi, const struct strbuf *line)
233 {
236 - return (((*content_top)->len <= line->len) &&
237 - !memcmp(line->buf, (*content_top)->buf, (*content_top)->len));
234 + struct strbuf *content_top = *(mi->content_top);
235 +
236 + return ((content_top->len <= line->len) &&
237 + !memcmp(line->buf, content_top->buf, content_top->len));
238 }
239
240 static void cleanup_subject(struct mailinfo *mi, struct strbuf *subject)
@@ -797,7 +797,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
797 static int find_boundary(struct mailinfo *mi, struct strbuf *line)
798 {
799 while (!strbuf_getline(line, mi->input, '\n')) {
800 - if (*content_top && is_multipart_boundary(line))
800 + if (*(mi->content_top) && is_multipart_boundary(mi, line))
801 return 1;
802 }
803 return 0;
@@ -809,18 +809,18 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
809
810 strbuf_addch(&newline, '\n');
811 again:
812 - if (line->len >= (*content_top)->len + 2 &&
813 - !memcmp(line->buf + (*content_top)->len, "--", 2)) {
812 + if (line->len >= (*(mi->content_top))->len + 2 &&
813 + !memcmp(line->buf + (*(mi->content_top))->len, "--", 2)) {
814 /* we hit an end boundary */
815 /* pop the current boundary off the stack */
816 - strbuf_release(*content_top);
817 - free(*content_top);
818 - *content_top = NULL;
816 + strbuf_release(*(mi->content_top));
817 + free(*(mi->content_top));
818 + *(mi->content_top) = NULL;
819
820 /* technically won't happen as is_multipart_boundary()
821 will fail first. But just in case..
822 */
823 - if (--content_top < content) {
823 + if (--mi->content_top < mi->content) {
824 fprintf(stderr, "Detected mismatched boundaries, "
825 "can't recover\n");
826 exit(1);
@@ -855,14 +855,14 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
855 struct strbuf prev = STRBUF_INIT;
856
857 /* Skip up to the first boundary */
858 - if (*content_top) {
858 + if (*(mi->content_top)) {
859 if (!find_boundary(mi, line))
860 goto handle_body_out;
861 }
862
863 do {
864 /* process any boundary lines */
865 - if (*content_top && is_multipart_boundary(line)) {
865 + if (*(mi->content_top) && is_multipart_boundary(mi, line)) {
866 /* flush any leftover */
867 if (prev.len) {
868 handle_filter(mi, &prev);
@@ -1026,6 +1026,7 @@ static void setup_mailinfo(struct mailinfo *mi)
1026 strbuf_init(&mi->charset, 0);
1027 mi->header_stage = 1;
1028 mi->use_inbody_headers = 1;
1029 + mi->content_top = mi->content;
1030 git_config(git_mailinfo_config, &mi);
1031 }
1032
@@ -1044,6 +1045,11 @@ static void clear_mailinfo(struct mailinfo *mi)
1045 for (i = 0; mi->s_hdr_data[i]; i++)
1046 strbuf_release(mi->s_hdr_data[i]);
1047 free(mi->s_hdr_data);
1048 +
1049 + while (mi->content < mi->content_top) {
1050 + free(*(mi->content_top));
1051 + mi->content_top--;
1052 + }
1053 }
1054
1055 static const char mailinfo_usage[] =