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