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

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

/********************************\
*                                *
*   Gestionnaire d'exceptions    *
*                                *
\********************************/

char *Estrdup(char *o)
{
	char *r;

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

void *Emalloc(size_t s)
{
	void *r;

	if (s) {
		if (!(r = malloc(s))) {
			exception(1, _("Out of memory."));
		}
	} else {
		return NULL;
	}
	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);
}