line-range.c 6.45 KB
Newer Older
Bo Yang's avatar
Bo Yang committed
1 2
#include "git-compat-util.h"
#include "line-range.h"
3 4 5
#include "xdiff-interface.h"
#include "strbuf.h"
#include "userdiff.h"
Bo Yang's avatar
Bo Yang committed
6 7 8

/*
 * Parse one item in the -L option
9 10 11 12 13 14 15 16 17 18 19 20
 *
 * '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 represented by -1.
 *
 * When parsing B, 'begin' must be the positive line number immediately
 * following the line computed for 'A'.
Bo Yang's avatar
Bo Yang committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 */
static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
			     void *data, long lines, long begin, long *ret)
{
	char *term;
	const char *line;
	long num;
	int reg_error;
	regex_t regexp;
	regmatch_t match[1];

	/* Allow "-L <something>,+20" to mean starting at <something>
	 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
	 * <something>.
	 */
36
	if (1 <= begin && (spec[0] == '+' || spec[0] == '-')) {
Bo Yang's avatar
Bo Yang committed
37 38
		num = strtol(spec + 1, &term, 10);
		if (term != spec + 1) {
39 40
			if (!ret)
				return term;
41 42
			if (num == 0)
				die("-L invalid empty range");
Bo Yang's avatar
Bo Yang committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
			if (spec[0] == '-')
				num = 0 - num;
			if (0 < num)
				*ret = begin + num - 2;
			else if (!num)
				*ret = begin;
			else
				*ret = begin + num;
			return term;
		}
		return spec;
	}
	num = strtol(spec, &term, 10);
	if (term != spec) {
57 58 59
		if (ret) {
			if (num <= 0)
				die("-L invalid line number: %ld", num);
60
			*ret = num;
61
		}
Bo Yang's avatar
Bo Yang committed
62 63
		return term;
	}
64

65 66 67 68 69 70 71 72
	if (begin < 0) {
		if (spec[0] != '^')
			begin = -begin;
		else {
			begin = 1;
			spec++;
		}
	}
73

Bo Yang's avatar
Bo Yang committed
74 75 76 77 78 79 80 81 82 83 84
	if (spec[0] != '/')
		return spec;

	/* it could be a regexp of form /.../ */
	for (term = (char *) spec + 1; *term && *term != '/'; term++) {
		if (*term == '\\')
			term++;
	}
	if (*term != '/')
		return spec;

85 86 87 88
	/* in the scan-only case we are not interested in the regex */
	if (!ret)
		return term+1;

Bo Yang's avatar
Bo Yang committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	/* try [spec+1 .. term-1] as regexp */
	*term = 0;
	begin--; /* input is in human terms */
	line = nth_line(data, begin);

	if (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&
	    !(reg_error = regexec(&regexp, line, 1, match, 0))) {
		const char *cp = line + match[0].rm_so;
		const char *nline;

		while (begin++ < lines) {
			nline = nth_line(data, begin);
			if (line <= cp && cp < nline)
				break;
			line = nline;
		}
		*ret = begin;
		regfree(&regexp);
		*term++ = '/';
		return term;
	}
	else {
		char errbuf[1024];
		regerror(reg_error, &regexp, errbuf, 1024);
113 114
		die("-L parameter '%s' starting at line %ld: %s",
		    spec + 1, begin + 1, errbuf);
Bo Yang's avatar
Bo Yang committed
115 116 117
	}
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)
{
	if (xecfg) {
		char buf[1];
		return xecfg->find_func(bol, eol - bol, buf, 1,
					xecfg->find_func_priv) >= 0;
	}

	if (bol == eol)
		return 0;
	if (isalpha(*bol) || *bol == '_' || *bol == '$')
		return 1;
	return 0;
}

