~dabisu/hashit/hashit

« back to all changes in this revision

Viewing changes to mobs.h

  • Committer: david
  • Date: 2008-01-07 20:14:35 UTC
  • Revision ID: david@pleyades.net-20080107201435-5yalow3catrdcivq
* CMakeLists.txt:
        - Create first version for CMake configuration
* src/hashit.c:
        - Update to remove MOBS references
* Added ignore patters for bazaar       
* Remove MOBS files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Revision: 65 $
2
 
    This file contains development & diagnostic helpers
3
 
    
4
 
    Copyright (C) 2005,2006 Rau'l Nu'n~ez de Arenas Coronado
5
 
    Report bugs to DervishD <bugs@dervishd.net>
6
 
 
7
 
         This program is free software; you can redistribute it and/or
8
 
          modify it under the terms of the GNU General Public License
9
 
            version 2 as published by the Free Software Foundation
10
 
 
11
 
        This program is distributed in the hope that it will be useful,
12
 
          but WITHOUT ANY WARRANTY; without even the implied warranty
13
 
            of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 
             See the GNU General Public License for more details.
15
 
 
16
 
       You should have received a copy of the GNU General Public License
17
 
              ('GPL') along with this program; if not, write to:
18
 
 
19
 
                        Free Software Foundation, Inc.
20
 
                          59 Temple Place, Suite 330
21
 
                          Boston, MA 02111-1307  USA
22
 
*/
23
 
#ifndef __MOBS_H__
24
 
#define __MOBS_H__
25
 
#include <stdio.h>
26
 
#include <sys/types.h>
27
 
#include <unistd.h>
28
 
#include <errno.h>
29
 
#include <stdlib.h>
30
 
#include <string.h>
31
 
#include <stdarg.h>
32
 
#include <stdint.h>
33
 
#include <wchar.h>
34
 
#include "config.h"
35
 
#endif
36
 
 
37
 
 
38
 
/*
39
 
    This macro is optional, but we assign here a default value so
40
 
the code of the rest of function and macros is a bit easier.
41
 
*/
42
 
#ifndef AUTHOR
43
 
#define AUTHOR ""
44
 
#endif
45
 
 
46
 
 
47
 
/*
48
 
    This macro should have been set to the module (object file) name.
49
 
I think that "(?)" is a sane default if it has not.
50
 
*/
51
 
#ifndef OBJNAME
52
 
#define OBJNAME "(?)"
53
 
#endif
54
 
 
55
 
 
56
 
/*
57
 
    This macro should have been set by the developer to
58
 
the project's name. Otherwise we complain, the project
59
 
MUST have a name. It's the Tao.
60
 
*/
61
 
#ifndef PROJECT
62
 
#error "'PROJECT' is undefined, and 'mobs.h' needs it!"
63
 
#endif
64
 
 
65
 
 
66
 
/*
67
 
    This macro should have been set by the developer to
68
 
the project's version. Otherwise we complain, because the
69
 
project should have a version. This is also the Tao...
70
 
*/
71
 
#ifndef VERSION
72
 
#error "'VERSION' is undefined, and 'mobs.h' needs it!"
73
 
#endif
74
 
 
75
 
 
76
 
/*
77
 
    This macro just stringifies its argument, no matter if it
78
 
is a literal or another macro. It's because of that it needs
79
 
the additional level of indirection. Only 'STRFY()' is meant
80
 
to be used. The other macro is just a helper.
81
 
*/
82
 
#ifndef STRFY
83
 
#define STRFY(mobs_expression) __MOBS_H_STRFY(mobs_expression)
84
 
    #ifndef __MOBS_H_STRFY
85
 
    #define __MOBS_H_STRFY(mobs_expression) #mobs_expression
86
 
    #endif
87
 
#endif
88
 
 
89
 
/*
90
 
    This macro does the same that 'BUG()', but with the semantics of
91
 
the C9x 'assert()' macro, and ignoring the value of 'errno'. The main
92
 
differences between 'assert()' and 'ASSERT()' may be the verbosity of
93
 
the latter and the format of the messages. Moreover, 'ASSERT()' cannot
94
 
be evaluated: take this into account when using it. A trace mark is
95
 
also printed for helping to locate this assertion.
96
 
 
97
 
    Use this macro to make assertions. Quite easy, isn't it? ;)))
98
 
*/
99
 
#undef ASSERT
100
 
#undef WASSERT
101
 
#ifndef NDEBUG
102
 
#define ASSERT(mobs_expr) do { if (!(mobs_expr)) {\
103
 
    fwide(stderr, -1);\
104
 
    fprintf(stderr, "*** ASSERTION FAILED, process %d aborting...\n", getpid());\
