Skip to content

Commit 173aef7

Browse files
committed
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 <[email protected]>
1 parent 849106d commit 173aef7

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

builtin/mailinfo.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
#include "utf8.h"
88
#include "strbuf.h"
99

10-
static FILE *cmitmsg, *patchfile, *fin, *fout;
10+
static FILE *cmitmsg, *patchfile;
1111

1212
static const char *metainfo_charset;
1313

1414
struct mailinfo {
15+
FILE *input;
16+
FILE *output;
17+
1518
struct strbuf name;
1619
struct strbuf email;
1720
int keep_subject;
@@ -788,16 +791,17 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
788791
return 1;
789792
}
790793

791-
static int find_boundary(struct strbuf *line)
794+
static int find_boundary(struct mailinfo *mi, struct strbuf *line)
792795
{
793-
while (!strbuf_getline(line, fin, '\n')) {
796+
while (!strbuf_getline(line, mi->input, '\n')) {
794797
if (*content_top && is_multipart_boundary(line))
795798
return 1;
796799
}
797800
return 0;
798801
}
799802

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)
801805
{
802806
struct strbuf newline = STRBUF_INIT;
803807

@@ -823,7 +827,7 @@ static int handle_boundary(struct strbuf *line, int *filter_stage, int *header_s
823827
strbuf_release(&newline);
824828

825829
/* skip to the next boundary */
826-
if (!find_boundary(line))
830+
if (!find_boundary(mi, line))
827831
return 0;
828832
goto again;
829833
}
@@ -833,26 +837,26 @@ static int handle_boundary(struct strbuf *line, int *filter_stage, int *header_s
833837
strbuf_reset(&charset);
834838

835839
/* slurp in this section's info */
836-
while (read_one_header_line(line, fin))
840+
while (read_one_header_line(line, mi->input))
837841
check_header(line, p_hdr_data, 0);
838842

839843
strbuf_release(&newline);
840844
/* replenish line */
841-
if (strbuf_getline(line, fin, '\n'))
845+
if (strbuf_getline(line, mi->input, '\n'))
842846
return 0;
843847
strbuf_addch(line, '\n');
844848
return 1;
845849
}
846850

847-
static void handle_body(struct strbuf *line)
851+
static void handle_body(struct mailinfo *mi, struct strbuf *line)
848852
{
849853
struct strbuf prev = STRBUF_INIT;
850854
int filter_stage = 0;
851855
int header_stage = 1;
852856

853857
/* Skip up to the first boundary */
854858
if (*content_top) {
855-
if (!find_boundary(line))
859+
if (!find_boundary(mi, line))
856860
goto handle_body_out;
857861
}
858862

@@ -864,7 +868,7 @@ static void handle_body(struct strbuf *line)
864868
handle_filter(&prev, &filter_stage, &header_stage);
865869
strbuf_reset(&prev);
866870
}
867-
if (!handle_boundary(line, &filter_stage, &header_stage))
871+
if (!handle_boundary(mi, line, &filter_stage, &header_stage))
868872
goto handle_body_out;
869873
}
870874

@@ -907,7 +911,7 @@ static void handle_body(struct strbuf *line)
907911
handle_filter(line, &filter_stage, &header_stage);
908912
}
909913

910-
} while (!strbuf_getwholeline(line, fin, '\n'));
914+
} while (!strbuf_getwholeline(line, mi->input, '\n'));
911915

912916
handle_body_out:
913917
strbuf_release(&prev);
@@ -949,29 +953,25 @@ static void handle_info(struct mailinfo *mi)
949953
cleanup_subject(mi, hdr);
950954
cleanup_space(hdr);
951955
}
952-
output_header_lines(fout, "Subject", hdr);
956+
output_header_lines(mi->output, "Subject", hdr);
953957
} else if (!strcmp(header[i], "From")) {
954958
cleanup_space(hdr);
955959
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);
958962
} else {
959963
cleanup_space(hdr);
960-
fprintf(fout, "%s: %s\n", header[i], hdr->buf);
964+
fprintf(mi->output, "%s: %s\n", header[i], hdr->buf);
961965
}
962966
}
963-
fprintf(fout, "\n");
967+
fprintf(mi->output, "\n");
964968
}
965969

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)
968971
{
969972
int peek;
970973
struct strbuf line = STRBUF_INIT;
971974

972-
fin = in;
973-
fout = out;
974-
975975
cmitmsg = fopen(msg, "w");
976976
if (!cmitmsg) {
977977
perror(msg);
@@ -988,15 +988,15 @@ static int mailinfo(struct mailinfo *mi,
988988
s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data));
989989

990990
do {
991-
peek = fgetc(in);
991+
peek = fgetc(mi->input);
992992
} while (isspace(peek));
993-
ungetc(peek, in);
993+
ungetc(peek, mi->input);
994994

995995
/* process the email header */
996-
while (read_one_header_line(&line, fin))
996+
while (read_one_header_line(&line, mi->input))
997997
check_header(&line, p_hdr_data, 1);
998998

999-
handle_body(&line);
999+
handle_body(mi, &line);
10001000
fclose(patchfile);
10011001

10021002
handle_info(mi);
@@ -1074,7 +1074,9 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
10741074
if (argc != 3)
10751075
usage(mailinfo_usage);
10761076

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]);
10781080
clear_mailinfo(&mi);
10791081

10801082
return status;

0 commit comments

Comments
 (0)