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 | ||
26 | ###----- C library ---------------------------------------------------------- | |
579d0169 | 27 | |
28 | cdef extern from 'errno.h': | |
29 | int errno | |
30 | enum: | |
31 | EINTR | |
32 | EAGAIN | |
33 | cdef extern from 'limits.h': | |
34 | enum: | |
35 | LONG_MAX | |
36 | cdef extern from 'math.h': | |
37 | double modf(double x, double *i) | |
38 | cdef extern from 'stddef.h': | |
39 | ctypedef int size_t | |
40 | cdef extern from 'string.h': | |
41 | void memcpy(void *p, void *q, size_t n) | |
42 | char *strerror(int err) | |
6a6bd8fa | 43 | size_t strlen(char *p) |
579d0169 | 44 | |
5b1830f3 | 45 | ###----- Unix interface ----------------------------------------------------- |
579d0169 | 46 | |
47 | cdef extern from 'sys/types.h': | |
48 | pass | |
49 | cdef extern from 'sys/time.h': | |
50 | struct timeval: | |
51 | int tv_sec | |
52 | int tv_usec | |
53 | ||
54 | cdef extern from 'sys/socket.h': | |
55 | struct sockaddr: | |
56 | int sa_family | |
57 | enum: | |
58 | AF_INET | |
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': | |
62 | struct in_addr: | |
63 | int s_addr | |
64 | int inet_aton(char *rp, in_addr *ia) | |
65 | char *inet_ntoa(in_addr ia) | |
66 | cdef extern from 'netinet/in.h': | |
67 | struct sockaddr_in: | |
68 | int sin_family | |
69 | in_addr sin_addr | |
70 | int sin_port | |
71 | int htons(int x) | |
72 | int htonl(int x) | |
73 | int ntohs(int x) | |
74 | int ntohl(int x) | |
75 | cdef extern from 'netdb.h': | |
76 | struct hostent: | |
77 | char *h_name | |
78 | char **h_aliases | |
79 | int h_addrtype | |
80 | int h_length | |
81 | char **h_addr_list | |
82 | char *h_addr | |
83 | int h_errno | |
84 | ||
5b1830f3 | 85 | ###----- Python ------------------------------------------------------------- |
579d0169 | 86 | |
87 | cdef extern from 'Python.h': | |
88 | ||
ae68e889 MW |
89 | ctypedef struct PyObject: |
90 | pass | |
91 | ctypedef struct PyTypeObject: | |
92 | pass | |
93 | ||
579d0169 | 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) | |
ae68e889 MW |
100 | char *PyString_AS_STRING(string) |
101 | int _PyString_Resize(PyObject **string, int size) except -1 | |
579d0169 | 102 | |
579d0169 | 103 | void Py_INCREF(PyObject *obj) |
104 | void Py_DECREF(PyObject *obj) | |
105 | ||
5b1830f3 | 106 | ###----- mLib basic stuff --------------------------------------------------- |
579d0169 | 107 | |
108 | cdef extern from 'mLib/alloc.h': | |
109 | char *xstrdup(char *p) | |
110 | void *xmalloc(size_t sz) | |
111 | void xfree(void *p) | |
112 | ||
113 | cdef extern from 'mLib/bits.h': | |
114 | ctypedef int uint32 | |
115 | ||
116 | cdef extern from 'mLib/dstr.h': | |
117 | ctypedef struct dstr: | |
118 | char *buf | |
119 | int len | |
120 | void DCREATE(dstr *d) | |
121 | void dstr_destroy(dstr *d) | |
122 | ||
5b1830f3 | 123 | ###----- CRC32 -------------------------------------------------------------- |
579d0169 | 124 | |
125 | cdef extern from 'mLib/crc32.h': | |
126 | uint32 c_crc32 "crc32" (uint32 a, void *p, int sz) | |
127 | ||
5b1830f3 | 128 | ###----- Universal hashing -------------------------------------------------- |
579d0169 | 129 | |
130 | cdef extern from 'mLib/unihash.h': | |
131 | ctypedef struct unihash_info: | |
132 | pass | |
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 | |
137 | ||
5b1830f3 | 138 | ###----- Symbol tables ------------------------------------------------------ |
579d0169 | 139 | |
140 | cdef extern from 'mLib/sym.h': | |
141 | ctypedef struct sym_table: | |
142 | pass | |
143 | ctypedef struct sym_base: | |
144 | pass | |
145 | ctypedef struct sym_iter: | |
146 | pass | |
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) | |
152 | int SYM_LEN(void *b) | |
153 | void sym_mkiter(sym_iter *i, sym_table *t) | |
154 | void *sym_next(sym_iter *i) | |
155 | ||
5b1830f3 | 156 | ###----- String utilities --------------------------------------------------- |
6a6bd8fa MW |
157 | |
158 | cdef extern from 'mLib/str.h': | |
159 | enum: | |
160 | STRF_QUOTE | |
95920c4f | 161 | STRF_PREFIX |
6a6bd8fa MW |
162 | char *str_qword(char **pp, unsigned f) |
163 | size_t str_qsplit(char *p, char **v, size_t c, char **rest, unsigned f) | |
95920c4f | 164 | int str_matchx(char *p, char *s, unsigned f) |
6a6bd8fa MW |
165 | void str_sanitize(char *d, char *p, size_t sz) |
166 | ||
ae68e889 MW |
167 | cdef extern from 'mLib/versioncmp.h': |
168 | int _versioncmp "versioncmp"(char *va, char *vb) | |
169 | ||
5b1830f3 | 170 | ###----- Form-urlencoding functions ----------------------------------------- |
5ce5170c MW |
171 | |
172 | cdef extern from 'mLib/url.h': | |
173 | struct url_ectx: | |
174 | unsigned f | |
175 | struct url_dctx: | |
176 | char *p | |
177 | unsigned f | |
178 | enum: | |
179 | URLF_STRICT | |
180 | URLF_LAX | |
181 | URLF_SEMI | |
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) | |
186 | ||
5b1830f3 | 187 | ###----- Atom stuff --------------------------------------------------------- |
579d0169 | 188 | |
5b1830f3 MW |
189 | ## Atoms. |
190 | ## | |
191 | ## Partly written in `real' C. | |
579d0169 | 192 | cdef extern from 'atom.h': |
193 | ctypedef struct atom: | |
194 | pass | |
195 | ctypedef struct atom_iter: | |
196 | pass | |
197 | ctypedef struct atom_table: | |
198 | pass | |
199 | atom_table *ATOM_GLOBAL | |
200 | void atom_mkiter(atom_iter *i, atom_table *t) | |
201 | atom *atom_next(atom_iter *) | |
202 | void atom_pysetup() | |
203 | atom_pywrap(atom *a) | |
204 | atom_pyintern(obj) | |
205 | atom *ATOM_A(obj) | |
206 | PyTypeObject atom_pytype | |
207 | ||
5b1830f3 | 208 | ## Association tables. |
579d0169 | 209 | cdef extern from 'mLib/assoc.h': |
210 | ctypedef struct assoc_table: | |
211 | pass | |
212 | ctypedef struct assoc_base: | |
213 | pass | |
214 | ctypedef struct assoc_iter: | |
215 | pass | |
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) | |
223 | ||
5b1830f3 | 224 | ###----- Double-ended arrays ------------------------------------------------ |
579d0169 | 225 | |
226 | cdef extern from 'array.h': | |
227 | void da_pysetup() | |
228 | PyTypeObject da_pytype | |
229 | PyTypeObject daiter_pytype | |
230 | ||
5b1830f3 | 231 | ###----- Line buffer -------------------------------------------------------- |
579d0169 | 232 | |
233 | cdef extern from 'mLib/lbuf.h': | |
234 | cdef struct lbuf: | |
235 | int f | |
236 | int delim | |
237 | size_t sz | |
238 | enum: | |
239 | LBUF_ENABLE | |
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, | |
5b1830f3 | 249 | void (*func)(char *s, size_t len, void *arg), void *arg) |
579d0169 | 250 | void lbuf_destroy(lbuf *b) |
251 | ||
5b1830f3 | 252 | ###----- Packet buffer ------------------------------------------------------ |
579d0169 | 253 | |
254 | cdef extern from 'mLib/pkbuf.h': | |
255 | ctypedef struct pkbuf: | |
256 | int f | |
257 | int want | |
258 | enum: | |
259 | PKBUF_ENABLE | |
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, | |
5b1830f3 MW |
265 | void (*func)(unsigned char *s, size_t len, |
266 | pkbuf *pk, size_t *keep, void *arg), | |
267 | void *arg) | |
579d0169 | 268 | void pkbuf_destroy(pkbuf *b) |
269 | ||
5b1830f3 | 270 | ###----- Select stuff ------------------------------------------------------- |
579d0169 | 271 | |
5b1830f3 | 272 | ## Basics. |
579d0169 | 273 | cdef extern from 'mLib/sel.h': |
274 | ctypedef struct sel_state: | |
275 | pass | |
276 | ctypedef struct sel_file: | |
277 | int fd | |
278 | ctypedef struct sel_timer: | |
279 | pass | |
280 | enum: | |
281 | _SEL_READ "SEL_READ" | |
282 | _SEL_WRITE "SEL_WRITE" | |
283 | _SEL_EXC "SEL_EXC" | |
284 | void sel_init(sel_state *s) | |
285 | void sel_initfile(sel_state *s, sel_file *f, int fd, unsigned mode, | |
5b1830f3 MW |
286 | void (*func)(int fd, unsigned mode, void *arg), |
287 | void *arg) | |
579d0169 | 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, | |
5b1830f3 MW |
292 | void (*func)(timeval *tv, void *arg), |
293 | void *arg) | |
579d0169 | 294 | void sel_rmtimer(sel_timer *t) |
295 | int sel_select(sel_state *s) except * | |
296 | ||
5b1830f3 | 297 | ### Non-blocking connection. |
579d0169 | 298 | cdef extern from 'mLib/conn.h': |
299 | ctypedef struct conn: | |
300 | pass | |
301 | int conn_fd(conn *c, sel_state *s, int fd, | |
5b1830f3 | 302 | void (*func)(int fd, void *arg), void *arg) |
579d0169 | 303 | void conn_kill(conn *c) |
304 | ||
5b1830f3 | 305 | ## Background name resolver. |
579d0169 | 306 | cdef extern from 'mLib/bres.h': |
307 | ctypedef struct bres_client: | |
308 | pass | |
309 | void bres_byname(bres_client *r, char *name, | |
5b1830f3 | 310 | void (*func)(hostent *h, void *arg), void *arg) |
579d0169 | 311 | void bres_byaddr(bres_client *r, in_addr addr, |
5b1830f3 | 312 | void (*func)(hostent *h, void *arg), void *arg) |
579d0169 | 313 | void bres_abort(bres_client *r) |
314 | void bres_exec(char *null) | |
315 | void bres_init(sel_state *s) | |
316 | ||
5b1830f3 | 317 | ## In-band signal handling. |
579d0169 | 318 | cdef extern from 'mLib/sig.h': |
319 | ctypedef struct sig: | |
320 | pass | |
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) | |
324 | ||
5b1830f3 | 325 | ## Line buffer. |
579d0169 | 326 | cdef extern from 'mLib/selbuf.h': |
327 | ctypedef struct selbuf: | |
328 | sel_file reader | |
329 | lbuf b | |
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, | |
5b1830f3 | 334 | void (*func)(char *s, size_t len, void *arg), void *arg) |
579d0169 | 335 | void selbuf_destroy(selbuf *b) |
336 | ||
5b1830f3 | 337 | ## Packet buffer. |
579d0169 | 338 | cdef extern from 'mLib/selpk.h': |
339 | ctypedef struct selpk: | |
340 | sel_file reader | |
341 | pkbuf pk | |
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, | |
5b1830f3 MW |
346 | void (*func)(unsigned char *p, size_t n, |
347 | pkbuf *pk, size_t *keep, void *arg), | |
348 | void *arg) | |
579d0169 | 349 | void selpk_destroy(selpk *b) |
350 | ||
5b1830f3 | 351 | ## Ident client. |
579d0169 | 352 | cdef extern from 'mLib/ident.h': |
353 | ctypedef struct ident_request: | |
354 | pass | |
355 | enum: | |
356 | IDENT_USERID | |
357 | IDENT_ERROR | |
358 | IDENT_BAD | |
359 | struct ident_userid: | |
360 | char *os | |
361 | char *user | |
362 | union ident_u: | |
363 | ident_userid userid | |
364 | char *error | |
365 | ctypedef struct ident_reply: | |
366 | int sport | |
367 | int dport | |
368 | int type | |
369 | ident_u u | |
370 | void ident(ident_request *rq, sel_state *s, | |
5b1830f3 MW |
371 | sockaddr_in *local, sockaddr_in *remote, |
372 | void (*func)(ident_reply *r, void *arg), | |
373 | void *arg) | |
579d0169 | 374 | void ident_abort(ident_request *rq) |
375 | ||
5b1830f3 | 376 | ###----- Error reporting ---------------------------------------------------- |
579d0169 | 377 | |
378 | cdef extern from 'mLib/quis.h': | |
379 | void _ego "ego"(char *prog) | |
380 | char *_quis "quis"() | |
ae68e889 | 381 | |
579d0169 | 382 | cdef extern from 'mLib/report.h': |
383 | void _moan "moan"(char *f, char *msg) | |
384 | ||
5b1830f3 | 385 | ###----- File comparison ---------------------------------------------------- |
ae68e889 MW |
386 | |
387 | cdef extern from 'mLib/fwatch.h': | |
388 | ctypedef struct fwatch: | |
389 | pass | |
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) | |
394 | ||
5b1830f3 | 395 | ###----- File descriptor hacking -------------------------------------------- |
ae68e889 MW |
396 | |
397 | cdef extern from 'mLib/fdflags.h': | |
398 | int _fdflags "fdflags"(int fd, | |
5b1830f3 MW |
399 | unsigned fbic, unsigned fxor, |
400 | unsigned fdbic, unsigned fdxor) | |
ae68e889 MW |
401 | |
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) | |
405 | ||
ea2fc600 MW |
406 | cdef extern from 'mLib/mdup.h': |
407 | ctypedef struct mdup_fd: | |
408 | int cur | |
409 | int want | |
410 | int _mdup "mdup"(mdup_fd *v, size_t n) | |
411 | ||
5b1830f3 | 412 | ###----- Daemon utilities --------------------------------------------------- |
ae68e889 MW |
413 | |
414 | cdef extern from 'mLib/daemonize.h': | |
415 | int _daemonize "daemonize"() | |
416 | void _detachtty "detachtty"() | |
417 | ||
5b1830f3 | 418 | ###----- Internal utilities ------------------------------------------------- |
579d0169 | 419 | |
420 | cdef extern from 'grim.h': | |
421 | int PSIZEOF(void *x) | |
422 | ||
5b1830f3 | 423 | ###----- That's all, folks -------------------------------------------------- |