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 ###----- C library ----------------------------------------------------------
28 cdef extern from 'errno.h':
33 cdef extern from 'limits.h':
36 cdef extern from 'math.h':
37 double modf(double x, double *i)
38 cdef extern from 'stddef.h':
40 cdef extern from 'string.h':
41 void memcpy(void *p, void *q, size_t n)
42 char *strerror(int err)
43 size_t strlen(char *p)
45 ###----- Unix interface -----------------------------------------------------
47 cdef extern from 'sys/types.h':
49 cdef extern from 'sys/time.h':
54 cdef extern from 'sys/socket.h':
59 int getsockname(int fd, sockaddr *pa, size_t *psz)
60 int getpeername(int fd, sockaddr *pa, size_t *psz)
61 cdef extern from 'arpa/inet.h':
64 int inet_aton(char *rp, in_addr *ia)
65 char *inet_ntoa(in_addr ia)
66 cdef extern from 'netinet/in.h':
75 cdef extern from 'netdb.h':
85 ###----- Python -------------------------------------------------------------
87 cdef extern from 'Python.h':
89 ctypedef struct PyObject:
91 ctypedef struct PyTypeObject:
94 object PyString_FromStringAndSize(char *p, int len)
95 int PyString_AsStringAndSize(obj, char **p, int *len) except -1
96 int PyObject_AsReadBuffer(obj, void **buf, int *len) except -1
97 int PyObject_TypeCheck(obj, ty)
98 object PyInt_FromLong(long i)
99 object PyLong_FromUnsignedLong(unsigned long i)
100 char *PyString_AS_STRING(string)
101 int _PyString_Resize(PyObject **string, int size) except -1
103 void Py_INCREF(PyObject *obj)
104 void Py_DECREF(PyObject *obj)
106 ###----- mLib basic stuff ---------------------------------------------------
108 cdef extern from 'mLib/alloc.h':
109 char *xstrdup(char *p)
110 void *xmalloc(size_t sz)
113 cdef extern from 'mLib/bits.h':
116 cdef extern from 'mLib/dstr.h':
117 ctypedef struct dstr:
120 void DCREATE(dstr *d)
121 void dstr_destroy(dstr *d)
123 ###----- CRC32 --------------------------------------------------------------
125 cdef extern from 'mLib/crc32.h':
126 uint32 c_crc32 "crc32" (uint32 a, void *p, int sz)
128 ###----- Universal hashing --------------------------------------------------
130 cdef extern from 'mLib/unihash.h':
131 ctypedef struct unihash_info:
133 void unihash_setkey(unihash_info *i, uint32 k)
134 uint32 UNIHASH_INIT(unihash_info *i)
135 uint32 unihash_hash(unihash_info *i, uint32 a, void *p, int sz)
136 unihash_info unihash_global
138 ###----- Symbol tables ------------------------------------------------------
140 cdef extern from 'mLib/sym.h':
141 ctypedef struct sym_table:
143 ctypedef struct sym_base:
145 ctypedef struct sym_iter:
147 void sym_create(sym_table *t)
148 void sym_destroy(sym_table *t)
149 void *sym_find(sym_table *t, char *n, long l, int sz, unsigned *f)
150 void sym_remove(sym_table *t, void *b)
151 char *SYM_NAME(void *b)
153 void sym_mkiter(sym_iter *i, sym_table *t)
154 void *sym_next(sym_iter *i)
156 ###----- String utilities ---------------------------------------------------
158 cdef extern from 'mLib/str.h':
162 char *str_qword(char **pp, unsigned f)
163 size_t str_qsplit(char *p, char **v, size_t c, char **rest, unsigned f)
164 int str_matchx(char *p, char *s, unsigned f)
165 void str_sanitize(char *d, char *p, size_t sz)
167 cdef extern from 'mLib/versioncmp.h':
168 int _versioncmp "versioncmp"(char *va, char *vb)
170 ###----- Form-urlencoding functions -----------------------------------------
172 cdef extern from 'mLib/url.h':
182 void url_initenc(url_ectx *ctx)
183 void url_enc(url_ectx *ctx, dstr *d, char *name, char *value)
184 void url_initdec(url_dctx *ctx, char *p)
185 int url_dec(url_dctx *ctx, dstr *n, dstr *v)
187 ###----- Atom stuff ---------------------------------------------------------
191 ## Partly written in `real' C.
192 cdef extern from 'atom.h':
193 ctypedef struct atom:
195 ctypedef struct atom_iter:
197 ctypedef struct atom_table:
199 atom_table *ATOM_GLOBAL
200 void atom_mkiter(atom_iter *i, atom_table *t)
201 atom *atom_next(atom_iter *)
206 PyTypeObject atom_pytype
208 ## Association tables.
209 cdef extern from 'mLib/assoc.h':
210 ctypedef struct assoc_table:
212 ctypedef struct assoc_base:
214 ctypedef struct assoc_iter:
216 void assoc_create(assoc_table *t)
217 void assoc_destroy(assoc_table *t)
218 void *assoc_find(assoc_table *t, atom *a, int sz, unsigned *f)
219 void assoc_remove(assoc_table *t, void *b)
220 atom *ASSOC_ATOM(void *b)
221 void assoc_mkiter(assoc_iter *i, assoc_table *t)
222 void *assoc_next(assoc_iter *i)
224 ###----- Double-ended arrays ------------------------------------------------
226 cdef extern from 'array.h':
228 PyTypeObject da_pytype
229 PyTypeObject daiter_pytype
231 ###----- Line buffer --------------------------------------------------------
233 cdef extern from 'mLib/lbuf.h':
240 _LBUF_CRLF "LBUF_CRLF"
241 _LBUF_STRICTCRLF "LBUF_STRICTCRLF"
242 void lbuf_flush(lbuf *b, char *p, size_t len)
243 void lbuf_close(lbuf *b)
244 size_t lbuf_free(lbuf *b, char **p)
245 void lbuf_setsize(lbuf *b, size_t sz)
246 void lbuf_enable(lbuf *b)
247 void lbuf_disable(lbuf *b)
248 void lbuf_init(lbuf *b,
249 void (*func)(char *s, size_t len, void *arg), void *arg)
250 void lbuf_destroy(lbuf *b)
252 ###----- Packet buffer ------------------------------------------------------
254 cdef extern from 'mLib/pkbuf.h':
255 ctypedef struct pkbuf:
260 void pkbuf_flush(pkbuf *pk, unsigned char *p, size_t len)
261 void pkbuf_close(pkbuf *pk)
262 size_t pkbuf_free(pkbuf *pk, unsigned char **p)
263 void pkbuf_want(pkbuf *pk, size_t sz)
264 void pkbuf_init(pkbuf *b,
265 void (*func)(unsigned char *s, size_t len,
266 pkbuf *pk, size_t *keep, void *arg),
268 void pkbuf_destroy(pkbuf *b)
270 ###----- Select stuff -------------------------------------------------------
273 cdef extern from 'mLib/sel.h':
274 ctypedef struct sel_state:
276 ctypedef struct sel_file:
278 ctypedef struct sel_timer:
282 _SEL_WRITE "SEL_WRITE"
284 void sel_init(sel_state *s)
285 void sel_initfile(sel_state *s, sel_file *f, int fd, unsigned mode,
286 void (*func)(int fd, unsigned mode, void *arg),
288 void sel_force(sel_file *f)
289 void sel_addfile(sel_file *f)
290 void sel_rmfile(sel_file *f)
291 void sel_addtimer(sel_state *s, sel_timer *t, timeval *tv,
292 void (*func)(timeval *tv, void *arg),
294 void sel_rmtimer(sel_timer *t)
295 int sel_select(sel_state *s) except *
297 ### Non-blocking connection.
298 cdef extern from 'mLib/conn.h':
299 ctypedef struct conn:
301 int conn_fd(conn *c, sel_state *s, int fd,
302 void (*func)(int fd, void *arg), void *arg)
303 void conn_kill(conn *c)
305 ## Background name resolver.
306 cdef extern from 'mLib/bres.h':
307 ctypedef struct bres_client:
309 void bres_byname(bres_client *r, char *name,
310 void (*func)(hostent *h, void *arg), void *arg)
311 void bres_byaddr(bres_client *r, in_addr addr,
312 void (*func)(hostent *h, void *arg), void *arg)
313 void bres_abort(bres_client *r)
314 void bres_exec(char *null)
315 void bres_init(sel_state *s)
317 ## In-band signal handling.
318 cdef extern from 'mLib/sig.h':
321 void sig_add(sig *s, int n, void (*func)(int n, void *arg), void *arg)
322 void sig_remove(sig *s)
323 void sig_init(sel_state *s)
326 cdef extern from 'mLib/selbuf.h':
327 ctypedef struct selbuf:
330 void selbuf_enable(selbuf *b)
331 void selbuf_disable(selbuf *b)
332 void selbuf_setsize(selbuf *b, size_t sz)
333 void selbuf_init(selbuf *b, sel_state *s, int fd,
334 void (*func)(char *s, size_t len, void *arg), void *arg)
335 void selbuf_destroy(selbuf *b)
338 cdef extern from 'mLib/selpk.h':
339 ctypedef struct selpk:
342 void selpk_enable(selpk *b)
343 void selpk_disable(selpk *b)
344 void selpk_want(selpk *b, size_t sz)
345 void selpk_init(selpk *b, sel_state *s, int fd,
346 void (*func)(unsigned char *p, size_t n,
347 pkbuf *pk, size_t *keep, void *arg),
349 void selpk_destroy(selpk *b)
352 cdef extern from 'mLib/ident.h':
353 ctypedef struct ident_request:
365 ctypedef struct ident_reply:
370 void ident(ident_request *rq, sel_state *s,
371 sockaddr_in *local, sockaddr_in *remote,
372 void (*func)(ident_reply *r, void *arg),
374 void ident_abort(ident_request *rq)
376 ###----- Error reporting ----------------------------------------------------
378 cdef extern from 'mLib/quis.h':
379 void _ego "ego"(char *prog)
382 cdef extern from 'mLib/report.h':
383 void _moan "moan"(char *f, char *msg)
385 ###----- File comparison ----------------------------------------------------
387 cdef extern from 'mLib/fwatch.h':
388 ctypedef struct fwatch:
390 void fwatch_init(fwatch *f, char *name)
391 void fwatch_initfd(fwatch *f, int fd)
392 int fwatch_update(fwatch *f, char *name)
393 int fwatch_updatefd(fwatch *f, int fd)
395 ###----- File descriptor hacking --------------------------------------------
397 cdef extern from 'mLib/fdflags.h':
398 int _fdflags "fdflags"(int fd,
399 unsigned fbic, unsigned fxor,
400 unsigned fdbic, unsigned fdxor)
402 cdef extern from 'mLib/fdpass.h':
403 int fdpass_send(int sock, int fd, void *p, size_t sz)
404 int fdpass_recv(int sock, int *fd, void *p, size_t sz)
406 cdef extern from 'mLib/mdup.h':
407 ctypedef struct mdup_fd:
410 int _mdup "mdup"(mdup_fd *v, size_t n)
412 ###----- Daemon utilities ---------------------------------------------------
414 cdef extern from 'mLib/daemonize.h':
415 int _daemonize "daemonize"()
416 void _detachtty "detachtty"()
418 ###----- Internal utilities -------------------------------------------------
420 cdef extern from 'grim.h':
423 ###----- That's all, folks --------------------------------------------------