2 * Sherlock Library -- Miscellaneous Functions
4 * (c) 1997--2001 Martin Mares <mj@ucw.cz>
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.
13 #ifndef _SHERLOCK_LIB_H
14 #define _SHERLOCK_LIB_H
16 #include "lib/config.h"
18 /* Tell libc we're going to use all extensions available */
22 /* Ugly structure handling macros */
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))
29 /* Some other macros */
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)))
39 #define L_DEBUG 'D' /* Debugging messages */
40 #define L_INFO 'I' /* Informational msgs, warnings and errors */
43 #define L_INFO_R 'i' /* Errors caused by external events */
46 #define L_FATAL '!' /* die() */
48 void log(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
49 void die(byte *, ...) NONRET;
50 void log_init(byte *);
51 void log_file(byte *);
55 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
57 #define ASSERT(x) do { } while(0)
61 #define DBG(x,y...) log(L_DEBUG, x,##y)
63 #define DBG(x,y...) do { } while(0)
66 /* Memory allocation */
70 * The standard dmalloc macros tend to produce lots of namespace
71 * conflicts and we use only xmalloc and xfree, so we can define
72 * the stubs ourselves.
74 #define DMALLOC_DISABLE
76 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
77 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
78 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
81 * Unfortunately, several libraries we might want to link to define
82 * their own xmalloc and we don't want to interfere with them, hence
85 #define xmalloc sh_xmalloc
86 void *xmalloc(unsigned);
87 void *xmalloc_zero(unsigned);
88 void *xrealloc(void *, unsigned);
89 #define xfree(x) free(x)
92 byte *stralloc(byte *);
98 struct odes { /* Object description */
100 struct mempool *pool, *local_pool;
103 struct oattr { /* Object attribute */
104 struct oattr *next, *same, *last_same;
109 void obj_dump(struct odes *);
110 struct odes *obj_new(struct mempool *);
111 void obj_free(struct odes *);
112 int obj_read(struct fastbuf *, struct odes *);
113 void obj_write(struct fastbuf *, struct odes *);
114 struct oattr *obj_find_attr(struct odes *, uns);
115 struct oattr *obj_find_attr_last(struct odes *, uns);
116 uns obj_del_attr(struct odes *, struct oattr *);
117 byte *obj_find_aval(struct odes *, uns);
118 struct oattr *obj_set_attr(struct odes *, uns, byte *);
119 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
120 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
122 /* Content-Type pattern matching and filters */
124 int match_ct_patt(byte *, byte *);
132 int wordsplit(byte *, byte **, uns);
134 /* pat(i)match.c: Matching of shell patterns */
136 int match_pattern(byte *, byte *);
137 int match_pattern_nocase(byte *, byte *);
141 void md5_to_hex(byte *, byte *);
142 void hex_to_md5(byte *, byte *);
145 #define MD5_HEX_SIZE 33
154 void init_timer(void);
159 typedef struct regex regex;
161 regex *rx_compile(byte *r, int icase);
162 void rx_free(regex *r);
163 int rx_match(regex *r, byte *s);
164 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
172 void *mmap_file(byte *name, unsigned *len, int writeable);
173 void munmap_file(void *start, unsigned len);
177 void setproctitle_init(int argc, char **argv);
178 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
182 void randomkey(byte *buf, uns size);