10
10
#include "strbuf.h"
11
11
#include "quote.h"
12
12
13
- typedef struct config_file {
14
- struct config_file * prev ;
15
- FILE * f ;
13
+ struct config_source {
14
+ struct config_source * prev ;
15
+ union {
16
+ FILE * file ;
17
+ } u ;
16
18
const char * name ;
17
19
int linenr ;
18
20
int eof ;
19
21
struct strbuf value ;
20
22
struct strbuf var ;
21
- } config_file ;
22
23
23
- static config_file * cf ;
24
+ int (* fgetc )(struct config_source * c );
25
+ int (* ungetc )(int c , struct config_source * conf );
26
+ long (* ftell )(struct config_source * c );
27
+ };
28
+
29
+ static struct config_source * cf ;
24
30
25
31
static int zlib_compression_seen ;
26
32
33
+ static int config_file_fgetc (struct config_source * conf )
34
+ {
35
+ return fgetc (conf -> u .file );
36
+ }
37
+
38
+ static int config_file_ungetc (int c , struct config_source * conf )
39
+ {
40
+ return ungetc (c , conf -> u .file );
41
+ }
42
+
43
+ static long config_file_ftell (struct config_source * conf )
44
+ {
45
+ return ftell (conf -> u .file );
46
+ }
47
+
27
48
#define MAX_INCLUDE_DEPTH 10
28
49
static const char include_depth_advice [] =
29
50
"exceeded maximum include depth (%d) while including\n"
@@ -168,15 +189,13 @@ int git_config_from_parameters(config_fn_t fn, void *data)
168
189
169
190
static int get_next_char (void )
170
191
{
171
- int c ;
172
- FILE * f = cf -> f ;
192
+ int c = cf -> fgetc (cf );
173
193
174
- c = fgetc (f );
175
194
if (c == '\r' ) {
176
195
/* DOS like systems */
177
- c = fgetc (f );
196
+ c = cf -> fgetc (cf );
178
197
if (c != '\n' ) {
179
- ungetc (c , f );
198
+ cf -> ungetc (c , cf );
180
199
c = '\r' ;
181
200
}
182
201
}
@@ -336,7 +355,7 @@ static int get_base_var(struct strbuf *name)
336
355
}
337
356
}
338
357
339
- static int git_parse_file (config_fn_t fn , void * data )
358
+ static int git_parse_source (config_fn_t fn , void * data )
340
359
{
341
360
int comment = 0 ;
342
361
int baselen = 0 ;
@@ -894,10 +913,11 @@ int git_default_config(const char *var, const char *value, void *dummy)
894
913
}
895
914
896
915
/*
897
- * The fields f and name of top need to be initialized before calling
916
+ * All source specific fields in the union, name and the callbacks
917
+ * fgetc, ungetc, ftell of top need to be initialized before calling
898
918
* this function.
899
919
*/
900
- static int do_config_from (struct config_file * top , config_fn_t fn , void * data )
920
+ static int do_config_from (struct config_source * top , config_fn_t fn , void * data )
901
921
{
902
922
int ret ;
903
923
@@ -909,7 +929,7 @@ static int do_config_from(struct config_file *top, config_fn_t fn, void *data)
909
929
strbuf_init (& top -> var , 1024 );
910
930
cf = top ;
911
931
912
- ret = git_parse_file (fn , data );
932
+ ret = git_parse_source (fn , data );
913
933
914
934
/* pop config-file parsing state stack */
915
935
strbuf_release (& top -> value );
@@ -926,10 +946,13 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
926
946
927
947
ret = -1 ;
928
948
if (f ) {
929
- config_file top ;
949
+ struct config_source top ;
930
950
931
- top .f = f ;
951
+ top .u . file = f ;
932
952
top .name = filename ;
953
+ top .fgetc = config_file_fgetc ;
954
+ top .ungetc = config_file_ungetc ;
955
+ top .ftell = config_file_ftell ;
933
956
934
957
ret = do_config_from (& top , fn , data );
935
958
@@ -1064,7 +1087,6 @@ static int store_aux(const char *key, const char *value, void *cb)
1064
1087
{
1065
1088
const char * ep ;
1066
1089
size_t section_len ;
1067
- FILE * f = cf -> f ;
1068
1090
1069
1091
switch (store .state ) {
1070
1092
case KEY_SEEN :
@@ -1076,7 +1098,7 @@ static int store_aux(const char *key, const char *value, void *cb)
1076
1098
return 1 ;
1077
1099
}
1078
1100
1079
- store .offset [store .seen ] = ftell (f );
1101
+ store .offset [store .seen ] = cf -> ftell (cf );
1080
1102
store .seen ++ ;
1081
1103
}
1082
1104
break ;
@@ -1103,19 +1125,19 @@ static int store_aux(const char *key, const char *value, void *cb)
1103
1125
* Do not increment matches: this is no match, but we
1104
1126
* just made sure we are in the desired section.
1105
1127
*/
1106
- store .offset [store .seen ] = ftell (f );
1128
+ store .offset [store .seen ] = cf -> ftell (cf );
1107
1129
/* fallthru */
1108
1130
case SECTION_END_SEEN :
1109
1131
case START :
1110
1132
if (matches (key , value )) {
1111
- store .offset [store .seen ] = ftell (f );
1133
+ store .offset [store .seen ] = cf -> ftell (cf );
1112
1134
store .state = KEY_SEEN ;
1113
1135
store .seen ++ ;
1114
1136
} else {
1115
1137
if (strrchr (key , '.' ) - key == store .baselen &&
1116
1138
!strncmp (key , store .key , store .baselen )) {
1117
1139
store .state = SECTION_SEEN ;
1118
- store .offset [store .seen ] = ftell (f );
1140
+ store .offset [store .seen ] = cf -> ftell (cf );
1119
1141
}
1120
1142
}
1121
1143
}
0 commit comments