* Parse one item in the -L option * * 'begin' is applicable only to relative range anchors. Absolute anchors * ignore this value. * * When parsing "-L A,B", parse_loc() is called once for A and once for B. * * When parsing A, 'begin' must be a negative number, the absolute value of * which is the line at which relative start-of-range anchors should be * based. Beginning of file is represe
| 19 | * following the line computed for 'A'. |
| 20 | */ |
| 21 | static const char *parse_loc(const char *spec, nth_line_fn_t nth_line, |
| 22 | void *data, long lines, long begin, long *ret) |
| 23 | { |
| 24 | char *term; |
| 25 | const char *line; |
| 26 | long num; |
| 27 | int reg_error; |
| 28 | regex_t regexp; |
| 29 | regmatch_t match[1]; |
| 30 | |
| 31 | /* Allow "-L <something>,+20" to mean starting at <something> |
| 32 | * for 20 lines, or "-L <something>,-5" for 5 lines ending at |
| 33 | * <something>. |
| 34 | */ |
| 35 | if (1 <= begin && (spec[0] == '+' || spec[0] == '-')) { |
| 36 | num = strtol(spec + 1, &term, 10); |
| 37 | if (term != spec + 1) { |
| 38 | if (!ret) |
| 39 | return term; |
| 40 | if (num == 0) |
| 41 | die("-L invalid empty range"); |
| 42 | if (spec[0] == '-') |
| 43 | num = 0 - num; |
| 44 | if (0 < num) |
| 45 | *ret = begin + num - 2; |
| 46 | else if (!num) |
| 47 | *ret = begin; |
| 48 | else |
| 49 | *ret = begin + num > 0 ? begin + num : 1; |
| 50 | return term; |
| 51 | } |
| 52 | return spec; |
| 53 | } |
| 54 | num = strtol(spec, &term, 10); |
| 55 | if (term != spec) { |
| 56 | if (ret) { |
| 57 | if (num <= 0) |
| 58 | die("-L invalid line number: %ld", num); |
| 59 | *ret = num; |
| 60 | } |
| 61 | return term; |
| 62 | } |
| 63 | |
| 64 | if (begin < 0) { |
| 65 | if (spec[0] != '^') |
| 66 | begin = -begin; |
| 67 | else { |
| 68 | begin = 1; |
| 69 | spec++; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (spec[0] != '/') |
| 74 | return spec; |
| 75 | |
| 76 | /* it could be a regexp of form /.../ */ |
| 77 | for (term = (char *) spec + 1; *term && *term != '/'; term++) { |
| 78 | if (*term == '\\') |