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