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