]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
eb079dcd6fe94139b24643c856a1dc7e21256d8e
[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
28 /* Some other macros */
29
30 #define MIN(a,b) (((a)<(b))?(a):(b))
31 #define MAX(a,b) (((a)>(b))?(a):(b))
32 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
33 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
34
35 /* Temporary Files */
36
37 #define TMP_DIR "tmp"
38 #define TMP_DIR_LEN 3
39
40 struct tempfile {
41   int fh;
42   byte name[32];
43 };
44
45 void open_temp(struct tempfile *, byte *);
46 void delete_temp(struct tempfile *);
47 u32 temprand(uns);
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 void log(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
61 void die(byte *, ...) NONRET;
62 void log_init(byte *);
63 void log_file(byte *);
64 void log_fork(void);
65
66 #ifdef DEBUG
67 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
68 #else
69 #define ASSERT(x) do { } while(0)
70 #endif
71
72 #ifdef LOCAL_DEBUG
73 #define DBG(x,y...) log(L_DEBUG, x,##y)
74 #else
75 #define DBG(x,y...) do { } while(0)
76 #endif
77
78 /* Memory allocation */
79
80 #ifdef DMALLOC
81 /*
82  * The standard dmalloc macros tend to produce lots of namespace
83  * conflicts and we use only xmalloc and xfree, so we can define
84  * the stubs ourselves.
85  */
86 #define DMALLOC_DISABLE
87 #include <dmalloc.h>
88 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
89 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
90 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
91 #else
92 /*
93  * Unfortunately, several libraries we might want to link to define
94  * their own xmalloc and we don't want to interfere with them, hence
95  * the renaming.
96  */
97 #define xmalloc sh_xmalloc
98 void *xmalloc(unsigned);
99 void *xmalloc_zero(unsigned);
100 void *xrealloc(void *, unsigned);
101 #define xfree(x) free(x)
102 #endif
103
104 byte *stralloc(byte *);
105
106 /* Objects */
107
108 struct fastbuf;
109
110 struct odes {                           /* Object description */
111   struct oattr *attrs;
112   struct mempool *pool, *local_pool;
113 };
114
115 struct oattr {                          /* Object attribute */
116   struct oattr *next, *same, *last_same;
117   byte attr;
118   byte val[1];
119 };
120
121 void obj_dump(struct odes *);
122 struct odes *obj_new(struct mempool *);
123 void obj_free(struct odes *);
124 int obj_read(struct fastbuf *, struct odes *);
125 void obj_write(struct fastbuf *, struct odes *);
126 struct oattr *obj_find_attr(struct odes *, uns);
127 struct oattr *obj_find_attr_last(struct odes *, uns);
128 uns obj_del_attr(struct odes *, struct oattr *);
129 byte *obj_find_aval(struct odes *, uns);
130 struct oattr *obj_set_attr(struct odes *, uns, byte *);
131 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
132 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
133
134 /* Content-Type pattern matching and filters */
135
136 int match_ct_patt(byte *, byte *);
137
138 /* log2.c */
139
140 int log2(u32);
141
142 /* wordsplit.c */
143
144 int wordsplit(byte *, byte **, uns);
145
146 /* pat(i)match.c: Matching of shell patterns */
147
148 int match_pattern(byte *, byte *);
149 int match_pattern_nocase(byte *, byte *);
150
151 /* md5hex.c */
152
153 void md5_to_hex(byte *, byte *);
154 void hex_to_md5(byte *, byte *);
155
156 #define MD5_SIZE 16
157 #define MD5_HEX_SIZE 33
158
159 /* prime.c */
160
161 int isprime(uns);
162 uns nextprime(uns);
163
164 /* timer.c */
165
166 void init_timer(void);
167 uns get_timer(void);
168
169 /* regex.c */
170
171 typedef struct regex regex;
172
173 regex *rx_compile(byte *r);
174 void rx_free(regex *r);
175 int rx_match(regex *r, byte *s);
176 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
177
178 /* random.c */
179
180 uns random_max(uns);
181
182 /* mmap.c */
183
184 void *mmap_file(byte *name, unsigned *len, int writeable);
185 void munmap_file(void *start, unsigned len);
186
187 #endif