7 static void cleanup_space(struct strbuf
*sb
)
10 for (pos
= 0; pos
< sb
->len
; pos
++) {
11 if (isspace(sb
->buf
[pos
])) {
13 for (cnt
= 0; isspace(sb
->buf
[pos
+ cnt
+ 1]); cnt
++);
14 strbuf_remove(sb
, pos
+ 1, cnt
);
19 static void get_sane_name(struct strbuf
*out
, struct strbuf
*name
, struct strbuf
*email
)
21 struct strbuf
*src
= name
;
22 if (name
->len
< 3 || 60 < name
->len
|| strchr(name
->buf
, '@') ||
23 strchr(name
->buf
, '<') || strchr(name
->buf
, '>'))
28 strbuf_addbuf(out
, src
);
31 static void parse_bogus_from(struct mailinfo
*mi
, const struct strbuf
*line
)
33 /* John Doe <johndoe> */
36 /* This is fallback, so do not bother if we already have an
42 bra
= strchr(line
->buf
, '<');
45 ket
= strchr(bra
, '>');
49 strbuf_reset(&mi
->email
);
50 strbuf_add(&mi
->email
, bra
+ 1, ket
- bra
- 1);
52 strbuf_reset(&mi
->name
);
53 strbuf_add(&mi
->name
, line
->buf
, bra
- line
->buf
);
54 strbuf_trim(&mi
->name
);
55 get_sane_name(&mi
->name
, &mi
->name
, &mi
->email
);
58 static const char *unquote_comment(struct strbuf
*outbuf
, const char *in
)
61 int take_next_literally
= 0;
63 strbuf_addch(outbuf
, '(');
65 while ((c
= *in
++) != 0) {
66 if (take_next_literally
== 1) {
67 take_next_literally
= 0;
71 take_next_literally
= 1;
74 in
= unquote_comment(outbuf
, in
);
77 strbuf_addch(outbuf
, ')');
82 strbuf_addch(outbuf
, c
);
88 static const char *unquote_quoted_string(struct strbuf
*outbuf
, const char *in
)
91 int take_next_literally
= 0;
93 while ((c
= *in
++) != 0) {
94 if (take_next_literally
== 1) {
95 take_next_literally
= 0;
99 take_next_literally
= 1;
106 strbuf_addch(outbuf
, c
);
112 static void unquote_quoted_pair(struct strbuf
*line
)
114 struct strbuf outbuf
;
115 const char *in
= line
->buf
;
118 strbuf_init(&outbuf
, line
->len
);
120 while ((c
= *in
++) != 0) {
123 in
= unquote_quoted_string(&outbuf
, in
);
126 in
= unquote_comment(&outbuf
, in
);
130 strbuf_addch(&outbuf
, c
);
133 strbuf_swap(&outbuf
, line
);
134 strbuf_release(&outbuf
);
138 static void handle_from(struct mailinfo
*mi
, const struct strbuf
*from
)
144 strbuf_init(&f
, from
->len
);
145 strbuf_addbuf(&f
, from
);
147 unquote_quoted_pair(&f
);
149 at
= strchr(f
.buf
, '@');
151 parse_bogus_from(mi
, from
);
156 * If we already have one email, don't take any confusing lines
158 if (mi
->email
.len
&& strchr(at
+ 1, '@')) {
163 /* Pick up the string around '@', possibly delimited with <>
164 * pair; that is the email part.
176 el
= strcspn(at
, " \n\t\r\v\f>");
177 strbuf_reset(&mi
->email
);
178 strbuf_add(&mi
->email
, at
, el
);
179 strbuf_remove(&f
, at
- f
.buf
, el
+ (at
[el
] ? 1 : 0));
181 /* The remainder is name. It could be
183 * - "John Doe <john.doe@xz>" (a), or
184 * - "john.doe@xz (John Doe)" (b), or
185 * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
187 * but we have removed the email part, so
189 * - remove extra spaces which could stay after email (case 'c'), and
190 * - trim from both ends, possibly removing the () pair at the end
191 * (cases 'a' and 'b').
195 if (f
.buf
[0] == '(' && f
.len
&& f
.buf
[f
.len
- 1] == ')') {
196 strbuf_remove(&f
, 0, 1);
197 strbuf_setlen(&f
, f
.len
- 1);
200 get_sane_name(&mi
->name
, &f
, &mi
->email
);
204 static void handle_header(struct strbuf
**out
, const struct strbuf
*line
)
207 *out
= xmalloc(sizeof(struct strbuf
));
208 strbuf_init(*out
, line
->len
);
212 strbuf_addbuf(*out
, line
);
215 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
216 * to have enough heuristics to grok MIME encoded patches often found
217 * on our mailing lists. For example, we do not even treat header lines
218 * case insensitively.
221 static int slurp_attr(const char *line
, const char *name
, struct strbuf
*attr
)
223 const char *ends
, *ap
= strcasestr(line
, name
);
226 strbuf_setlen(attr
, 0);
236 sz
= strcspn(ap
, ends
);
237 strbuf_add(attr
, ap
, sz
);
241 static void handle_content_type(struct mailinfo
*mi
, struct strbuf
*line
)
243 struct strbuf
*boundary
= xmalloc(sizeof(struct strbuf
));
244 strbuf_init(boundary
, line
->len
);
246 if (slurp_attr(line
->buf
, "boundary=", boundary
)) {
247 strbuf_insert(boundary
, 0, "--", 2);
248 if (++mi
->content_top
>= &mi
->content
[MAX_BOUNDARIES
]) {
249 error("Too many boundaries to handle");
250 mi
->input_error
= -1;
251 mi
->content_top
= &mi
->content
[MAX_BOUNDARIES
] - 1;
254 *(mi
->content_top
) = boundary
;
257 slurp_attr(line
->buf
, "charset=", &mi
->charset
);
260 strbuf_release(boundary
);
265 static void handle_content_transfer_encoding(struct mailinfo
*mi
,
266 const struct strbuf
*line
)
268 if (strcasestr(line
->buf
, "base64"))
269 mi
->transfer_encoding
= TE_BASE64
;
270 else if (strcasestr(line
->buf
, "quoted-printable"))
271 mi
->transfer_encoding
= TE_QP
;
273 mi
->transfer_encoding
= TE_DONTCARE
;
276 static int is_multipart_boundary(struct mailinfo
*mi
, const struct strbuf
*line
)
278 struct strbuf
*content_top
= *(mi
->content_top
);
280 return ((content_top
->len
<= line
->len
) &&
281 !memcmp(line
->buf
, content_top
->buf
, content_top
->len
));
284 static void cleanup_subject(struct mailinfo
*mi
, struct strbuf
*subject
)
288 while (at
< subject
->len
) {
292 switch (subject
->buf
[at
]) {
294 if (subject
->len
<= at
+ 3)
296 if ((subject
->buf
[at
+ 1] == 'e' ||
297 subject
->buf
[at
+ 1] == 'E') &&
298 subject
->buf
[at
+ 2] == ':') {
299 strbuf_remove(subject
, at
, 3);
304 case ' ': case '\t': case ':':
305 strbuf_remove(subject
, at
, 1);
308 pos
= strchr(subject
->buf
+ at
, ']');
311 remove
= pos
- subject
->buf
+ at
+ 1;
312 if (!mi
->keep_non_patch_brackets_in_subject
||
314 memmem(subject
->buf
+ at
, remove
, "PATCH", 5)))
315 strbuf_remove(subject
, at
, remove
);
319 * If the input had a space after the ], keep
320 * it. We don't bother with finding the end of
321 * the space, since we later normalize it
324 if (isspace(subject
->buf
[at
]))
331 strbuf_trim(subject
);
334 #define MAX_HDR_PARSED 10
335 static const char *header
[MAX_HDR_PARSED
] = {
336 "From","Subject","Date",
339 static inline int cmp_header(const struct strbuf
*line
, const char *hdr
)
341 int len
= strlen(hdr
);
342 return !strncasecmp(line
->buf
, hdr
, len
) && line
->len
> len
&&
343 line
->buf
[len
] == ':' && isspace(line
->buf
[len
+ 1]);
346 static int is_format_patch_separator(const char *line
, int len
)
348 static const char SAMPLE
[] =
349 "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
352 if (len
!= strlen(SAMPLE
))
354 if (!skip_prefix(line
, "From ", &cp
))
356 if (strspn(cp
, "0123456789abcdef") != 40)
359 return !memcmp(SAMPLE
+ (cp
- line
), cp
, strlen(SAMPLE
) - (cp
- line
));
362 static struct strbuf
*decode_q_segment(const struct strbuf
*q_seg
, int rfc2047
)
364 const char *in
= q_seg
->buf
;
366 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
367 strbuf_init(out
, q_seg
->len
);
369 while ((c
= *in
++) != 0) {
373 break; /* drop trailing newline */
374 strbuf_addch(out
, (hexval(d
) << 4) | hexval(*in
++));
377 if (rfc2047
&& c
== '_') /* rfc2047 4.2 (2) */
379 strbuf_addch(out
, c
);
384 static struct strbuf
*decode_b_segment(const struct strbuf
*b_seg
)
386 /* Decode in..ep, possibly in-place to ot */
387 int c
, pos
= 0, acc
= 0;
388 const char *in
= b_seg
->buf
;
389 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
390 strbuf_init(out
, b_seg
->len
);
392 while ((c
= *in
++) != 0) {
397 else if ('A' <= c
&& c
<= 'Z')
399 else if ('a' <= c
&& c
<= 'z')
401 else if ('0' <= c
&& c
<= '9')
404 continue; /* garbage */
410 strbuf_addch(out
, (acc
| (c
>> 4)));
414 strbuf_addch(out
, (acc
| (c
>> 2)));
418 strbuf_addch(out
, (acc
| c
));
426 static int convert_to_utf8(struct mailinfo
*mi
,
427 struct strbuf
*line
, const char *charset
)
431 if (!mi
->metainfo_charset
|| !charset
|| !*charset
)
434 if (same_encoding(mi
->metainfo_charset
, charset
))
436 out
= reencode_string(line
->buf
, mi
->metainfo_charset
, charset
);
438 mi
->input_error
= -1;
439 return error("cannot convert from %s to %s",
440 charset
, mi
->metainfo_charset
);
442 strbuf_attach(line
, out
, strlen(out
), strlen(out
));
446 static void decode_header(struct mailinfo
*mi
, struct strbuf
*it
)
449 struct strbuf outbuf
= STRBUF_INIT
, *dec
;
450 struct strbuf charset_q
= STRBUF_INIT
, piecebuf
= STRBUF_INIT
;
451 int found_error
= 1; /* pessimism */
454 while (in
- it
->buf
<= it
->len
&& (ep
= strstr(in
, "=?")) != NULL
) {
456 strbuf_reset(&charset_q
);
457 strbuf_reset(&piecebuf
);
461 * We are about to process an encoded-word
462 * that begins at ep, but there is something
463 * before the encoded word.
466 for (scan
= in
; scan
< ep
; scan
++)
470 if (scan
!= ep
|| in
== it
->buf
) {
472 * We should not lose that "something",
473 * unless we have just processed an
474 * encoded-word, and there is only LWS
475 * before the one we are about to process.
477 strbuf_add(&outbuf
, in
, ep
- in
);
481 * ep : "=?iso-2022-jp?B?GyR...?= foo"
482 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
486 if (ep
- it
->buf
>= it
->len
|| !(cp
= strchr(ep
, '?')))
489 if (cp
+ 3 - it
->buf
> it
->len
)
491 strbuf_add(&charset_q
, ep
, cp
- ep
);
494 if (!encoding
|| cp
[2] != '?')
496 ep
= strstr(cp
+ 3, "?=");
499 strbuf_add(&piecebuf
, cp
+ 3, ep
- cp
- 3);
500 switch (tolower(encoding
)) {
504 dec
= decode_b_segment(&piecebuf
);
507 dec
= decode_q_segment(&piecebuf
, 1);
510 if (convert_to_utf8(mi
, dec
, charset_q
.buf
))
513 strbuf_addbuf(&outbuf
, dec
);
518 strbuf_addstr(&outbuf
, in
);
520 strbuf_addbuf(it
, &outbuf
);
523 strbuf_release(&outbuf
);
524 strbuf_release(&charset_q
);
525 strbuf_release(&piecebuf
);
528 mi
->input_error
= -1;
531 static int check_header(struct mailinfo
*mi
,
532 const struct strbuf
*line
,
533 struct strbuf
*hdr_data
[], int overwrite
)
536 struct strbuf sb
= STRBUF_INIT
;
538 /* search for the interesting parts */
539 for (i
= 0; header
[i
]; i
++) {
540 int len
= strlen(header
[i
]);
541 if ((!hdr_data
[i
] || overwrite
) && cmp_header(line
, header
[i
])) {
542 /* Unwrap inline B and Q encoding, and optionally
543 * normalize the meta information to utf8.
545 strbuf_add(&sb
, line
->buf
+ len
+ 2, line
->len
- len
- 2);
546 decode_header(mi
, &sb
);
547 handle_header(&hdr_data
[i
], &sb
);
549 goto check_header_out
;
554 if (cmp_header(line
, "Content-Type")) {
555 len
= strlen("Content-Type: ");
556 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
557 decode_header(mi
, &sb
);
558 strbuf_insert(&sb
, 0, "Content-Type: ", len
);
559 handle_content_type(mi
, &sb
);
561 goto check_header_out
;
563 if (cmp_header(line
, "Content-Transfer-Encoding")) {
564 len
= strlen("Content-Transfer-Encoding: ");
565 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
566 decode_header(mi
, &sb
);
567 handle_content_transfer_encoding(mi
, &sb
);
569 goto check_header_out
;
571 if (cmp_header(line
, "Message-Id")) {
572 len
= strlen("Message-Id: ");
573 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
574 decode_header(mi
, &sb
);
575 if (mi
->add_message_id
)
576 mi
->message_id
= strbuf_detach(&sb
, NULL
);
578 goto check_header_out
;
587 * Returns 1 if the given line or any line beginning with the given line is an
588 * in-body header (that is, check_header will succeed when passed
591 static int is_inbody_header(const struct mailinfo
*mi
,
592 const struct strbuf
*line
)
595 for (i
= 0; header
[i
]; i
++)
596 if (!mi
->s_hdr_data
[i
] && cmp_header(line
, header
[i
]))
601 static void decode_transfer_encoding(struct mailinfo
*mi
, struct strbuf
*line
)
605 switch (mi
->transfer_encoding
) {
607 ret
= decode_q_segment(line
, 0);
610 ret
= decode_b_segment(line
);
617 strbuf_addbuf(line
, ret
);
622 static inline int patchbreak(const struct strbuf
*line
)
626 /* Beginning of a "diff -" header? */
627 if (starts_with(line
->buf
, "diff -"))
630 /* CVS "Index: " line? */
631 if (starts_with(line
->buf
, "Index: "))
635 * "--- <filename>" starts patches without headers
636 * "---<sp>*" is a manual separator
641 if (starts_with(line
->buf
, "---")) {
642 /* space followed by a filename? */
643 if (line
->buf
[3] == ' ' && !isspace(line
->buf
[4]))
645 /* Just whitespace? */
646 for (i
= 3; i
< line
->len
; i
++) {
647 unsigned char c
= line
->buf
[i
];
658 static int is_scissors_line(const char *line
)
661 int scissors
= 0, gap
= 0;
662 const char *first_nonblank
= NULL
, *last_nonblank
= NULL
;
663 int visible
, perforation
= 0, in_perforation
= 0;
665 for (c
= line
; *c
; c
++) {
667 if (in_perforation
) {
674 if (first_nonblank
== NULL
)
681 if ((!memcmp(c
, ">8", 2) || !memcmp(c
, "8<", 2) ||
682 !memcmp(c
, ">%", 2) || !memcmp(c
, "%<", 2))) {
693 * The mark must be at least 8 bytes long (e.g. "-- >8 --").
694 * Even though there can be arbitrary cruft on the same line
695 * (e.g. "cut here"), in order to avoid misidentification, the
696 * perforation must occupy more than a third of the visible
697 * width of the line, and dashes and scissors must occupy more
698 * than half of the perforation.
701 if (first_nonblank
&& last_nonblank
)
702 visible
= last_nonblank
- first_nonblank
+ 1;
705 return (scissors
&& 8 <= visible
&&
706 visible
< perforation
* 3 &&
707 gap
* 2 < perforation
);
710 static void flush_inbody_header_accum(struct mailinfo
*mi
)
712 if (!mi
->inbody_header_accum
.len
)
714 if (!check_header(mi
, &mi
->inbody_header_accum
, mi
->s_hdr_data
, 0))
715 die("BUG: inbody_header_accum, if not empty, must always contain a valid in-body header");
716 strbuf_reset(&mi
->inbody_header_accum
);
719 static int check_inbody_header(struct mailinfo
*mi
, const struct strbuf
*line
)
721 if (mi
->inbody_header_accum
.len
&&
722 (line
->buf
[0] == ' ' || line
->buf
[0] == '\t')) {
723 if (mi
->use_scissors
&& is_scissors_line(line
->buf
)) {
725 * This is a scissors line; do not consider this line
726 * as a header continuation line.
728 flush_inbody_header_accum(mi
);
731 strbuf_strip_suffix(&mi
->inbody_header_accum
, "\n");
732 strbuf_addbuf(&mi
->inbody_header_accum
, line
);
736 flush_inbody_header_accum(mi
);
738 if (starts_with(line
->buf
, ">From") && isspace(line
->buf
[5]))
739 return is_format_patch_separator(line
->buf
+ 1, line
->len
- 1);
740 if (starts_with(line
->buf
, "[PATCH]") && isspace(line
->buf
[7])) {
742 for (i
= 0; header
[i
]; i
++)
743 if (!strcmp("Subject", header
[i
])) {
744 handle_header(&mi
->s_hdr_data
[i
], line
);
749 if (is_inbody_header(mi
, line
)) {
750 strbuf_addbuf(&mi
->inbody_header_accum
, line
);
756 static int handle_commit_msg(struct mailinfo
*mi
, struct strbuf
*line
)
758 assert(!mi
->filter_stage
);
760 if (mi
->header_stage
) {
761 if (!line
->len
|| (line
->len
== 1 && line
->buf
[0] == '\n')) {
762 if (mi
->inbody_header_accum
.len
) {
763 flush_inbody_header_accum(mi
);
764 mi
->header_stage
= 0;
770 if (mi
->use_inbody_headers
&& mi
->header_stage
) {
771 mi
->header_stage
= check_inbody_header(mi
, line
);
772 if (mi
->header_stage
)
775 /* Only trim the first (blank) line of the commit message
776 * when ignoring in-body headers.
778 mi
->header_stage
= 0;
780 /* normalize the log message to UTF-8. */
781 if (convert_to_utf8(mi
, line
, mi
->charset
.buf
))
782 return 0; /* mi->input_error already set */
784 if (mi
->use_scissors
&& is_scissors_line(line
->buf
)) {
787 strbuf_setlen(&mi
->log_message
, 0);
788 mi
->header_stage
= 1;
791 * We may have already read "secondary headers"; purge
792 * them to give ourselves a clean restart.
794 for (i
= 0; header
[i
]; i
++) {
795 if (mi
->s_hdr_data
[i
])
796 strbuf_release(mi
->s_hdr_data
[i
]);
797 mi
->s_hdr_data
[i
] = NULL
;
802 if (patchbreak(line
)) {
804 strbuf_addf(&mi
->log_message
,
805 "Message-Id: %s\n", mi
->message_id
);
809 strbuf_addbuf(&mi
->log_message
, line
);
813 static void handle_patch(struct mailinfo
*mi
, const struct strbuf
*line
)
815 fwrite(line
->buf
, 1, line
->len
, mi
->patchfile
);
819 static void handle_filter(struct mailinfo
*mi
, struct strbuf
*line
)
821 switch (mi
->filter_stage
) {
823 if (!handle_commit_msg(mi
, line
))
827 handle_patch(mi
, line
);
832 static int is_rfc2822_header(const struct strbuf
*line
)
835 * The section that defines the loosest possible
836 * field name is "3.6.8 Optional fields".
838 * optional-field = field-name ":" unstructured CRLF
839 * field-name = 1*ftext
840 * ftext = %d33-57 / %59-126
843 char *cp
= line
->buf
;
845 /* Count mbox From headers as headers */
846 if (starts_with(cp
, "From ") || starts_with(cp
, ">From "))
849 while ((ch
= *cp
++)) {
852 if ((33 <= ch
&& ch
<= 57) ||
853 (59 <= ch
&& ch
<= 126))
860 static int read_one_header_line(struct strbuf
*line
, FILE *in
)
862 struct strbuf continuation
= STRBUF_INIT
;
864 /* Get the first part of the line. */
865 if (strbuf_getline_lf(line
, in
))
869 * Is it an empty line or not a valid rfc2822 header?
870 * If so, stop here, and return false ("not a header")
873 if (!line
->len
|| !is_rfc2822_header(line
)) {
874 /* Re-add the newline */
875 strbuf_addch(line
, '\n');
880 * Now we need to eat all the continuation lines..
881 * Yuck, 2822 header "folding"
890 if (peek
!= ' ' && peek
!= '\t')
892 if (strbuf_getline_lf(&continuation
, in
))
894 continuation
.buf
[0] = ' ';
895 strbuf_rtrim(&continuation
);
896 strbuf_addbuf(line
, &continuation
);
898 strbuf_release(&continuation
);
903 static int find_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
905 while (!strbuf_getline_lf(line
, mi
->input
)) {
906 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
))
912 static int handle_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
914 struct strbuf newline
= STRBUF_INIT
;
916 strbuf_addch(&newline
, '\n');
918 if (line
->len
>= (*(mi
->content_top
))->len
+ 2 &&
919 !memcmp(line
->buf
+ (*(mi
->content_top
))->len
, "--", 2)) {
920 /* we hit an end boundary */
921 /* pop the current boundary off the stack */
922 strbuf_release(*(mi
->content_top
));
923 FREE_AND_NULL(*(mi
->content_top
));
925 /* technically won't happen as is_multipart_boundary()
926 will fail first. But just in case..
928 if (--mi
->content_top
< mi
->content
) {
929 error("Detected mismatched boundaries, can't recover");
930 mi
->input_error
= -1;
931 mi
->content_top
= mi
->content
;
934 handle_filter(mi
, &newline
);
935 strbuf_release(&newline
);
939 /* skip to the next boundary */
940 if (!find_boundary(mi
, line
))
945 /* set some defaults */
946 mi
->transfer_encoding
= TE_DONTCARE
;
947 strbuf_reset(&mi
->charset
);
949 /* slurp in this section's info */
950 while (read_one_header_line(line
, mi
->input
))
951 check_header(mi
, line
, mi
->p_hdr_data
, 0);
953 strbuf_release(&newline
);
955 if (strbuf_getline_lf(line
, mi
->input
))
957 strbuf_addch(line
, '\n');
961 static void handle_body(struct mailinfo
*mi
, struct strbuf
*line
)
963 struct strbuf prev
= STRBUF_INIT
;
965 /* Skip up to the first boundary */
966 if (*(mi
->content_top
)) {
967 if (!find_boundary(mi
, line
))
968 goto handle_body_out
;
972 /* process any boundary lines */
973 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
)) {
974 /* flush any leftover */
976 handle_filter(mi
, &prev
);
979 if (!handle_boundary(mi
, line
))
980 goto handle_body_out
;
983 /* Unwrap transfer encoding */
984 decode_transfer_encoding(mi
, line
);
986 switch (mi
->transfer_encoding
) {
990 struct strbuf
**lines
, **it
, *sb
;
992 /* Prepend any previous partial lines */
993 strbuf_insert(line
, 0, prev
.buf
, prev
.len
);
997 * This is a decoded line that may contain
998 * multiple new lines. Pass only one chunk
999 * at a time to handle_filter()
1001 lines
= strbuf_split(line
, '\n');
1002 for (it
= lines
; (sb
= *it
); it
++) {
1003 if (*(it
+ 1) == NULL
) /* The last line */
1004 if (sb
->buf
[sb
->len
- 1] != '\n') {
1005 /* Partial line, save it for later. */
1006 strbuf_addbuf(&prev
, sb
);
1009 handle_filter(mi
, sb
);
1012 * The partial chunk is saved in "prev" and will be
1013 * appended by the next iteration of read_line_with_nul().
1015 strbuf_list_free(lines
);
1019 handle_filter(mi
, line
);
1022 if (mi
->input_error
)
1024 } while (!strbuf_getwholeline(line
, mi
->input
, '\n'));
1026 flush_inbody_header_accum(mi
);
1029 strbuf_release(&prev
);
1032 static void output_header_lines(FILE *fout
, const char *hdr
, const struct strbuf
*data
)
1034 const char *sp
= data
->buf
;
1036 char *ep
= strchr(sp
, '\n');
1042 fprintf(fout
, "%s: %.*s\n", hdr
, len
, sp
);
1049 static void handle_info(struct mailinfo
*mi
)
1054 for (i
= 0; header
[i
]; i
++) {
1055 /* only print inbody headers if we output a patch file */
1056 if (mi
->patch_lines
&& mi
->s_hdr_data
[i
])
1057 hdr
= mi
->s_hdr_data
[i
];
1058 else if (mi
->p_hdr_data
[i
])
1059 hdr
= mi
->p_hdr_data
[i
];
1063 if (!strcmp(header
[i
], "Subject")) {
1064 if (!mi
->keep_subject
) {
1065 cleanup_subject(mi
, hdr
);
1068 output_header_lines(mi
->output
, "Subject", hdr
);
1069 } else if (!strcmp(header
[i
], "From")) {
1071 handle_from(mi
, hdr
);
1072 fprintf(mi
->output
, "Author: %s\n", mi
->name
.buf
);
1073 fprintf(mi
->output
, "Email: %s\n", mi
->email
.buf
);
1076 fprintf(mi
->output
, "%s: %s\n", header
[i
], hdr
->buf
);
1079 fprintf(mi
->output
, "\n");
1082 int mailinfo(struct mailinfo
*mi
, const char *msg
, const char *patch
)
1086 struct strbuf line
= STRBUF_INIT
;
1088 cmitmsg
= fopen(msg
, "w");
1093 mi
->patchfile
= fopen(patch
, "w");
1094 if (!mi
->patchfile
) {
1100 mi
->p_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->p_hdr_data
)));
1101 mi
->s_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->s_hdr_data
)));
1104 peek
= fgetc(mi
->input
);
1107 return error("empty patch: '%s'", patch
);
1109 } while (isspace(peek
));
1110 ungetc(peek
, mi
->input
);
1112 /* process the email header */
1113 while (read_one_header_line(&line
, mi
->input
))
1114 check_header(mi
, &line
, mi
->p_hdr_data
, 1);
1116 handle_body(mi
, &line
);
1117 fwrite(mi
->log_message
.buf
, 1, mi
->log_message
.len
, cmitmsg
);
1119 fclose(mi
->patchfile
);
1122 strbuf_release(&line
);
1123 return mi
->input_error
;
1126 static int git_mailinfo_config(const char *var
, const char *value
, void *mi_
)
1128 struct mailinfo
*mi
= mi_
;
1130 if (!starts_with(var
, "mailinfo."))
1131 return git_default_config(var
, value
, NULL
);
1132 if (!strcmp(var
, "mailinfo.scissors")) {
1133 mi
->use_scissors
= git_config_bool(var
, value
);
1136 /* perhaps others here */
1140 void setup_mailinfo(struct mailinfo
*mi
)
1142 memset(mi
, 0, sizeof(*mi
));
1143 strbuf_init(&mi
->name
, 0);
1144 strbuf_init(&mi
->email
, 0);
1145 strbuf_init(&mi
->charset
, 0);
1146 strbuf_init(&mi
->log_message
, 0);
1147 strbuf_init(&mi
->inbody_header_accum
, 0);
1148 mi
->header_stage
= 1;
1149 mi
->use_inbody_headers
= 1;
1150 mi
->content_top
= mi
->content
;
1151 git_config(git_mailinfo_config
, mi
);
1154 void clear_mailinfo(struct mailinfo
*mi
)
1158 strbuf_release(&mi
->name
);
1159 strbuf_release(&mi
->email
);
1160 strbuf_release(&mi
->charset
);
1161 strbuf_release(&mi
->inbody_header_accum
);
1162 free(mi
->message_id
);
1164 for (i
= 0; mi
->p_hdr_data
[i
]; i
++)
1165 strbuf_release(mi
->p_hdr_data
[i
]);
1166 free(mi
->p_hdr_data
);
1167 for (i
= 0; mi
->s_hdr_data
[i
]; i
++)
1168 strbuf_release(mi
->s_hdr_data
[i
]);
1169 free(mi
->s_hdr_data
);
1171 while (mi
->content
< mi
->content_top
) {
1172 free(*(mi
->content_top
));
1176 strbuf_release(&mi
->log_message
);