]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
avoid compiler warnings with debugging turned off
[libucw.git] / lib / lib.h
1 /*
2  *      The UCW Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2006 Martin Mares <mj@ucw.cz>
5  *      (c) 2005 Tomas Valla <tom@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_LIB_H
12 #define _UCW_LIB_H
13
14 #include "lib/config.h"
15 #include <stdarg.h>
16
17 /* Tell libc we're going to use all extensions available */
18
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif
22
23 /* Ugly structure handling macros */
24
25 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
26 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
27 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
28 #define ALIGN_PTR(p, s) ((addr_int_t)(p) % (s) ? (typeof(p))((addr_int_t)(p) + (s) - (addr_int_t)(p) % (s)) : (p))
29 #define UNALIGNED_PART(ptr, type) (((addr_int_t) (ptr)) % sizeof(type))
30
31 /* Some other macros */
32
33 #define MIN(a,b) (((a)<(b))?(a):(b))
34 #define MAX(a,b) (((a)>(b))?(a):(b))
35 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
36 #define ABS(x) ((x) < 0 ? -(x) : (x))
37 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
38 #define STRINGIFY(x) #x
39 #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
40 #define GLUE(x,y) x##y
41 #define GLUE_(x,y) x##_##y
42
43 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
44 #define REV_COMPARE(x,y) COMPARE(y,x)
45 #define COMPARE_LT(x,y) do { if ((x)<(y)) return 1; if ((x)>(y)) return 0; } while(0)
46 #define COMPARE_GT(x,y) COMPARE_LT(y,x)
47
48 #define ROL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(uns)*8 - (bits))))      /* Bitwise rotation of an uns to the left */
49
50 /* GCC Extensions */
51
52 #ifdef __GNUC__
53
54 #undef inline
55 #define NONRET __attribute__((noreturn))
56 #define UNUSED __attribute__((unused))
57 #define CONSTRUCTOR __attribute__((constructor))
58 #define PACKED __attribute__((packed))
59 #define CONST __attribute__((const))
60 #define PURE __attribute__((const))
61 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
62 #define likely(x) __builtin_expect((x),1)
63 #define unlikely(x) __builtin_expect((x),0)
64
65 #else
66 #error This program requires the GNU C compiler.
67 #endif
68
69 /* Logging */
70
71 #define L_DEBUG         'D'             /* Debugging messages */
72 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
73 #define L_WARN          'W'
74 #define L_ERROR         'E'
75 #define L_INFO_R        'i'             /* Errors caused by external events */
76 #define L_WARN_R        'w'
77 #define L_ERROR_R       'e'
78 #define L_FATAL         '!'             /* die() */
79
80 extern char *log_title;                 /* NULL - print no title, default is log_progname */
81 extern char *log_filename;              /* Expanded name of the current log file */
82 extern volatile int log_switch_nest;    /* log_switch() nesting counter, increment to disable automatic switches */
83 extern int log_pid;                     /* 0 if shouldn't be logged */
84 extern int log_precise_timings;         /* Include microsecond timestamps in log messages */
85 extern void (*log_die_hook)(void);
86 struct tm;
87 extern void (*log_switch_hook)(struct tm *tm);
88
89 void log_msg(unsigned int cat, const char *msg, ...) FORMAT_CHECK(printf,2,3);
90 #define log log_msg
91 void vlog_msg(unsigned int cat, const char *msg, va_list args);
92 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
93 void log_init(byte *argv0);
94 void log_file(byte *name);
95 void log_fork(void);
96 int log_switch(void);
97
98 void assert_failed(char *assertion, char *file, int line) NONRET;
99 void assert_failed_noinfo(void) NONRET;
100
101 #ifdef DEBUG_ASSERTS
102 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
103 #else
104 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); } while(0)
105 #endif
106
107 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
108
109 #ifdef LOCAL_DEBUG
110 #define DBG(x,y...) log(L_DEBUG, x,##y)
111 #else
112 #define DBG(x,y...) do { } while(0)
113 #endif
114
115 static inline void log_switch_disable(void) { log_switch_nest++; }
116 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
117
118 /* Memory allocation */
119
120 #define xmalloc sh_xmalloc
121 #define xrealloc sh_xrealloc
122 #define xfree sh_xfree
123
124 #ifdef DEBUG_DMALLOC
125 /*
126  * The standard dmalloc macros tend to produce lots of namespace
127  * conflicts and we use only xmalloc and xfree, so we can define
128  * the stubs ourselves.
129  */
130 #define DMALLOC_DISABLE
131 #include <dmalloc.h>
132 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
133 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
134 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
135 #else
136 /*
137  * Unfortunately, several libraries we might want to link to define
138  * their own xmalloc and we don't want to interfere with them, hence
139  * the renaming.
140  */
141 void *xmalloc(unsigned);
142 void *xrealloc(void *, unsigned);
143 void xfree(void *);
144 #endif
145
146 void *xmalloc_zero(unsigned);
147 byte *xstrdup(byte *);
148
149 /* Content-Type pattern matching and filters */
150
151 int match_ct_patt(byte *, byte *);
152
153 /* wordsplit.c */
154
155 int sepsplit(byte *str, byte sep, byte **rec, uns max);
156 int wordsplit(byte *, byte **, uns);
157
158 /* pat(i)match.c: Matching of shell patterns */
159
160 int match_pattern(byte *, byte *);
161 int match_pattern_nocase(byte *, byte *);
162
163 /* md5hex.c */
164
165 void md5_to_hex(byte *, byte *);
166 void hex_to_md5(byte *, byte *);
167
168 #define MD5_SIZE 16
169 #define MD5_HEX_SIZE 33
170
171 /* prime.c */
172
173 int isprime(uns);
174 uns nextprime(uns);
175
176 /* primetable.c */
177
178 uns next_table_prime(uns x);
179 uns prev_table_prime(uns x);
180
181 /* timer.c */
182
183 struct timeval;
184
185 void init_timer(void);
186 uns get_timer(void);
187 void get_last_timeval(struct timeval *tv);
188
189 /* regex.c */
190
191 typedef struct regex regex;
192
193 regex *rx_compile(byte *r, int icase);
194 void rx_free(regex *r);
195 int rx_match(regex *r, byte *s);
196 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
197
198 /* random.c */
199
200 uns random_max(uns);
201
202 /* mmap.c */
203
204 void *mmap_file(byte *name, unsigned *len, int writeable);
205 void munmap_file(void *start, unsigned len);
206
207 /* proctitle.c */
208
209 void setproctitle_init(int argc, char **argv);
210 void setproctitle(char *msg, ...) FORMAT_CHECK(printf,1,2);
211 char *getproctitle(void);
212
213 /* randomkey.c */
214
215 void randomkey(byte *buf, uns size);
216
217 /* exitstatus.c */
218
219 #define EXIT_STATUS_MSG_SIZE 32
220 int format_exit_status(byte *msg, int stat);
221
222 /* runcmd.c */
223
224 int run_command(byte *cmd, ...);
225 void NONRET exec_command(byte *cmd, ...);
226 void echo_command(byte *buf, int size, byte *cmd, ...);
227 int run_command_v(byte *cmd, va_list args);
228 void NONRET exec_command_v(byte *cmd, va_list args);
229 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
230
231 /* carefulio.c */
232
233 int careful_read(int fd, void *buf, int len);
234 int careful_write(int fd, void *buf, int len);
235
236 /* sync.c */
237
238 void sync_dir(byte *name);
239
240 /* sighandler.c */
241
242 typedef int (*sh_sighandler_t)(int);
243   /* obtains signum, returns nonzero if abort() should be called */
244 extern sh_sighandler_t signal_handler[];
245
246 struct sigaction;
247 void handle_signal(int signum, struct sigaction *oldact);
248 void unhandle_signal(int signum, struct sigaction *oldact);
249
250 #endif