Browse Source

repl: import python module

Aqua-sama 2 months ago
parent
commit
078349c43f
Signed by: Aqua-sama <aqua@iserlohn-fortress.net> GPG Key ID: 5378B8349C1D5ADA
1 changed files with 43 additions and 2 deletions
  1. 43
    2
      src/cli/cli.cpp

+ 43
- 2
src/cli/cli.cpp View File

@@ -1,9 +1,47 @@
1 1
 #include <stdio.h>
2 2
 #include <stdlib.h>
3
-//#include <string.h>
4 3
 #include <linenoise.h>
5 4
 #include <Python.h>
6 5
 
6
+static int numargs=0;
7
+
8
+/* Return the number of arguments of the application command line */
9
+static PyObject* emb_numargs(PyObject *self, PyObject *args)
10
+{
11
+    if(!PyArg_ParseTuple(args, ":numargs"))
12
+        return NULL;
13
+
14
+    numargs = 19;
15
+    return PyLong_FromLong(numargs);
16
+}
17
+
18
+static PyMethodDef EmbMethods[] = {
19
+    /* ml_name  Name of the method
20
+     * ml_meth  pointer to C implementation
21
+     * ml_flags flag bits indicating how it should be called
22
+     * ml_doc   docstring
23
+     */
24
+    {"numargs", emb_numargs, METH_VARARGS, "Return the number of arguments received by the process."},
25
+    {NULL, NULL, 0, NULL}
26
+};
27
+
28
+static PyModuleDef EmbModule = {
29
+    /* m_base */ PyModuleDef_HEAD_INIT, // base module, always HEAD_INIT
30
+    /* m_name */ "emb", // module name
31
+    /* m_doc  */ NULL,  // Docstring for the module; usually a docstring variable created with PyDoc_STRVAR()
32
+    /* m_size */ -1, //
33
+    /* m_methods */ EmbMethods, // A pointer to a table of module-level functions
34
+    /* m_slots */   NULL,       // An array of slot definitions for multi-phase initialization
35
+    /* traverse */ NULL, // A traversal function to call during GC traversal of the module object
36
+    /* clear    */ NULL, // A clear function to call during GC clearing of the module object
37
+    /* free     */ NULL  // A function to call during deallocation of the module object
38
+};
39
+
40
+static PyObject* PyInit_emb(void)
41
+{
42
+    return PyModule_Create(&EmbModule);
43
+}
44
+
7 45
 int main(int argc, char** argv)
8 46
 {
9 47
     printf("cli test application\n");
@@ -17,6 +55,7 @@ int main(int argc, char** argv)
17 55
     // inform the interpreter about paths to run-time libraries
18 56
     Py_SetProgramName(program);  /* optional but recommended */
19 57
 
58
+    printf("import emb: %i\n", PyImport_AppendInittab("emb", &PyInit_emb));
20 59
 
21 60
     // Initialize the python interpreter
22 61
     Py_Initialize();
@@ -29,13 +68,14 @@ int main(int argc, char** argv)
29 68
         char *cmd = linenoise(prompt);
30 69
 
31 70
         if(cmd == nullptr || *cmd == '\0') {
32
-            printf("break\n");
71
+            printf("breaking out of repl\n");
33 72
             free(cmd);
34 73
             break;
35 74
         }
36 75
 
37 76
         //printf("echo(%i):'%s'\n", strlen(cmd), cmd);
38 77
         PyRun_SimpleString(cmd);
78
+        linenoiseHistoryAdd(cmd);
39 79
         free(cmd);
40 80
     }
41 81
 
@@ -49,3 +89,4 @@ int main(int argc, char** argv)
49 89
 
50 90
     return 0;
51 91
 }
92
+

Loading…
Cancel
Save