diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2019-03-06 14:56:10 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2019-03-06 14:56:10 +0200 |
commit | 078349c43f56f857aad4a8792ff04d60ec19343a (patch) | |
tree | 68434d79089cc0980284a3051c4bd7781be7f8c9 /src | |
parent | Fixed failing build if manpage is disabled (diff) | |
download | smolbote-078349c43f56f857aad4a8792ff04d60ec19343a.tar.xz |
repl: import python module
Diffstat (limited to 'src')
-rw-r--r-- | src/cli/cli.cpp | 45 |
1 files changed, 43 insertions, 2 deletions
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 <stdio.h> #include <stdlib.h> -//#include <string.h> #include <linenoise.h> #include <Python.h> +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; } + |