2 #include "xdiff-interface.h"
4 static int parse_num(char **cp_p
, int *num_p
)
10 while ('0' <= *cp
&& *cp
<= '9')
11 num
= num
* 10 + *cp
++ - '0';
12 if (!(read_some
= cp
- *cp_p
))
19 int parse_hunk_header(char *line
, int len
,
25 if (parse_num(&cp
, ob
)) {
27 return error("malformed diff output: %s", line
);
31 if (parse_num(&cp
, on
))
36 if (*cp
++ != ' ' || *cp
++ != '+')
38 if (parse_num(&cp
, nb
))
42 if (parse_num(&cp
, nn
))
47 return -!!memcmp(cp
, " @@", 3);
50 static void consume_one(void *priv_
, char *s
, unsigned long size
)
52 struct xdiff_emit_state
*priv
= priv_
;
55 unsigned long this_size
;
56 ep
= memchr(s
, '\n', size
);
57 this_size
= (ep
== NULL
) ? size
: (ep
- s
+ 1);
58 priv
->consume(priv
, s
, this_size
);
64 int xdiff_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
66 struct xdiff_emit_state
*priv
= priv_
;
69 for (i
= 0; i
< nbuf
; i
++) {
70 if (mb
[i
].ptr
[mb
[i
].size
-1] != '\n') {
72 priv
->remainder
= xrealloc(priv
->remainder
,
73 priv
->remainder_size
+
75 memcpy(priv
->remainder
+ priv
->remainder_size
,
76 mb
[i
].ptr
, mb
[i
].size
);
77 priv
->remainder_size
+= mb
[i
].size
;
81 /* we have a complete line */
82 if (!priv
->remainder
) {
83 consume_one(priv
, mb
[i
].ptr
, mb
[i
].size
);
86 priv
->remainder
= xrealloc(priv
->remainder
,
87 priv
->remainder_size
+
89 memcpy(priv
->remainder
+ priv
->remainder_size
,
90 mb
[i
].ptr
, mb
[i
].size
);
91 consume_one(priv
, priv
->remainder
,
92 priv
->remainder_size
+ mb
[i
].size
);
93 free(priv
->remainder
);
94 priv
->remainder
= NULL
;
95 priv
->remainder_size
= 0;
97 if (priv
->remainder
) {
98 consume_one(priv
, priv
->remainder
, priv
->remainder_size
);
99 free(priv
->remainder
);
100 priv
->remainder
= NULL
;
101 priv
->remainder_size
= 0;
107 * Trim down common substring at the end of the buffers,
108 * but leave at least ctx lines at the end.
110 static void trim_common_tail(mmfile_t
*a
, mmfile_t
*b
, long ctx
)
112 const int blk
= 1024;
113 long trimmed
= 0, recovered
= 0, i
;
114 char *ap
= a
->ptr
+ a
->size
;
115 char *bp
= b
->ptr
+ b
->size
;
116 long smaller
= (a
->size
< b
->size
) ? a
->size
: b
->size
;
118 while (blk
+ trimmed
<= smaller
&& !memcmp(ap
- blk
, bp
- blk
, blk
)) {
124 for (i
= 0, recovered
= 0; recovered
< trimmed
&& i
<= ctx
; i
++) {
125 while (recovered
< trimmed
&& ap
[recovered
] != '\n')
128 a
->size
-= (trimmed
- recovered
);
129 b
->size
-= (trimmed
- recovered
);
132 int xdi_diff(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
, xdemitconf_t
const *xecfg
, xdemitcb_t
*xecb
)
137 trim_common_tail(&a
, &b
, xecfg
->ctxlen
);
139 return xdl_diff(&a
, &b
, xpp
, xecfg
, xecb
);
142 int read_mmfile(mmfile_t
*ptr
, const char *filename
)
148 if (stat(filename
, &st
))
149 return error("Could not stat %s", filename
);
150 if ((f
= fopen(filename
, "rb")) == NULL
)
151 return error("Could not open %s", filename
);
152 sz
= xsize_t(st
.st_size
);
153 ptr
->ptr
= xmalloc(sz
);
154 if (fread(ptr
->ptr
, sz
, 1, f
) != 1)
155 return error("Could not read %s", filename
);
161 #define FIRST_FEW_BYTES 8000
162 int buffer_is_binary(const char *ptr
, unsigned long size
)
164 if (FIRST_FEW_BYTES
< size
)
165 size
= FIRST_FEW_BYTES
;
166 return !!memchr(ptr
, 0, size
);
177 static long ff_regexp(const char *line
, long len
,
178 char *buffer
, long buffer_size
, void *priv
)
180 char *line_buffer
= xstrndup(line
, len
); /* make NUL terminated */
181 struct ff_regs
*regs
= priv
;
182 regmatch_t pmatch
[2];
185 for (i
= 0; i
< regs
->nr
; i
++) {
186 struct ff_reg
*reg
= regs
->array
+ i
;
187 if (reg
->negate
^ !!regexec(®
->re
,
188 line_buffer
, 2, pmatch
, 0)) {
193 i
= pmatch
[1].rm_so
>= 0 ? 1 : 0;
194 line
+= pmatch
[i
].rm_so
;
195 result
= pmatch
[i
].rm_eo
- pmatch
[i
].rm_so
;
196 if (result
> buffer_size
)
197 result
= buffer_size
;
199 while (result
> 0 && (isspace(line
[result
- 1]) ||
200 line
[result
- 1] == '\n'))
202 memcpy(buffer
, line
, result
);
207 void xdiff_set_find_func(xdemitconf_t
*xecfg
, const char *value
)
210 struct ff_regs
*regs
;
212 xecfg
->find_func
= ff_regexp
;
213 regs
= xecfg
->find_func_priv
= xmalloc(sizeof(struct ff_regs
));
214 for (i
= 0, regs
->nr
= 1; value
[i
]; i
++)
215 if (value
[i
] == '\n')
217 regs
->array
= xmalloc(regs
->nr
* sizeof(struct ff_reg
));
218 for (i
= 0; i
< regs
->nr
; i
++) {
219 struct ff_reg
*reg
= regs
->array
+ i
;
220 const char *ep
= strchr(value
, '\n'), *expression
;
223 reg
->negate
= (*value
== '!');
224 if (reg
->negate
&& i
== regs
->nr
- 1)
225 die("Last expression must not be negated: %s", value
);
229 expression
= buffer
= xstrndup(value
, ep
- value
);
232 if (regcomp(®
->re
, expression
, 0))
233 die("Invalid regexp to look for hunk header: %s", expression
);