mailinfo: move global "FILE *fin, *fout" to struct mailinfo

This requires us to pass "struct mailinfo" to more functions throughout the codepath that read input lines. Incidentally, later steps are helped by this patch passing the struct to more callchains. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 14, 2015 at 15:40 UTC 173aef7c2ee9aa0021dad7c70d30f9099be4a134
1 file changed +28 -26
builtin/mailinfo.c
+28 -26
@@ -7,11 +7,14 @@
7 #include "utf8.h"
8 #include "strbuf.h"
9
10 -static FILE *cmitmsg, *patchfile, *fin, *fout;
10 +static FILE *cmitmsg, *patchfile;
11
12 static const char *metainfo_charset;
13
14 struct mailinfo {
15 + FILE *input;
16 + FILE *output;
17 +
18 struct strbuf name;
19 struct strbuf email;
20 int keep_subject;
@@ -788,16 +791,17 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
791 return 1;
792 }
793
791 -static int find_boundary(struct strbuf *line)
794 +static int find_boundary(struct mailinfo *mi, struct strbuf *line)
795 {
793 - while (!strbuf_getline(line, fin, '\n')) {
796 + while (!strbuf_getline(line, mi->input, '\n')) {
797 if (*content_top && is_multipart_boundary(line))
798 return 1;
799 }
800 return 0;
801 }
802
800 -static int handle_boundary(struct strbuf *line, int *filter_stage, int *header_stage)
803 +static int handle_boundary(struct mailinfo *mi, struct strbuf *line,
804 + int *filter_stage, int *header_stage)
805 {
806 struct strbuf newline = STRBUF_INIT;
807
@@ -823,7 +827,7 @@ again:
827 strbuf_release(&newline);
828
829 /* skip to the next boundary */
826 - if (!find_boundary(line))
830 + if (!find_boundary(mi, line))
831 return 0;
832 goto again;
833 }
@@ -833,18 +837,18 @@ again:
837 strbuf_reset(&charset);
838
839 /* slurp in this section's info */
836 - while (read_one_header_line(line, fin))
840 + while (read_one_header_line(line, mi->input))
841 check_header(line, p_hdr_data, 0);
842
843 strbuf_release(&newline);
844 /* replenish line */
841 - if (strbuf_getline(line, fin, '\n'))
845 + if (strbuf_getline(line, mi->input, '\n'))
846 return 0;
847 strbuf_addch(line, '\n');
848 return 1;
849 }
850
847 -static void handle_body(struct strbuf *line)
851 +static void handle_body(struct mailinfo *mi, struct strbuf *line)
852 {
853 struct strbuf prev = STRBUF_INIT;
854 int filter_stage = 0;
@@ -852,7 +856,7 @@ static void handle_body(struct strbuf *line)
856
857 /* Skip up to the first boundary */
858 if (*content_top) {
855 - if (!find_boundary(line))
859 + if (!find_boundary(mi, line))
860 goto handle_body_out;
861 }
862
@@ -864,7 +868,7 @@ static void handle_body(struct strbuf *line)
868 handle_filter(&prev, &filter_stage, &header_stage);
869 strbuf_reset(&prev);
870 }
867 - if (!handle_boundary(line, &filter_stage, &header_stage))
871 + if (!handle_boundary(mi, line, &filter_stage, &header_stage))
872 goto handle_body_out;
873 }
874
@@ -907,7 +911,7 @@ static void handle_body(struct strbuf *line)
911 handle_filter(line, &filter_stage, &header_stage);
912 }
913
910 - } while (!strbuf_getwholeline(line, fin, '\n'));
914 + } while (!strbuf_getwholeline(line, mi->input, '\n'));
915
916 handle_body_out:
917 strbuf_release(&prev);
@@ -949,29 +953,25 @@ static void handle_info(struct mailinfo *mi)
953 cleanup_subject(mi, hdr);
954 cleanup_space(hdr);
955 }
952 - output_header_lines(fout, "Subject", hdr);
956 + output_header_lines(mi->output, "Subject", hdr);
957 } else if (!strcmp(header[i], "From")) {
958 cleanup_space(hdr);
959 handle_from(mi, hdr);
956 - fprintf(fout, "Author: %s\n", mi->name.buf);
957 - fprintf(fout, "Email: %s\n", mi->email.buf);
960 + fprintf(mi->output, "Author: %s\n", mi->name.buf);
961 + fprintf(mi->output, "Email: %s\n", mi->email.buf);
962 } else {
963 cleanup_space(hdr);
960 - fprintf(fout, "%s: %s\n", header[i], hdr->buf);
964 + fprintf(mi->output, "%s: %s\n", header[i], hdr->buf);
965 }
966 }
963 - fprintf(fout, "\n");
967 + fprintf(mi->output, "\n");
968 }
969
966 -static int mailinfo(struct mailinfo *mi,
967 - FILE *in, FILE *out, const char *msg, const char *patch)
970 +static int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
971 {
972 int peek;
973 struct strbuf line = STRBUF_INIT;
974
972 - fin = in;
973 - fout = out;
974 -
975 cmitmsg = fopen(msg, "w");
976 if (!cmitmsg) {
977 perror(msg);
@@ -988,15 +988,15 @@ static int mailinfo(struct mailinfo *mi,
988 s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data));
989
990 do {
991 - peek = fgetc(in);
991 + peek = fgetc(mi->input);
992 } while (isspace(peek));
993 - ungetc(peek, in);
993 + ungetc(peek, mi->input);
994
995 /* process the email header */
996 - while (read_one_header_line(&line, fin))
996 + while (read_one_header_line(&line, mi->input))
997 check_header(&line, p_hdr_data, 1);
998
999 - handle_body(&line);
999 + handle_body(mi, &line);
1000 fclose(patchfile);
1001
1002 handle_info(mi);
@@ -1074,7 +1074,9 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
1074 if (argc != 3)
1075 usage(mailinfo_usage);
1076
1077 - status = !!mailinfo(&mi, stdin, stdout, argv[1], argv[2]);
1077 + mi.input = stdin;
1078 + mi.output = stdout;
1079 + status = !!mailinfo(&mi, argv[1], argv[2]);
1080 clear_mailinfo(&mi);
1081
1082 return status;