summaryrefslogtreecommitdiff
path: root/common.c
blob: fbc9ae228d4938f5dbfefdd44ce94e58bad3494b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stddef.h>
#include <string.h>

void
indicate_file_area(FILE* fd, size_t line, size_t column, size_t span) {
	char margin[] = "  ";
	/* FIXME use proper line counting, not this hack */
	char buf[1024];
	char *line_start = buf;

	rewind(fd);
	for (; line; line--) {
		fgets(buf, sizeof(buf), fd);
	}

	while (*line_start == '\t' || *line_start == ' ') {
		line_start++;
	}

	fputs(margin, stderr);
	fputs(line_start, stderr);

	/* corner case (still needed?) - buf was just return */
	if (strlen(buf) == 1 && buf[0] == '\n') {
		fputc('\n', stderr);
	}

	fputs(margin, stderr);
	for (column--; column; column--) {
		fputc(' ', stderr);
	}
	for (; span; span--) {
		fputc('^', stderr);
	}
	fputc('\n', stderr);
}