static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,
						 regex_t *regexp)
{
	int reg_error;
	regmatch_t match[1];
	while (1) {
		const char *bol, *eol;
		reg_error = regexec(regexp, start, 1, match, 0);
		if (reg_error == REG_NOMATCH)
			return NULL;
		else if (reg_error) {
			char errbuf[1024];
			regerror(reg_error, regexp, errbuf, 1024);
			die("-L parameter: regexec() failed: %s", errbuf);
		}
		/* determine extent of line matched */
		bol = start+match[0].rm_so;
		eol = start+match[0].rm_eo;
		while (bol > start && *bol != '\n')
			bol--;
		if (*bol == '\n')
			bol++;
		while (*eol && *eol != '\n')
			eol++;
		if (*eol == '\n')
			eol++;
		/* is it a funcname line? */
		if (match_funcname(xecfg, (char*) bol, (char*) eol))
			return bol;
		start = eol;
	}
}

static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,
167
					void *cb_data, long lines, long anchor, long *begin, long *end,
168 169 170 171 172 173 174 175 176 177 178
					const char *path)
{
	char *pattern;
	const char *term;
	struct userdiff_driver *drv;
	xdemitconf_t *xecfg = NULL;
	const char *start;
	const char *p;
	int reg_error;
	regex_t regexp;

179 180 181 182 183
	if (*arg == '^') {
		anchor = 1;
		arg++;
	}

184 185 186 187 188 189 190 191 192 193 194 195 196 197
	assert(*arg == ':');
	term = arg+1;
	while (*term && *term != ':') {
		if (*term == '\\' && *(term+1))
			term++;
		term++;
	}
	if (term == arg+1)
		return NULL;
	if (!begin) /* skip_range_arg case */
		return term;

	pattern = xstrndup(arg+1, term-(arg+1));

198 199
	anchor--; /* input is in human terms */
	start = nth_line_cb(cb_data, anchor);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

	drv = userdiff_find_by_path(path);
	if (drv && drv->funcname.pattern) {
		const struct userdiff_funcname *pe = &drv->funcname;
		xecfg = xcalloc(1, sizeof(*xecfg));
		xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
	}

	reg_error = regcomp(&regexp, pattern, REG_NEWLINE);
	if (reg_error) {
		char errbuf[1024];
		regerror(reg_error, &regexp, errbuf, 1024);
		die("-L parameter '%s': %s", pattern, errbuf);
	}

	p = find_funcname_matching_regexp(xecfg, (char*) start, &regexp);
	if (!p)
217 218
		die("-L parameter '%s' starting at line %ld: no match",
		    pattern, anchor + 1);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	*begin = 0;
	while (p > nth_line_cb(cb_data, *begin))
		(*begin)++;

	if (*begin >= lines)
		die("-L parameter '%s' matches at EOF", pattern);

	*end = *begin+1;
	while (*end < lines) {
		const char *bol = nth_line_cb(cb_data, *end);
		const char *eol = nth_line_cb(cb_data, *end+1);
		if (match_funcname(xecfg, bol, eol))
			break;
		(*end)++;
	}

	regfree(&regexp);
	free(xecfg);
	free(pattern);

	/* compensate for 1-based numbering */
	(*begin)++;

	return term;
}

Bo Yang's avatar
Bo Yang committed
245
int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
246 247
		    void *cb_data, long lines, long anchor,
		    long *begin, long *end, const char *path)
Bo Yang's avatar
Bo Yang committed
248
{
249 250
	*begin = *end = 0;

251 252 253 254 255
	if (anchor < 1)
		anchor = 1;
	if (anchor > lines)
		anchor = lines + 1;

256
	if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':')) {
257
		arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, anchor, begin, end, path);
258 259 260 261 262
		if (!arg || *arg)
			return -1;
		return 0;
	}

263
	arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
Bo Yang's avatar
Bo Yang committed
264 265 266 267 268 269 270

	if (*arg == ',')
		arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);

	if (*arg)
		return -1;

271
	if (*begin && *end && *end < *begin) {
René Scharfe's avatar
René Scharfe committed
272
		SWAP(*end, *begin);
273 274
	}

Bo Yang's avatar
Bo Yang committed
275 276
	return 0;
}
277 278 279

const char *skip_range_arg(const char *arg)
{
280
	if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':'))
281
		return parse_range_funcname(arg, NULL, NULL, 0, 0, NULL, NULL, NULL);
282

283 284 285 286 287 288 289
	arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);

	if (*arg == ',')
		arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);

	return arg;
}