6 static void cleanup_space(struct strbuf
*sb
)
9 for (pos
= 0; pos
< sb
->len
; pos
++) {
10 if (isspace(sb
->buf
[pos
])) {
12 for (cnt
= 0; isspace(sb
->buf
[pos
+ cnt
+ 1]); cnt
++);
13 strbuf_remove(sb
, pos
+ 1, cnt
);
18 static void get_sane_name(struct strbuf
*out
, struct strbuf
*name
, struct strbuf
*email
)
20 struct strbuf
*src
= name
;
21 if (name
->len
< 3 || 60 < name
->len
|| strchr(name
->buf
, '@') ||
22 strchr(name
->buf
, '<') || strchr(name
->buf
, '>'))
27 strbuf_addbuf(out
, src
);
30 static void parse_bogus_from(struct mailinfo
*mi
, const struct strbuf
*line
)
32 /* John Doe <johndoe> */
35 /* This is fallback, so do not bother if we already have an
41 bra
= strchr(line
->buf
, '<');
44 ket
= strchr(bra
, '>');
48 strbuf_reset(&mi
->email
);
49 strbuf_add(&mi
->email
, bra
+ 1, ket
- bra
- 1);
51 strbuf_reset(&mi
->name
);
52 strbuf_add(&mi
->name
, line
->buf
, bra
- line
->buf
);
53 strbuf_trim(&mi
->name
);
54 get_sane_name(&mi
->name
, &mi
->name
, &mi
->email
);
57 static const char *unquote_comment(struct strbuf
*outbuf
, const char *in
)
60 int take_next_litterally
= 0;
62 strbuf_addch(outbuf
, '(');
64 while ((c
= *in
++) != 0) {
65 if (take_next_litterally
== 1) {
66 take_next_litterally
= 0;
70 take_next_litterally
= 1;
73 in
= unquote_comment(outbuf
, in
);
76 strbuf_addch(outbuf
, ')');
81 strbuf_addch(outbuf
, c
);
87 static const char *unquote_quoted_string(struct strbuf
*outbuf
, const char *in
)
90 int take_next_litterally
= 0;
92 while ((c
= *in
++) != 0) {
93 if (take_next_litterally
== 1) {
94 take_next_litterally
= 0;
98 take_next_litterally
= 1;
105 strbuf_addch(outbuf
, c
);
111 static void unquote_quoted_pair(struct strbuf
*line
)
113 struct strbuf outbuf
;
114 const char *in
= line
->buf
;
117 strbuf_init(&outbuf
, line
->len
);
119 while ((c
= *in
++) != 0) {
122 in
= unquote_quoted_string(&outbuf
, in
);
125 in
= unquote_comment(&outbuf
, in
);
129 strbuf_addch(&outbuf
, c
);
132 strbuf_swap(&outbuf
, line
);
133 strbuf_release(&outbuf
);
137 static void handle_from(struct mailinfo
*mi
, const struct strbuf
*from
)
143 strbuf_init(&f
, from
->len
);
144 strbuf_addbuf(&f
, from
);
146 unquote_quoted_pair(&f
);
148 at
= strchr(f
.buf
, '@');
150 parse_bogus_from(mi
, from
);
155 * If we already have one email, don't take any confusing lines
157 if (mi
->email
.len
&& strchr(at
+ 1, '@')) {
162 /* Pick up the string around '@', possibly delimited with <>
163 * pair; that is the email part.
175 el
= strcspn(at
, " \n\t\r\v\f>");
176 strbuf_reset(&mi
->email
);
177 strbuf_add(&mi
->email
, at
, el
);
178 strbuf_remove(&f
, at
- f
.buf
, el
+ (at
[el
] ? 1 : 0));
180 /* The remainder is name. It could be
182 * - "John Doe <john.doe@xz>" (a), or
183 * - "john.doe@xz (John Doe)" (b), or
184 * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
186 * but we have removed the email part, so
188 * - remove extra spaces which could stay after email (case 'c'), and
189 * - trim from both ends, possibly removing the () pair at the end
190 * (cases 'a' and 'b').
194 if (f
.buf
[0] == '(' && f
.len
&& f
.buf
[f
.len
- 1] == ')') {
195 strbuf_remove(&f
, 0, 1);
196 strbuf_setlen(&f
, f
.len
- 1);
199 get_sane_name(&mi
->name
, &f
, &mi
->email
);
203 static void handle_header(struct strbuf
**out
, const struct strbuf
*line
)
206 *out
= xmalloc(sizeof(struct strbuf
));
207 strbuf_init(*out
, line
->len
);
211 strbuf_addbuf(*out
, line
);
214 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
215 * to have enough heuristics to grok MIME encoded patches often found
216 * on our mailing lists. For example, we do not even treat header lines
217 * case insensitively.
220 static int slurp_attr(const char *line
, const char *name
, struct strbuf
*attr
)
222 const char *ends
, *ap
= strcasestr(line
, name
);
225 strbuf_setlen(attr
, 0);
235 sz
= strcspn(ap
, ends
);
236 strbuf_add(attr
, ap
, sz
);
240 static void handle_content_type(struct mailinfo
*mi
, struct strbuf
*line
)
242 struct strbuf
*boundary
= xmalloc(sizeof(struct strbuf
));
243 strbuf_init(boundary
, line
->len
);
245 if (slurp_attr(line
->buf
, "boundary=", boundary
)) {
246 strbuf_insert(boundary
, 0, "--", 2);
247 if (++mi
->content_top
>= &mi
->content
[MAX_BOUNDARIES
]) {
248 error("Too many boundaries to handle");
249 mi
->input_error
= -1;
250 mi
->content_top
= &mi
->content
[MAX_BOUNDARIES
] - 1;
253 *(mi
->content_top
) = boundary
;
256 slurp_attr(line
->buf
, "charset=", &mi
->charset
);
259 strbuf_release(boundary
);
264 static void handle_content_transfer_encoding(struct mailinfo
*mi
,
265 const struct strbuf
*line
)
267 if (strcasestr(line
->buf
, "base64"))
268 mi
->transfer_encoding
= TE_BASE64
;
269 else if (strcasestr(line
->buf
, "quoted-printable"))
270 mi
->transfer_encoding
= TE_QP
;
272 mi
->transfer_encoding
= TE_DONTCARE
;
275 static int is_multipart_boundary(struct mailinfo
*mi
, const struct strbuf
*line
)
277 struct strbuf
*content_top
= *(mi
->content_top
);
279 return ((content_top
->len
<= line
->len
) &&
280 !memcmp(line
->buf
, content_top
->buf
, content_top
->len
));
283 static void cleanup_subject(struct mailinfo
*mi
, struct strbuf
*subject
)
287 while (at
< subject
->len
) {
291 switch (subject
->buf
[at
]) {
293 if (subject
->len
<= at
+ 3)
295 if ((subject
->buf
[at
+ 1] == 'e' ||
296 subject
->buf
[at
+ 1] == 'E') &&
297 subject
->buf
[at
+ 2] == ':') {
298 strbuf_remove(subject
, at
, 3);
303 case ' ': case '\t': case ':':
304 strbuf_remove(subject
, at
, 1);
307 pos
= strchr(subject
->buf
+ at
, ']');
310 remove
= pos
- subject
->buf
+ at
+ 1;
311 if (!mi
->keep_non_patch_brackets_in_subject
||
313 memmem(subject
->buf
+ at
, remove
, "PATCH", 5)))
314 strbuf_remove(subject
, at
, remove
);
318 * If the input had a space after the ], keep
319 * it. We don't bother with finding the end of
320 * the space, since we later normalize it
323 if (isspace(subject
->buf
[at
]))
330 strbuf_trim(subject
);
333 #define MAX_HDR_PARSED 10
334 static const char *header
[MAX_HDR_PARSED
] = {
335 "From","Subject","Date",
338 static inline int cmp_header(const struct strbuf
*line
, const char *hdr
)
340 int len
= strlen(hdr
);
341 return !strncasecmp(line
->buf
, hdr
, len
) && line
->len
> len
&&
342 line
->buf
[len
] == ':' && isspace(line
->buf
[len
+ 1]);
345 static int is_format_patch_separator(const char *line
, int len
)
347 static const char SAMPLE
[] =
348 "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
351 if (len
!= strlen(SAMPLE
))
353 if (!skip_prefix(line
, "From ", &cp
))
355 if (strspn(cp
, "0123456789abcdef") != 40)
358 return !memcmp(SAMPLE
+ (cp
- line
), cp
, strlen(SAMPLE
) - (cp
- line
));
361 static struct strbuf
*decode_q_segment(const struct strbuf
*q_seg
, int rfc2047
)
363 const char *in
= q_seg
->buf
;
365 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
366 strbuf_init(out
, q_seg
->len
);
368 while ((c
= *in
++) != 0) {
372 break; /* drop trailing newline */
375 strbuf_addch(out
, ch
);
379 /* garbage -- fall through */
381 if (rfc2047
&& c
== '_') /* rfc2047 4.2 (2) */
383 strbuf_addch(out
, c
);
388 static struct strbuf
*decode_b_segment(const struct strbuf
*b_seg
)
390 /* Decode in..ep, possibly in-place to ot */
391 int c
, pos
= 0, acc
= 0;
392 const char *in
= b_seg
->buf
;
393 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
394 strbuf_init(out
, b_seg
->len
);
396 while ((c
= *in
++) != 0) {
401 else if ('A' <= c
&& c
<= 'Z')
403 else if ('a' <= c
&& c
<= 'z')
405 else if ('0' <= c
&& c
<= '9')
408 continue; /* garbage */
414 strbuf_addch(out
, (acc
| (c
>> 4)));
418 strbuf_addch(out
, (acc
| (c
>> 2)));
422 strbuf_addch(out
, (acc
| c
));
430 static int convert_to_utf8(struct mailinfo
*mi
,
431 struct strbuf
*line
, const char *charset
)
435 if (!mi
->metainfo_charset
|| !charset
|| !*charset
)
438 if (same_encoding(mi
->metainfo_charset
, charset
))
440 out
= reencode_string(line
->buf
, mi
->metainfo_charset
, charset
);
442 mi
->input_error
= -1;
443 return error("cannot convert from %s to %s",
444 charset
, mi
->metainfo_charset
);
446 strbuf_attach(line
, out
, strlen(out
), strlen(out
));
450 static void decode_header(struct mailinfo
*mi
, struct strbuf
*it
)
453 struct strbuf outbuf
= STRBUF_INIT
, *dec
;
454 struct strbuf charset_q
= STRBUF_INIT
, piecebuf
= STRBUF_INIT
;
455 int found_error
= 1; /* pessimism */
458 while (in
- it
->buf
<= it
->len
&& (ep
= strstr(in
, "=?")) != NULL
) {
460 strbuf_reset(&charset_q
);
461 strbuf_reset(&piecebuf
);
465 * We are about to process an encoded-word
466 * that begins at ep, but there is something
467 * before the encoded word.
470 for (scan
= in
; scan
< ep
; scan
++)
474 if (scan
!= ep
|| in
== it
->buf
) {
476 * We should not lose that "something",
477 * unless we have just processed an
478 * encoded-word, and there is only LWS
479 * before the one we are about to process.
481 strbuf_add(&outbuf
, in
, ep
- in
);
485 * ep : "=?iso-2022-jp?B?GyR...?= foo"
486 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
490 if (ep
- it
->buf
>= it
->len
|| !(cp
= strchr(ep
, '?')))
493 if (cp
+ 3 - it
->buf
> it
->len
)
495 strbuf_add(&charset_q
, ep
, cp
- ep
);
498 if (!encoding
|| cp
[2] != '?')
500 ep
= strstr(cp
+ 3, "?=");
503 strbuf_add(&piecebuf
, cp
+ 3, ep
- cp
- 3);
504 switch (tolower(encoding
)) {
508 dec
= decode_b_segment(&piecebuf
);
511 dec
= decode_q_segment(&piecebuf
, 1);
514 if (convert_to_utf8(mi
, dec
, charset_q
.buf
))
517 strbuf_addbuf(&outbuf
, dec
);
522 strbuf_addstr(&outbuf
, in
);
524 strbuf_addbuf(it
, &outbuf
);
527 strbuf_release(&outbuf
);
528 strbuf_release(&charset_q
);
529 strbuf_release(&piecebuf
);
532 mi
->input_error
= -1;
535 static int check_header(struct mailinfo
*mi
,
536 const struct strbuf
*line
,
537 struct strbuf
*hdr_data
[], int overwrite
)
540 struct strbuf sb
= STRBUF_INIT
;
542 /* search for the interesting parts */
543 for (i
= 0; header
[i
]; i
++) {
544 int len
= strlen(header
[i
]);
545 if ((!hdr_data
[i
] || overwrite
) && cmp_header(line
, header
[i
])) {
546 /* Unwrap inline B and Q encoding, and optionally
547 * normalize the meta information to utf8.
549 strbuf_add(&sb
, line
->buf
+ len
+ 2, line
->len
- len
- 2);
550 decode_header(mi
, &sb
);
551 handle_header(&hdr_data
[i
], &sb
);
553 goto check_header_out
;
558 if (cmp_header(line
, "Content-Type")) {
559 len
= strlen("Content-Type: ");
560 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
561 decode_header(mi
, &sb
);
562 strbuf_insert(&sb
, 0, "Content-Type: ", len
);
563 handle_content_type(mi
, &sb
);
565 goto check_header_out
;
567 if (cmp_header(line
, "Content-Transfer-Encoding")) {
568 len
= strlen("Content-Transfer-Encoding: ");
569 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
570 decode_header(mi
, &sb
);
571 handle_content_transfer_encoding(mi
, &sb
);
573 goto check_header_out
;
575 if (cmp_header(line
, "Message-Id")) {
576 len
= strlen("Message-Id: ");
577 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
578 decode_header(mi
, &sb
);
579 if (mi
->add_message_id
)
580 mi
->message_id
= strbuf_detach(&sb
, NULL
);
582 goto check_header_out
;
585 /* for inbody stuff */
586 if (starts_with(line
->buf
, ">From") && isspace(line
->buf
[5])) {
587 ret
= is_format_patch_separator(line
->buf
+ 1, line
->len
- 1);
588 goto check_header_out
;
590 if (starts_with(line
->buf
, "[PATCH]") && isspace(line
->buf
[7])) {
591 for (i
= 0; header
[i
]; i
++) {
592 if (!strcmp("Subject", header
[i
])) {
593 handle_header(&hdr_data
[i
], line
);
595 goto check_header_out
;
605 static void decode_transfer_encoding(struct mailinfo
*mi
, struct strbuf
*line
)
609 switch (mi
->transfer_encoding
) {
611 ret
= decode_q_segment(line
, 0);
614 ret
= decode_b_segment(line
);
621 strbuf_addbuf(line
, ret
);
626 static inline int patchbreak(const struct strbuf
*line
)
630 /* Beginning of a "diff -" header? */
631 if (starts_with(line
->buf
, "diff -"))
634 /* CVS "Index: " line? */
635 if (starts_with(line
->buf
, "Index: "))
639 * "--- <filename>" starts patches without headers
640 * "---<sp>*" is a manual separator
645 if (starts_with(line
->buf
, "---")) {
646 /* space followed by a filename? */
647 if (line
->buf
[3] == ' ' && !isspace(line
->buf
[4]))
649 /* Just whitespace? */
650 for (i
= 3; i
< line
->len
; i
++) {
651 unsigned char c
= line
->buf
[i
];
662 static int is_scissors_line(const struct strbuf
*line
)
664 size_t i
, len
= line
->len
;
665 int scissors
= 0, gap
= 0;
666 int first_nonblank
= -1;
667 int last_nonblank
= 0, visible
, perforation
= 0, in_perforation
= 0;
668 const char *buf
= line
->buf
;
670 for (i
= 0; i
< len
; i
++) {
671 if (isspace(buf
[i
])) {
672 if (in_perforation
) {
679 if (first_nonblank
< 0)
687 (!memcmp(buf
+ i
, ">8", 2) || !memcmp(buf
+ i
, "8<", 2) ||
688 !memcmp(buf
+ i
, ">%", 2) || !memcmp(buf
+ i
, "%<", 2))) {
699 * The mark must be at least 8 bytes long (e.g. "-- >8 --").
700 * Even though there can be arbitrary cruft on the same line
701 * (e.g. "cut here"), in order to avoid misidentification, the
702 * perforation must occupy more than a third of the visible
703 * width of the line, and dashes and scissors must occupy more
704 * than half of the perforation.
707 visible
= last_nonblank
- first_nonblank
+ 1;
708 return (scissors
&& 8 <= visible
&&
709 visible
< perforation
* 3 &&
710 gap
* 2 < perforation
);
713 static int handle_commit_msg(struct mailinfo
*mi
, struct strbuf
*line
)
715 assert(!mi
->filter_stage
);
717 if (mi
->header_stage
) {
718 if (!line
->len
|| (line
->len
== 1 && line
->buf
[0] == '\n'))
722 if (mi
->use_inbody_headers
&& mi
->header_stage
) {
723 mi
->header_stage
= check_header(mi
, line
, mi
->s_hdr_data
, 0);
724 if (mi
->header_stage
)
727 /* Only trim the first (blank) line of the commit message
728 * when ignoring in-body headers.
730 mi
->header_stage
= 0;
732 /* normalize the log message to UTF-8. */
733 if (convert_to_utf8(mi
, line
, mi
->charset
.buf
))
734 return 0; /* mi->input_error already set */
736 if (mi
->use_scissors
&& is_scissors_line(line
)) {
739 strbuf_setlen(&mi
->log_message
, 0);
740 mi
->header_stage
= 1;
743 * We may have already read "secondary headers"; purge
744 * them to give ourselves a clean restart.
746 for (i
= 0; header
[i
]; i
++) {
747 if (mi
->s_hdr_data
[i
])
748 strbuf_release(mi
->s_hdr_data
[i
]);
749 mi
->s_hdr_data
[i
] = NULL
;
754 if (patchbreak(line
)) {
756 strbuf_addf(&mi
->log_message
,
757 "Message-Id: %s\n", mi
->message_id
);
761 strbuf_addbuf(&mi
->log_message
, line
);
765 static void handle_patch(struct mailinfo
*mi
, const struct strbuf
*line
)
767 fwrite(line
->buf
, 1, line
->len
, mi
->patchfile
);
771 static void handle_filter(struct mailinfo
*mi
, struct strbuf
*line
)
773 switch (mi
->filter_stage
) {
775 if (!handle_commit_msg(mi
, line
))
779 handle_patch(mi
, line
);
784 static int is_rfc2822_header(const struct strbuf
*line
)
787 * The section that defines the loosest possible
788 * field name is "3.6.8 Optional fields".
790 * optional-field = field-name ":" unstructured CRLF
791 * field-name = 1*ftext
792 * ftext = %d33-57 / %59-126
795 char *cp
= line
->buf
;
797 /* Count mbox From headers as headers */
798 if (starts_with(cp
, "From ") || starts_with(cp
, ">From "))
801 while ((ch
= *cp
++)) {
804 if ((33 <= ch
&& ch
<= 57) ||
805 (59 <= ch
&& ch
<= 126))
812 static int read_one_header_line(struct strbuf
*line
, FILE *in
)
814 struct strbuf continuation
= STRBUF_INIT
;
816 /* Get the first part of the line. */
817 if (strbuf_getline_lf(line
, in
))
821 * Is it an empty line or not a valid rfc2822 header?
822 * If so, stop here, and return false ("not a header")
825 if (!line
->len
|| !is_rfc2822_header(line
)) {
826 /* Re-add the newline */
827 strbuf_addch(line
, '\n');
832 * Now we need to eat all the continuation lines..
833 * Yuck, 2822 header "folding"
838 peek
= fgetc(in
); ungetc(peek
, in
);
839 if (peek
!= ' ' && peek
!= '\t')
841 if (strbuf_getline_lf(&continuation
, in
))
843 continuation
.buf
[0] = ' ';
844 strbuf_rtrim(&continuation
);
845 strbuf_addbuf(line
, &continuation
);
847 strbuf_release(&continuation
);
852 static int find_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
854 while (!strbuf_getline_lf(line
, mi
->input
)) {
855 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
))
861 static int handle_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
863 struct strbuf newline
= STRBUF_INIT
;
865 strbuf_addch(&newline
, '\n');
867 if (line
->len
>= (*(mi
->content_top
))->len
+ 2 &&
868 !memcmp(line
->buf
+ (*(mi
->content_top
))->len
, "--", 2)) {
869 /* we hit an end boundary */
870 /* pop the current boundary off the stack */
871 strbuf_release(*(mi
->content_top
));
872 free(*(mi
->content_top
));
873 *(mi
->content_top
) = NULL
;
875 /* technically won't happen as is_multipart_boundary()
876 will fail first. But just in case..
878 if (--mi
->content_top
< mi
->content
) {
879 error("Detected mismatched boundaries, can't recover");
880 mi
->input_error
= -1;
881 mi
->content_top
= mi
->content
;
884 handle_filter(mi
, &newline
);
885 strbuf_release(&newline
);
889 /* skip to the next boundary */
890 if (!find_boundary(mi
, line
))
895 /* set some defaults */
896 mi
->transfer_encoding
= TE_DONTCARE
;
897 strbuf_reset(&mi
->charset
);
899 /* slurp in this section's info */
900 while (read_one_header_line(line
, mi
->input
))
901 check_header(mi
, line
, mi
->p_hdr_data
, 0);
903 strbuf_release(&newline
);
905 if (strbuf_getline_lf(line
, mi
->input
))
907 strbuf_addch(line
, '\n');
911 static void handle_body(struct mailinfo
*mi
, struct strbuf
*line
)
913 struct strbuf prev
= STRBUF_INIT
;
915 /* Skip up to the first boundary */
916 if (*(mi
->content_top
)) {
917 if (!find_boundary(mi
, line
))
918 goto handle_body_out
;
922 /* process any boundary lines */
923 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
)) {
924 /* flush any leftover */
926 handle_filter(mi
, &prev
);
929 if (!handle_boundary(mi
, line
))
930 goto handle_body_out
;
933 /* Unwrap transfer encoding */
934 decode_transfer_encoding(mi
, line
);
936 switch (mi
->transfer_encoding
) {
940 struct strbuf
**lines
, **it
, *sb
;
942 /* Prepend any previous partial lines */
943 strbuf_insert(line
, 0, prev
.buf
, prev
.len
);
947 * This is a decoded line that may contain
948 * multiple new lines. Pass only one chunk
949 * at a time to handle_filter()
951 lines
= strbuf_split(line
, '\n');
952 for (it
= lines
; (sb
= *it
); it
++) {
953 if (*(it
+ 1) == NULL
) /* The last line */
954 if (sb
->buf
[sb
->len
- 1] != '\n') {
955 /* Partial line, save it for later. */
956 strbuf_addbuf(&prev
, sb
);
959 handle_filter(mi
, sb
);
962 * The partial chunk is saved in "prev" and will be
963 * appended by the next iteration of read_line_with_nul().
965 strbuf_list_free(lines
);
969 handle_filter(mi
, line
);
974 } while (!strbuf_getwholeline(line
, mi
->input
, '\n'));
977 strbuf_release(&prev
);
980 static void output_header_lines(FILE *fout
, const char *hdr
, const struct strbuf
*data
)
982 const char *sp
= data
->buf
;
984 char *ep
= strchr(sp
, '\n');
990 fprintf(fout
, "%s: %.*s\n", hdr
, len
, sp
);
997 static void handle_info(struct mailinfo
*mi
)
1002 for (i
= 0; header
[i
]; i
++) {
1003 /* only print inbody headers if we output a patch file */
1004 if (mi
->patch_lines
&& mi
->s_hdr_data
[i
])
1005 hdr
= mi
->s_hdr_data
[i
];
1006 else if (mi
->p_hdr_data
[i
])
1007 hdr
= mi
->p_hdr_data
[i
];
1011 if (!strcmp(header
[i
], "Subject")) {
1012 if (!mi
->keep_subject
) {
1013 cleanup_subject(mi
, hdr
);
1016 output_header_lines(mi
->output
, "Subject", hdr
);
1017 } else if (!strcmp(header
[i
], "From")) {
1019 handle_from(mi
, hdr
);
1020 fprintf(mi
->output
, "Author: %s\n", mi
->name
.buf
);
1021 fprintf(mi
->output
, "Email: %s\n", mi
->email
.buf
);
1024 fprintf(mi
->output
, "%s: %s\n", header
[i
], hdr
->buf
);
1027 fprintf(mi
->output
, "\n");
1030 int mailinfo(struct mailinfo
*mi
, const char *msg
, const char *patch
)
1034 struct strbuf line
= STRBUF_INIT
;
1036 cmitmsg
= fopen(msg
, "w");
1041 mi
->patchfile
= fopen(patch
, "w");
1042 if (!mi
->patchfile
) {
1048 mi
->p_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->p_hdr_data
)));
1049 mi
->s_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->s_hdr_data
)));
1052 peek
= fgetc(mi
->input
);
1053 } while (isspace(peek
));
1054 ungetc(peek
, mi
->input
);
1056 /* process the email header */
1057 while (read_one_header_line(&line
, mi
->input
))
1058 check_header(mi
, &line
, mi
->p_hdr_data
, 1);
1060 handle_body(mi
, &line
);
1061 fwrite(mi
->log_message
.buf
, 1, mi
->log_message
.len
, cmitmsg
);
1063 fclose(mi
->patchfile
);
1066 strbuf_release(&line
);
1067 return mi
->input_error
;
1070 static int git_mailinfo_config(const char *var
, const char *value
, void *mi_
)
1072 struct mailinfo
*mi
= mi_
;
1074 if (!starts_with(var
, "mailinfo."))
1075 return git_default_config(var
, value
, NULL
);
1076 if (!strcmp(var
, "mailinfo.scissors")) {
1077 mi
->use_scissors
= git_config_bool(var
, value
);
1080 /* perhaps others here */
1084 void setup_mailinfo(struct mailinfo
*mi
)
1086 memset(mi
, 0, sizeof(*mi
));
1087 strbuf_init(&mi
->name
, 0);
1088 strbuf_init(&mi
->email
, 0);
1089 strbuf_init(&mi
->charset
, 0);
1090 strbuf_init(&mi
->log_message
, 0);
1091 mi
->header_stage
= 1;
1092 mi
->use_inbody_headers
= 1;
1093 mi
->content_top
= mi
->content
;
1094 git_config(git_mailinfo_config
, mi
);
1097 void clear_mailinfo(struct mailinfo
*mi
)
1101 strbuf_release(&mi
->name
);
1102 strbuf_release(&mi
->email
);
1103 strbuf_release(&mi
->charset
);
1104 free(mi
->message_id
);
1106 for (i
= 0; mi
->p_hdr_data
[i
]; i
++)
1107 strbuf_release(mi
->p_hdr_data
[i
]);
1108 free(mi
->p_hdr_data
);
1109 for (i
= 0; mi
->s_hdr_data
[i
]; i
++)
1110 strbuf_release(mi
->s_hdr_data
[i
]);
1111 free(mi
->s_hdr_data
);
1113 while (mi
->content
< mi
->content_top
) {
1114 free(*(mi
->content_top
));
1118 strbuf_release(&mi
->log_message
);