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