1 #include "git-compat-util.h"
2 #include "line-range.h"
3 #include "xdiff-interface.h"
7 * Parse one item in the -L option
9 * 'begin' is applicable only to relative range anchors. Absolute anchors
12 * When parsing "-L A,B", parse_loc() is called once for A and once for B.
14 * When parsing A, 'begin' must be a negative number, the absolute value of
15 * which is the line at which relative start-of-range anchors should be
16 * based. Beginning of file is represented by -1.
18 * When parsing B, 'begin' must be the positive line number immediately
19 * following the line computed for 'A'.
21 static const char *parse_loc(const char *spec
, nth_line_fn_t nth_line
,
22 void *data
, long lines
, long begin
, long *ret
)
31 /* Allow "-L <something>,+20" to mean starting at <something>
32 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
35 if (1 <= begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
36 num
= strtol(spec
+ 1, &term
, 10);
37 if (term
!= spec
+ 1) {
41 die("-L invalid empty range");
45 *ret
= begin
+ num
- 2;
49 *ret
= begin
+ num
> 0 ? begin
+ num
: 1;
54 num
= strtol(spec
, &term
, 10);
58 die("-L invalid line number: %ld", num
);
76 /* it could be a regexp of form /.../ */
77 for (term
= (char *) spec
+ 1; *term
&& *term
!= '/'; term
++) {
84 /* in the scan-only case we are not interested in the regex */
88 /* try [spec+1 .. term-1] as regexp */
90 begin
--; /* input is in human terms */
91 line
= nth_line(data
, begin
);
93 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
94 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
95 const char *cp
= line
+ match
[0].rm_so
;
98 while (begin
++ < lines
) {
99 nline
= nth_line(data
, begin
);
100 if (line
<= cp
&& cp
< nline
)
111 regerror(reg_error
, ®exp
, errbuf
, 1024);
112 die("-L parameter '%s' starting at line %ld: %s",
113 spec
+ 1, begin
+ 1, errbuf
);
117 static int match_funcname(xdemitconf_t
*xecfg
, const char *bol
, const char *eol
)
121 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
122 xecfg
->find_func_priv
) >= 0;
127 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
132 static const char *find_funcname_matching_regexp(xdemitconf_t
*xecfg
, const char *start
,
138 const char *bol
, *eol
;
139 reg_error
= regexec(regexp
, start
, 1, match
, 0);
140 if (reg_error
== REG_NOMATCH
)
142 else if (reg_error
) {
144 regerror(reg_error
, regexp
, errbuf
, 1024);
145 die("-L parameter: regexec() failed: %s", errbuf
);
147 /* determine extent of line matched */
148 bol
= start
+match
[0].rm_so
;
149 eol
= start
+match
[0].rm_eo
;
150 while (bol
> start
&& *--bol
!= '\n')
154 while (*eol
&& *eol
!= '\n')
158 /* is it a funcname line? */
159 if (match_funcname(xecfg
, (char*) bol
, (char*) eol
))
166 static const char *parse_range_funcname(
167 const char *arg
, nth_line_fn_t nth_line_cb
,
168 void *cb_data
, long lines
, long anchor
, long *begin
, long *end
,
169 const char *path
, struct index_state
*istate
)
173 struct userdiff_driver
*drv
;
174 xdemitconf_t
*xecfg
= NULL
;
187 while (*term
&& *term
!= ':') {
188 if (*term
== '\\' && *(term
+1))
194 if (!begin
) /* skip_range_arg case */
197 pattern
= xstrndup(arg
+1, term
-(arg
+1));
199 anchor
--; /* input is in human terms */
200 start
= nth_line_cb(cb_data
, anchor
);
202 drv
= userdiff_find_by_path(istate
, path
);
203 if (drv
&& drv
->funcname
.pattern
) {
204 const struct userdiff_funcname
*pe
= &drv
->funcname
;
205 CALLOC_ARRAY(xecfg
, 1);
206 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
209 reg_error
= regcomp(®exp
, pattern
, REG_NEWLINE
);
212 regerror(reg_error
, ®exp
, errbuf
, 1024);
213 die("-L parameter '%s': %s", pattern
, errbuf
);
216 p
= find_funcname_matching_regexp(xecfg
, (char*) start
, ®exp
);
218 die("-L parameter '%s' starting at line %ld: no match",
219 pattern
, anchor
+ 1);
221 while (p
> nth_line_cb(cb_data
, *begin
))
225 die("-L parameter '%s' matches at EOF", pattern
);
228 while (*end
< lines
) {
229 const char *bol
= nth_line_cb(cb_data
, *end
);
230 const char *eol
= nth_line_cb(cb_data
, *end
+1);
231 if (match_funcname(xecfg
, bol
, eol
))
238 xdiff_clear_find_func(xecfg
);
242 /* compensate for 1-based numbering */
248 int parse_range_arg(const char *arg
, nth_line_fn_t nth_line_cb
,
249 void *cb_data
, long lines
, long anchor
,
250 long *begin
, long *end
,
251 const char *path
, struct index_state
*istate
)
260 if (*arg
== ':' || (*arg
== '^' && *(arg
+ 1) == ':')) {
261 arg
= parse_range_funcname(arg
, nth_line_cb
, cb_data
,
262 lines
, anchor
, begin
, end
,
269 arg
= parse_loc(arg
, nth_line_cb
, cb_data
, lines
, -anchor
, begin
);
272 arg
= parse_loc(arg
+ 1, nth_line_cb
, cb_data
, lines
, *begin
+ 1, end
);
277 if (*begin
&& *end
&& *end
< *begin
) {
284 const char *skip_range_arg(const char *arg
, struct index_state
*istate
)
286 if (*arg
== ':' || (*arg
== '^' && *(arg
+ 1) == ':'))
287 return parse_range_funcname(arg
, NULL
, NULL
,
291 arg
= parse_loc(arg
, NULL
, NULL
, 0, -1, NULL
);
294 arg
= parse_loc(arg
+1, NULL
, NULL
, 0, 0, NULL
);