summaryrefslogtreecommitdiff
path: root/util/err.c
blob: 0bd6424788b9dc37837b4d8e486d5beed916b40c (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#define USE_COLORED_TEXT 1

#if USE_COLORED_TEXT
#define TEXT_ERROR(x) "\x1b[91m" x "\x1b[0m"
#define TEXT_INFO(x) "\x1b[94m" x "\x1b[0m"
#define TEXT_WARN(x) "\x1b[93m" x "\x1b[0m"
#define TEXT_IMPORTANT(x) "\x1b[1m" x "\x1b[0m"
#else
#define TEXT_ERROR(x) x
#define TEXT_INFO(x) x
#define TEXT_WARN(x) x
#define TEXT_IMPORTANT(x) x
#endif

typedef uint32_t LineNo;

typedef struct {
	LineNo line;
	char *code;
} Location;

/* file name of file being processed */
static const char *err_filename;

/* Write directly to the error file */
static void err_fwrite(const void *data, size_t size, size_t n) {
	fwrite(data, size, n, stderr);
}

static void err_fprint(const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	va_end(args);
}

static void err_vfprint(const char *fmt, va_list args) {
	vfprintf(stderr, fmt, args);
}

static void err_print_header_(LineNo line) {
	err_fprint(TEXT_ERROR("error:") " at line %lu of %s:\n", (unsigned long)line, err_filename);
}

static void info_print_header_(LineNo line) {
	err_fprint(TEXT_INFO("info:") " at line %lu of %s:\n", (unsigned long)line, err_filename);
}

static void warn_print_header_(LineNo line) {
	err_fprint(TEXT_WARN("warning:") " at line %lu of %s:\n", (unsigned long)line, err_filename);
}

static void err_print_footer_(const char *context) {
	err_fprint("\n\there --> ");
	const char *end = strchr(context, '\n');
	int has_newline = end != NULL;
	if (!has_newline)
		end = strchr(context, '\0');
	assert(end);
	err_fwrite(context, 1, (size_t)(end - context));
	if (!has_newline)
		err_fprint("<end of file>");
	err_fprint("\n");
}

/* Write nicely-formatted errors to the error file */


static void err_vprint(Location where, const char *fmt, va_list args) {
	err_print_header_(where.line);
	err_vfprint(fmt, args);
	err_print_footer_(where.code);
}

static void err_print(Location where, const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	err_vprint(where, fmt, args);
	va_end(args);
}

static void info_print(Location where, const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	info_print_header_(where.line);
	err_vfprint(fmt, args);
	err_print_footer_(where.code);
	va_end(args);
}

static void warn_print(Location where, const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	warn_print_header_(where.line);
	err_vfprint(fmt, args);
	err_print_footer_(where.code);
	va_end(args);
}

static void *err_malloc(size_t size) {
	void *ret = malloc(size);
	if (!ret) {
		fprintf(stderr, "Error: Out of memory.\n");
		abort();
	}
	return ret;
}

static void *err_calloc(size_t n, size_t size) {
	void *ret = calloc(n, size);
	if (!ret) {
		fprintf(stderr, "Error: Out of memory.\n");
		abort();
	}
	return ret;
}

static void *err_realloc(void *data, size_t new_size) {
	void *ret = realloc(data, new_size);
	if (!ret) {
		fprintf(stderr, "Error: Out of memory.\n");
		abort();
	}
	return ret;
}