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