]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
22169423260926312e91fe588ac85b6e0bc162e0
[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 ABS(x) ((x) < 0 ? -(x) : (x))
34 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
35
36 /* Logging */
37
38 #define L_DEBUG         'D'             /* Debugging messages */
39 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
40 #define L_WARN          'W'
41 #define L_ERROR         'E'
42 #define L_INFO_R        'i'             /* Errors caused by external events */
43 #define L_WARN_R        'w'
44 #define L_ERROR_R       'e'
45 #define L_FATAL         '!'             /* die() */
46
47 void log(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
48 void die(byte *, ...) NONRET;
49 void log_init(byte *);
50 void log_file(byte *);
51 void log_fork(void);
52
53 #ifdef DEBUG
54 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
55 #else
56 #define ASSERT(x) do { } while(0)
57 #endif
58
59 #ifdef LOCAL_DEBUG
60 #define DBG(x,y...) log(L_DEBUG, x,##y)
61 #else
62 #define DBG(x,y...) do { } while(0)
63 #endif
64
65 /* Memory allocation */
66
67 #ifdef DMALLOC
68 /*
69  * The standard dmalloc macros tend to produce lots of namespace
70  * conflicts and we use only xmalloc and xfree, so we can define
71  * the stubs ourselves.
72  */
73 #define DMALLOC_DISABLE
74 #include <dmalloc.h>
75 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
76 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
77 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
78 #else
79 /*
80  * Unfortunately, several libraries we might want to link to define
81  * their own xmalloc and we don't want to interfere with them, hence
82  * the renaming.
83  */
84 #define xmalloc sh_xmalloc
85 void *xmalloc(unsigned);
86 void *xmalloc_zero(unsigned);
87 void *xrealloc(void *, unsigned);
88 #define xfree(x) free(x)
89 #endif
90
91 byte *stralloc(byte *);
92
93 /* Objects */
94
95 struct fastbuf;
96
97 struct odes {                           /* Object description */
98   struct oattr *attrs;
99   struct mempool *pool, *local_pool;
100 };
101
102 struct oattr {                          /* Object attribute */
103   struct oattr *next, *same, *last_same;
104   byte attr;
105   byte val[1];
106 };
107
108 void obj_dump(struct odes *);
109 struct odes *obj_new(struct mempool *);
110 void obj_free(struct odes *);
111 int obj_read(struct fastbuf *, struct odes *);
112 void obj_write(struct fastbuf *, struct odes *);
113 struct oattr *obj_find_attr(struct odes *, uns);
114 struct oattr *obj_find_attr_last(struct odes *, uns);
115 uns obj_del_attr(struct odes *, struct oattr *);
116 byte *obj_find_aval(struct odes *, uns);
117 struct oattr *obj_set_attr(struct odes *, uns, byte *);
118 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
119 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
120
121 /* Content-Type pattern matching and filters */
122
123 int match_ct_patt(byte *, byte *);
124
125 /* log2.c */
126
127 int log2(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 /* proctitle.c */
175
176 void setproctitle_init(int argc, char **argv);
177 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
178
179 #endif