summaryrefslogtreecommitdiff
path: root/common.c
blob: 8e17cc03de9c6a4f2ca26b698b3743541bb1d0ed (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
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stddef.h>
#include <string.h>

void
indicate_file_area(FILE* fd, size_t line, size_t column, size_t span) {
	const char margin[] = "  ";

	char buf[1024] = { '\0' };
	char *s = buf;
	char c = '\0';

	rewind(fd);
	while (line && !feof(fd) && fgets(buf, sizeof(buf), fd)) {
		s = buf;
		while (*s) {
			if (*(s++) == '\n') {
				line--;
			}
		}
	}

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

	fputs(margin, stderr);
	fputs(s, 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);
	}

	c = span == 1 ? '^' : '"';
	for (; span; span--) {
		fputc(c, stderr);
	}
	fputc('\n', stderr);
}