105
 
    fprintf(stderr, "*** Assertion \"(%s)\" failed at %s()@%s:%d\n", #mobs_expr, __func__, __FILE__, __LINE__);\
106
 
    if (strlen(AUTHOR))\
107
 
        fputs("Report bug to "AUTHOR"\n", stderr);\
108
 
    fflush(stderr);\
109
 
    abort();\
110
 
}} while (0)
111
 
#define WASSERT(mobs_expr) do { if (!(mobs_expr)) {\
112
 
    fwide(stderr, 1);\
113
 
    fwprintf(stderr, L"*** ASSERTION FAILED, process %d aborting...\n", getpid());\
114
 
    fwprintf(stderr, L"*** Assertion \"(%s)\" failed at %s()@%s:%d\n", #mobs_expr, __func__, __FILE__, __LINE__);\
115
 
    if (wcslen(L"" AUTHOR ""))\
116
 
        fputws(L"Report bug to "AUTHOR"\n", stderr);\
117
 
    fflush(stderr);\
118
 
    abort();\
119
 
}} while (0)
120
 
#else
121
 
#define ASSERT(mobs_expr) do {(void)(mobs_expr);} while(0)
122
 
#define WASSERT(mobs_expr) do {(void)(mobs_expr);} while(0)
123
 
#endif
124
 
 
125
 
 
126
 
 
127
 
/* This is a helper function to prettyprint the program name */
128
 
#ifndef __MOBS_progname
129
 
#define __MOBS_progname
130
 
static inline const char *progname (const char *);
131
 
static inline const char *progname (const char *mobs_string) {
132
 
    char *mobs_where=NULL;
133
 
    
134
 
    ASSERT(mobs_string != NULL);
135
 
 
136
 
    mobs_where=strrchr(mobs_string, '/');
137
 
    return mobs_where?mobs_where+1:mobs_string;
138
 
}
139
 
 
140
 
static inline const wchar_t *wprogname (const wchar_t *);
141
 
static inline const wchar_t *wprogname (const wchar_t *mobs_string) {
142
 
    wchar_t *mobs_where=NULL;
143
 
    
144
 
    WASSERT(mobs_string != NULL);
145
 
 
146
 
    mobs_where=wcsrchr(mobs_string, L'/');
147
 
    return mobs_where?mobs_where+1:mobs_string;
148
 
}
149
 
#endif
150
 
 
151
 
 
152
 
/* This one is pretty large ;))). The GPL disclaimer, just in case... */
153
 
#ifndef GPL_DISCLAIMER
154
 
#define GPL_DISCLAIMER() do {\
155
 
    fwide(stdout, -1);\
156
 
    fputs("This program is part of '"PROJECT"-"VERSION"'\n", stdout);\
157
 
    fputc('\n', stdout);\
158
 
    fputs("This program is free software;\n", stdout);\
159
 
    fputs("you can redistribute it and/or modify it under the terms of the\n", stdout);\
160
 
    fputs("GNU General Public License as published by the Free Software Foundation;\n", stdout);\
161
 
    fputs("either version 2 of the License, or (at your option) any later version.\n", stdout);\
162
 
    fputc('\n', stdout);\
163
 
    fputs("This program is distributed in the hope that it will be useful,\n", stdout);\
164
 
    fputs("but WITHOUT ANY WARRANTY; without even the implied warranty\n", stdout);\
165
 
    fputs("of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n", stdout);\
166
 
    fputs("See the GNU General Public License for more details.\n", stdout);\
167
 
    fputc('\n', stdout);\
168
 
    fputs("You should have received a copy of the GNU General Public License\n", stdout);\
169
 
    fputs("('GPL') along with this program; if not, write to:\n", stdout);\
170
 
    fputs("\tFree Software Foundation, Inc.\n", stdout);\
171
 
    fputs("\t59 Temple Place, Suite 330\n", stdout);\
172
 
    fputs("\tBoston, MA 02111-1307  USA\n", stdout);\
173
 
    fflush(stdout);\
174
 
} while(0)
175
 
#endif
176
 
 
177
 
#ifndef WGPL_DISCLAIMER
178
 
#define WGPL_DISCLAIMER() do {\
179
 
    fwide(stdout, 1);\
180
 
    fputws(L"This program is part of '"PROJECT"-"VERSION"'\n", stdout);\
181
 
    fputwc(L'\n', stdout);\
182
 
    fputws(L"This program is free software;\n", stdout);\
183
 
    fputws(L"you can redistribute it and/or modify it under the terms of the\n", stdout);\
184
 
    fputws(L"GNU General Public License as published by the Free Software Foundation;\n", stdout);\
185
 
    fputws(L"either version 2 of the License, or (at your option) any later version.\n", stdout);\
186
 
    fputwc(L'\n', stdout);\
