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