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) {
32 *ret = begin + num - 2;
41 num = strtol(spec, &term, 10);
50 /* it could be a regexp of form /.../ */
51 for (term = (char *) spec + 1; *term && *term != '/'; term++) {
58 /* in the scan-only case we are not interested in the regex */
62 /* try [spec+1 .. term-1] as regexp */
64 begin--; /* input is in human terms */
65 line = nth_line(data, begin);
67 if (!(reg_error = regcomp(®exp, spec + 1, REG_NEWLINE)) &&
68 !(reg_error = regexec(®exp, line, 1, match, 0))) {
69 const char *cp = line + match[0].rm_so;
72 while (begin++ < lines) {
73 nline = nth_line(data, begin);
74 if (line <= cp && cp < nline)
85 regerror(reg_error, ®exp, errbuf, 1024);
86 die("-L parameter '%s': %s", spec + 1, errbuf);
90 static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)
94 return xecfg->find_func(bol, eol - bol, buf, 1,
95 xecfg->find_func_priv) >= 0;
100 if (isalpha(*bol) || *bol == '_' || *bol == '$')
105 static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,
111 const char *bol, *eol;
112 reg_error = regexec(regexp, start, 1, match, 0);
113 if (reg_error == REG_NOMATCH)
115 else if (reg_error) {
117 regerror(reg_error, regexp, errbuf, 1024);
118 die("-L parameter: regexec() failed: %s", errbuf);
120 /* determine extent of line matched */
121 bol = start+match[0].rm_so;
122 eol = start+match[0].rm_eo;
123 while (bol > start && *bol != '\n')
127 while (*eol && *eol != '\n')
131 /* is it a funcname line? */
132 if (match_funcname(xecfg, (char*) bol, (char*) eol))
138 static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,
139 void *cb_data, long lines, long *begin, long *end,
144 struct userdiff_driver *drv;
145 xdemitconf_t *xecfg = NULL;
153 while (*term && *term != ':') {
154 if (*term == '\\' && *(term+1))
160 if (!begin) /* skip_range_arg case */
163 pattern = xstrndup(arg+1, term-(arg+1));
165 start = nth_line_cb(cb_data, 0);
167 drv = userdiff_find_by_path(path);
168 if (drv && drv->funcname.pattern) {
169 const struct userdiff_funcname *pe = &drv->funcname;
170 xecfg = xcalloc(1, sizeof(*xecfg));
171 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
174 reg_error = regcomp(®exp, pattern, REG_NEWLINE);
177 regerror(reg_error, ®exp, errbuf, 1024);
178 die("-L parameter '%s': %s", pattern, errbuf);
181 p = find_funcname_matching_regexp(xecfg, (char*) start, ®exp);
183 die("-L parameter '%s': no match", pattern);
185 while (p > nth_line_cb(cb_data, *begin))
189 die("-L parameter '%s' matches at EOF", pattern);
192 while (*end < lines) {
193 const char *bol = nth_line_cb(cb_data, *end);
194 const char *eol = nth_line_cb(cb_data, *end+1);
195 if (match_funcname(xecfg, bol, eol))
204 /* compensate for 1-based numbering */
210 int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
211 void *cb_data, long lines, long *begin, long *end,
217 arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
223 arg = parse_loc(arg, nth_line_cb, cb_data, lines, 1, begin);
226 arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
231 if (*begin && *end && *end < *begin) {
233 tmp = *end; *end = *begin; *begin = tmp;
239 const char *skip_range_arg(const char *arg)
242 return parse_range_funcname(arg, NULL, NULL, 0, NULL, NULL, NULL);
244 arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
247 arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);