aboutsummaryrefslogtreecommitdiff
path: root/src/cli/cli.cpp
blob: 322c3659462f5a11c05132e01f08a3b60da674ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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;
}