3 ### Basic definitions, used all over
5 ### (c) 2005 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to mLib.
12 ### mLib/Python is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
17 ### mLib/Python is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
22 ### You should have received a copy of the GNU General Public License
23 ### along with mLib/Python; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ###--------------------------------------------------------------------------
29 cdef extern from 'errno.h':
34 cdef extern from 'limits.h':
37 cdef extern from 'math.h':
38 double modf(double x, double *i)
39 cdef extern from 'stddef.h':
41 cdef extern from 'string.h':
42 void memcpy(void *p, void *q, size_t n)
43 char *strerror(int err)
44 size_t strlen(char *p)
46 ###--------------------------------------------------------------------------
49 cdef extern from 'sys/types.h':
51 cdef extern from 'sys/time.h':
56 cdef extern from 'sys/socket.h':
61 int getsockname(int fd, sockaddr *pa, size_t *psz)
62 int getpeername(int fd, sockaddr *pa, size_t *psz)
63 cdef extern from 'arpa/inet.h':
66 int inet_aton(char *rp, in_addr *ia)
67 char *inet_ntoa(in_addr ia)
68 cdef extern from 'netinet/in.h':
77 cdef extern from 'netdb.h':
87 ###--------------------------------------------------------------------------
90 cdef extern from 'Python.h':
92 ctypedef struct PyObject:
94 ctypedef struct PyTypeObject:
97 object PyString_FromStringAndSize(char *p, int len)
98 int PyString_AsStringAndSize(obj, char **p, int *len) except -1
99 int PyObject_AsReadBuffer(obj, void **buf, int *len) except -1
100 int PyObject_TypeCheck(obj, ty)
101 object PyInt_FromLong(long i)
102 object PyLong_FromUnsignedLong(unsigned long i)
103 char *PyString_AS_STRING(string)
104 int _PyString_Resize(PyObject **string, int size) except -1
106 void Py_INCREF(PyObject *obj)
107 void Py_DECREF(PyObject *obj)
109 ###--------------------------------------------------------------------------
110 ### mLib basic stuff.
112 cdef extern from 'mLib/alloc.h':
113 char *xstrdup(char *p)
114 void *xmalloc(size_t sz)
117 cdef extern from 'mLib/bits.h':
120 cdef extern from 'mLib/dstr.h':
121 ctypedef struct dstr:
124 void DCREATE(dstr *d)
125 void dstr_destroy(dstr *d)
127 ###--------------------------------------------------------------------------
130 cdef extern from 'mLib/crc32.h':
131 uint32 c_crc32 "crc32" (uint32 a, void *p, int sz)
133 ###--------------------------------------------------------------------------
134 ### Universal hashing.
136 cdef extern from 'mLib/unihash.h':
137 ctypedef struct unihash_info:
139 void unihash_setkey(unihash_info *i, uint32 k)
140 uint32 UNIHASH_INIT(unihash_info *i)
141 uint32 unihash_hash(unihash_info *i, uint32 a, void *p, int sz)
142 unihash_info unihash_global
144 ###--------------------------------------------------------------------------
147 cdef extern from 'mLib/sym.h':
148 ctypedef struct sym_table:
150 ctypedef struct sym_base:
152 ctypedef struct sym_iter:
154 void sym_create(sym_table *t)
155 void sym_destroy(sym_table *t)
156 void *sym_find(sym_table *t, char *n, long l, int sz, unsigned *f)
157 void sym_remove(sym_table *t, void *b)
158 char *SYM_NAME(void *b)
160 void sym_mkiter(sym_iter *i, sym_table *t)
161 void *sym_next(sym_iter *i)
163 ###--------------------------------------------------------------------------
164 ### String utilities.
166 cdef extern from 'mLib/str.h':
170 char *str_qword(char **pp, unsigned f)
171 size_t str_qsplit(char *p, char **v, size_t c, char **rest, unsigned f)
172 int str_matchx(char *p, char *s, unsigned f)
173 void str_sanitize(char *d, char *p, size_t sz)
175 cdef extern from 'mLib/versioncmp.h':
176 int _versioncmp "versioncmp"(char *va, char *vb)
178 ###--------------------------------------------------------------------------
179 ### Form-urlencoding functions.
181 cdef extern from 'mLib/url.h':
191 void url_initenc(url_ectx *ctx)
192 void url_enc(url_ectx *ctx, dstr *d, char *name, char *value)
193 void url_initdec(url_dctx *ctx, char *p)
194 int url_dec(url_dctx *ctx, dstr *n, dstr *v)
196 ###--------------------------------------------------------------------------
201 ## Partly written in `real' C.
202 cdef extern from 'atom.h':
203 ctypedef struct atom:
205 ctypedef struct atom_iter:
207 ctypedef struct atom_table:
209 atom_table *ATOM_GLOBAL
210 void atom_mkiter(atom_iter *i, atom_table *t)
211 atom *atom_next(atom_iter *)
216 PyTypeObject atom_pytype
218 ## Association tables.
219 cdef extern from 'mLib/assoc.h':
220 ctypedef struct assoc_table:
222 ctypedef struct assoc_base:
224 ctypedef struct assoc_iter:
226 void assoc_create(assoc_table *t)
227 void assoc_destroy(assoc_table *t)
228 void *assoc_find(assoc_table *t, atom *a, int sz, unsigned *f)
229 void assoc_remove(assoc_table *t, void *b)
230 atom *ASSOC_ATOM(void *b)
231 void assoc_mkiter(assoc_iter *i, assoc_table *t)
232 void *assoc_next(assoc_iter *i)
234 ###--------------------------------------------------------------------------
235 ### Double-ended arrays.
237 cdef extern from 'array.h':
239 PyTypeObject da_pytype
240 PyTypeObject daiter_pytype
242 ###--------------------------------------------------------------------------
245 cdef extern from 'mLib/lbuf.h':
252 _LBUF_CRLF "LBUF_CRLF"
253 _LBUF_STRICTCRLF "LBUF_STRICTCRLF"
254 void lbuf_flush(lbuf *b, char *p, size_t len)
255 void lbuf_close(lbuf *b)
256 size_t lbuf_free(lbuf *b, char **p)
257 void lbuf_setsize(lbuf *b, size_t sz)
258 void lbuf_enable(lbuf *b)
259 void lbuf_disable(lbuf *b)
260 void lbuf_init(lbuf *b,
261 void (*func)(char *s, size_t len, void *arg), void *arg)
262 void lbuf_destroy(lbuf *b)
264 ###--------------------------------------------------------------------------
267 cdef extern from 'mLib/pkbuf.h':
268 ctypedef struct pkbuf:
273 void pkbuf_flush(pkbuf *pk, unsigned char *p, size_t len)
274 void pkbuf_close(pkbuf *pk)
275 size_t pkbuf_free(pkbuf *pk, unsigned char **p)
276 void pkbuf_want(pkbuf *pk, size_t sz)
277 void pkbuf_init(pkbuf *b,
278 void (*func)(unsigned char *s, size_t len,
279 pkbuf *pk, size_t *keep, void *arg),
281 void pkbuf_destroy(pkbuf *b)
283 ###--------------------------------------------------------------------------
287 cdef extern from 'mLib/sel.h':
288 ctypedef struct sel_state:
290 ctypedef struct sel_file:
292 ctypedef struct sel_timer:
296 _SEL_WRITE "SEL_WRITE"
298 void sel_init(sel_state *s)
299 void sel_initfile(sel_state *s, sel_file *f, int fd, unsigned mode,
300 void (*func)(int fd, unsigned mode, void *arg),
302 void sel_force(sel_file *f)
303 void sel_addfile(sel_file *f)
304 void sel_rmfile(sel_file *f)
305 void sel_addtimer(sel_state *s, sel_timer *t, timeval *tv,
306 void (*func)(timeval *tv, void *arg),
308 void sel_rmtimer(sel_timer *t)
309 int sel_select(sel_state *s) except *
311 ### Non-blocking connection.
312 cdef extern from 'mLib/conn.h':
313 ctypedef struct conn:
315 int conn_fd(conn *c, sel_state *s, int fd,
316 void (*func)(int fd, void *arg), void *arg)
317 void conn_kill(conn *c)
319 ## Background name resolver.
320 cdef extern from 'mLib/bres.h':
321 ctypedef struct bres_client:
323 void bres_byname(bres_client *r, char *name,
324 void (*func)(hostent *h, void *arg), void *arg)
325 void bres_byaddr(bres_client *r, in_addr addr,
326 void (*func)(hostent *h, void *arg), void *arg)
327 void bres_abort(bres_client *r)
328 void bres_exec(char *null)
329 void bres_init(sel_state *s)
331 ## In-band signal handling.
332 cdef extern from 'mLib/sig.h':
335 void sig_add(sig *s, int n, void (*func)(int n, void *arg), void *arg)
336 void sig_remove(sig *s)
337 void sig_init(sel_state *s)
340 cdef extern from 'mLib/selbuf.h':
341 ctypedef struct selbuf:
344 void selbuf_enable(selbuf *b)
345 void selbuf_disable(selbuf *b)
346 void selbuf_setsize(selbuf *b, size_t sz)
347 void selbuf_init(selbuf *b, sel_state *s, int fd,
348 void (*func)(char *s, size_t len, void *arg), void *arg)
349 void selbuf_destroy(selbuf *b)
352 cdef extern from 'mLib/selpk.h':
353 ctypedef struct selpk:
356 void selpk_enable(selpk *b)
357 void selpk_disable(selpk *b)
358 void selpk_want(selpk *b, size_t sz)
359 void selpk_init(selpk *b, sel_state *s, int fd,
360 void (*func)(unsigned char *p, size_t n,
361 pkbuf *pk, size_t *keep, void *arg),
363 void selpk_destroy(selpk *b)
366 cdef extern from 'mLib/ident.h':
367 ctypedef struct ident_request:
379 ctypedef struct ident_reply:
384 void ident(ident_request *rq, sel_state *s,
385 sockaddr_in *local, sockaddr_in *remote,
386 void (*func)(ident_reply *r, void *arg),
388 void ident_abort(ident_request *rq)
390 ###--------------------------------------------------------------------------
393 cdef extern from 'mLib/quis.h':
394 void _ego "ego"(char *prog)
397 cdef extern from 'mLib/report.h':
398 void _moan "moan"(char *f, char *msg)
400 ###--------------------------------------------------------------------------
403 cdef extern from 'mLib/fwatch.h':
404 ctypedef struct fwatch:
406 void fwatch_init(fwatch *f, char *name)
407 void fwatch_initfd(fwatch *f, int fd)
408 int fwatch_update(fwatch *f, char *name)
409 int fwatch_updatefd(fwatch *f, int fd)
411 ###--------------------------------------------------------------------------
412 ### File descriptor hacking.
414 cdef extern from 'mLib/fdflags.h':
415 int _fdflags "fdflags"(int fd,
416 unsigned fbic, unsigned fxor,
417 unsigned fdbic, unsigned fdxor)
419 cdef extern from 'mLib/fdpass.h':
420 int fdpass_send(int sock, int fd, void *p, size_t sz)
421 int fdpass_recv(int sock, int *fd, void *p, size_t sz)
423 cdef extern from 'mLib/mdup.h':
424 ctypedef struct mdup_fd:
427 int _mdup "mdup"(mdup_fd *v, size_t n)
429 ###--------------------------------------------------------------------------
430 ### Daemon utilities.
432 cdef extern from 'mLib/daemonize.h':
433 int _daemonize "daemonize"()
434 void _detachtty "detachtty"()
436 ###--------------------------------------------------------------------------
437 ### Internal utilities.
439 cdef extern from 'grim.h':
442 ###----- That's all, folks --------------------------------------------------