Bug squashing.
[pyke] / catacomb.c
index acc0e35..65d3985 100644 (file)
 
 /*----- Main code ---------------------------------------------------------*/
 
-static void setconstants(PyObject *mod)
-{
-  static const struct { const char *name; unsigned long value; } consts[] = {
+static const struct nameval 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),
+  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),
+  C(KOPEN_READ), C(KOPEN_WRITE), C(KOPEN_NOFILE),
+  C(KEXP_FOREVER), C(KEXP_EXPIRE),
+  C(KF_ENCMASK), C(KENC_BINARY), C(KENC_MP), C(KENC_STRUCT),
+    C(KENC_ENCRYPT), C(KENC_STRING), C(KENC_EC),
+  C(KF_CATMASK), C(KCAT_SYMM), C(KCAT_PRIV), C(KCAT_PUB), C(KCAT_SHARE),
+  C(KF_NONSECRET),
+  C(KF_BURN), C(KF_OPT),
+#define ENTRY(tag, val, str) C(KERR_##tag),
+  KEY_ERRORS(ENTRY)
+#undef ENTRY
 #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);
-  }
-}
-
-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);
-}
-
-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;
-  }
-  return (1);
-end:
-  return (0);
-}
-
-int convu32(PyObject *o, void *pp)
-{
-  unsigned long u;
-  uint32 *p = pp;
-
-  if (!convulong(o, &u)) goto end;
-  if (u > 0xffffffff) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
-
-int convuint(PyObject *o, void *pp)
-{
-  unsigned long u;
-  unsigned *p = pp;
-
-  if (!convulong(o, &u)) goto end;
-  if (u > UINT_MAX) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
-
-int convmpw(PyObject *o, void *pp)
-{
-  unsigned long u;
-  unsigned *p = pp;
-
-  if (!convulong(o, &u)) goto end;
-  if (u > MPW_MAX) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
-
-int convszt(PyObject *o, void *pp)
-{
-  unsigned long u;
-  size_t *p = pp;
-
-  if (!convulong(o, &u)) goto end;
-  if (u > ~(size_t)0) TYERR("out of range");
-  *p = u;
-  return (1);
-end:
-  return (0);
-}
-
-int convbool(PyObject *o, void *pp)
-{
-  *(int *)pp = PyObject_IsTrue(o);
-  return (1);
-}
+  { 0 }
+};
 
 PyObject *mexp_common(PyObject *me, PyObject *arg,
                      size_t efsz,
@@ -221,37 +119,6 @@ end:
   return (z);
 }
 
-DA_DECL(method_v, PyMethodDef);
-static method_v global_pymethods = DA_INIT;
-void addmethods(const PyMethodDef *m)
-{
-  size_t n;
-
-  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);
-}
-
-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 PyObject *smallprimes(void)
 {
   PyObject *v = PyList_New(NPRIME);
@@ -262,23 +129,32 @@ static PyObject *smallprimes(void)
   return (v);
 }
 
-PyTypeObject *inittype(PyTypeObject *tyskel)
+static PyObject *meth__ego(PyObject *me, PyObject *arg)
+{
+  char *argv0;
+  if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
+    return (0);
+  if (strcmp(QUIS, "<UNNAMED>") == 0)
+    ego(argv0);
+  RETURN_NONE;
+}
+
+static PyMethodDef methods[] = {
+#define METHNAME(func) meth_##func
+  METH (_ego,                  "_ego(ARGV0)")
+#undef METHNAME
+  { 0 }
+};
+
+void init_base(void)
 {
-  PyTypeObject *ty = newtype(&PyType_Type, tyskel);
-  ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
-  PyType_Ready(ty);
-  return (ty);
-}
-
-void init_base(void) {
-  static const PyMethodDef mzero = { 0 };
   PyObject *mod;
+  addmethods(methods);
   INIT_MODULES;
-  DA_PUSH(&global_pymethods, mzero);
-  mod = Py_InitModule("catacomb._base", DA(&global_pymethods));
+  mod = Py_InitModule("catacomb._base", donemethods());
   INSERT_MODULES;
   INSERT("smallprimes", smallprimes());
-  setconstants(mod);
+  setconstants(mod, consts);
 }
 
 /*----- That's all, folks -------------------------------------------------*/