mailinfo: do not let handle_boundary() touch global "line" directly
[gitweb.git] / builtin / mailinfo.c
index dd9aad27236f35239c0aac162c3b760ba94c8143..9b3f349a11be03223d93d4e7fa0e013ba4155912 100644 (file)
@@ -28,11 +28,19 @@ static int use_scissors;
 static int add_message_id;
 static int use_inbody_headers = 1;
 
-#define MAX_HDR_PARSED 10
 #define MAX_BOUNDARIES 5
 
-static void cleanup_space(struct strbuf *sb);
-
+static void cleanup_space(struct strbuf *sb)
+{
+       size_t pos, cnt;
+       for (pos = 0; pos < sb->len; pos++) {
+               if (isspace(sb->buf[pos])) {
+                       sb->buf[pos] = ' ';
+                       for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++);
+                       strbuf_remove(sb, pos + 1, cnt);
+               }
+       }
+}
 
 static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
 {
@@ -272,18 +280,7 @@ static void cleanup_subject(struct strbuf *subject)
        strbuf_trim(subject);
 }
 
-static void cleanup_space(struct strbuf *sb)
-{
-       size_t pos, cnt;
-       for (pos = 0; pos < sb->len; pos++) {
-               if (isspace(sb->buf[pos])) {
-                       sb->buf[pos] = ' ';
-                       for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++);
-                       strbuf_remove(sb, pos + 1, cnt);
-               }
-       }
-}
-
+#define MAX_HDR_PARSED 10
 static const char *header[MAX_HDR_PARSED] = {
        "From","Subject","Date",
 };
@@ -646,27 +643,25 @@ static int is_scissors_line(const struct strbuf *line)
                gap * 2 < perforation);
 }
 