187
 
    fputws(L"This program is distributed in the hope that it will be useful,\n", stdout);\
188
 
    fputws(L"but WITHOUT ANY WARRANTY; without even the implied warranty\n", stdout);\
189
 
    fputws(L"of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n", stdout);\
190
 
    fputws(L"See the GNU General Public License for more details.\n", stdout);\
191
 
    fputwc(L'\n', stdout);\
192
 
    fputws(L"You should have received a copy of the GNU General Public License\n", stdout);\
193
 
    fputws(L"('GPL') along with this program; if not, write to:\n", stdout);\
194
 
    fputws(L"\tFree Software Foundation, Inc.\n", stdout);\
195
 
    fputws(L"\t59 Temple Place, Suite 330\n", stdout);\
196
 
    fputws(L"\tBoston, MA 02111-1307  USA\n", stdout);\
197
 
    fflush(stdout);\
198
 
} while(0)
199
 
#endif
200
 
 
201
 
 
202
 
 
203
 
/*
204
 
    This function makes the program die nicely with 'EXIT_FAILURE',
205
 
outputting a formatted message to 'stderr', followed by '\n'.
206
 
 
207
 
    By now there is no further information in the output message.
208
 
*/
209
 
#ifndef __MOBS_die
210
 
#define __MOBS_die
211
 
static inline void die (const char *, ...);
212
 
static inline void die (const char *mobs_format, ...) {
213
 
 
214
 
    va_list mobs_args;
215
 
 
216
 
    ASSERT(mobs_format);
217
 
    
218
 
    fwide(stdout, -1);
219
 
 
220
 
    va_start(mobs_args, mobs_format);
221
 
    vfprintf(stderr, mobs_format, mobs_args);
222
 
    va_end(mobs_args);
223
 
 
224
 
    fflush(stderr);
225
 
 
226
 
    exit(EXIT_FAILURE);
227
 
}
228
 
 
229
 
static inline void wdie (const wchar_t *, ...);
230
 
static inline void wdie (const wchar_t *mobs_format, ...) {
231
 
 
232
 
    va_list mobs_args;
233
 
 
234
 
    WASSERT(mobs_format);
235
 
 
236
 
    fwide(stdout, 1);
237
 
    
238
 
    va_start(mobs_args, mobs_format);
239
 
    vfwprintf(stderr, mobs_format, mobs_args);
240
 
    va_end(mobs_args);
241
 
 
242
 
    fflush(stderr);
243
 
 
244
 
    exit(EXIT_FAILURE);
245
 
}
246
 
#endif
247
 
 
248
 
 
249
 
/*
250
 
    This macro logs an internal error and aborts.
251
 
 
252
 
    Use this to report *impossible* error conditions, that is, failed
253
 
assertions (consider using 'ASSERT()' instead for a more classical
254
 
semantics). It's not intended to produce information useful to the user.
255
 
 
256
 
    It prints the 'errno' value (only if nonzero), its associated message
257
 
and a trace mark, but the caller must take into account that this information
258
 
is not always meaningful, specially the trace mark.
259
 
 
260
 
    Since BUG's occur in production releases, this is unconditionally defined
261
 
in order to help the users on sending bug reports with good information ;)
262
 
It outputs the project name and version for filling the bug report :)
263
 
*/
264
 
#ifndef BUG
265
 
#define BUG() do {\
266
 
    int mobs_errno=errno;\
267
 
    fwide(stderr, -1);\
268
 
    fputs("*** INTERNAL ERROR, please report so it can be fixed!\n",stderr);\
269
 
    fprintf(stderr, "*** Process %d aborting...\n", getpid());\
270
 
    fprintf(stderr, "*** errno <%d> (%s)\n", mobs_errno, strerror(mobs_errno));\
271
 
    fputs("*** '"PROJECT"-"VERSION" (" OBJNAME ") ", stderr);\
272
 
    fprintf(stderr, "%s()@%s:%d\n", __func__, __FILE__, __LINE__);\
273
 
    if (strlen(AUTHOR))\
274
 
        fputs("*** Report bug to "AUTHOR"\n", stderr);\
275
 
    fflush(stderr);\
276
 
    abort();\
277
 
} while (0)
278
 
#endif
279
 
 
280
 
#ifndef WBUG
281
 
#define WBUG() do {\
282
 
    int mobs_errno=errno;\
283
 
    fwide(stderr, 1);\
284
 
    fputws(L"*** INTERNAL ERROR, please report so it can be fixed!\n",stderr);\
285
 
    fwprintf(stderr, L"*** Process %d aborting...\n", getpid());\
286
 
    fwprintf(stderr, L"*** errno <%d> (%s)\n", mobs_errno, strerror(mobs_errno));\
287
 
    fputws(L"*** '"PROJECT"-"VERSION" (" OBJNAME ") ", stderr);\
