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);
59 die("-L invalid line number: %ld", num);
77 /* it could be a regexp of form /.../ */
78 for (term = (char *) spec + 1; *term && *term != '/'; term++) {
85 /* in the scan-only case we are not interested in the regex */
89 /* try [spec+1 .. term-1] as regexp */
91 begin--; /* input is in human terms */
92 line = nth_line(data, begin);
94 if (!(reg_error = regcomp(®exp, spec + 1, REG_NEWLINE)) &&
95 !(reg_error = regexec(®exp, line, 1, match, 0))) {
96 const char *cp = line + match[0].rm_so;
99 while (begin++ < lines) {
100 nline = nth_line(data, begin);
101 if (line <= cp && cp < nline)
112 regerror(reg_error, ®exp, errbuf, 1024);
113 die("-L parameter '%s' starting at line %ld: %s",
114 spec + 1, begin + 1, errbuf);
118 static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)
122 return xecfg->find_func(bol, eol - bol, buf, 1,
123 xecfg->find_func_priv) >= 0;
128 if (isalpha(*bol) || *bol == '_' || *bol == '$')
133 static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,
139 const char *bol, *eol;
140 reg_error = regexec(regexp, start, 1, match, 0);
141 if (reg_error == REG_NOMATCH)
143 else if (reg_error) {
145 regerror(reg_error, regexp, errbuf, 1024);
146 die("-L parameter: regexec() failed: %s", errbuf);
148 /* determine extent of line matched */
149 bol = start+match[0].rm_so;
150 eol = start+match[0].rm_eo;
151 while (bol > start && *bol != '\n')
155 while (*eol && *eol != '\n')
159 /* is it a funcname line? */
160 if (match_funcname(xecfg, (char*) bol, (char*) eol))
166 static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,
167 void *cb_data, long lines, long anchor, long *begin, long *end,
172 struct userdiff_driver *drv;
173 xdemitconf_t *xecfg = NULL;
186 while (*term && *term != ':') {
187 if (*term == '\\' && *(term+1))
193 if (!begin) /* skip_range_arg case */
196 pattern = xstrndup(arg+1, term-(arg+1));
198 anchor--; /* input is in human terms */
199 start = nth_line_cb(cb_data, anchor);
201 drv = userdiff_find_by_path(path);
202 if (drv && drv->funcname.pattern) {
203 const struct userdiff_funcname *pe = &drv->funcname;
204 xecfg = xcalloc(1, sizeof(*xecfg));
205 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
208 reg_error = regcomp(®exp, pattern, REG_NEWLINE);
211 regerror(reg_error, ®exp, errbuf, 1024);
212 die("-L parameter '%s': %s", pattern, errbuf);
215 p = find_funcname_matching_regexp(xecfg, (char*) start, ®exp);
217 die("-L parameter '%s' starting at line %ld: no match",
218 pattern, anchor + 1);
220 while (p > nth_line_cb(cb_data, *begin))
224 die("-L parameter '%s' matches at EOF", pattern);
227 while (*end < lines) {
228 const char *bol = nth_line_cb(cb_data, *end);
229 const char *eol = nth_line_cb(cb_data, *end+1);
230 if (match_funcname(xecfg, bol, eol))
239 /* compensate for 1-based numbering */
245 int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
246 void *cb_data, long lines, long anchor,
247 long *begin, long *end, const char *path)
256 if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':')) {
257 arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, anchor, begin, end, path);
263 arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
266 arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
271 if (*begin && *end && *end < *begin) {
278 const char *skip_range_arg(const char *arg)
280 if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':'))
281 return parse_range_funcname(arg, NULL, NULL, 0, 0, NULL, NULL, NULL);
283 arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
286 arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);