From 078349c43f56f857aad4a8792ff04d60ec19343a Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 6 Mar 2019 14:56:10 +0200 Subject: repl: import python module --- src/cli/cli.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index ae3795e..322c365 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -1,9 +1,47 @@ #include #include -//#include #include #include +static int numargs=0; + +/* Return the number of arguments of the application command line */ +static PyObject* emb_numargs(PyObject *self, PyObject *args) +{ + if(!PyArg_ParseTuple(args, ":numargs")) + return NULL; + + numargs = 19; + return PyLong_FromLong(numargs); +} + +static PyMethodDef EmbMethods[] = { + /* ml_name Name of the method + * ml_meth pointer to C implementation + * ml_flags flag bits indicating how it should be called + * ml_doc docstring + */ + {"numargs", emb_numargs, METH_VARARGS, "Return the number of arguments received by the process."}, + {NULL, NULL, 0, NULL} +}; + +static PyModuleDef EmbModule = { + /* m_base */ PyModuleDef_HEAD_INIT, // base module, always HEAD_INIT + /* m_name */ "emb", // module name + /* m_doc */ NULL, // Docstring for the module; usually a docstring variable created with PyDoc_STRVAR() + /* m_size */ -1, // + /* m_methods */ EmbMethods, // A pointer to a table of module-level functions + /* m_slots */ NULL, // An array of slot definitions for multi-phase initialization + /* traverse */ NULL, // A traversal function to call during GC traversal of the module object + /* clear */ NULL, // A clear function to call during GC clearing of the module object + /* free */ NULL // A function to call during deallocation of the module object +}; + +static PyObject* PyInit_emb(void) +{ + return PyModule_Create(&EmbModule); +} + int main(int argc, char** argv) { printf("cli test application\n"); @@ -17,6 +55,7 @@ int main(int argc, char** argv) // inform the interpreter about paths to run-time libraries Py_SetProgramName(program); /* optional but recommended */ + printf("import emb: %i\n", PyImport_AppendInittab("emb", &PyInit_emb)); // Initialize the python interpreter Py_Initialize(); @@ -29,13 +68,14 @@ int main(int argc, char** argv) char *cmd = linenoise(prompt); if(cmd == nullptr || *cmd == '\0') { - printf("break\n"); + printf("breaking out of repl\n"); free(cmd); break; } //printf("echo(%i):'%s'\n", strlen(cmd), cmd); PyRun_SimpleString(cmd); + linenoiseHistoryAdd(cmd); free(cmd); } @@ -49,3 +89,4 @@ int main(int argc, char** argv) return 0; } + -- cgit v1.2.1