]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
ad19f505f38ea191ac9e34406b66d7d8db63d368
[libucw.git] / lib / lib.h
1 /*
2  *      Sherlock Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2001 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
21 /* Tell libc we're going to use all extensions available */
22
23 #define _GNU_SOURCE
24
25 /* Ugly structure handling macros */
26
27 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
28 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
29 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
30 #define UNALIGNED_PART(ptr, type) (((long) (ptr)) % sizeof(type))
31
32 /* Some other macros */
33
34 #define MIN(a,b) (((a)<(b))?(a):(b))
35 #define MAX(a,b) (((a)>(b))?(a):(b))
36 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
37 #define ABS(x) ((x) < 0 ? -(x) : (x))
38 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
39
40 /* Logging */
41
42 #define L_DEBUG         'D'             /* Debugging messages */
43 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
44 #define L_WARN          'W'
45 #define L_ERROR         'E'
46 #define L_INFO_R        'i'             /* Errors caused by external events */
47 #define L_WARN_R        'w'
48 #define L_ERROR_R       'e'
49 #define L_FATAL         '!'             /* die() */
50
51 void log(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
52 void die(byte *, ...) NONRET;
53 void assert_failed(char *assertion, char *file, int line) NONRET;
54 void log_init(byte *);
55 void log_file(byte *);
56 void log_fork(void);
57
58 #ifdef DEBUG
59 #define ASSERT(x) do { if (!(x)) assert_failed(#x, __FILE__, __LINE__); } while(0)
60 #else
61 #define ASSERT(x) do { } while(0)
62 #endif
63
64 #ifdef LOCAL_DEBUG
65 #define DBG(x,y...) log(L_DEBUG, x,##y)
66 #else
67 #define DBG(x,y...) do { } while(0)
68 #endif
69
70 /* Memory allocation */
71
72 #ifdef DMALLOC
73 /*
74  * The standard dmalloc macros tend to produce lots of namespace
75  * conflicts and we use only xmalloc and xfree, so we can define
76  * the stubs ourselves.
77  */
78 #define DMALLOC_DISABLE
79 #include <dmalloc.h>
80 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
81 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
82 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
83 #else
84 /*
85  * Unfortunately, several libraries we might want to link to define
86  * their own xmalloc and we don't want to interfere with them, hence
87  * the renaming.
88  */
89 #define xmalloc sh_xmalloc
90 void *xmalloc(unsigned);
91 void *xrealloc(void *, unsigned);
92 #define xfree(x) free(x)
93 #endif
94
95 void *xmalloc_zero(unsigned);
96 byte *stralloc(byte *);
97
98 /* Objects */
99
100 struct fastbuf;
101
102 struct odes {                           /* Object description */
103   struct oattr *attrs;
104   struct mempool *pool, *local_pool;
105 };
106
107 struct oattr {                          /* Object attribute */
108   struct oattr *next, *same, *last_same;
109   byte attr;
110   byte val[1];
111 };
112
113 void obj_dump(struct odes *);
114 struct odes *obj_new(struct mempool *);
115 void obj_free(struct odes *);
116 int obj_read(struct fastbuf *, struct odes *);
117 void obj_write(struct fastbuf *, struct odes *);
118 struct oattr *obj_find_attr(struct odes *, uns);
119 struct oattr *obj_find_attr_last(struct odes *, uns);
120 uns obj_del_attr(struct odes *, struct oattr *);
121 byte *obj_find_aval(struct odes *, uns);
122 struct oattr *obj_set_attr(struct odes *, uns, byte *);
123 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
124 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
125 struct oattr *obj_prepend_attr(struct odes *, uns, byte *);
126 struct oattr *obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v);
127
128 /* Content-Type pattern matching and filters */
129
130 int match_ct_patt(byte *, byte *);
131
132 /* log2.c */
133
134 int log2(u32);
135
136 /* wordsplit.c */
137
138 int wordsplit(byte *, byte **, uns);
139
140 /* pat(i)match.c: Matching of shell patterns */
141
142 int match_pattern(byte *, byte *);
143 int match_pattern_nocase(byte *, byte *);
144
145 /* md5hex.c */
146
147 void md5_to_hex(byte *, byte *);
148 void hex_to_md5(byte *, byte *);
149
150 #define MD5_SIZE 16
151 #define MD5_HEX_SIZE 33
152
153 /* prime.c */
154
155 int isprime(uns);
156 uns nextprime(uns);
157
158 /* timer.c */
159
160 void init_timer(void);
161 uns get_timer(void);
162
163 /* regex.c */
164
165 typedef struct regex regex;
166
167 regex *rx_compile(byte *r, int icase);
168 void rx_free(regex *r);
169 int rx_match(regex *r, byte *s);
170 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
171
172 /* random.c */
173
174 uns random_max(uns);
175
176 /* mmap.c */
177
178 void *mmap_file(byte *name, unsigned *len, int writeable);
179 void munmap_file(void *start, unsigned len);
180
181 /* proctitle.c */
182
183 void setproctitle_init(int argc, char **argv);
184 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
185
186 /* randomkey.c */
187
188 void randomkey(byte *buf, uns size);
189
190 #endif