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 */
23 #define NULL ((void *)0)
26 /* Ugly structure handling macros */
28 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
29 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
30 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
42 void open_temp(struct tempfile *, byte *);
43 void delete_temp(struct tempfile *);
48 #define L_DEBUG 'D' /* Debugging messages */
49 #define L_INFO 'I' /* Informational msgs, warnings and errors */
52 #define L_INFO_R 'i' /* Errors caused by external events */
55 #define L_FATAL '!' /* die() */
57 void log(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
58 void die(byte *, ...) NONRET;
59 void log_init(byte *);
60 void log_file(byte *);
64 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
66 #define ASSERT(x) do { } while(0)
70 #define DBG(x,y...) log(L_DEBUG, x,##y)
72 #define DBG(x,y...) do { } while(0)
75 /* Memory allocation */
79 * The standard dmalloc macros tend to produce lots of namespace
80 * conflicts and we use only xmalloc and xfree, so we can define
81 * the stubs ourselves.
83 #define DMALLOC_DISABLE
85 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
86 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
87 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
90 * Unfortunately, several libraries we might want to link to define
91 * their own xmalloc and we don't want to interfere with them, hence
94 #define xmalloc sh_xmalloc
95 void *xmalloc(unsigned);
96 void *xmalloc_zero(unsigned);
97 void *xrealloc(void *, unsigned);
98 #define xfree(x) free(x)
101 byte *stralloc(byte *);
107 struct odes { /* Object description */
109 struct mempool *pool, *local_pool;
112 struct oattr { /* Object attribute */
113 struct oattr *next, *same, *last_same;
118 void obj_dump(struct odes *);
119 struct odes *obj_new(struct mempool *);
120 void obj_free(struct odes *);
121 int obj_read(struct fastbuf *, struct odes *);
122 void obj_write(struct fastbuf *, struct odes *);
123 struct oattr *obj_find_attr(struct odes *, uns);
124 struct oattr *obj_find_attr_last(struct odes *, uns);
125 uns obj_del_attr(struct odes *, struct oattr *);
126 byte *obj_find_aval(struct odes *, uns);
127 struct oattr *obj_set_attr(struct odes *, uns, byte *);
128 struct oattr *obj_set_attr_num(struct odes *, uns, uns);
129 struct oattr *obj_add_attr(struct odes *, struct oattr *, uns, byte *);
131 /* Content-Type pattern matching and filters */
133 int match_ct_patt(byte *, byte *);
141 int wordsplit(byte *, byte **, uns);
143 /* pat(i)match.c: Matching of shell patterns */
145 int match_pattern(byte *, byte *);
146 int match_pattern_nocase(byte *, byte *);
150 void md5_to_hex(byte *, byte *);
151 void hex_to_md5(byte *, byte *);
154 #define MD5_HEX_SIZE 33
163 void init_timer(void);
168 typedef struct regex regex;
170 regex *rx_compile(byte *r);
171 void rx_free(regex *r);
172 int rx_match(regex *r, byte *s);
173 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
181 void *mmap_file(byte *name, unsigned *len);