]>
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 | |
50 | *ret = begin + num; | |
51 | return term; | |
52 | } | |
53 | return spec; | |
54 | } | |
55 | num = strtol(spec, &term, 10); | |
56 | if (term != spec) { | |
12da1d1f TR |
57 | if (ret) |
58 | *ret = num; | |
25ed3412 BY |
59 | return term; |
60 | } | |
815834e9 ES |
61 | |
62 | if (begin < 0) | |
63 | begin = -begin; | |
64 | ||
25ed3412 BY |
65 | if (spec[0] != '/') |
66 | return spec; | |
67 | ||
68 | /* it could be a regexp of form /.../ */ | |
69 | for (term = (char *) spec + 1; *term && *term != '/'; term++) { | |
70 | if (*term == '\\') | |
71 | term++; | |
72 | } | |
73 | if (*term != '/') | |
74 | return spec; | |
75 | ||
12da1d1f TR |
76 | /* in the scan-only case we are not interested in the regex */ |
77 | if (!ret) | |
78 | return term+1; | |
79 | ||
25ed3412 BY |
80 | /* try [spec+1 .. term-1] as regexp */ |
81 | *term = 0; | |
82 | begin--; /* input is in human terms */ | |
83 | line = nth_line(data, begin); | |
84 | ||
85 | if (!(reg_error = regcomp(®exp, spec + 1, REG_NEWLINE)) && | |
86 | !(reg_error = regexec(®exp, line, 1, match, 0))) { | |
87 | const char *cp = line + match[0].rm_so; | |
88 | const char *nline; | |
89 | ||
90 | while (begin++ < lines) { | |
91 | nline = nth_line(data, begin); | |
92 | if (line <= cp && cp < nline) | |
93 | break; | |
94 | line = nline; | |
95 | } | |
96 | *ret = begin; | |
97 | regfree(®exp); | |
98 | *term++ = '/'; | |
99 | return term; | |
100 | } | |
101 | else { | |
102 | char errbuf[1024]; | |
103 | regerror(reg_error, ®exp, errbuf, 1024); | |
815834e9 ES |
104 | die("-L parameter '%s' starting at line %ld: %s", |
105 | spec + 1, begin + 1, errbuf); | |
25ed3412 BY |
106 | } |
107 | } | |
108 | ||
13b8f68c TR |
109 | static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol) |
110 | { | |
111 | if (xecfg) { | |
112 | char buf[1]; | |
113 | return xecfg->find_func(bol, eol - bol, buf, 1, | |
114 | xecfg->find_func_priv) >= 0; | |
115 | } | |
116 | ||
117 | if (bol == eol) | |
118 | return 0; | |
119 | if (isalpha(*bol) || *bol == '_' || *bol == '$') | |
120 | return 1; | |
121 | return 0; | |
122 | } | |
123 | ||
124 | static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start, | |
125 | regex_t *regexp) | |
126 | { | |
127 | int reg_error; | |
128 | regmatch_t match[1]; | |
129 | while (1) { | |
130 | const char *bol, *eol; | |
131 | reg_error = regexec(regexp, start, 1, match, 0); | |
132 | if (reg_error == REG_NOMATCH) | |
133 | return NULL; | |
134 | else if (reg_error) { | |
135 | char errbuf[1024]; | |
136 | regerror(reg_error, regexp, errbuf, 1024); | |
137 | die("-L parameter: regexec() failed: %s", errbuf); | |
138 | } | |
139 | /* determine extent of line matched */ | |
140 | bol = start+match[0].rm_so; | |
141 | eol = start+match[0].rm_eo; | |
142 | while (bol > start && *bol != '\n') | |
143 | bol--; | |
144 | if (*bol == '\n') | |
145 | bol++; | |
146 | while (*eol && *eol != '\n') | |
147 | eol++; | |
148 | if (*eol == '\n') | |
149 | eol++; | |
150 | /* is it a funcname line? */ | |
151 | if (match_funcname(xecfg, (char*) bol, (char*) eol)) | |
152 | return bol; | |
153 | start = eol; | |
154 | } | |
155 | } | |
156 | ||
157 | static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb, | |
158 | void *cb_data, long lines, long *begin, long *end, | |
159 | const char *path) | |
160 | { | |
161 | char *pattern; | |
162 | const char *term; | |
163 | struct userdiff_driver *drv; | |
164 | xdemitconf_t *xecfg = NULL; | |
165 | const char *start; | |
166 | const char *p; | |
167 | int reg_error; | |
168 | regex_t regexp; | |
169 | ||
170 | assert(*arg == ':'); | |
171 | term = arg+1; | |
172 | while (*term && *term != ':') { | |
173 | if (*term == '\\' && *(term+1)) | |
174 | term++; | |
175 | term++; | |
176 | } | |
177 | if (term == arg+1) | |
178 | return NULL; | |
179 | if (!begin) /* skip_range_arg case */ | |
180 | return term; | |
181 | ||
182 | pattern = xstrndup(arg+1, term-(arg+1)); | |
183 | ||
184 | start = nth_line_cb(cb_data, 0); | |
185 | ||
186 | drv = userdiff_find_by_path(path); | |
187 | if (drv && drv->funcname.pattern) { | |
188 | const struct userdiff_funcname *pe = &drv->funcname; | |
189 | xecfg = xcalloc(1, sizeof(*xecfg)); | |
190 | xdiff_set_find_func(xecfg, pe->pattern, pe->cflags); | |
191 | } | |
192 | ||
193 | reg_error = regcomp(®exp, pattern, REG_NEWLINE); | |
194 | if (reg_error) { | |
195 | char errbuf[1024]; | |
196 | regerror(reg_error, ®exp, errbuf, 1024); | |
197 | die("-L parameter '%s': %s", pattern, errbuf); | |
198 | } | |
199 | ||
200 | p = find_funcname_matching_regexp(xecfg, (char*) start, ®exp); | |
201 | if (!p) | |
202 | die("-L parameter '%s': no match", pattern); | |
203 | *begin = 0; | |
204 | while (p > nth_line_cb(cb_data, *begin)) | |
205 | (*begin)++; | |
206 | ||
207 | if (*begin >= lines) | |
208 | die("-L parameter '%s' matches at EOF", pattern); | |
209 | ||
210 | *end = *begin+1; | |
211 | while (*end < lines) { | |
212 | const char *bol = nth_line_cb(cb_data, *end); | |
213 | const char *eol = nth_line_cb(cb_data, *end+1); | |
214 | if (match_funcname(xecfg, bol, eol)) | |
215 | break; | |
216 | (*end)++; | |
217 | } | |
218 | ||
219 | regfree(®exp); | |
220 | free(xecfg); | |
221 | free(pattern); | |
222 | ||
223 | /* compensate for 1-based numbering */ | |
224 | (*begin)++; | |
225 | ||
226 | return term; | |
227 | } | |
228 | ||
25ed3412 | 229 | int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb, |
815834e9 ES |
230 | void *cb_data, long lines, long anchor, |
231 | long *begin, long *end, const char *path) | |
25ed3412 | 232 | { |
3bf65f9e ES |
233 | *begin = *end = 0; |
234 | ||
815834e9 ES |
235 | if (anchor < 1) |
236 | anchor = 1; | |
237 | if (anchor > lines) | |
238 | anchor = lines + 1; | |
239 | ||
13b8f68c TR |
240 | if (*arg == ':') { |
241 | arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path); | |
242 | if (!arg || *arg) | |
243 | return -1; | |
244 | return 0; | |
245 | } | |
246 | ||
815834e9 | 247 | arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin); |
25ed3412 BY |
248 | |
249 | if (*arg == ',') | |
250 | arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end); | |
251 | ||
252 | if (*arg) | |
253 | return -1; | |
254 | ||
3bf65f9e ES |
255 | if (*begin && *end && *end < *begin) { |
256 | long tmp; | |
257 | tmp = *end; *end = *begin; *begin = tmp; | |
258 | } | |
259 | ||
25ed3412 BY |
260 | return 0; |
261 | } | |
12da1d1f TR |
262 | |
263 | const char *skip_range_arg(const char *arg) | |
264 | { | |
13b8f68c TR |
265 | if (*arg == ':') |
266 | return parse_range_funcname(arg, NULL, NULL, 0, NULL, NULL, NULL); | |
267 | ||
12da1d1f TR |
268 | arg = parse_loc(arg, NULL, NULL, 0, -1, NULL); |
269 | ||
270 | if (*arg == ',') | |
271 | arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL); | |
272 | ||
273 | return arg; | |
274 | } |