summaryrefslogtreecommitdiff
path: root/lib/Exceptions.cc
blob: 2633902451f4ad299c046c2f40c337d67eacbe52 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 *  Baltisot
 *  Copyright (C) 1999-2003 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* $Id: Exceptions.cc,v 1.35 2004-11-27 21:46:04 pixel Exp $ */

#include <string.h>
#include <errno.h>
#include <stddef.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#else
#ifndef __APPLE__
#include <malloc.h>
#endif
#endif
#ifdef DEBUG
#include <iostream>
#endif
#ifdef HAVE_PIPE
#include <unistd.h>
#endif
#include "BString.h"
#include "Exceptions.h"
#include "generic.h"
#include "gettext.h"

char GeneralException::t[BUFSIZ];

std::vector<String> Base::context;

GeneralException::GeneralException(String emsg) : msg(emsg.strdup()) {
    UNLOCK;
#ifdef DEBUG
    printm(M_BARE, String(_("Generating a General Exception error: '")) + msg + "'.\n");
#endif
}
GeneralException::GeneralException() : msg(0) {
    UNLOCK;
#ifdef DEBUG
    printm(M_BARE, String(_("Generating a General Exception error: '")) + msg + "'.\n");
#endif
}
GeneralException::GeneralException(const GeneralException & e) : msg(strdup(e.msg)) {
    UNLOCK;
#ifdef DEBUG
    printm(M_BARE, String(_("Generating a General Exception error: '")) + msg + "'.\n");
#endif
}

GeneralException::~GeneralException() {
    free(msg);
}

TaskNotFound::TaskNotFound() : GeneralException(_("Task not found")) { }

const char * GeneralException::GetMsg() const {
    return msg;
}

MemoryException::MemoryException(ssize_t s) {
    sprintf(t, _("Failed allocating %u bytes."), s);
    msg = strdup(t);
}

IOException::IOException(String fn, op_t op, ssize_t s) {
    sprintf(t, _("An error has occured while %s %u bytes on %s: %s"), op == IO_WRITE ? _("writing") : _("reading"),
	    s, fn.to_charp(), strerror(errno));
    msg = strdup(t);
}

IOGeneral::IOGeneral(String fn) : GeneralException(fn) { }

IOGeneral::IOGeneral() { }

IOAgain::IOAgain() : IOGeneral(_("No more bytes for reading or writing.")) {
#ifdef DEBUG
    printm(M_BARE, String(_("Generating an IOAgain exception: '")) + GetMsg() + "'.\n");
#endif
}

TaskSwitch::TaskSwitch() : GeneralException(_("Switching task in a non-tasked environnement")) {
#ifdef DEBUG
    printm(M_BARE, String(_("Generating a TaskSwitch exception: '")) + GetMsg() + "'.\n");
#endif
}

Exit::Exit(int a_code) : GeneralException(_("Exitting with code " + a_code)), code(a_code) {
#ifdef DEBUG
    printm(M_BARE, String(_("Generating an Exit exception: '")) + GetMsg() + "'.\n");
#endif
}

int Exit::GetCode() {
    return code;
}

char * xstrdup(const char * s) {
    char * r;
    
    r = (char *) xmalloc(strlen(s) + 1);
    strcpy(r, s);
    return r;
}

void * xmalloc(size_t s) throw (GeneralException) {
    char * r;
    
    if (s == 0) {
	return 0;
    }
    
    if (!(r = (char *) ::malloc(s))) {
	throw MemoryException(s);
    }
#ifdef DEBUG
//    Base::printm(M_BARE, String(_("Allocating %i bytes of memory, got it at %p\n")), s, r);
#endif
    
    memset(r, 0, s);
    
    return (void *)(r);
}

void * xrealloc(void * ptr, size_t s) {
#ifdef DEBUG
    void * r = realloc(ptr, s);
    Base::printm(M_BARE, String(_("Reallocating pointer at %p for %i bytes, now at %p\n")), ptr, s, r);
    return r;
#else
    return realloc(ptr, s);
#endif
}

void xfree(unsigned char *& p) {
#ifdef DEBUG
//    Base::printm(M_BARE, String(_("Freeing pointer at %p\n")), p);
#endif
    if (p) {
	::free(p);
	p = 0;
    }
}

#ifdef HAVE_PIPE
int xpipe(int * p, int flag) throw (GeneralException) {
    if (pipe(p)) {
	throw GeneralException(String(_("Error creating pipe: ")) + strerror(errno));
    }
			            
    return p[flag];
}
#else
int xpipe(int *, int) throw (GeneralException) {
    throw GeneralException(_("Function pipe() not supported by this system.\n"));
}
#endif

#ifdef HAVE_FORK
pid_t xfork() throw (GeneralException) {
    pid_t p;
    
    p = fork();
    
    if (p == -1) {
	throw GeneralException(_("Was not able to fork().\n"));
    }
    
    return p;
}
#else
pid_t xfork() throw (GeneralException) {
    throw GeneralException(_("Function fork() not supported by this system.\n"));
}
#endif

void xexit(int status) throw (GeneralException) {
    throw Exit(status);
}

void xexception(const String & err) throw (GeneralException) {
    throw GeneralException(err);
}

char * Base::strdup(const char * s) {
    return xstrdup(s);
}

void * Base::malloc(ssize_t s) {
    return xmalloc(s);
}

void * Base::realloc(void * p, size_t s) {
    return xrealloc(p, s);
}

void * Base::calloc(size_t n, size_t s) {
    return xmalloc(n * s);
}

void * Base::operator new(size_t s) {
#ifdef DEBUG
    printm(M_BARE, _("Operator new(s) called. Allocating memory.\n"));
#endif
    return xmalloc(s);
}

void * Base::operator new(size_t s, void * p) {
#ifdef DEBUG
    printm(M_BARE, String(_("Operator new(s, p) called with p = %p and s = %i. Erasing memory.\n")), p, s);
#endif
    return memset(p, 0, s);
}

void Base::operator delete(void * p) {
#ifdef DEBUG
    printm(M_BARE, _("Operator delete(p) called. Freeing memory.\n"));
#endif
    free(p);
}

int Base::pipe(int * p, int flag) {
    return xpipe(p, flag);
}

pid_t Base::fork() {
    return xfork();
}

void Base::exit(int status) {
    xexit(status);
}

void Base::flushcontext() {
    context.clear();
}

void Base::pushcontext(const String & c) {
    context.push_back(c);
}

void Base::popcontext() {
    context.pop_back();
}

void Base::exception(const String & err) {
    int c;
    std::vector<String>::iterator i;
    printm(M_ERROR, "Error detected, showing context.\n");
    for (i = context.begin(), c = 0; i != context.end(); i++, c++) {
	printm(M_ERROR, " (%i) - " + *i + "\n", c);
    }
    
    printm(M_ERROR, "  Error description: " + err);

    xexception(err);
}