@@@ cython and python 3 wip
[mLib-python] / defs.pxi
CommitLineData
5b1830f3
MW
1### -*-pyrex-*-
2###
3### Basic definitions, used all over
4###
5### (c) 2005 Straylight/Edgeware
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the Python interface to mLib.
11###
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.
16###
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.
21###
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.
25
976d8e49
MW
26###--------------------------------------------------------------------------
27### C library.
579d0169 28
29cdef extern from 'errno.h':
30 int errno
31 enum:
32 EINTR
33 EAGAIN
34cdef extern from 'limits.h':
35 enum:
36 LONG_MAX
37cdef extern from 'math.h':
38 double modf(double x, double *i)
39cdef extern from 'stddef.h':
40 ctypedef int size_t
41cdef extern from 'string.h':
42 void memcpy(void *p, void *q, size_t n)
43 char *strerror(int err)
6a6bd8fa 44 size_t strlen(char *p)
579d0169 45
976d8e49
MW
46###--------------------------------------------------------------------------
47### Unix interface.
579d0169 48
49cdef extern from 'sys/types.h':
50 pass
51cdef extern from 'sys/time.h':
52 struct timeval:
53 int tv_sec
54 int tv_usec
55
56cdef extern from 'sys/socket.h':
78911cdb 57 ctypedef int socklen_t
579d0169 58 struct sockaddr:
59 int sa_family
60 enum:
61 AF_INET
62 int getsockname(int fd, sockaddr *pa, size_t *psz)
63 int getpeername(int fd, sockaddr *pa, size_t *psz)
64cdef extern from 'arpa/inet.h':
65 struct in_addr:
66 int s_addr
67 int inet_aton(char *rp, in_addr *ia)
68 char *inet_ntoa(in_addr ia)
69cdef extern from 'netinet/in.h':
70 struct sockaddr_in:
71 int sin_family
72 in_addr sin_addr
73 int sin_port
74 int htons(int x)
75 int htonl(int x)
76 int ntohs(int x)
77 int ntohl(int x)
78cdef extern from 'netdb.h':
79 struct hostent:
80 char *h_name
81 char **h_aliases
82 int h_addrtype
83 int h_length
84 char **h_addr_list
85 char *h_addr
86 int h_errno
87
976d8e49
MW
88###--------------------------------------------------------------------------
89### Python.
579d0169 90
91cdef extern from 'Python.h':
92
ae68e889
MW
93 ctypedef struct PyObject:
94 pass
95 ctypedef struct PyTypeObject:
96 pass
97
78911cdb 98 int PyObject_AsReadBuffer(obj, void **buf, Py_ssize_t *len) except -1
579d0169 99 object PyInt_FromLong(long i)
100 object PyLong_FromUnsignedLong(unsigned long i)
c316167f 101 void PyErr_Clear()
579d0169 102
579d0169 103 void Py_INCREF(PyObject *obj)
104 void Py_DECREF(PyObject *obj)
105
efb0bf0e
MW
106 PyTypeObject *Py_TYPE(PyObject *obj)
107 Py_ssize_t Py_SIZE(PyObject *obj)
108 Py_ssize_t Py_REFCNT(PyObject *obj)
109
976d8e49
MW
110###--------------------------------------------------------------------------
111### mLib basic stuff.
579d0169 112
113cdef extern from 'mLib/alloc.h':
114 char *xstrdup(char *p)
115 void *xmalloc(size_t sz)
116 void xfree(void *p)
117
118cdef extern from 'mLib/bits.h':
119 ctypedef int uint32
120
121cdef extern from 'mLib/dstr.h':
122 ctypedef struct dstr:
123 char *buf
124 int len
125 void DCREATE(dstr *d)
126 void dstr_destroy(dstr *d)
127
976d8e49
MW
128###--------------------------------------------------------------------------
129### CRC32.
579d0169 130
131cdef extern from 'mLib/crc32.h':
efb0bf0e 132 uint32 _crc32 "crc32" (uint32 a, void *p, int sz)
579d0169 133
976d8e49
MW
134###--------------------------------------------------------------------------
135### Universal hashing.
579d0169 136
137cdef extern from 'mLib/unihash.h':
138 ctypedef struct unihash_info:
139 pass
140 void unihash_setkey(unihash_info *i, uint32 k)
141 uint32 UNIHASH_INIT(unihash_info *i)
142 uint32 unihash_hash(unihash_info *i, uint32 a, void *p, int sz)
143 unihash_info unihash_global
144
976d8e49
MW
145###--------------------------------------------------------------------------
146### Symbol tables.
579d0169 147
148cdef extern from 'mLib/sym.h':
149 ctypedef struct sym_table:
150 pass
151 ctypedef struct sym_base:
152 pass
153 ctypedef struct sym_iter:
154 pass
155 void sym_create(sym_table *t)
156 void sym_destroy(sym_table *t)
157 void *sym_find(sym_table *t, char *n, long l, int sz, unsigned *f)
158 void sym_remove(sym_table *t, void *b)
159 char *SYM_NAME(void *b)
160 int SYM_LEN(void *b)
161 void sym_mkiter(sym_iter *i, sym_table *t)
162 void *sym_next(sym_iter *i)
163
976d8e49
MW
164###--------------------------------------------------------------------------
165### String utilities.
6a6bd8fa
MW
166
167cdef extern from 'mLib/str.h':
168 enum:
169 STRF_QUOTE
95920c4f 170 STRF_PREFIX
6a6bd8fa
MW
171 char *str_qword(char **pp, unsigned f)
172 size_t str_qsplit(char *p, char **v, size_t c, char **rest, unsigned f)
965caf5f 173 bint str_matchx(char *p, char *s, unsigned f)
6a6bd8fa
MW
174 void str_sanitize(char *d, char *p, size_t sz)
175
ae68e889 176cdef extern from 'mLib/versioncmp.h':
efb0bf0e 177 int _versioncmp "versioncmp" (char *va, char *vb)
ae68e889 178
976d8e49 179###--------------------------------------------------------------------------
965caf5f
MW
180### Binary encoding functions.
181
182cdef extern from 'mLib/codec.h':
183 ctypedef struct codec
184 ctypedef struct codec_class:
185 char *name
186 codec *(*encoder)(unsigned f, char *ind, unsigned max)
187 codec *(*decoder)(unsigned f)
188 ctypedef struct codec_ops:
189 codec_class *c
190 int (*code)(codec *c, const void *p, size_t, dstr *d)
191 void (*destroy)(codec *c)
192 ctypedef struct codec:
193 codec_ops *ops
194 enum:
195 _CDCF_LOWERC "CDCF_LOWERC"
196 _CDCF_IGNCASE "CDCF_IGNCASE"
197 _CDCF_NOEQPAD "CDCF_NOEQPAD"
198 _CDCF_IGNEQPAD "CDCF_IGNEQPAD"
199 _CDCF_IGNEQMID "CDCF_IGNEQMID"
200 _CDCF_IGNZPAD "CDCF_IGNZPAD"
201 _CDCF_IGNNEWL "CDCF_IGNNEWL"
202 _CDCF_IGNSPC "CDCF_IGNSPC"
203 _CDCF_IGNINVCH "CDCF_IGNINVCH"
204 _CDCF_IGNJUNK "CDCF_IGNJUNK"
205 enum:
206 _CDCERR_OK "CDCERR_OK"
207 _CDCERR_INVCH "CDCERR_INVCH"
208 _CDCERR_INVEQPAD "CDCERR_INVEQPAD"
209 _CDCERR_INVZPAD "CDCERR_INVZPAD"
210 char *_codec_strerror "codec_strerror" (int err)
211
212###--------------------------------------------------------------------------
976d8e49 213### Form-urlencoding functions.
5ce5170c
MW
214
215cdef extern from 'mLib/url.h':
216 struct url_ectx:
217 unsigned f
218 struct url_dctx:
219 char *p
220 unsigned f
221 enum:
222 URLF_STRICT
223 URLF_LAX
224 URLF_SEMI
225 void url_initenc(url_ectx *ctx)
226 void url_enc(url_ectx *ctx, dstr *d, char *name, char *value)
227 void url_initdec(url_dctx *ctx, char *p)
228 int url_dec(url_dctx *ctx, dstr *n, dstr *v)
229
976d8e49
MW
230###--------------------------------------------------------------------------
231### Atom stuff.
579d0169 232
5b1830f3
MW
233## Atoms.
234##
235## Partly written in `real' C.
579d0169 236cdef extern from 'atom.h':
237 ctypedef struct atom:
238 pass
239 ctypedef struct atom_iter:
240 pass
241 ctypedef struct atom_table:
242 pass
243 atom_table *ATOM_GLOBAL
244 void atom_mkiter(atom_iter *i, atom_table *t)
245 atom *atom_next(atom_iter *)
246 void atom_pysetup()
efb0bf0e
MW
247 object atom_pywrap(atom *a)
248 object atom_pyintern(object obj)
249 atom *ATOM_A(object obj)
579d0169 250 PyTypeObject atom_pytype
251
5b1830f3 252## Association tables.
579d0169 253cdef extern from 'mLib/assoc.h':
254 ctypedef struct assoc_table:
255 pass
256 ctypedef struct assoc_base:
257 pass
258 ctypedef struct assoc_iter:
259 pass
260 void assoc_create(assoc_table *t)
261 void assoc_destroy(assoc_table *t)
262 void *assoc_find(assoc_table *t, atom *a, int sz, unsigned *f)
263 void assoc_remove(assoc_table *t, void *b)
264 atom *ASSOC_ATOM(void *b)
265 void assoc_mkiter(assoc_iter *i, assoc_table *t)
266 void *assoc_next(assoc_iter *i)
267
976d8e49
MW
268###--------------------------------------------------------------------------
269### Double-ended arrays.
579d0169 270
271cdef extern from 'array.h':
272 void da_pysetup()
273 PyTypeObject da_pytype
274 PyTypeObject daiter_pytype
275
976d8e49
MW
276###--------------------------------------------------------------------------
277### Line buffer.
579d0169 278
279cdef extern from 'mLib/lbuf.h':
280 cdef struct lbuf:
281 int f
282 int delim
283 size_t sz
284 enum:
285 LBUF_ENABLE
286 _LBUF_CRLF "LBUF_CRLF"
287 _LBUF_STRICTCRLF "LBUF_STRICTCRLF"
288 void lbuf_flush(lbuf *b, char *p, size_t len)
289 void lbuf_close(lbuf *b)
290 size_t lbuf_free(lbuf *b, char **p)
291 void lbuf_setsize(lbuf *b, size_t sz)
292 void lbuf_enable(lbuf *b)
293 void lbuf_disable(lbuf *b)
294 void lbuf_init(lbuf *b,
5b1830f3 295 void (*func)(char *s, size_t len, void *arg), void *arg)
579d0169 296 void lbuf_destroy(lbuf *b)
297
976d8e49
MW
298###--------------------------------------------------------------------------
299### Packet buffer.
579d0169 300
301cdef extern from 'mLib/pkbuf.h':
302 ctypedef struct pkbuf:
303 int f
304 int want
305 enum:
306 PKBUF_ENABLE
307 void pkbuf_flush(pkbuf *pk, unsigned char *p, size_t len)
308 void pkbuf_close(pkbuf *pk)
309 size_t pkbuf_free(pkbuf *pk, unsigned char **p)
310 void pkbuf_want(pkbuf *pk, size_t sz)
311 void pkbuf_init(pkbuf *b,
5b1830f3
MW
312 void (*func)(unsigned char *s, size_t len,
313 pkbuf *pk, size_t *keep, void *arg),
314 void *arg)
579d0169 315 void pkbuf_destroy(pkbuf *b)
316
976d8e49
MW
317###--------------------------------------------------------------------------
318### Select stuff.
579d0169 319
5b1830f3 320## Basics.
579d0169 321cdef extern from 'mLib/sel.h':
322 ctypedef struct sel_state:
323 pass
324 ctypedef struct sel_file:
325 int fd
326 ctypedef struct sel_timer:
327 pass
328 enum:
329 _SEL_READ "SEL_READ"
330 _SEL_WRITE "SEL_WRITE"
331 _SEL_EXC "SEL_EXC"
332 void sel_init(sel_state *s)
333 void sel_initfile(sel_state *s, sel_file *f, int fd, unsigned mode,
5b1830f3
MW
334 void (*func)(int fd, unsigned mode, void *arg),
335 void *arg)
579d0169 336 void sel_force(sel_file *f)
337 void sel_addfile(sel_file *f)
338 void sel_rmfile(sel_file *f)
339 void sel_addtimer(sel_state *s, sel_timer *t, timeval *tv,
5b1830f3
MW
340 void (*func)(timeval *tv, void *arg),
341 void *arg)
579d0169 342 void sel_rmtimer(sel_timer *t)
343 int sel_select(sel_state *s) except *
344
5b1830f3 345### Non-blocking connection.
579d0169 346cdef extern from 'mLib/conn.h':
347 ctypedef struct conn:
348 pass
349 int conn_fd(conn *c, sel_state *s, int fd,
5b1830f3 350 void (*func)(int fd, void *arg), void *arg)
579d0169 351 void conn_kill(conn *c)
352
5b1830f3 353## Background name resolver.
579d0169 354cdef extern from 'mLib/bres.h':
355 ctypedef struct bres_client:
356 pass
357 void bres_byname(bres_client *r, char *name,
5b1830f3 358 void (*func)(hostent *h, void *arg), void *arg)
579d0169 359 void bres_byaddr(bres_client *r, in_addr addr,
5b1830f3 360 void (*func)(hostent *h, void *arg), void *arg)
579d0169 361 void bres_abort(bres_client *r)
362 void bres_exec(char *null)
363 void bres_init(sel_state *s)
364
5b1830f3 365## In-band signal handling.
579d0169 366cdef extern from 'mLib/sig.h':
367 ctypedef struct sig:
368 pass
369 void sig_add(sig *s, int n, void (*func)(int n, void *arg), void *arg)
370 void sig_remove(sig *s)
371 void sig_init(sel_state *s)
372
5b1830f3 373## Line buffer.
579d0169 374cdef extern from 'mLib/selbuf.h':
375 ctypedef struct selbuf:
376 sel_file reader
377 lbuf b
378 void selbuf_enable(selbuf *b)
379 void selbuf_disable(selbuf *b)
380 void selbuf_setsize(selbuf *b, size_t sz)
381 void selbuf_init(selbuf *b, sel_state *s, int fd,
5b1830f3 382 void (*func)(char *s, size_t len, void *arg), void *arg)
579d0169 383 void selbuf_destroy(selbuf *b)
384
5b1830f3 385## Packet buffer.
579d0169 386cdef extern from 'mLib/selpk.h':
387 ctypedef struct selpk:
388 sel_file reader
389 pkbuf pk
390 void selpk_enable(selpk *b)
391 void selpk_disable(selpk *b)
392 void selpk_want(selpk *b, size_t sz)
393 void selpk_init(selpk *b, sel_state *s, int fd,
5b1830f3
MW
394 void (*func)(unsigned char *p, size_t n,
395 pkbuf *pk, size_t *keep, void *arg),
396 void *arg)
579d0169 397 void selpk_destroy(selpk *b)
398
5b1830f3 399## Ident client.
579d0169 400cdef extern from 'mLib/ident.h':
401 ctypedef struct ident_request:
402 pass
403 enum:
404 IDENT_USERID
405 IDENT_ERROR
406 IDENT_BAD
407 struct ident_userid:
408 char *os
409 char *user
410 union ident_u:
411 ident_userid userid
412 char *error
413 ctypedef struct ident_reply:
414 int sport
415 int dport
416 int type
417 ident_u u
418 void ident(ident_request *rq, sel_state *s,
5b1830f3
MW
419 sockaddr_in *local, sockaddr_in *remote,
420 void (*func)(ident_reply *r, void *arg),
421 void *arg)
579d0169 422 void ident_abort(ident_request *rq)
423
976d8e49
MW
424###--------------------------------------------------------------------------
425### Error reporting.
579d0169 426
427cdef extern from 'mLib/quis.h':
428 void _ego "ego"(char *prog)
429 char *_quis "quis"()
ae68e889 430
579d0169 431cdef extern from 'mLib/report.h':
432 void _moan "moan"(char *f, char *msg)
433
976d8e49
MW
434###--------------------------------------------------------------------------
435### File comparison.
ae68e889
MW
436
437cdef extern from 'mLib/fwatch.h':
438 ctypedef struct fwatch:
439 pass
440 void fwatch_init(fwatch *f, char *name)
441 void fwatch_initfd(fwatch *f, int fd)
965caf5f
MW
442 bint fwatch_update(fwatch *f, char *name)
443 bint fwatch_updatefd(fwatch *f, int fd)
ae68e889 444
976d8e49
MW
445###--------------------------------------------------------------------------
446### File descriptor hacking.
ae68e889
MW
447
448cdef extern from 'mLib/fdflags.h':
449 int _fdflags "fdflags"(int fd,
5b1830f3
MW
450 unsigned fbic, unsigned fxor,
451 unsigned fdbic, unsigned fdxor)
ae68e889
MW
452
453cdef extern from 'mLib/fdpass.h':
454 int fdpass_send(int sock, int fd, void *p, size_t sz)
455 int fdpass_recv(int sock, int *fd, void *p, size_t sz)
456
ea2fc600
MW
457cdef extern from 'mLib/mdup.h':
458 ctypedef struct mdup_fd:
459 int cur
460 int want
461 int _mdup "mdup"(mdup_fd *v, size_t n)
462
976d8e49
MW
463###--------------------------------------------------------------------------
464### Daemon utilities.
ae68e889
MW
465
466cdef extern from 'mLib/daemonize.h':
467 int _daemonize "daemonize"()
468 void _detachtty "detachtty"()
469
976d8e49
MW
470###--------------------------------------------------------------------------
471### Internal utilities.
579d0169 472
473cdef extern from 'grim.h':
474 int PSIZEOF(void *x)
965caf5f
MW
475 object BIN_FROMSTRLEN(const void *p, Py_ssize_t sz)
476 void *BIN_PTR(object bin)
477 void BIN_SETLEN(object bin, Py_ssize_t sz)
478 int TEXT_CHECK(object p)
479 char *TEXT_PTR(str s)
efb0bf0e 480 void TEXT_PTRLEN(str s, const char **p, Py_ssize_t *sz)
965caf5f
MW
481 str TEXT_FROMSTR(const char *p)
482 str TEXT_FROMSTRLEN(const char *p, Py_ssize_t sz)
579d0169 483
5b1830f3 484###----- That's all, folks --------------------------------------------------