288
 
    fwprintf(stderr, L"%s()@%s:%d\n", __func__, __FILE__, __LINE__);\
289
 
    if (wcslen(L"" AUTHOR ""))\
290
 
        fputws(L"*** Report bug to "AUTHOR"\n", stderr);\
291
 
    fflush(stderr);\
292
 
    abort();\
293
 
} while (0)
294
 
#endif
295
 
 
296
 
 
297
 
/*
298
 
    This function shows the message (the format string, you know...)
299
 
together with information about the current 'errno' value (strerror...)
300
 
Just for convenience... The actual format may change...
301
 
 
302
 
    'errno' is not changed.
303
 
*/
304
 
#ifndef __MOBS_bang
305
 
#define __MOBS_bang
306
 
static inline void bang (const char *, ...);
307
 
static inline void bang (const char *mobs_format, ...) {
308
 
 
309
 
    va_list mobs_args;
310
 
    int mobs_errno=errno;
311
 
 
312
 
    ASSERT(mobs_format);
313
 
 
314
 
    fwide(stderr, -1);
315
 
    
316
 
    va_start(mobs_args, mobs_format);
317
 
    vfprintf(stderr, mobs_format, mobs_args);
318
 
    va_end(mobs_args);
319
 
 
320
 
    fprintf(stderr, "errno <%d> (%s)\n", mobs_errno, strerror(mobs_errno));
321
 
    fflush(stderr);
322
 
 
323
 
    errno=mobs_errno;
324
 
}
325
 
 
326
 
static inline void wbang (const wchar_t *, ...);
327
 
static inline void wbang (const wchar_t *mobs_format, ...) {
328
 
 
329
 
    va_list mobs_args;
330
 
    int mobs_errno=errno;
331
 
 
332
 
    WASSERT(mobs_format);
333
 
 
334
 
    fwide(stderr, 1);
335
 
    
336
 
    va_start(mobs_args, mobs_format);
337
 
    vfwprintf(stderr, mobs_format, mobs_args);
338
 
    va_end(mobs_args);
339
 
 
340
 
    fwprintf(stderr, L"errno <%d> (%s)\n", mobs_errno, strerror(mobs_errno));
341
 
    fflush(stderr);
342
 
 
343
 
    errno=mobs_errno;
344
 
}
345
 
 
346
 
 
347
 
#endif
348
 
 
349
 
 
350
 
/*
351
 
    This function outputs a formatted string as specified by 'format', but
352
 
preceeded by the module name and PID, followed by a '\n'.
353
 
 
354
 
    Please note that there is no way of testing whether the arguments are
355
 
correct or meaningful according with 'format'. Double check the format
356
 
string and the parameters, since 'SAY()' will silently fail and fuck up
357
 
your code if you don't use it correctly ;)))))
358
 
 
359
 
    Use this function to watch variables and the like. If you just want to
360
 
trace code use the 'T' macro instead. This is useful too if you want to say
361
 
something before 'BUG()'.
362
 
 
363
 
    Please note that if 'NDEBUG' is defined, the funcion does nothing, and
364
 
that the 'T' macro is really an alias to 'SAY()'.
365
 
 
366
 
    'errno' is not changed.
367
 
*/
368
 
#undef SAY
369
 
#undef WSAY
370
 
#ifndef NDEBUG
371
 
#define SAY(mobs_format,...) do {\
372
 
    int mobs_errno=errno;\
373
 
    fwide(stderr, -1);\
374
 
    fprintf(stderr, OBJNAME" [%d] ", getpid());\
375
 
    if (mobs_format) fprintf(stderr, mobs_format, ##__VA_ARGS__);\
376
 
    fputc('\n', stderr);\
377
 
    fflush(stderr);\
378
 
    errno=mobs_errno;\
379
 
} while (0)
380
 
#define WSAY(mobs_format,...) do {\
381
 
    int mobs_errno=errno;\
382
 
    fwide(stderr, 1);\
383
 
    fwprintf(stderr, L""OBJNAME" [%d] ", getpid());\
384
 
    if (mobs_format) fwprintf(stderr, mobs_format, ##__VA_ARGS__);\
385
 
    fputwc('\n', stderr);\
386
 
    fflush(stderr);\
387
 
    errno=mobs_errno;\
388
 
} while (0)
389
 
#else
390
 
#define SAY(mobs_format,...) do {} while(0)
391
 
#define WSAY(mobs_format,...) do {} while(0)
392
 
#endif
393
 
#undef T
394
 
#undef WT
395
 
#define T() SAY("%s()@%s:%d", __func__, __FILE__, __LINE__)
396
 
#define WT() WSAY(L"%s()@%s:%d", __func__, __FILE__, __LINE__)