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