algorithms.c: Add bindings for STROBE.
[catacomb-python] / catacomb.c
index acc0e35..23906eb 100644 (file)
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Where the fun begins
  *
  * (c) 2004 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * 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.
- * 
+ *
  * Catacomb/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 Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 /*----- Main code ---------------------------------------------------------*/
 
-static void setconstants(PyObject *mod)
+PyObject *mexp_common(PyObject *me, PyObject *arg,
+                     size_t efsz,
+                     PyObject *(*id)(PyObject *),
+                     int (*fill)(void *, PyObject *,
+                                 PyObject *, PyObject *),
+                     PyObject *(*exp)(PyObject *, void *, size_t),
+                     void (*drop)(void *))
 {
-  static const struct { const char *name; unsigned long value; } consts[] = {
-#define C(x) { #x, x }
-    C(FTY_PRIME), C(FTY_BINARY),
-    C(PGEN_PASS), C(PGEN_FAIL), C(PGEN_BEGIN), C(PGEN_TRY), C(PGEN_DONE),
-      C(PGEN_ABORT),
-    C(MPW_MAX),
-    C(PMODE_READ), C(PMODE_VERIFY),
-#undef C
-    { 0 }
-  };
-  int i;
-  PyObject *x;
-
-  for (i = 0; consts[i].name; i++) {
-    if (consts[i].value > LONG_MAX)
-      x = PyLong_FromUnsignedLong(consts[i].value);
-    else
-      x = PyInt_FromLong(consts[i].value);
-    PyModule_AddObject(mod, (/*unconst*/ char *)consts[i].name, x);
+  size_t i = 0, o, n;
+  int flat;
+  PyObject *qq = 0, *x = 0, *y = 0, *z = 0, *it = 0;
+  char *v = 0;
+
+  if (PyTuple_Size(arg) == 1) arg = PyTuple_GET_ITEM(arg, 0);
+  it = PyObject_GetIter(arg); if (!it) goto end;
+  qq = PyIter_Next(it);
+  if (!qq) {
+    if (!PyErr_Occurred()) z = id(me);
+    else goto end;
+  }
+  flat = !PySequence_Check(qq);
+  if (!PySequence_Check(arg))
+    n = 16;
+  else {
+    n = PySequence_Size(arg);
+    if (n == (size_t)-1 && PyErr_Occurred()) goto end;
+    if (flat) n /= 2;
+    if (!n) n = 16;
   }
-}
-
-PyObject *getu32(uint32 w)
-{
-  if (w <= 0x7fffffff)
-    return (PyInt_FromLong(w));
-  else
-    return (PyLong_FromUnsignedLong(w));
-}
-
-PyObject *getbool(int b)
-{
-  if (b) RETURN_TRUE;
-  else RETURN_FALSE;
-}
 
-PyObject *abstract_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
-{
-  PyErr_SetString(PyExc_TypeError, "can't instantiate this class");
-  return (0);
-}
+  v = xmalloc(n*efsz);
+  o = 0;
+  for (;;) {
+    if (!flat) {
+      if (!PySequence_Check(qq) || PySequence_Size(qq) != 2)
+       TYERR("want a sequence of pairs");
+      x = PySequence_GetItem(qq, 0);
+      y = PySequence_GetItem(qq, 1);
+    } else {
+      x = qq; qq = 0;
+      y = PyIter_Next(it);
+      if (!y) {
+       if (PyErr_Occurred()) goto end;
+       VALERR("must have even number of operands");
+      }
+    }
+    if (!x || !y) goto end;
 
-int convulong(PyObject *o, void *pp)
-{
-  long i;
-  unsigned long *p = pp;
-  PyObject *t;
-
-  if (PyInt_Check(o)) {
-    i = PyInt_AS_LONG(o);
-    if (i < 0) TYERR("must be nonnegative");
-    *p = i;
-  } else {
-    if ((t = PyNumber_Long(o)) == 0) goto end;
-    *p = PyLong_AsUnsignedLong(t);
-    Py_DECREF(t);
-    if (PyErr_Occurred()) goto end;
+    if (i >= n) { n *= 2; v = xrealloc(v, n*efsz, i*efsz); }
+    if (fill(v + o, me, x, y)) {
+      if (PyErr_Occurred()) goto end;
+      TYERR("type mismatch");
+    }
+    i++; o += efsz;
+    Py_DECREF(x); x = 0;
+    Py_DECREF(y); y = 0;
+    Py_XDECREF(qq);
+
+    qq = PyIter_Next(it);
+    if (!qq) {
+      if (PyErr_Occurred()) goto end;
+      else break;
+    }
   }
-  return (1);
-end:
-  return (0);
-}
 
-int convu32(PyObject *o, void *pp)
-{
-  unsigned long u;
-  uint32 *p = pp;
+  z = exp(me, v, i);
 
-  if (!convulong(o, &u)) goto end;
-  if (u > 0xffffffff) TYERR("out of range");
-  *p = u;
-  return (1);
 end:
-  return (0);
+  while (i--) { o -= efsz; drop(v + o); }
+  xfree(v);
+  Py_XDECREF(it); Py_XDECREF(qq); Py_XDECREF(x); Py_XDECREF(y);
+  return (z);
 }
 
-int convuint(PyObject *o, void *pp)
+int convmpw(PyObject *o, void *pp)
 {
   unsigned long u;
   unsigned *p = pp;
 
   if (!convulong(o, &u)) goto end;
-  if (u > UINT_MAX) TYERR("out of range");
+  if (u > MPW_MAX) VALERR("out of range");
   *p = u;
   return (1);
 end:
   return (0);
 }
 
-int convmpw(PyObject *o, void *pp)
-{
-  unsigned long u;
-  unsigned *p = pp;
+static PyTypeObject *thingtab_pytype;
 
-  if (!convulong(o, &u)) goto end;
-  if (u > MPW_MAX) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
+typedef struct thingentry {
+  sym_base _b;
+  PyObject *val;
+} thingentry;
+#define THING_VAL(x) (((thingentry *)(x))->val)
+
+typedef struct thingtab_pyobj {
+  GMAP_PYOBJ_HEAD
+  sym_table t;
+} thingtab_pyobj;
+#define THINGTAB_T(x) (&((thingtab_pyobj *)(x))->t)
 
-int convszt(PyObject *o, void *pp)
+static void *thingtab_gmlookup(PyObject *me, PyObject *key, unsigned *f)
 {
-  unsigned long u;
-  size_t *p = pp;
+  const char *p;
 
-  if (!convulong(o, &u)) goto end;
-  if (u > ~(size_t)0) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
+  p = TEXT_STR(key); if (!p) return (0);
+  return (sym_find(THINGTAB_T(me), p, -1, 0, f));
 }
 
-int convbool(PyObject *o, void *pp)
+static void thingtab_gmiterinit(PyObject *me, void *i)
+  { sym_mkiter(i, THINGTAB_T(me)); }
+
+static void *thingtab_gmiternext(PyObject *me, void *i)
+  { sym_iter *it = i; void *e; SYM_NEXT(it, e); return (e); }
+
+static PyObject *thingtab_gmentrykey(PyObject *me, void *e)
+  { return (TEXT_FROMSTR(SYM_NAME(e))); }
+
+static PyObject *thingtab_gmentryvalue(PyObject *me, void *e)
+  { PyObject *rc = THING_VAL(e); RETURN_OBJ(rc); }
+
+static const gmap_ops thingtab_gmops = {
+  sizeof(sym_iter),
+  thingtab_gmlookup,
+  thingtab_gmiterinit,
+  thingtab_gmiternext,
+  thingtab_gmentrykey,
+  thingtab_gmentryvalue
+};
+
+static Py_ssize_t thing_pysize(PyObject *me)
+  { return gmap_pysize_from_sym(THINGTAB_T(me)); }
+
+static const PyMappingMethods thingtab_pymapping = {
+  thing_pysize,
+  gmap_pylookup,
+  0
+};
+
+static thingtab_pyobj *make_thingtab(void)
 {
-  *(int *)pp = PyObject_IsTrue(o);
-  return (1);
+  thingtab_pyobj *map = PyObject_NEW(thingtab_pyobj, thingtab_pytype);
+
+  map->gmops = &thingtab_gmops;
+  sym_create(&map->t);
+  return (map);
 }
 
-PyObject *mexp_common(PyObject *me, PyObject *arg,
-                     size_t efsz,
-                     PyObject *(*id)(PyObject *),
-                     int (*fill)(void *, PyObject *,
-                                 PyObject *, PyObject *),
-                     PyObject *(*exp)(PyObject *, void *, int),
-                     void (*drop)(void *))
+PyObject *make_algtab(const void *tab, size_t esz,
+                     const char *(*namefn)(const void *),
+                     PyObject *(*valfn)(const void *))
 {
-  int i = 0, j, n, flat;
-  PyObject *qq, *x, *y, *z = 0;
-  char *v = 0, *vv;
-
-  if (PyTuple_Size(arg) == 1)
-    arg = PyTuple_GetItem(arg, 0);
-  Py_INCREF(arg);
-  if (!PySequence_Check(arg)) TYERR("not a sequence");
-  n = PySequence_Size(arg); if (!n) { z = id(me); goto end; }
-  x = PySequence_GetItem(arg, 0);
-  if (PySequence_Check(x))
-    flat = 0;
-  else {
-    if (n % 2) VALERR("must have even number of arguments");
-    n /= 2;
-    flat = 1;
+  thingtab_pyobj *map = make_thingtab();
+  const char *p = tab;
+  const char *name;
+  thingentry *e;
+  unsigned f;
+
+  for (;;) {
+    name = namefn(p); if (!name) break;
+    e = sym_find(&map->t, name, -1, sizeof(*e), &f); assert(!f);
+    e->val = valfn(p);
+    p += esz;
   }
-  Py_DECREF(x);
+  return ((PyObject *)map);
+}
 
-  v = xmalloc(n * efsz);
-  for (i = j = 0, vv = v; i < n; i++, vv += efsz) {
-    if (flat) {
-      x = PySequence_GetItem(arg, j++);
-      y = PySequence_GetItem(arg, j++);
-    } else {
-      qq = PySequence_GetItem(arg, j++);
-      if (!qq) goto end;
-      if (!PySequence_Check(qq) || PySequence_Size(qq) != 2) {
-       Py_DECREF(qq);
-       TYERR("want a sequence of pairs");
-      }
-      x = PySequence_GetItem(qq, 0);
-      y = PySequence_GetItem(qq, 1);
-      Py_DECREF(qq);
-    }
-    if (!x || !y) goto end;
-    if (fill(vv, me, x, y)) {
-      Py_DECREF(x);
-      Py_DECREF(y);
-      if (!PyErr_Occurred())
-       PyErr_SetString(PyExc_TypeError, "type mismatch");
-      goto end;
+PyObject *make_grouptab(const void *tab, size_t esz,
+                       const char *(*namefn)(const void *),
+                       int (*ixfn)(const void *), PyObject *(*valfn)(int))
+{
+  thingtab_pyobj *map = make_thingtab();
+  struct { const char *name; int ix; } *ixtab = 0;
+  PyObject **valtab, **vv;
+  size_t i = 0, n = 0;
+  const char *p = tab;
+  const char *name;
+  thingentry *e;
+  unsigned f;
+
+  for (;;) {
+    name = namefn(p); if (!name) break;
+    if (i >= n) {
+      if (!n) n = 16;
+      else n *= 2;
+      ixtab = xrealloc(ixtab, n*sizeof(*ixtab), i*sizeof(*ixtab));
     }
-    Py_DECREF(x);
-    Py_DECREF(y);
+    ixtab[i].name = name; ixtab[i].ix = ixfn(p); assert(ixtab[i].ix >= 0);
+    p += esz; i++;
   }
-  z = exp(me, v, n);
+  n = i;
 
-end:
-  if (v) {
-    for (j = 0, vv = v; j < i; j++, vv += efsz)
-      drop(vv);
-    xfree(v);
-  }
-  Py_DECREF(arg);
-  return (z);
-}
+  valtab = xmalloc(n*sizeof(*valtab));
+  for (i = 0; i < n; i++) valtab[i] = 0;
 
-DA_DECL(method_v, PyMethodDef);
-static method_v global_pymethods = DA_INIT;
-void addmethods(const PyMethodDef *m)
-{
-  size_t n;
+  for (i = 0; i < n; i++) {
+    e = sym_find(&map->t, ixtab[i].name, -1, sizeof(*e), &f); assert(!f);
+    vv = &valtab[ixtab[i].ix];
+    if (*vv) Py_INCREF(*vv);
+    else *vv = valfn(ixtab[i].ix);
+    e->val = *vv;
+  }
 
-  for (n = 0; m[n].ml_name; n++);
-  DA_ENSURE(&global_pymethods, n);
-  memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods),
-        m, n * sizeof(*m));
-  DA_EXTEND(&global_pymethods, n);
+  xfree(ixtab); xfree(valtab);
+  return ((PyObject *)map);
 }
 
-static const PyTypeObject emptytype = { 0 };
-
-void *newtype(PyTypeObject *metaty, const PyTypeObject *skel)
-{
-  PyTypeObject *ty =
-#if PY_VERSION_HEX < 0x02030000
-    (PyTypeObject *)_PyObject_GC_Malloc(metaty, 0);
-#else
-    (PyTypeObject *)_PyObject_GC_Malloc(_PyObject_VAR_SIZE(metaty, 0));
-#endif
-  if (!skel) skel = &emptytype;
-  memcpy(ty, skel, sizeof(*skel));
-  if (ty->tp_base) Py_INCREF(ty->tp_base);
-  PyObject_INIT(ty, metaty);
-  Py_INCREF(metaty);
-  return (ty);
-}
+static const PyTypeObject thingtab_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "_MiscTable",                                /* @tp_name@ */
+  sizeof(thingtab_pyobj),              /* @tp_basicsize@ */
+  0,                                   /* @tp_itemsize@ */
+
+  0,                                   /* @tp_dealloc@ */
+  0,                                   /* @tp_print@ */
+  0,                                   /* @tp_getattr@ */
+  0,                                   /* @tp_setattr@ */
+  0,                                   /* @tp_compare@ */
+  0,                                   /* @tp_repr@ */
+  0,                                   /* @tp_as_number@ */
+  PYSEQUENCE(gmap),                    /* @tp_as_sequence@ */
+  PYMAPPING(thingtab),                 /* @tp_as_mapping@ */
+  0,                                   /* @tp_hash@ */
+  0,                                   /* @tp_call@ */
+  0,                                   /* @tp_str@ */
+  0,                                   /* @tp_getattro@ */
+  0,                                   /* @tp_setattro@ */
+  0,                                   /* @tp_as_buffer@ */
+  Py_TPFLAGS_DEFAULT |                 /* @tp_flags@ */
+    Py_TPFLAGS_BASETYPE,
+
+  /* @tp_doc@ */
+  "Class for tables of algorithms and abstract-group data.\n"
+  "  Not instantiable by users.",
+
+  0,                                   /* @tp_traverse@ */
+  0,                                   /* @tp_clear@ */
+  0,                                   /* @tp_richcompare@ */
+  0,                                   /* @tp_weaklistoffset@ */
+  gmap_pyiter,                         /* @tp_iter@ */
+  0,                                   /* @tp_iternext@ */
+  PYMETHODS(gmapro),                   /* @tp_methods@ */
+  0,                                   /* @tp_members@ */
+  0,                                   /* @tp_getset@ */
+  0,                                   /* @tp_base@ */
+  0,                                   /* @tp_dict@ */
+  0,                                   /* @tp_descr_get@ */
+  0,                                   /* @tp_descr_set@ */
+  0,                                   /* @tp_dictoffset@ */
+  0,                                   /* @tp_init@ */
+  PyType_GenericAlloc,                 /* @tp_alloc@ */
+  abstract_pynew,                      /* @tp_new@ */
+  0,                                   /* @tp_free@ */
+  0                                    /* @tp_is_gc@ */
+};
 
 static PyObject *smallprimes(void)
 {
@@ -258,27 +292,78 @@ static PyObject *smallprimes(void)
   int i;
 
   for (i = 0; i < NPRIME; i++)
-    PyList_SetItem(v, i, PyInt_FromLong(primetab[i]));
+    PyList_SET_ITEM(v, i, PyInt_FromLong(primetab[i]));
   return (v);
 }
 
-PyTypeObject *inittype(PyTypeObject *tyskel)
+static PyObject *meth__ego(PyObject *me, PyObject *arg)
 {
-  PyTypeObject *ty = newtype(&PyType_Type, tyskel);
-  ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
-  PyType_Ready(ty);
-  return (ty);
+  char *argv0;
+  if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
+    return (0);
+  if (STRCMP(QUIS, ==, "<UNNAMED>"))
+    ego(argv0);
+  RETURN_NONE;
 }
 
-void init_base(void) {
-  static const PyMethodDef mzero = { 0 };
+static const PyMethodDef methods[] = {
+#define METHNAME(func) meth_##func
+  METH (_ego,          "_ego(ARGV0)")
+#undef METHNAME
+  { 0 }
+};
+
+static void init_random(void)
+{
+#if PY_VERSION_HEX >= 0x02060000
+  char *seed;
+  uint32 r;
+
+  if (!Py_HashRandomizationFlag) return;
+  seed = getenv("PYTHONHASHSEED");
+  if (!seed || STRCMP(seed, ==, "random")) r = GR_WORD(&rand_global);
+  else r = strtoul(seed, 0, 0);
+  if (!r) r = 0xe011f220; /* zero doesn't work well */
+  unihash_setkey(&unihash_global, r);
+#endif
+}
+
+#ifdef PY3
+static PyModuleDef moddef = {
+  PyModuleDef_HEAD_INIT,
+  "catacomb._base",                    /* @m_name@ */
+  "Low-level module for Catacomb bindings.  Use `catacomb' instead.",
+                                       /* @m_doc@ */
+  0,                                   /* @m_size@ */
+  0,                                   /* @m_methods@ */
+  0,                                   /* @m_slots@ */
+  0,                                   /* @m_traverse@ */
+  0,                                   /* @m_clear@ */
+  0                                    /* @m_free@ */
+};
+#endif
+
+EXPORT PyMODINIT_FUNC PY23(init_base, PyInit__base)(void)
+{
   PyObject *mod;
+
+  modname = TEXT_FROMSTR("catacomb");
+  addmethods(methods);
   INIT_MODULES;
-  DA_PUSH(&global_pymethods, mzero);
-  mod = Py_InitModule("catacomb._base", DA(&global_pymethods));
+  INITTYPE(thingtab, root);
+  init_random();
+#ifdef PY3
+  moddef.m_methods = donemethods();
+  mod = PyModule_Create(&moddef);
+#else
+  mod = Py_InitModule("catacomb._base", donemethods());
+#endif
   INSERT_MODULES;
+  INSERT("_MiscTable", thingtab_pytype);
   INSERT("smallprimes", smallprimes());
-  setconstants(mod);
+#ifdef PY3
+  return (mod);
+#endif
 }
 
 /*----- That's all, folks -------------------------------------------------*/