diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/cli/cli.cpp | 92 | ||||
-rw-r--r-- | test/cli/meson.build | 4 |
2 files changed, 96 insertions, 0 deletions
diff --git a/test/cli/cli.cpp b/test/cli/cli.cpp new file mode 100644 index 0000000..322c365 --- /dev/null +++ b/test/cli/cli.cpp @@ -0,0 +1,92 @@ +#include <stdio.h> +#include <stdlib.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"); + + wchar_t *program = Py_DecodeLocale(argv[0], NULL); + if (program == NULL) { + fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); + exit(1); + } + + // 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(); + + PyRun_SimpleString("print('Python interpreter ready')\n"); + + const char* prompt = "poi> "; + + while(true) { + char *cmd = linenoise(prompt); + + if(cmd == nullptr || *cmd == '\0') { + printf("breaking out of repl\n"); + free(cmd); + break; + } + + //printf("echo(%i):'%s'\n", strlen(cmd), cmd); + PyRun_SimpleString(cmd); + linenoiseHistoryAdd(cmd); + free(cmd); + } + + // finalize the interpreter + if (Py_FinalizeEx() < 0) { + exit(120); + } + + // + PyMem_RawFree(program); + + return 0; +} + diff --git a/test/cli/meson.build b/test/cli/meson.build new file mode 100644 index 0000000..298e1db --- /dev/null +++ b/test/cli/meson.build @@ -0,0 +1,4 @@ +cli_demo = executable('cli', install: false, dependencies: [ optional_deps ], + sources: [ 'cli.cpp' ] +) + |