Commit | Line | Data |
---|---|---|
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 | |
29 | cdef extern from 'errno.h': | |
30 | int errno | |
31 | enum: | |
32 | EINTR | |
33 | EAGAIN | |
34 | cdef extern from 'limits.h': | |
35 | enum: | |
36 | LONG_MAX | |
37 | cdef extern from 'math.h': | |
38 | double modf(double x, double *i) | |
39 | cdef extern from 'stddef.h': | |
40 | ctypedef int size_t | |
41 | cdef 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 | |
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': | |
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) | |
64 | cdef 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) | |
69 | cdef 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) | |
78 | cdef 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 | |
91 | cdef extern from 'Python.h': | |
92 | ||
ae68e889 MW |
93 | ctypedef struct PyObject: |
94 | pass | |
95 | ctypedef struct PyTypeObject: | |
96 | pass | |
97 | ||
78911cdb MW |
98 | object PyString_FromStringAndSize(char *p, Py_ssize_t len) |
99 | int PyString_AsStringAndSize(obj, char **p, Py_ssize_t *len) except -1 | |
100 | int PyObject_AsReadBuffer(obj, void **buf, Py_ssize_t *len) except -1 | |
579d0169 | 101 | object PyInt_FromLong(long i) |
102 | object PyLong_FromUnsignedLong(unsigned long i) | |
ae68e889 MW |
103 | char *PyString_AS_STRING(string) |
104 | int _PyString_Resize(PyObject **string, int size) except -1 | |
c316167f | 105 | void PyErr_Clear() |
579d0169 | 106 | |
579d0169 | 107 | void Py_INCREF(PyObject *obj) |
108 | void Py_DECREF(PyObject *obj) | |
109 | ||
976d8e49 MW |
110 | ###-------------------------------------------------------------------------- |
111 | ### mLib basic stuff. | |
579d0169 | 112 | |
113 | cdef extern from 'mLib/alloc.h': | |
114 | char *xstrdup(char *p) | |
115 | void *xmalloc(size_t sz) | |
116 | void xfree(void *p) | |
117 | ||
118 | cdef extern from 'mLib/bits.h': | |
119 | ctypedef int uint32 | |
120 | ||
121 | cdef 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 | |
131 | cdef extern from 'mLib/crc32.h': | |
132 | uint32 c_crc32 "crc32" (uint32 a, void *p, int sz) | |
133 | ||
976d8e49 MW |
134 | ###-------------------------------------------------------------------------- |
135 | ### Universal hashing. | |
579d0169 | 136 | |
137 | cdef 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 | |
148 | cdef 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 | |
167 | cdef 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) | |
95920c4f | 173 | int str_matchx(char *p, char *s, unsigned f) |
6a6bd8fa MW |
174 | void str_sanitize(char *d, char *p, size_t sz) |
175 | ||
ae68e889 MW |
176 | cdef extern from 'mLib/versioncmp.h': |
177 | int _versioncmp "versioncmp"(char *va, char *vb) | |
178 | ||
976d8e49 MW |
179 | ###-------------------------------------------------------------------------- |
180 | ### Form-urlencoding functions. | |
5ce5170c MW |
181 | |
182 | cdef extern from 'mLib/url.h': | |
183 | struct url_ectx: | |
184 | unsigned f | |
185 | struct url_dctx: | |
186 | char *p | |
187 | unsigned f | |
188 | enum: | |
189 | URLF_STRICT | |
190 | URLF_LAX | |
191 | URLF_SEMI | |
192 | void url_initenc(url_ectx *ctx) | |
193 | void url_enc(url_ectx *ctx, dstr *d, char *name, char *value) | |
194 | void url_initdec(url_dctx *ctx, char *p) | |
195 | int url_dec(url_dctx *ctx, dstr *n, dstr *v) | |
196 | ||
976d8e49 MW |
197 | ###-------------------------------------------------------------------------- |
198 | ### Atom stuff. | |
579d0169 | 199 | |
5b1830f3 MW |
200 | ## Atoms. |
201 | ## | |
202 | ## Partly written in `real' C. | |
579d0169 | 203 | cdef extern from 'atom.h': |
204 | ctypedef struct atom: | |
205 | pass | |
206 | ctypedef struct atom_iter: | |
207 | pass | |
208 | ctypedef struct atom_table: | |
209 | pass | |
210 | atom_table *ATOM_GLOBAL | |
211 | void atom_mkiter(atom_iter *i, atom_table *t) | |
212 | atom *atom_next(atom_iter *) | |
213 | void atom_pysetup() | |
214 | atom_pywrap(atom *a) | |
215 | atom_pyintern(obj) | |
216 | atom *ATOM_A(obj) | |
217 | PyTypeObject atom_pytype | |
218 | ||
5b1830f3 | 219 | ## Association tables. |
579d0169 | 220 | cdef extern from 'mLib/assoc.h': |
221 | ctypedef struct assoc_table: | |
222 | pass | |
223 | ctypedef struct assoc_base: | |
224 | pass | |
225 | ctypedef struct assoc_iter: | |
226 | pass | |
227 | void assoc_create(assoc_table *t) | |
228 | void assoc_destroy(assoc_table *t) | |
229 | void *assoc_find(assoc_table *t, atom *a, int sz, unsigned *f) | |
230 | void assoc_remove(assoc_table *t, void *b) | |
231 | atom *ASSOC_ATOM(void *b) | |
232 | void assoc_mkiter(assoc_iter *i, assoc_table *t) | |
233 | void *assoc_next(assoc_iter *i) | |
234 | ||
976d8e49 MW |
235 | ###-------------------------------------------------------------------------- |
236 | ### Double-ended arrays. | |
579d0169 | 237 | |
238 | cdef extern from 'array.h': | |
239 | void da_pysetup() | |
240 | PyTypeObject da_pytype | |
241 | PyTypeObject daiter_pytype | |
242 | ||
976d8e49 MW |
243 | ###-------------------------------------------------------------------------- |
244 | ### Line buffer. | |
579d0169 | 245 | |
246 | cdef extern from 'mLib/lbuf.h': | |
247 | cdef struct lbuf: | |
248 | int f | |
249 | int delim | |
250 | size_t sz | |
251 | enum: | |
252 | LBUF_ENABLE | |
253 | _LBUF_CRLF "LBUF_CRLF" | |
254 | _LBUF_STRICTCRLF "LBUF_STRICTCRLF" | |
255 | void lbuf_flush(lbuf *b, char *p, size_t len) | |
256 | void lbuf_close(lbuf *b) | |
257 | size_t lbuf_free(lbuf *b, char **p) | |
258 | void lbuf_setsize(lbuf *b, size_t sz) | |
259 | void lbuf_enable(lbuf *b) | |
260 | void lbuf_disable(lbuf *b) | |
261 | void lbuf_init(lbuf *b, | |
5b1830f3 | 262 | void (*func)(char *s, size_t len, void *arg), void *arg) |
579d0169 | 263 | void lbuf_destroy(lbuf *b) |
264 | ||
976d8e49 MW |
265 | ###-------------------------------------------------------------------------- |
266 | ### Packet buffer. | |
579d0169 | 267 | |
268 | cdef extern from 'mLib/pkbuf.h': | |
269 | ctypedef struct pkbuf: | |
270 | int f | |
271 | int want | |
272 | enum: | |
273 | PKBUF_ENABLE | |
274 | void pkbuf_flush(pkbuf *pk, unsigned char *p, size_t len) | |
275 | void pkbuf_close(pkbuf *pk) | |
276 | size_t pkbuf_free(pkbuf *pk, unsigned char **p) | |
277 | void pkbuf_want(pkbuf *pk, size_t sz) | |
278 | void pkbuf_init(pkbuf *b, | |
5b1830f3 MW |
279 | void (*func)(unsigned char *s, size_t len, |
280 | pkbuf *pk, size_t *keep, void *arg), | |
281 | void *arg) | |
579d0169 | 282 | void pkbuf_destroy(pkbuf *b) |
283 | ||
976d8e49 MW |
284 | ###-------------------------------------------------------------------------- |
285 | ### Select stuff. | |
579d0169 | 286 | |
5b1830f3 | 287 | ## Basics. |
579d0169 | 288 | cdef extern from 'mLib/sel.h': |
289 | ctypedef struct sel_state: | |
290 | pass | |
291 | ctypedef struct sel_file: | |
292 | int fd | |
293 | ctypedef struct sel_timer: | |
294 | pass | |
295 | enum: | |
296 | _SEL_READ "SEL_READ" | |
297 | _SEL_WRITE "SEL_WRITE" | |
298 | _SEL_EXC "SEL_EXC" | |
299 | void sel_init(sel_state *s) | |
300 | void sel_initfile(sel_state *s, sel_file *f, int fd, unsigned mode, | |
5b1830f3 MW |
301 | void (*func)(int fd, unsigned mode, void *arg), |
302 | void *arg) | |
579d0169 | 303 | void sel_force(sel_file *f) |
304 | void sel_addfile(sel_file *f) | |
305 | void sel_rmfile(sel_file *f) | |
306 | void sel_addtimer(sel_state *s, sel_timer *t, timeval *tv, | |
5b1830f3 MW |
307 | void (*func)(timeval *tv, void *arg), |
308 | void *arg) | |
579d0169 | 309 | void sel_rmtimer(sel_timer *t) |
310 | int sel_select(sel_state *s) except * | |
311 | ||
5b1830f3 | 312 | ### Non-blocking connection. |
579d0169 | 313 | cdef extern from 'mLib/conn.h': |
314 | ctypedef struct conn: | |
315 | pass | |
316 | int conn_fd(conn *c, sel_state *s, int fd, | |
5b1830f3 | 317 | void (*func)(int fd, void *arg), void *arg) |
579d0169 | 318 | void conn_kill(conn *c) |
319 | ||
5b1830f3 | 320 | ## Background name resolver. |
579d0169 | 321 | cdef extern from 'mLib/bres.h': |
322 | ctypedef struct bres_client: | |
323 | pass | |
324 | void bres_byname(bres_client *r, char *name, | |
5b1830f3 | 325 | void (*func)(hostent *h, void *arg), void *arg) |
579d0169 | 326 | void bres_byaddr(bres_client *r, in_addr addr, |
5b1830f3 | 327 | void (*func)(hostent *h, void *arg), void *arg) |
579d0169 | 328 | void bres_abort(bres_client *r) |
329 | void bres_exec(char *null) | |
330 | void bres_init(sel_state *s) | |
331 | ||
5b1830f3 | 332 | ## In-band signal handling. |
579d0169 | 333 | cdef extern from 'mLib/sig.h': |
334 | ctypedef struct sig: | |
335 | pass | |
336 | void sig_add(sig *s, int n, void (*func)(int n, void *arg), void *arg) | |
337 | void sig_remove(sig *s) | |
338 | void sig_init(sel_state *s) | |
339 | ||
5b1830f3 | 340 | ## Line buffer. |
579d0169 | 341 | cdef extern from 'mLib/selbuf.h': |
342 | ctypedef struct selbuf: | |
343 | sel_file reader | |
344 | lbuf b | |
345 | void selbuf_enable(selbuf *b) | |
346 | void selbuf_disable(selbuf *b) | |
347 | void selbuf_setsize(selbuf *b, size_t sz) | |
348 | void selbuf_init(selbuf *b, sel_state *s, int fd, | |
5b1830f3 | 349 | void (*func)(char *s, size_t len, void *arg), void *arg) |
579d0169 | 350 | void selbuf_destroy(selbuf *b) |
351 | ||
5b1830f3 | 352 | ## Packet buffer. |
579d0169 | 353 | cdef extern from 'mLib/selpk.h': |
354 | ctypedef struct selpk: | |
355 | sel_file reader | |
356 | pkbuf pk | |
357 | void selpk_enable(selpk *b) | |
358 | void selpk_disable(selpk *b) | |
359 | void selpk_want(selpk *b, size_t sz) | |
360 | void selpk_init(selpk *b, sel_state *s, int fd, | |
5b1830f3 MW |
361 | void (*func)(unsigned char *p, size_t n, |
362 | pkbuf *pk, size_t *keep, void *arg), | |
363 | void *arg) | |
579d0169 | 364 | void selpk_destroy(selpk *b) |
365 | ||
5b1830f3 | 366 | ## Ident client. |
579d0169 | 367 | cdef extern from 'mLib/ident.h': |
368 | ctypedef struct ident_request: | |
369 | pass | |
370 | enum: | |
371 | IDENT_USERID | |
372 | IDENT_ERROR | |
373 | IDENT_BAD | |
374 | struct ident_userid: | |
375 | char *os | |
376 | char *user | |
377 | union ident_u: | |
378 | ident_userid userid | |
379 | char *error | |
380 | ctypedef struct ident_reply: | |
381 | int sport | |
382 | int dport | |
383 | int type | |
384 | ident_u u | |
385 | void ident(ident_request *rq, sel_state *s, | |
5b1830f3 MW |
386 | sockaddr_in *local, sockaddr_in *remote, |
387 | void (*func)(ident_reply *r, void *arg), | |
388 | void *arg) | |
579d0169 | 389 | void ident_abort(ident_request *rq) |
390 | ||
976d8e49 MW |
391 | ###-------------------------------------------------------------------------- |
392 | ### Error reporting. | |
579d0169 | 393 | |
394 | cdef extern from 'mLib/quis.h': | |
395 | void _ego "ego"(char *prog) | |
396 | char *_quis "quis"() | |
ae68e889 | 397 | |
579d0169 | 398 | cdef extern from 'mLib/report.h': |
399 | void _moan "moan"(char *f, char *msg) | |
400 | ||
976d8e49 MW |
401 | ###-------------------------------------------------------------------------- |
402 | ### File comparison. | |
ae68e889 MW |
403 | |
404 | cdef extern from 'mLib/fwatch.h': | |
405 | ctypedef struct fwatch: | |
406 | pass | |
407 | void fwatch_init(fwatch *f, char *name) | |
408 | void fwatch_initfd(fwatch *f, int fd) | |
409 | int fwatch_update(fwatch *f, char *name) | |
410 | int fwatch_updatefd(fwatch *f, int fd) | |
411 | ||
976d8e49 MW |
412 | ###-------------------------------------------------------------------------- |
413 | ### File descriptor hacking. | |
ae68e889 MW |
414 | |
415 | cdef extern from 'mLib/fdflags.h': | |
416 | int _fdflags "fdflags"(int fd, | |
5b1830f3 MW |
417 | unsigned fbic, unsigned fxor, |
418 | unsigned fdbic, unsigned fdxor) | |
ae68e889 MW |
419 | |
420 | cdef extern from 'mLib/fdpass.h': | |
421 | int fdpass_send(int sock, int fd, void *p, size_t sz) | |
422 | int fdpass_recv(int sock, int *fd, void *p, size_t sz) | |
423 | ||
ea2fc600 MW |
424 | cdef extern from 'mLib/mdup.h': |
425 | ctypedef struct mdup_fd: | |
426 | int cur | |
427 | int want | |
428 | int _mdup "mdup"(mdup_fd *v, size_t n) | |
429 | ||
976d8e49 MW |
430 | ###-------------------------------------------------------------------------- |
431 | ### Daemon utilities. | |
ae68e889 MW |
432 | |
433 | cdef extern from 'mLib/daemonize.h': | |
434 | int _daemonize "daemonize"() | |
435 | void _detachtty "detachtty"() | |
436 | ||
976d8e49 MW |
437 | ###-------------------------------------------------------------------------- |
438 | ### Internal utilities. | |
579d0169 | 439 | |
440 | cdef extern from 'grim.h': | |
441 | int PSIZEOF(void *x) | |
704500e1 | 442 | ctypedef void *cvp |
579d0169 | 443 | |
5b1830f3 | 444 | ###----- That's all, folks -------------------------------------------------- |