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