]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
_GNU_SOURCE can be already defined by compiler switches, avoid warnings.
[libucw.git] / lib / lib.h
1 /*
2  *      The UCW Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2005 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 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 void (*log_die_hook)(void);
85 struct tm;
86 extern void (*log_switch_hook)(struct tm *tm);
87
88 void log_msg(unsigned int cat, const char *msg, ...) FORMAT_CHECK(printf,2,3);
89 #define log log_msg
90 void vlog_msg(unsigned int cat, const char *msg, va_list args);
91 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
92 void log_init(byte *argv0);
93 void log_file(byte *name);
94 void log_fork(void);
95 int log_switch(void);
96
97 void assert_failed(char *assertion, char *file, int line) NONRET;
98 void assert_failed_noinfo(void) NONRET;
99
100 #ifdef DEBUG_ASSERTS
101 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
102 #else
103 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); } while(0)
104 #endif
105
106 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
107
108 #ifdef LOCAL_DEBUG
109 #define DBG(x,y...) log(L_DEBUG, x,##y)
110 #else
111 #define DBG(x,y...) do { } while(0)
112 #endif
113
114 static inline void log_switch_disable(void) { log_switch_nest++; }
115 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
116
117 /* Memory allocation */
118
119 #define xmalloc sh_xmalloc
120 #define xrealloc sh_xrealloc
121 #define xfree sh_xfree
122
123 #ifdef DEBUG_DMALLOC
124 /*
125  * The standard dmalloc macros tend to produce lots of namespace
126  * conflicts and we use only xmalloc and xfree, so we can define
127  * the stubs ourselves.
128  */
129 #define DMALLOC_DISABLE
130 #include <dmalloc.h>
131 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
132 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
133 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
134 #else
135 /*
136  * Unfortunately, several libraries we might want to link to define
137  * their own xmalloc and we don't want to interfere with them, hence
138  * the renaming.
139  */
140 void *xmalloc(unsigned);
141 void *xrealloc(void *, unsigned);
142 #define sh_xfree(x) free(x)
143 #endif
144
145 void *xmalloc_zero(unsigned);
146 byte *xstrdup(byte *);
147
148 /* Content-Type pattern matching and filters */
149
150 int match_ct_patt(byte *, byte *);
151
152 /* wordsplit.c */
153
154 int sepsplit(byte *str, byte sep, byte **rec, uns max);
155 int wordsplit(byte *, byte **, uns);
156
157 /* pat(i)match.c: Matching of shell patterns */
158
159 int match_pattern(byte *, byte *);
160 int match_pattern_nocase(byte *, byte *);
161
162 /* md5hex.c */
163
164 void md5_to_hex(byte *, byte *);
165 void hex_to_md5(byte *, byte *);
166
167 #define MD5_SIZE 16
168 #define MD5_HEX_SIZE 33
169
170 /* prime.c */
171
172 int isprime(uns);
173 uns nextprime(uns);
174
175 /* primetable.c */
176
177 uns next_table_prime(uns x);
178 uns prev_table_prime(uns x);
179
180 /* timer.c */
181
182 struct timeval;
183
184 void init_timer(void);
185 uns get_timer(void);
186 void get_last_timeval(struct timeval *tv);
187
188 /* regex.c */
189
190 typedef struct regex regex;
191
192 regex *rx_compile(byte *r, int icase);
193 void rx_free(regex *r);
194 int rx_match(regex *r, byte *s);
195 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
196
197 /* random.c */
198
199 uns random_max(uns);
200
201 /* mmap.c */
202
203 void *mmap_file(byte *name, unsigned *len, int writeable);
204 void munmap_file(void *start, unsigned len);
205
206 /* proctitle.c */
207
208 void setproctitle_init(int argc, char **argv);
209 void setproctitle(char *msg, ...) FORMAT_CHECK(printf,1,2);
210
211 /* randomkey.c */
212
213 void randomkey(byte *buf, uns size);
214
215 /* exitstatus.c */
216
217 #define EXIT_STATUS_MSG_SIZE 32
218 int format_exit_status(byte *msg, int stat);
219
220 /* runcmd.c */
221
222 int run_command(byte *cmd, ...);
223 void NONRET exec_command(byte *cmd, ...);
224 void echo_command(byte *buf, int size, byte *cmd, ...);
225 int run_command_v(byte *cmd, va_list args);
226 void NONRET exec_command_v(byte *cmd, va_list args);
227 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
228
229 /* carefulio.c */
230
231 int careful_read(int fd, void *buf, int len);
232 int careful_write(int fd, void *buf, int len);
233
234 /* sync.c */
235
236 void sync_dir(byte *name);
237
238 /* sighandler.c */
239
240 typedef int (*sh_sighandler_t)(int);
241   /* obtains signum, returns nonzero if abort() should be called */
242 extern sh_sighandler_t signal_handler[];
243
244 struct sigaction;
245 void handle_signal(int signum, struct sigaction *oldact);
246 void unhandle_signal(int signum, struct sigaction *oldact);
247
248 #endif