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