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);
74 /* it could be a regexp of form /.../ */
75 for (term = (char *) spec + 1; *term && *term != '/'; term++) {
82 /* in the scan-only case we are not interested in the regex */
86 /* try [spec+1 .. term-1] as regexp */
88 begin--; /* input is in human terms */
89 line = nth_line(data, begin);
91 if (!(reg_error = regcomp(®exp, spec + 1, REG_NEWLINE)) &&
92 !(reg_error = regexec(®exp, line, 1, match, 0))) {
93 const char *cp = line + match[0].rm_so;
96 while (begin++ < lines) {
97 nline = nth_line(data, begin);
98 if (line <= cp && cp < nline)
109 regerror(reg_error, ®exp, errbuf, 1024);
110 die("-L parameter '%s' starting at line %ld: %s",
111 spec + 1, begin + 1, errbuf);
115 static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)
119 return xecfg->find_func(bol, eol - bol, buf, 1,
120 xecfg->find_func_priv) >= 0;
125 if (isalpha(*bol) || *bol == '_' || *bol == '$')
130 static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,
136 const char *bol, *eol;
137 reg_error = regexec(regexp, start, 1, match, 0);
138 if (reg_error == REG_NOMATCH)
140 else if (reg_error) {
142 regerror(reg_error, regexp, errbuf, 1024);
143 die("-L parameter: regexec() failed: %s", errbuf);
145 /* determine extent of line matched */
146 bol = start+match[0].rm_so;
147 eol = start+match[0].rm_eo;
148 while (bol > start && *bol != '\n')
152 while (*eol && *eol != '\n')
156 /* is it a funcname line? */
157 if (match_funcname(xecfg, (char*) bol, (char*) eol))
163 static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,
164 void *cb_data, long lines, long anchor, long *begin, long *end,
169 struct userdiff_driver *drv;
170 xdemitconf_t *xecfg = NULL;
178 while (*term && *term != ':') {
179 if (*term == '\\' && *(term+1))
185 if (!begin) /* skip_range_arg case */
188 pattern = xstrndup(arg+1, term-(arg+1));
190 anchor--; /* input is in human terms */
191 start = nth_line_cb(cb_data, anchor);
193 drv = userdiff_find_by_path(path);
194 if (drv && drv->funcname.pattern) {
195 const struct userdiff_funcname *pe = &drv->funcname;
196 xecfg = xcalloc(1, sizeof(*xecfg));
197 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
200 reg_error = regcomp(®exp, pattern, REG_NEWLINE);
203 regerror(reg_error, ®exp, errbuf, 1024);
204 die("-L parameter '%s': %s", pattern, errbuf);
207 p = find_funcname_matching_regexp(xecfg, (char*) start, ®exp);
209 die("-L parameter '%s' starting at line %ld: no match",
210 pattern, anchor + 1);
212 while (p > nth_line_cb(cb_data, *begin))
216 die("-L parameter '%s' matches at EOF", pattern);
219 while (*end < lines) {
220 const char *bol = nth_line_cb(cb_data, *end);
221 const char *eol = nth_line_cb(cb_data, *end+1);
222 if (match_funcname(xecfg, bol, eol))
231 /* compensate for 1-based numbering */
237 int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
238 void *cb_data, long lines, long anchor,
239 long *begin, long *end, const char *path)
249 arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, anchor, begin, end, path);
255 arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
258 arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
263 if (*begin && *end && *end < *begin) {
265 tmp = *end; *end = *begin; *begin = tmp;
271 const char *skip_range_arg(const char *arg)
274 return parse_range_funcname(arg, NULL, NULL, 0, 0, NULL, NULL, NULL);
276 arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
279 arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);