-static int handle_commit_msg(struct strbuf *line)
+static int handle_commit_msg(struct strbuf *line, int *still_looking)
 {
-       static int still_looking = 1;
-
        if (!cmitmsg)
                return 0;
 
-       if (still_looking) {
+       if (*still_looking) {
                if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
                        return 0;
        }
 
-       if (use_inbody_headers && still_looking) {
-               still_looking = check_header(line, s_hdr_data, 0);
-               if (still_looking)
+       if (use_inbody_headers && *still_looking) {
+               *still_looking = check_header(line, s_hdr_data, 0);
+               if (*still_looking)
                        return 0;
        } else
                /* Only trim the first (blank) line of the commit message
                 * when ignoring in-body headers.
                 */
-               still_looking = 0;
+               *still_looking = 0;
 
        /* normalize the log message to UTF-8. */
        if (metainfo_charset)
@@ -678,7 +673,7 @@ static int handle_commit_msg(struct strbuf *line)
                        die_errno("Could not rewind output message file");
                if (ftruncate(fileno(cmitmsg), 0))
                        die_errno("Could not truncate output message file at scissors");
-               still_looking = 1;
+               *still_looking = 1;
 
                /*
                 * We may have already read "secondary headers"; purge
@@ -710,16 +705,13 @@ static void handle_patch(const struct strbuf *line)
        patch_lines++;
 }
 
-static void handle_filter(struct strbuf *line)
+static void handle_filter(struct strbuf *line, int *filter_stage, int *header_stage)
 {
-       static int filter = 0;
-
-       /* filter tells us which part we left off on */
-       switch (filter) {
+       switch (*filter_stage) {
        case 0:
-               if (!handle_commit_msg(line))
+               if (!handle_commit_msg(line, header_stage))
                        break;
-               filter++;
+               (*filter_stage)++;
        case 1:
                handle_patch(line);
                break;
@@ -803,14 +795,14 @@ static int find_boundary(void)
        return 0;
 }
 
-static int handle_boundary(void)
+static int handle_boundary(struct strbuf *line, int *filter_stage, int *header_stage)
 {
        struct strbuf newline = STRBUF_INIT;
 
        strbuf_addch(&newline, '\n');
 again:
-       if (line.len >= (*content_top)->len + 2 &&
-           !memcmp(line.buf + (*content_top)->len, "--", 2)) {
+       if (line->len >= (*content_top)->len + 2 &&
+           !memcmp(line->buf + (*content_top)->len, "--", 2)) {
                /* we hit an end boundary */
                /* pop the current boundary off the stack */
                strbuf_release(*content_top);
@@ -825,7 +817,7 @@ static int handle_boundary(void)
                                        "can't recover\n");
                        exit(1);
                }
-               handle_filter(&newline);
+               handle_filter(&newline, filter_stage, header_stage);
                strbuf_release(&newline);
 
                /* skip to the next boundary */
@@ -839,20 +831,22 @@ static int handle_boundary(void)
        strbuf_reset(&charset);
 
        /* slurp in this section's info */
-       while (read_one_header_line(&line, fin))
-               check_header(&line, p_hdr_data, 0);
+       while (read_one_header_line(line, fin))
+               check_header(line, p_hdr_data, 0);
 
        strbuf_release(&newline);
        /* replenish line */
-       if (strbuf_getline(&line, fin, '\n'))
+       if (strbuf_getline(line, fin, '\n'))
                return 0;
-       strbuf_addch(&line, '\n');
+       strbuf_addch(line, '\n');
        return 1;
 }
 
-static void handle_body(void)
+static void handle_body(struct strbuf *line)
 {
        struct strbuf prev = STRBUF_INIT;
+       int filter_stage = 0;
+       int header_stage = 1;
 
        /* Skip up to the first boundary */
        if (*content_top) {
@@ -862,18 +856,18 @@ static void handle_body(void)
 
        do {
                /* process any boundary lines */
-               if (*content_top && is_multipart_boundary(&line)) {
+               if (*content_top && is_multipart_boundary(line)) {
                        /* flush any leftover */
                        if (prev.len) {
-                               handle_filter(&prev);
+                               handle_filter(&prev, &filter_stage, &header_stage);
                                strbuf_reset(&prev);
                        }
-                       if (!handle_boundary())
+                       if (!handle_boundary(line, &filter_stage, &header_stage))
                                goto handle_body_out;
                }
 
                /* Unwrap transfer encoding */
-               decode_transfer_encoding(&line);
+               decode_transfer_encoding(line);
 
                switch (transfer_encoding) {
                case TE_BASE64:
@@ -882,7 +876,7 @@ static void handle_body(void)
                        struct strbuf **lines, **it, *sb;
 
                        /* Prepend any previous partial lines */
-                       strbuf_insert(&line, 0, prev.buf, prev.len);
+                       strbuf_insert(line, 0, prev.buf, prev.len);
                        strbuf_reset(&prev);
 
                        /*
@@ -890,7 +884,7 @@ static void handle_body(void)
                         * multiple new lines.  Pass only one chunk
                         * at a time to handle_filter()
                         */
-                       lines = strbuf_split(&line, '\n');
+                       lines = strbuf_split(line, '\n');
                        for (it = lines; (sb = *it); it++) {
                                if (*(it + 1) == NULL) /* The last line */
                                        if (sb->buf[sb->len - 1] != '\n') {
@@ -898,7 +892,7 @@ static void handle_body(void)
                                                strbuf_addbuf(&prev, sb);
                                                break;
                                        }
-                               handle_filter(sb);
+                               handle_filter(sb, &filter_stage, &header_stage);
                        }
                        /*
                         * The partial chunk is saved in "prev" and will be
@@ -908,10 +902,10 @@ static void handle_body(void)
                        break;
                }
                default:
-                       handle_filter(&line);
+                       handle_filter(line, &filter_stage, &header_stage);
                }
 
-       } while (!strbuf_getwholeline(&line, fin, '\n'));
+       } while (!strbuf_getwholeline(line, fin, '\n'));
 
 handle_body_out:
        strbuf_release(&prev);
@@ -997,7 +991,7 @@ static int mailinfo(FILE *in, FILE *out, const char *msg, const char *patch)
        while (read_one_header_line(&line, fin))
                check_header(&line, p_hdr_data, 1);
 
-       handle_body();
+       handle_body(&line);
        fclose(patchfile);
 
        handle_info();