General reorganization.
[mLib-python] / defs.pxi
diff --git a/defs.pxi b/defs.pxi
new file mode 100644 (file)
index 0000000..10502f7
--- /dev/null
+++ b/defs.pxi
@@ -0,0 +1,365 @@
+# -*-pyrex-*-
+#
+# $Id$
+#
+# Basic definitions, used all over
+#
+# (c) 2005 Straylight/Edgeware
+#
+
+#----- Licensing notice -----------------------------------------------------
+#
+# This file is part of the Python interface to mLib.
+#
+# mLib/Python is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# mLib/Python is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with mLib/Python; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+#----- C library ------------------------------------------------------------
+
+cdef extern from 'errno.h':
+  int errno
+  enum:
+    EINTR
+    EAGAIN
+cdef extern from 'limits.h':
+  enum:
+    LONG_MAX
+cdef extern from 'math.h':
+  double modf(double x, double *i)
+cdef extern from 'stddef.h':
+  ctypedef int size_t
+cdef extern from 'string.h':
+  void memcpy(void *p, void *q, size_t n)
+  char *strerror(int err)
+
+#----- Unix interface -------------------------------------------------------
+
+cdef extern from 'sys/types.h':
+  pass
+cdef extern from 'sys/time.h':
+  struct timeval:
+    int tv_sec
+    int tv_usec
+
+cdef extern from 'sys/socket.h':
+  struct sockaddr:
+    int sa_family
+  enum:
+    AF_INET
+  int getsockname(int fd, sockaddr *pa, size_t *psz)
+  int getpeername(int fd, sockaddr *pa, size_t *psz)
+cdef extern from 'arpa/inet.h':
+  struct in_addr:
+    int s_addr
+  int inet_aton(char *rp, in_addr *ia)
+  char *inet_ntoa(in_addr ia)
+cdef extern from 'netinet/in.h':
+  struct sockaddr_in:
+    int sin_family
+    in_addr sin_addr
+    int sin_port
+  int htons(int x)
+  int htonl(int x)
+  int ntohs(int x)
+  int ntohl(int x)
+cdef extern from 'netdb.h':
+  struct hostent:
+    char *h_name
+    char **h_aliases
+    int h_addrtype
+    int h_length
+    char **h_addr_list
+    char *h_addr
+  int h_errno
+
+#----- Python ---------------------------------------------------------------
+
+cdef extern from 'Python.h':
+
+  object PyString_FromStringAndSize(char *p, int len)
+  int PyString_AsStringAndSize(obj, char **p, int *len) except -1
+  int PyObject_AsReadBuffer(obj, void **buf, int *len) except -1
+  int PyObject_TypeCheck(obj, ty)
+  object PyInt_FromLong(long i)
+  object PyLong_FromUnsignedLong(unsigned long i)
+
+  ctypedef struct PyObject:
+    pass
+  ctypedef struct PyTypeObject:
+    pass
+  void Py_INCREF(PyObject *obj)
+  void Py_DECREF(PyObject *obj)
+
+#----- mLib basic stuff -----------------------------------------------------
+
+cdef extern from 'mLib/alloc.h':
+  char *xstrdup(char *p)
+  void *xmalloc(size_t sz)
+  void xfree(void *p)
+
+cdef extern from 'mLib/bits.h':
+  ctypedef int uint32
+
+cdef extern from 'mLib/dstr.h':
+  ctypedef struct dstr:
+    char *buf
+    int len
+  void DCREATE(dstr *d)
+  void dstr_destroy(dstr *d)
+
+#----- CRC32 ----------------------------------------------------------------
+
+cdef extern from 'mLib/crc32.h':
+  uint32 c_crc32 "crc32" (uint32 a, void *p, int sz)
+
+#----- Universal hashing ----------------------------------------------------
+
+cdef extern from 'mLib/unihash.h':
+  ctypedef struct unihash_info:
+    pass
+  void unihash_setkey(unihash_info *i, uint32 k)
+  uint32 UNIHASH_INIT(unihash_info *i)
+  uint32 unihash_hash(unihash_info *i, uint32 a, void *p, int sz)
+  unihash_info unihash_global
+
+#----- Symbol tables --------------------------------------------------------
+
+cdef extern from 'mLib/sym.h':
+  ctypedef struct sym_table:
+    pass
+  ctypedef struct sym_base:
+    pass
+  ctypedef struct sym_iter:
+    pass
+  void sym_create(sym_table *t)
+  void sym_destroy(sym_table *t)
+  void *sym_find(sym_table *t, char *n, long l, int sz, unsigned *f)
+  void sym_remove(sym_table *t, void *b)
+  char *SYM_NAME(void *b)
+  int SYM_LEN(void *b)
+  void sym_mkiter(sym_iter *i, sym_table *t)
+  void *sym_next(sym_iter *i)
+
+#----- Atom stuff -----------------------------------------------------------
+
+# --- Atoms ---
+#
+# Partly written in `real' C.
+
+cdef extern from 'atom.h':
+  ctypedef struct atom:
+    pass
+  ctypedef struct atom_iter:
+    pass
+  ctypedef struct atom_table:
+    pass
+  atom_table *ATOM_GLOBAL
+  void atom_mkiter(atom_iter *i, atom_table *t)
+  atom *atom_next(atom_iter *)
+  void atom_pysetup()
+  atom_pywrap(atom *a)
+  atom_pyintern(obj)
+  atom *ATOM_A(obj)
+  PyTypeObject atom_pytype
+
+# --- Association tables ---
+
+cdef extern from 'mLib/assoc.h':
+  ctypedef struct assoc_table:
+    pass
+  ctypedef struct assoc_base:
+    pass
+  ctypedef struct assoc_iter:
+    pass
+  void assoc_create(assoc_table *t)
+  void assoc_destroy(assoc_table *t)
+  void *assoc_find(assoc_table *t, atom *a, int sz, unsigned *f)
+  void assoc_remove(assoc_table *t, void *b)
+  atom *ASSOC_ATOM(void *b)
+  void assoc_mkiter(assoc_iter *i, assoc_table *t)
+  void *assoc_next(assoc_iter *i)
+
+#----- Double-ended arrays --------------------------------------------------
+
+cdef extern from 'array.h':
+  void da_pysetup()
+  PyTypeObject da_pytype
+  PyTypeObject daiter_pytype
+
+#----- Line buffer ----------------------------------------------------------
+
+cdef extern from 'mLib/lbuf.h':
+  cdef struct lbuf:
+    int f
+    int delim
+    size_t sz
+  enum:
+    LBUF_ENABLE
+    _LBUF_CRLF "LBUF_CRLF"
+    _LBUF_STRICTCRLF "LBUF_STRICTCRLF"
+  void lbuf_flush(lbuf *b, char *p, size_t len)
+  void lbuf_close(lbuf *b)
+  size_t lbuf_free(lbuf *b, char **p)
+  void lbuf_setsize(lbuf *b, size_t sz)
+  void lbuf_enable(lbuf *b)
+  void lbuf_disable(lbuf *b)
+  void lbuf_init(lbuf *b,
+                 void (*func)(char *s, size_t len, void *arg), void *arg)
+  void lbuf_destroy(lbuf *b)
+
+#----- Packet buffer --------------------------------------------------------
+
+cdef extern from 'mLib/pkbuf.h':
+  ctypedef struct pkbuf:
+    int f
+    int want
+  enum:
+    PKBUF_ENABLE
+  void pkbuf_flush(pkbuf *pk, unsigned char *p, size_t len)
+  void pkbuf_close(pkbuf *pk)
+  size_t pkbuf_free(pkbuf *pk, unsigned char **p)
+  void pkbuf_want(pkbuf *pk, size_t sz)
+  void pkbuf_init(pkbuf *b,
+                  void (*func)(unsigned char *s, size_t len,
+                               pkbuf *pk, size_t *keep, void *arg),
+                  void *arg)
+  void pkbuf_destroy(pkbuf *b)
+
+#----- Select stuff ---------------------------------------------------------
+
+# --- Basics ---
+
+cdef extern from 'mLib/sel.h':
+  ctypedef struct sel_state:
+    pass
+  ctypedef struct sel_file:
+    int fd
+  ctypedef struct sel_timer:
+    pass
+  enum:
+    _SEL_READ "SEL_READ"
+    _SEL_WRITE "SEL_WRITE"
+    _SEL_EXC "SEL_EXC"
+  void sel_init(sel_state *s)
+  void sel_initfile(sel_state *s, sel_file *f, int fd, unsigned mode,
+                    void (*func)(int fd, unsigned mode, void *arg),
+                    void *arg)
+  void sel_force(sel_file *f)
+  void sel_addfile(sel_file *f)
+  void sel_rmfile(sel_file *f)
+  void sel_addtimer(sel_state *s, sel_timer *t, timeval *tv,
+                    void (*func)(timeval *tv, void *arg),
+                    void *arg)
+  void sel_rmtimer(sel_timer *t)
+  int sel_select(sel_state *s) except *
+
+# --- Non-blocking connection ---
+
+cdef extern from 'mLib/conn.h':
+  ctypedef struct conn:
+    pass
+  int conn_fd(conn *c, sel_state *s, int fd,
+              void (*func)(int fd, void *arg), void *arg)
+  void conn_kill(conn *c)
+
+# --- Background name resolver ---
+
+cdef extern from 'mLib/bres.h':
+  ctypedef struct bres_client:
+    pass
+  void bres_byname(bres_client *r, char *name,
+                   void (*func)(hostent *h, void *arg), void *arg)
+  void bres_byaddr(bres_client *r, in_addr addr,
+                   void (*func)(hostent *h, void *arg), void *arg)
+  void bres_abort(bres_client *r)
+  void bres_exec(char *null)
+  void bres_init(sel_state *s)
+
+# --- In-band signal handling ---
+
+cdef extern from 'mLib/sig.h':
+  ctypedef struct sig:
+    pass
+  void sig_add(sig *s, int n, void (*func)(int n, void *arg), void *arg)
+  void sig_remove(sig *s)
+  void sig_init(sel_state *s)
+
+# --- Line buffer ---
+
+cdef extern from 'mLib/selbuf.h':
+  ctypedef struct selbuf:
+    sel_file reader
+    lbuf b
+  void selbuf_enable(selbuf *b)
+  void selbuf_disable(selbuf *b)
+  void selbuf_setsize(selbuf *b, size_t sz)
+  void selbuf_init(selbuf *b, sel_state *s, int fd,
+                   void (*func)(char *s, size_t len, void *arg), void *arg)
+  void selbuf_destroy(selbuf *b)
+
+# --- Packet buffer ---
+
+cdef extern from 'mLib/selpk.h':
+  ctypedef struct selpk:
+    sel_file reader
+    pkbuf pk
+  void selpk_enable(selpk *b)
+  void selpk_disable(selpk *b)
+  void selpk_want(selpk *b, size_t sz)
+  void selpk_init(selpk *b, sel_state *s, int fd,
+                  void (*func)(unsigned char *p, size_t n,
+                               pkbuf *pk, size_t *keep, void *arg),
+                  void *arg)
+  void selpk_destroy(selpk *b)
+
+# --- Ident client ---
+
+cdef extern from 'mLib/ident.h':
+  ctypedef struct ident_request:
+    pass
+  enum:
+    IDENT_USERID
+    IDENT_ERROR
+    IDENT_BAD
+  struct ident_userid:
+    char *os
+    char *user
+  union ident_u:
+    ident_userid userid
+    char *error
+  ctypedef struct ident_reply:
+    int sport
+    int dport
+    int type
+    ident_u u
+  void ident(ident_request *rq, sel_state *s,
+             sockaddr_in *local, sockaddr_in *remote,
+             void (*func)(ident_reply *r, void *arg),
+             void *arg)
+  void ident_abort(ident_request *rq)
+
+#----- Error reporting ------------------------------------------------------
+
+cdef extern from 'mLib/quis.h':
+  void _ego "ego"(char *prog)
+  char *_quis "quis"()
+cdef extern from 'mLib/report.h':
+  void _moan "moan"(char *f, char *msg)
+
+#----- Internal utilities ---------------------------------------------------
+
+cdef extern from 'grim.h':
+  int PSIZEOF(void *x)
+
+#----- That's all, folks ----------------------------------------------------