]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
Merged obj2buck.h and buck2obj.h to object.h, the number of includes
[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 #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
46 /* Logging */
47
48 #define L_DEBUG         'D'             /* Debugging messages */
49 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
50 #define L_WARN          'W'
51 #define L_ERROR         'E'
52 #define L_INFO_R        'i'             /* Errors caused by external events */
53 #define L_WARN_R        'w'
54 #define L_ERROR_R       'e'
55 #define L_FATAL         '!'             /* die() */
56
57 extern char *log_title;                 /* NULL - print no title, default is log_progname */
58 extern char *log_filename;              /* Expanded name of the current log file */
59 extern int log_switch_nest;             /* log_switch() nesting counter, increment to disable automatic switches */
60 extern int log_pid;                     /* 0 if shouldn't be logged */
61 extern void (*log_die_hook)(void);
62 struct tm;
63 extern void (*log_switch_hook)(struct tm *tm);
64
65 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
66 #define log log_msg
67 void vlog_msg(unsigned int cat, const char *msg, va_list args);
68 void die(byte *, ...) NONRET;
69 void log_init(byte *argv0);
70 void log_file(byte *name);
71 void log_fork(void);
72 void log_switch(void);
73
74 #ifdef DEBUG
75 void assert_failed(char *assertion, char *file, int line) NONRET;
76 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
77 #else
78 void assert_failed(void) NONRET;
79 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed(); } while(0)
80 #endif
81
82 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
83
84 #ifdef LOCAL_DEBUG
85 #define DBG(x,y...) log(L_DEBUG, x,##y)
86 #else
87 #define DBG(x,y...) do { } while(0)
88 #endif
89
90 static inline void log_switch_disable(void) { log_switch_nest++; }
91 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
92
93 /* Memory allocation */
94
95 #ifdef DEBUG_DMALLOC
96 /*
97  * The standard dmalloc macros tend to produce lots of namespace
98  * conflicts and we use only xmalloc and xfree, so we can define
99  * the stubs ourselves.
100  */
101 #define DMALLOC_DISABLE
102 #include <dmalloc.h>
103 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
104 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
105 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
106 #else
107 /*
108  * Unfortunately, several libraries we might want to link to define
109  * their own xmalloc and we don't want to interfere with them, hence
110  * the renaming.
111  */
112 #define xmalloc sh_xmalloc
113 void *xmalloc(unsigned);
114 void *xrealloc(void *, unsigned);
115 #define xfree(x) free(x)
116 #endif
117
118 void *xmalloc_zero(unsigned);
119 byte *xstrdup(byte *);
120
121 /* Content-Type pattern matching and filters */
122
123 int match_ct_patt(byte *, byte *);
124
125 /* log2.c */
126
127 int fls(u32);
128
129 /* wordsplit.c */
130
131 int wordsplit(byte *, byte **, uns);
132
133 /* pat(i)match.c: Matching of shell patterns */
134
135 int match_pattern(byte *, byte *);
136 int match_pattern_nocase(byte *, byte *);
137
138 /* md5hex.c */
139
140 void md5_to_hex(byte *, byte *);
141 void hex_to_md5(byte *, byte *);
142
143 #define MD5_SIZE 16
144 #define MD5_HEX_SIZE 33
145
146 /* prime.c */
147
148 int isprime(uns);
149 uns nextprime(uns);
150
151 /* timer.c */
152
153 void init_timer(void);
154 uns get_timer(void);
155
156 /* regex.c */
157
158 typedef struct regex regex;
159
160 regex *rx_compile(byte *r, int icase);
161 void rx_free(regex *r);
162 int rx_match(regex *r, byte *s);
163 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
164
165 /* random.c */
166
167 uns random_max(uns);
168
169 /* mmap.c */
170
171 void *mmap_file(byte *name, unsigned *len, int writeable);
172 void munmap_file(void *start, unsigned len);
173
174 /* partmap.c */
175
176 struct partmap;
177 struct partmap *partmap_open(byte *name, int writeable);
178 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
179 void partmap_close(struct partmap *p);
180 sh_off_t partmap_size(struct partmap *p);
181
182 /* proctitle.c */
183
184 void setproctitle_init(int argc, char **argv);
185 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
186
187 /* randomkey.c */
188
189 void randomkey(byte *buf, uns size);
190
191 /* exitstatus.c */
192
193 #define EXIT_STATUS_MSG_SIZE 32
194 int format_exit_status(byte *msg, int stat);
195
196 /* runcmd.c */
197
198 int run_command(byte *cmd, ...);
199 void NONRET exec_command(byte *cmd, ...);
200 void echo_command(byte *buf, int size, byte *cmd, ...);
201 int run_command_v(byte *cmd, va_list args);
202 void NONRET exec_command_v(byte *cmd, va_list args);
203 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
204
205 /* carefulio.c */
206
207 int careful_read(int fd, void *buf, int len);
208 int careful_write(int fd, void *buf, int len);
209
210 /* sighandler.c */
211
212 typedef int (*sh_sighandler_t)(int);
213   /* obtains signum, returns nonzero if abort() should be called */
214 extern sh_sighandler_t signal_handler[];
215
216 struct sigaction;
217 void handle_signal(int signum, struct sigaction *oldact);
218 void unhandle_signal(int signum, struct sigaction *oldact);
219
220 #endif