str: Support mLib's `str' functions.
[mLib-python] / defs.pxi
1 # -*-pyrex-*-
2 #
3 # $Id$
4 #
5 # Basic definitions, used all over
6 #
7 # (c) 2005 Straylight/Edgeware
8 #
9
10 #----- Licensing notice -----------------------------------------------------
11 #
12 # This file is part of the Python interface to mLib.
13 #
14 # mLib/Python is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # mLib/Python is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with mLib/Python; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28 #----- C library ------------------------------------------------------------
29
30 cdef extern from 'errno.h':
31 int errno
32 enum:
33 EINTR
34 EAGAIN
35 cdef extern from 'limits.h':
36 enum:
37 LONG_MAX
38 cdef extern from 'math.h':
39 double modf(double x, double *i)
40 cdef extern from 'stddef.h':
41 ctypedef int size_t
42 cdef extern from 'string.h':
43 void memcpy(void *p, void *q, size_t n)
44 char *strerror(int err)
45 size_t strlen(char *p)
46
47 #----- Unix interface -------------------------------------------------------
48
49 cdef extern from 'sys/types.h':
50 pass
51 cdef extern from 'sys/time.h':
52 struct timeval:
53 int tv_sec
54 int tv_usec
55
56 cdef extern from 'sys/socket.h':
57 struct sockaddr:
58 int sa_family
59 enum:
60 AF_INET
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':
64 struct in_addr:
65 int s_addr
66 int inet_aton(char *rp, in_addr *ia)
67 char *inet_ntoa(in_addr ia)
68 cdef extern from 'netinet/in.h':
69 struct sockaddr_in:
70 int sin_family
71 in_addr sin_addr
72 int sin_port
73 int htons(int x)
74 int htonl(int x)
75 int ntohs(int x)
76 int ntohl(int x)
77 cdef extern from 'netdb.h':
78 struct hostent:
79 char *h_name
80 char **h_aliases
81 int h_addrtype
82 int h_length
83 char **h_addr_list
84 char *h_addr
85 int h_errno
86
87 #----- Python ---------------------------------------------------------------
88
89 cdef extern from 'Python.h':
90
91 object PyString_FromStringAndSize(char *p, int len)
92 int PyString_AsStringAndSize(obj, char **p, int *len) except -1
93 int PyObject_AsReadBuffer(obj, void **buf, int *len) except -1
94 int PyObject_TypeCheck(obj, ty)
95 object PyInt_FromLong(long i)
96 object PyLong_FromUnsignedLong(unsigned long i)
97
98 ctypedef struct PyObject:
99 pass
100 ctypedef struct PyTypeObject:
101 pass
102 void Py_INCREF(PyObject *obj)
103 void Py_DECREF(PyObject *obj)
104
105 #----- mLib basic stuff -----------------------------------------------------
106
107 cdef extern from 'mLib/alloc.h':
108 char *xstrdup(char *p)
109 void *xmalloc(size_t sz)
110 void xfree(void *p)
111
112 cdef extern from 'mLib/bits.h':
113 ctypedef int uint32
114
115 cdef extern from 'mLib/dstr.h':
116 ctypedef struct dstr:
117 char *buf
118 int len
119 void DCREATE(dstr *d)
120 void dstr_destroy(dstr *d)
121
122 #----- CRC32 ----------------------------------------------------------------
123
124 cdef extern from 'mLib/crc32.h':
125 uint32 c_crc32 "crc32" (uint32 a, void *p, int sz)
126
127 #----- Universal hashing ----------------------------------------------------
128
129 cdef extern from 'mLib/unihash.h':
130 ctypedef struct unihash_info:
131 pass
132 void unihash_setkey(unihash_info *i, uint32 k)
133 uint32 UNIHASH_INIT(unihash_info *i)
134 uint32 unihash_hash(unihash_info *i, uint32 a, void *p, int sz)
135 unihash_info unihash_global
136
137 #----- Symbol tables --------------------------------------------------------
138
139 cdef extern from 'mLib/sym.h':
140 ctypedef struct sym_table:
141 pass
142 ctypedef struct sym_base:
143 pass
144 ctypedef struct sym_iter:
145 pass
146 void sym_create(sym_table *t)
147 void sym_destroy(sym_table *t)
148 void *sym_find(sym_table *t, char *n, long l, int sz, unsigned *f)
149 void sym_remove(sym_table *t, void *b)
150 char *SYM_NAME(void *b)
151 int SYM_LEN(void *b)
152 void sym_mkiter(sym_iter *i, sym_table *t)
153 void *sym_next(sym_iter *i)
154
155 #----- String utilities -----------------------------------------------------
156
157 cdef extern from 'mLib/str.h':
158 enum:
159 STRF_QUOTE
160 char *str_qword(char **pp, unsigned f)
161 size_t str_qsplit(char *p, char **v, size_t c, char **rest, unsigned f)
162 int str_match(char *p, char *s)
163 void str_sanitize(char *d, char *p, size_t sz)
164
165 #----- Atom stuff -----------------------------------------------------------
166
167 # --- Atoms ---
168 #
169 # Partly written in `real' C.
170
171 cdef extern from 'atom.h':
172 ctypedef struct atom:
173 pass
174 ctypedef struct atom_iter:
175 pass
176 ctypedef struct atom_table:
177 pass
178 atom_table *ATOM_GLOBAL
179 void atom_mkiter(atom_iter *i, atom_table *t)
180 atom *atom_next(atom_iter *)
181 void atom_pysetup()
182 atom_pywrap(atom *a)
183 atom_pyintern(obj)
184 atom *ATOM_A(obj)
185 PyTypeObject atom_pytype
186
187 # --- Association tables ---
188
189 cdef extern from 'mLib/assoc.h':
190 ctypedef struct assoc_table:
191 pass
192 ctypedef struct assoc_base:
193 pass
194 ctypedef struct assoc_iter:
195 pass
196 void assoc_create(assoc_table *t)
197 void assoc_destroy(assoc_table *t)
198 void *assoc_find(assoc_table *t, atom *a, int sz, unsigned *f)
199 void assoc_remove(assoc_table *t, void *b)
200 atom *ASSOC_ATOM(void *b)
201 void assoc_mkiter(assoc_iter *i, assoc_table *t)
202 void *assoc_next(assoc_iter *i)
203
204 #----- Double-ended arrays --------------------------------------------------
205
206 cdef extern from 'array.h':
207 void da_pysetup()
208 PyTypeObject da_pytype
209 PyTypeObject daiter_pytype
210
211 #----- Line buffer ----------------------------------------------------------
212
213 cdef extern from 'mLib/lbuf.h':
214 cdef struct lbuf:
215 int f
216 int delim
217 size_t sz
218 enum:
219 LBUF_ENABLE
220 _LBUF_CRLF "LBUF_CRLF"
221 _LBUF_STRICTCRLF "LBUF_STRICTCRLF"
222 void lbuf_flush(lbuf *b, char *p, size_t len)
223 void lbuf_close(lbuf *b)
224 size_t lbuf_free(lbuf *b, char **p)
225 void lbuf_setsize(lbuf *b, size_t sz)
226 void lbuf_enable(lbuf *b)
227 void lbuf_disable(lbuf *b)
228 void lbuf_init(lbuf *b,
229 void (*func)(char *s, size_t len, void *arg), void *arg)
230 void lbuf_destroy(lbuf *b)
231
232 #----- Packet buffer --------------------------------------------------------
233
234 cdef extern from 'mLib/pkbuf.h':
235 ctypedef struct pkbuf:
236 int f
237 int want
238 enum:
239 PKBUF_ENABLE
240 void pkbuf_flush(pkbuf *pk, unsigned char *p, size_t len)
241 void pkbuf_close(pkbuf *pk)
242 size_t pkbuf_free(pkbuf *pk, unsigned char **p)
243 void pkbuf_want(pkbuf *pk, size_t sz)
244 void pkbuf_init(pkbuf *b,
245 void (*func)(unsigned char *s, size_t len,
246 pkbuf *pk, size_t *keep, void *arg),
247 void *arg)
248 void pkbuf_destroy(pkbuf *b)
249
250 #----- Select stuff ---------------------------------------------------------
251
252 # --- Basics ---
253
254 cdef extern from 'mLib/sel.h':
255 ctypedef struct sel_state:
256 pass
257 ctypedef struct sel_file:
258 int fd
259 ctypedef struct sel_timer:
260 pass
261 enum:
262 _SEL_READ "SEL_READ"
263 _SEL_WRITE "SEL_WRITE"
264 _SEL_EXC "SEL_EXC"
265 void sel_init(sel_state *s)
266 void sel_initfile(sel_state *s, sel_file *f, int fd, unsigned mode,
267 void (*func)(int fd, unsigned mode, void *arg),
268 void *arg)
269 void sel_force(sel_file *f)
270 void sel_addfile(sel_file *f)
271 void sel_rmfile(sel_file *f)
272 void sel_addtimer(sel_state *s, sel_timer *t, timeval *tv,
273 void (*func)(timeval *tv, void *arg),
274 void *arg)
275 void sel_rmtimer(sel_timer *t)
276 int sel_select(sel_state *s) except *
277
278 # --- Non-blocking connection ---
279
280 cdef extern from 'mLib/conn.h':
281 ctypedef struct conn:
282 pass
283 int conn_fd(conn *c, sel_state *s, int fd,
284 void (*func)(int fd, void *arg), void *arg)
285 void conn_kill(conn *c)
286
287 # --- Background name resolver ---
288
289 cdef extern from 'mLib/bres.h':
290 ctypedef struct bres_client:
291 pass
292 void bres_byname(bres_client *r, char *name,
293 void (*func)(hostent *h, void *arg), void *arg)
294 void bres_byaddr(bres_client *r, in_addr addr,
295 void (*func)(hostent *h, void *arg), void *arg)
296 void bres_abort(bres_client *r)
297 void bres_exec(char *null)
298 void bres_init(sel_state *s)
299
300 # --- In-band signal handling ---
301
302 cdef extern from 'mLib/sig.h':
303 ctypedef struct sig:
304 pass
305 void sig_add(sig *s, int n, void (*func)(int n, void *arg), void *arg)
306 void sig_remove(sig *s)
307 void sig_init(sel_state *s)
308
309 # --- Line buffer ---
310
311 cdef extern from 'mLib/selbuf.h':
312 ctypedef struct selbuf:
313 sel_file reader
314 lbuf b
315 void selbuf_enable(selbuf *b)
316 void selbuf_disable(selbuf *b)
317 void selbuf_setsize(selbuf *b, size_t sz)
318 void selbuf_init(selbuf *b, sel_state *s, int fd,
319 void (*func)(char *s, size_t len, void *arg), void *arg)
320 void selbuf_destroy(selbuf *b)
321
322 # --- Packet buffer ---
323
324 cdef extern from 'mLib/selpk.h':
325 ctypedef struct selpk:
326 sel_file reader
327 pkbuf pk
328 void selpk_enable(selpk *b)
329 void selpk_disable(selpk *b)
330 void selpk_want(selpk *b, size_t sz)
331 void selpk_init(selpk *b, sel_state *s, int fd,
332 void (*func)(unsigned char *p, size_t n,
333 pkbuf *pk, size_t *keep, void *arg),
334 void *arg)
335 void selpk_destroy(selpk *b)
336
337 # --- Ident client ---
338
339 cdef extern from 'mLib/ident.h':
340 ctypedef struct ident_request:
341 pass
342 enum:
343 IDENT_USERID
344 IDENT_ERROR
345 IDENT_BAD
346 struct ident_userid:
347 char *os
348 char *user
349 union ident_u:
350 ident_userid userid
351 char *error
352 ctypedef struct ident_reply:
353 int sport
354 int dport
355 int type
356 ident_u u
357 void ident(ident_request *rq, sel_state *s,
358 sockaddr_in *local, sockaddr_in *remote,
359 void (*func)(ident_reply *r, void *arg),
360 void *arg)
361 void ident_abort(ident_request *rq)
362
363 #----- Error reporting ------------------------------------------------------
364
365 cdef extern from 'mLib/quis.h':
366 void _ego "ego"(char *prog)
367 char *_quis "quis"()
368 cdef extern from 'mLib/report.h':
369 void _moan "moan"(char *f, char *msg)
370
371 #----- Internal utilities ---------------------------------------------------
372
373 cdef extern from 'grim.h':
374 int PSIZEOF(void *x)
375
376 #----- That's all, folks ----------------------------------------------------