|
6 | 6 |
|
7 | 7 | /*
|
8 | 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'. |
9 | 21 | */
|
10 | 22 | static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
|
11 | 23 | void *data, long lines, long begin, long *ret)
|
@@ -46,6 +58,10 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
|
46 | 58 | *ret = num;
|
47 | 59 | return term;
|
48 | 60 | }
|
| 61 | + |
| 62 | + if (begin < 0) |
| 63 | + begin = -begin; |
| 64 | + |
49 | 65 | if (spec[0] != '/')
|
50 | 66 | return spec;
|
51 | 67 |
|
@@ -85,7 +101,8 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
|
85 | 101 | else {
|
86 | 102 | char errbuf[1024];
|
87 | 103 | regerror(reg_error, ®exp, errbuf, 1024);
|
88 |
| - die("-L parameter '%s': %s", spec + 1, errbuf); |
| 104 | + die("-L parameter '%s' starting at line %ld: %s", |
| 105 | + spec + 1, begin + 1, errbuf); |
89 | 106 | }
|
90 | 107 | }
|
91 | 108 |
|
@@ -210,19 +227,24 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
|
210 | 227 | }
|
211 | 228 |
|
212 | 229 | int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
|
213 |
| - void *cb_data, long lines, long *begin, long *end, |
214 |
| - const char *path) |
| 230 | + void *cb_data, long lines, long anchor, |
| 231 | + long *begin, long *end, const char *path) |
215 | 232 | {
|
216 | 233 | *begin = *end = 0;
|
217 | 234 |
|
| 235 | + if (anchor < 1) |
| 236 | + anchor = 1; |
| 237 | + if (anchor > lines) |
| 238 | + anchor = lines + 1; |
| 239 | + |
218 | 240 | if (*arg == ':') {
|
219 | 241 | arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
|
220 | 242 | if (!arg || *arg)
|
221 | 243 | return -1;
|
222 | 244 | return 0;
|
223 | 245 | }
|
224 | 246 |
|
225 |
| - arg = parse_loc(arg, nth_line_cb, cb_data, lines, 1, begin); |
| 247 | + arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin); |
226 | 248 |
|
227 | 249 | if (*arg == ',')
|
228 | 250 | arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
|
|
0 commit comments