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 static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
11 void *data, long lines, long begin, long *ret)
20 /* Allow "-L <something>,+20" to mean starting at <something>
21 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
24 if (1 <= begin && (spec[0] == '+' || spec[0] == '-')) {
25 num = strtol(spec + 1, &term, 10);
26 if (term != spec + 1) {
30 die("-L invalid empty range");
34 *ret = begin + num - 2;
43 num = strtol(spec, &term, 10);
52 /* it could be a regexp of form /.../ */
53 for (term = (char *) spec + 1; *term && *term != '/'; term++) {
60 /* in the scan-only case we are not interested in the regex */
64 /* try [spec+1 .. term-1] as regexp */
66 begin--; /* input is in human terms */
67 line = nth_line(data, begin);
69 if (!(reg_error = regcomp(®exp, spec + 1, REG_NEWLINE)) &&
70 !(reg_error = regexec(®exp, line, 1, match, 0))) {
71 const char *cp = line + match[0].rm_so;
74 while (begin++ < lines) {
75 nline = nth_line(data, begin);
76 if (line <= cp && cp < nline)
87 regerror(reg_error, ®exp, errbuf, 1024);
88 die("-L parameter '%s': %s", spec + 1, errbuf);
92 static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)
96 return xecfg->find_func(bol, eol - bol, buf, 1,
97 xecfg->find_func_priv) >= 0;
102 if (isalpha(*bol) || *bol == '_' || *bol == '$')
107 static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,
113 const char *bol, *eol;
114 reg_error = regexec(regexp, start, 1, match, 0);
115 if (reg_error == REG_NOMATCH)
117 else if (reg_error) {
119 regerror(reg_error, regexp, errbuf, 1024);
120 die("-L parameter: regexec() failed: %s", errbuf);
122 /* determine extent of line matched */
123 bol = start+match[0].rm_so;
124 eol = start+match[0].rm_eo;
125 while (bol > start && *bol != '\n')
129 while (*eol && *eol != '\n')
133 /* is it a funcname line? */
134 if (match_funcname(xecfg, (char*) bol, (char*) eol))
140 static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,
141 void *cb_data, long lines, long *begin, long *end,
146 struct userdiff_driver *drv;
147 xdemitconf_t *xecfg = NULL;
155 while (*term && *term != ':') {
156 if (*term == '\\' && *(term+1))
162 if (!begin) /* skip_range_arg case */
165 pattern = xstrndup(arg+1, term-(arg+1));
167 start = nth_line_cb(cb_data, 0);
169 drv = userdiff_find_by_path(path);
170 if (drv && drv->funcname.pattern) {
171 const struct userdiff_funcname *pe = &drv->funcname;
172 xecfg = xcalloc(1, sizeof(*xecfg));
173 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
176 reg_error = regcomp(®exp, pattern, REG_NEWLINE);
179 regerror(reg_error, ®exp, errbuf, 1024);
180 die("-L parameter '%s': %s", pattern, errbuf);
183 p = find_funcname_matching_regexp(xecfg, (char*) start, ®exp);
185 die("-L parameter '%s': no match", pattern);
187 while (p > nth_line_cb(cb_data, *begin))
191 die("-L parameter '%s' matches at EOF", pattern);
194 while (*end < lines) {
195 const char *bol = nth_line_cb(cb_data, *end);
196 const char *eol = nth_line_cb(cb_data, *end+1);
197 if (match_funcname(xecfg, bol, eol))
206 /* compensate for 1-based numbering */
212 int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
213 void *cb_data, long lines, long *begin, long *end,
219 arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
225 arg = parse_loc(arg, nth_line_cb, cb_data, lines, 1, begin);
228 arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
233 if (*begin && *end && *end < *begin) {
235 tmp = *end; *end = *begin; *begin = tmp;
241 const char *skip_range_arg(const char *arg)
244 return parse_range_funcname(arg, NULL, NULL, 0, NULL, NULL, NULL);
246 arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
249 arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);