]>
Commit | Line | Data |
---|---|---|
25ed3412 BY |
1 | #include "git-compat-util.h" |
2 | #include "line-range.h" | |
13b8f68c TR |
3 | #include "xdiff-interface.h" |
4 | #include "strbuf.h" | |
5 | #include "userdiff.h" | |
25ed3412 BY |
6 | |
7 | /* | |
8 | * Parse one item in the -L option | |
815834e9 ES |
9 | * |
10 | * 'begin' is applicable only to relative range anchors. Absolute anchors | |
11 | * ignore this value. | |
12 | * | |
13 | * When parsing "-L A,B", parse_loc() is called once for A and once for B. | |
14 | * | |
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. | |
18 | * | |
19 | * When parsing B, 'begin' must be the positive line number immediately | |
20 | * following the line computed for 'A'. | |
25ed3412 BY |
21 | */ |
22 | static const char *parse_loc(const char *spec, nth_line_fn_t nth_line, | |
23 | void *data, long lines, long begin, long *ret) | |
24 | { | |
25 | char *term; | |
26 | const char *line; | |
27 | long num; | |
28 | int reg_error; | |
29 | regex_t regexp; | |
30 | regmatch_t match[1]; | |
31 | ||
32 | /* Allow "-L <something>,+20" to mean starting at <something> | |
33 | * for 20 lines, or "-L <something>,-5" for 5 lines ending at | |
34 | * <something>. | |
35 | */ | |
5d57cac6 | 36 | if (1 <= begin && (spec[0] == '+' || spec[0] == '-')) { |
25ed3412 BY |
37 | num = strtol(spec + 1, &term, 10); |
38 | if (term != spec + 1) { | |
12da1d1f TR |
39 | if (!ret) |
40 | return term; | |
abba3539 ES |
41 | if (num == 0) |
42 | die("-L invalid empty range"); | |
25ed3412 BY |
43 | if (spec[0] == '-') |
44 | num = 0 - num; | |
45 | if (0 < num) | |
46 | *ret = begin + num - 2; | |
47 | else if (!num) | |
48 | *ret = begin; | |
49 | else | |
96cfa94e | 50 | *ret = begin + num > 0 ? begin + num : 1; |
25ed3412 BY |
51 | return term; |
52 | } | |
53 | return spec; | |
54 | } | |
55 | num = strtol(spec, &term, 10); | |
56 | if (term != spec) { | |
5ce922a0 ES |
57 | if (ret) { |
58 | if (num <= 0) | |
59 | die("-L invalid line number: %ld", num); | |
12da1d1f | 60 | *ret = num; |
5ce922a0 | 61 | } |
25ed3412 BY |
62 | return term; |
63 | } | |
815834e9 | 64 | |
a6ac5f98 ES |
65 | if (begin < 0) { |
66 | if (spec[0] != '^') | |
67 | begin = -begin; | |
68 | else { | |
69 | begin = 1; | |
70 | spec++; | |
71 | } | |
72 | } | |
815834e9 | 73 | |
25ed3412 BY |
74 | if (spec[0] != '/') |
75 | return spec; | |
76 | ||
77 | /* it could be a regexp of form /.../ */ | |
78 | for (term = (char *) spec + 1; *term && *term != '/'; term++) { | |
79 | if (*term == '\\') | |
80 | term++; | |
81 | } | |
82 | if (*term != '/') | |
83 | return spec; | |
84 | ||
12da1d1f TR |
85 | /* in the scan-only case we are not interested in the regex */ |
86 | if (!ret) | |
87 | return term+1; | |
88 | ||
25ed3412 BY |
89 | /* try [spec+1 .. term-1] as regexp */ |
90 | *term = 0; | |
91 | begin--; /* input is in human terms */ | |
92 | line = nth_line(data, begin); | |
93 | ||
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; | |
97 | const char *nline; | |
98 | ||
99 | while (begin++ < lines) { | |
100 | nline = nth_line(data, begin); | |
101 | if (line <= cp && cp < nline) | |
102 | break; | |
103 | line = nline; | |
104 | } | |
105 | *ret = begin; | |
106 | regfree(®exp); | |
107 | *term++ = '/'; | |
108 | return term; | |
109 | } | |
110 | else { | |
111 | char errbuf[1024]; | |
112 | regerror(reg_error, ®exp, errbuf, 1024); | |
815834e9 ES |
113 | die("-L parameter '%s' starting at line %ld: %s", |
114 | spec + 1, begin + 1, errbuf); | |
25ed3412 BY |
115 | } |
116 | } | |
117 | ||
13b8f68c TR |
118 | static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol) |
119 | { | |
120 | if (xecfg) { | |
121 | char buf[1]; | |
122 | return xecfg->find_func(bol, eol - bol, buf, 1, | |
123 | xecfg->find_func_priv) >= 0; | |
124 | } | |
125 | ||
126 | if (bol == eol) | |
127 | return 0; | |
128 | if (isalpha(*bol) || *bol == '_' || *bol == '$') | |
129 | return 1; | |
130 | return 0; | |
131 | } | |
132 | ||
133 | static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start, | |
134 | regex_t *regexp) | |
135 | { | |
136 | int reg_error; | |
137 | regmatch_t match[1]; | |
4e57c88e | 138 | while (*start) { |
13b8f68c TR |
139 | const char *bol, *eol; |
140 | reg_error = regexec(regexp, start, 1, match, 0); | |
141 | if (reg_error == REG_NOMATCH) | |
142 | return NULL; | |
143 | else if (reg_error) { | |
144 | char errbuf[1024]; | |
145 | regerror(reg_error, regexp, errbuf, 1024); | |
146 | die("-L parameter: regexec() failed: %s", errbuf); | |
147 | } | |
148 | /* determine extent of line matched */ | |
149 | bol = start+match[0].rm_so; | |
150 | eol = start+match[0].rm_eo; | |
4e57c88e LKS |
151 | while (bol > start && *--bol != '\n') |
152 | ; /* nothing */ | |
13b8f68c TR |
153 | if (*bol == '\n') |
154 | bol++; | |
155 | while (*eol && *eol != '\n') | |
156 | eol++; | |
157 | if (*eol == '\n') | |
158 | eol++; | |
159 | /* is it a funcname line? */ | |
160 | if (match_funcname(xecfg, (char*) bol, (char*) eol)) | |
161 | return bol; | |
162 | start = eol; | |
163 | } | |
4e57c88e | 164 | return NULL; |
13b8f68c TR |
165 | } |
166 | ||
80e03855 NTND |
167 | static const char *parse_range_funcname( |
168 | const char *arg, nth_line_fn_t nth_line_cb, | |
169 | void *cb_data, long lines, long anchor, long *begin, long *end, | |
170 | const char *path, struct index_state *istate) | |
13b8f68c TR |
171 | { |
172 | char *pattern; | |
173 | const char *term; | |
174 | struct userdiff_driver *drv; | |
175 | xdemitconf_t *xecfg = NULL; | |
176 | const char *start; | |
177 | const char *p; | |
178 | int reg_error; | |
179 | regex_t regexp; | |
180 | ||
215e76c7 ES |
181 | if (*arg == '^') { |
182 | anchor = 1; | |
183 | arg++; | |
184 | } | |
185 | ||
13b8f68c TR |
186 | assert(*arg == ':'); |
187 | term = arg+1; | |
188 | while (*term && *term != ':') { | |
189 | if (*term == '\\' && *(term+1)) | |
190 | term++; | |
191 | term++; | |
192 | } | |
193 | if (term == arg+1) | |
194 | return NULL; | |
195 | if (!begin) /* skip_range_arg case */ | |
196 | return term; | |
197 | ||
198 | pattern = xstrndup(arg+1, term-(arg+1)); | |
199 | ||
1ce761a5 ES |
200 | anchor--; /* input is in human terms */ |
201 | start = nth_line_cb(cb_data, anchor); | |
13b8f68c | 202 | |
80e03855 | 203 | drv = userdiff_find_by_path(istate, path); |
13b8f68c TR |
204 | if (drv && drv->funcname.pattern) { |
205 | const struct userdiff_funcname *pe = &drv->funcname; | |
ca56dadb | 206 | CALLOC_ARRAY(xecfg, 1); |
13b8f68c TR |
207 | xdiff_set_find_func(xecfg, pe->pattern, pe->cflags); |
208 | } | |
209 | ||
210 | reg_error = regcomp(®exp, pattern, REG_NEWLINE); | |
211 | if (reg_error) { | |
212 | char errbuf[1024]; | |
213 | regerror(reg_error, ®exp, errbuf, 1024); | |
214 | die("-L parameter '%s': %s", pattern, errbuf); | |
215 | } | |
216 | ||
217 | p = find_funcname_matching_regexp(xecfg, (char*) start, ®exp); | |
218 | if (!p) | |
1ce761a5 ES |
219 | die("-L parameter '%s' starting at line %ld: no match", |
220 | pattern, anchor + 1); | |
13b8f68c TR |
221 | *begin = 0; |
222 | while (p > nth_line_cb(cb_data, *begin)) | |
223 | (*begin)++; | |
224 | ||
225 | if (*begin >= lines) | |
226 | die("-L parameter '%s' matches at EOF", pattern); | |
227 | ||
228 | *end = *begin+1; | |
229 | while (*end < lines) { | |
230 | const char *bol = nth_line_cb(cb_data, *end); | |
231 | const char *eol = nth_line_cb(cb_data, *end+1); | |
232 | if (match_funcname(xecfg, bol, eol)) | |
233 | break; | |
234 | (*end)++; | |
235 | } | |
236 | ||
237 | regfree(®exp); | |
238 | free(xecfg); | |
239 | free(pattern); | |
240 | ||
241 | /* compensate for 1-based numbering */ | |
242 | (*begin)++; | |
243 | ||
244 | return term; | |
245 | } | |
246 | ||
25ed3412 | 247 | int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb, |
815834e9 | 248 | void *cb_data, long lines, long anchor, |
80e03855 NTND |
249 | long *begin, long *end, |
250 | const char *path, struct index_state *istate) | |
25ed3412 | 251 | { |
3bf65f9e ES |
252 | *begin = *end = 0; |
253 | ||
815834e9 ES |
254 | if (anchor < 1) |
255 | anchor = 1; | |
256 | if (anchor > lines) | |
257 | anchor = lines + 1; | |
258 | ||
215e76c7 | 259 | if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':')) { |
80e03855 NTND |
260 | arg = parse_range_funcname(arg, nth_line_cb, cb_data, |
261 | lines, anchor, begin, end, | |
262 | path, istate); | |
13b8f68c TR |
263 | if (!arg || *arg) |
264 | return -1; | |
265 | return 0; | |
266 | } | |
267 | ||
815834e9 | 268 | arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin); |
25ed3412 BY |
269 | |
270 | if (*arg == ',') | |
271 | arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end); | |
272 | ||
273 | if (*arg) | |
274 | return -1; | |
275 | ||
3bf65f9e | 276 | if (*begin && *end && *end < *begin) { |
35d803bc | 277 | SWAP(*end, *begin); |
3bf65f9e ES |
278 | } |
279 | ||
25ed3412 BY |
280 | return 0; |
281 | } | |
12da1d1f | 282 | |
80e03855 | 283 | const char *skip_range_arg(const char *arg, struct index_state *istate) |
12da1d1f | 284 | { |
215e76c7 | 285 | if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':')) |
80e03855 NTND |
286 | return parse_range_funcname(arg, NULL, NULL, |
287 | 0, 0, NULL, NULL, | |
288 | NULL, istate); | |
13b8f68c | 289 | |
12da1d1f TR |
290 | arg = parse_loc(arg, NULL, NULL, 0, -1, NULL); |
291 | ||
292 | if (*arg == ',') | |
293 | arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL); | |
294 | ||
295 | return arg; | |
296 | } |