1 #include "git-compat-util.h"
2 #include "line-range.h"
3 #include "xdiff-interface.h"
8 * Parse one item in the -L option
10 * 'begin' is applicable only to relative range anchors. Absolute anchors
13 * When parsing "-L A,B", parse_loc() is called once for A and once for B.
15 * When parsing A, 'begin' must be a negative number, the absolute value of
16 * which is the line at which relative start-of-range anchors should be
17 * based. Beginning of file is represented by -1.
19 * When parsing B, 'begin' must be the positive line number immediately
20 * following the line computed for 'A'.
22 static const char *parse_loc(const char *spec
, nth_line_fn_t nth_line
,
23 void *data
, long lines
, long begin
, long *ret
)
32 /* Allow "-L <something>,+20" to mean starting at <something>
33 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
36 if (1 <= begin
&& (spec
[0] == '+' || spec
[0] == '-')) {
37 num
= strtol(spec
+ 1, &term
, 10);
38 if (term
!= spec
+ 1) {
42 die("-L invalid empty range");
46 *ret
= begin
+ num
- 2;
55 num
= strtol(spec
, &term
, 10);
68 /* it could be a regexp of form /.../ */
69 for (term
= (char *) spec
+ 1; *term
&& *term
!= '/'; term
++) {
76 /* in the scan-only case we are not interested in the regex */
80 /* try [spec+1 .. term-1] as regexp */
82 begin
--; /* input is in human terms */
83 line
= nth_line(data
, begin
);
85 if (!(reg_error
= regcomp(®exp
, spec
+ 1, REG_NEWLINE
)) &&
86 !(reg_error
= regexec(®exp
, line
, 1, match
, 0))) {
87 const char *cp
= line
+ match
[0].rm_so
;
90 while (begin
++ < lines
) {
91 nline
= nth_line(data
, begin
);
92 if (line
<= cp
&& cp
< nline
)
103 regerror(reg_error
, ®exp
, errbuf
, 1024);
104 die("-L parameter '%s' starting at line %ld: %s",
105 spec
+ 1, begin
+ 1, errbuf
);
109 static int match_funcname(xdemitconf_t
*xecfg
, const char *bol
, const char *eol
)
113 return xecfg
->find_func(bol
, eol
- bol
, buf
, 1,
114 xecfg
->find_func_priv
) >= 0;
119 if (isalpha(*bol
) || *bol
== '_' || *bol
== '$')
124 static const char *find_funcname_matching_regexp(xdemitconf_t
*xecfg
, const char *start
,
130 const char *bol
, *eol
;
131 reg_error
= regexec(regexp
, start
, 1, match
, 0);
132 if (reg_error
== REG_NOMATCH
)
134 else if (reg_error
) {
136 regerror(reg_error
, regexp
, errbuf
, 1024);
137 die("-L parameter: regexec() failed: %s", errbuf
);
139 /* determine extent of line matched */
140 bol
= start
+match
[0].rm_so
;
141 eol
= start
+match
[0].rm_eo
;
142 while (bol
> start
&& *bol
!= '\n')
146 while (*eol
&& *eol
!= '\n')
150 /* is it a funcname line? */
151 if (match_funcname(xecfg
, (char*) bol
, (char*) eol
))
157 static const char *parse_range_funcname(const char *arg
, nth_line_fn_t nth_line_cb
,
158 void *cb_data
, long lines
, long *begin
, long *end
,
163 struct userdiff_driver
*drv
;
164 xdemitconf_t
*xecfg
= NULL
;
172 while (*term
&& *term
!= ':') {
173 if (*term
== '\\' && *(term
+1))
179 if (!begin
) /* skip_range_arg case */
182 pattern
= xstrndup(arg
+1, term
-(arg
+1));
184 start
= nth_line_cb(cb_data
, 0);
186 drv
= userdiff_find_by_path(path
);
187 if (drv
&& drv
->funcname
.pattern
) {
188 const struct userdiff_funcname
*pe
= &drv
->funcname
;
189 xecfg
= xcalloc(1, sizeof(*xecfg
));
190 xdiff_set_find_func(xecfg
, pe
->pattern
, pe
->cflags
);
193 reg_error
= regcomp(®exp
, pattern
, REG_NEWLINE
);
196 regerror(reg_error
, ®exp
, errbuf
, 1024);
197 die("-L parameter '%s': %s", pattern
, errbuf
);
200 p
= find_funcname_matching_regexp(xecfg
, (char*) start
, ®exp
);
202 die("-L parameter '%s': no match", pattern
);
204 while (p
> nth_line_cb(cb_data
, *begin
))
208 die("-L parameter '%s' matches at EOF", pattern
);
211 while (*end
< lines
) {
212 const char *bol
= nth_line_cb(cb_data
, *end
);
213 const char *eol
= nth_line_cb(cb_data
, *end
+1);
214 if (match_funcname(xecfg
, bol
, eol
))
223 /* compensate for 1-based numbering */
229 int parse_range_arg(const char *arg
, nth_line_fn_t nth_line_cb
,
230 void *cb_data
, long lines
, long anchor
,
231 long *begin
, long *end
, const char *path
)
241 arg
= parse_range_funcname(arg
, nth_line_cb
, cb_data
, lines
, begin
, end
, path
);
247 arg
= parse_loc(arg
, nth_line_cb
, cb_data
, lines
, -anchor
, begin
);
250 arg
= parse_loc(arg
+ 1, nth_line_cb
, cb_data
, lines
, *begin
+ 1, end
);
255 if (*begin
&& *end
&& *end
< *begin
) {
257 tmp
= *end
; *end
= *begin
; *begin
= tmp
;
263 const char *skip_range_arg(const char *arg
)
266 return parse_range_funcname(arg
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
268 arg
= parse_loc(arg
, NULL
, NULL
, 0, -1, NULL
);
271 arg
= parse_loc(arg
+1, NULL
, NULL
, 0, 0, NULL
);