summaryrefslogtreecommitdiff
path: root/lib/exceptions.c
blob: 231ca6397267ab0897ec301f0e7353fc4a072105 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include "exceptions.h"

char * contexts[128];
int clevel = 0;

char * Estrdup(char * o) {
        char * r;
        
        if (!(r = strdup(o))) {
                exception(1, _("Out of memory."));
        }
        return r;       
}

void * Emalloc(size_t s) {
        void * r;
        
        if (!(r = malloc(s))) {
                exception(1, _("Out of memory."));
        }
        return r;
}

void pushcontext(char * c) {
	if (clevel == 128) {
		exception(1, _("Too much error contexts during pushcontext()."));
	}
	contexts[clevel++] = Estrdup(c);
}

void popcontext(void) {
	if (clevel == 0) {
		exception(1, _("Error context empty, but popcontext() called."));
	}
	free(contexts[--clevel]);
}

void flushcontext(void) {
	while (clevel) {
		popcontext();
	}
}

void exception(int level, char *msg)
{
	int i;
	
	fprintf(stderr, "Error detected. Showing context.\n");
	for (i = 0; i < clevel; i++) {
		fprintf(stderr, " (%i) - %s\n", i, contexts[i]);
	}
        fprintf(stderr, "  Error description: %s\n", msg);
        exit(level);
}