chiark / gitweb /
@@@ mLib-python Pyke wip
[mLib-python] / ui.c
1 /* -*-c-*-
2  *
3  * mLib user interface
4  *
5  * (c) 2019 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 it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation; either version 2 of the License, or (at your
15  * option) any later version.
16  *
17  * mLib/Python is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * 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
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "mLib-python.h"
31
32 /*----- Program name ------------------------------------------------------*/
33
34 static int set_program_name(void)
35 {
36   PyObject *p = TEXT_FROMSTR(pn__name);
37   int rc = -1;
38
39   if (!home_module) SYSERR("home module not set");
40   if (PyObject_SetAttrString(home_module, "quis", p)) goto end;
41   pn__name = TEXT_PTR(p); p = 0; rc = 0;
42 end:
43   Py_XDECREF(p);
44   return (rc);
45 }
46
47 static PyObject *meth_ego(PyObject *me, PyObject *arg)
48 {
49   char *p;
50   const char *old;
51
52   if (!PyArg_ParseTuple(arg, "s:ego", &p)) goto end;
53   old = pn__name; ego(p);
54   if (set_program_name()) { pn__name = old; goto end; }
55   RETURN_NONE;
56 end:
57   return (0);
58 }
59
60 /*----- Error reporting ---------------------------------------------------*/
61
62 static PyObject *meth_moan(PyObject *me, PyObject *arg)
63 {
64   char *p;
65
66   if (!PyArg_ParseTuple(arg, "s:moan", &p)) goto end;
67   moan("%s", p);
68   RETURN_NONE;
69 end:
70   return (0);
71 }
72
73 static PyObject *meth_die(PyObject *me, PyObject *arg, PyObject *kw)
74 {
75   const char *const kwlist[] = { "msg", "rc", 0 };
76   char *p;
77   int rc = 126;
78   PyObject *rcobj = 0;
79
80   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|i:moan", KWLIST, &p, &rc))
81     goto end;
82   rcobj = PyInt_FromLong(rc); if (!rcobj) goto end;
83   moan("%s", p);
84   PyErr_SetObject(PyExc_SystemExit, rcobj);
85 end:
86   Py_XDECREF(rcobj);
87   return (0);
88 }
89
90 /*----- Main code ---------------------------------------------------------*/
91
92 static const PyMethodDef methods[] = {
93 #define METHNAME(name) meth_##name
94   METH  (ego,           "ego(PROG): set program name")
95   METH  (moan,          "moan(MSG): report a warning")
96   KWMETH(die,          "die(MSG, [rc = 126]): report a fatal error and exit")
97 #undef METHNAME
98   { 0 }
99 };
100
101 void ui_pyinit(void)
102 {
103   addmethods(methods);
104 }
105
106 void ui_pyinsert(PyObject *mod)
107 {
108 }
109
110 int ui_pyready(void) { return (set_program_name()); }
111
112 /*----- That's all, folks -------------------------------------------------*/