]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
Fix escaping of "+" characters in outgoing parameters. (BTW: when Galeon
[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 log_init(byte *);
54 void log_file(byte *);
55 void log_fork(void);
56
57 #ifdef DEBUG
58 void assert_failed(char *assertion, char *file, int line) NONRET;
59 #define ASSERT(x) do { if (!(x)) assert_failed(#x, __FILE__, __LINE__); } while(0)
60 #else
61 void assert_failed(void) NONRET;
62 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed(); } while(0)
63 #endif
64
65 #ifdef LOCAL_DEBUG
66 #define DBG(x,y...) log(L_DEBUG, x,##y)
67 #else
68 #define DBG(x,y...) do { } while(0)
69 #endif
70
71 /* Memory allocation */
72
73 #ifdef DMALLOC
74 /*
75  * The standard dmalloc macros tend to produce lots of namespace
76  * conflicts and we use only xmalloc and xfree, so we can define
77  * the stubs ourselves.
78  */
79 #define DMALLOC_DISABLE
80 #include <dmalloc.h>
81 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
82 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
83 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
84 #else
85 /*
86  * Unfortunately, several libraries we might want to link to define
87  * their own xmalloc and we don't want to interfere with them, hence
88  * the renaming.
89  */
90 #define xmalloc sh_xmalloc
91 void *xmalloc(unsigned);
92 void *xrealloc(void *, unsigned);
93 #define xfree(x) free(x)
94 #endif
95
96 void *xmalloc_zero(unsigned);
97 byte *stralloc(byte *);
98
99 /* Objects */
100
101 struct fastbuf;
102
103 struct odes {                           /* Object description */
104   struct oattr *attrs;
105   struct mempool *pool, *local_pool;
106 };
107
108 struct oattr {                          /* Object attribute */
109   struct oattr *next, *same, *last_same;
110   byte attr;
111   byte val[1];
112 };
113
114 void obj_dump(struct odes *);
115 struct odes *obj_new(struct mempool *);
116 void obj_free(struct odes *);
117 int obj_read(struct fastbuf *, struct odes *);
118 void obj_write(struct fastbuf *, struct odes *);
119 struct oattr *obj_find_attr(struct odes *, uns);
120 struct oattr *obj_find_attr_last(struct odes *, uns);
121 uns obj_del_attr(struct odes *, struct oattr *);
122 byte *obj_find_aval(struct odes *, uns);
123 struct oattr *obj_set_attr(struct odes *, uns, byte *);
124 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
125 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
126 struct oattr *obj_prepend_attr(struct odes *, uns, byte *);
127 struct oattr *obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v);
128
129 /* Content-Type pattern matching and filters */
130
131 int match_ct_patt(byte *, byte *);
132
133 /* log2.c */
134
135 int log2(u32);
136
137 /* wordsplit.c */
138
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 /* 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 #endif