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