aboutsummaryrefslogtreecommitdiff
path: root/src/cli/cli.cpp
blob: ae3795edfd510f2e608a23576f9bedf870b8c2ad (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
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
#include <linenoise.h>
#include <Python.h>

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 */


    // 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("break\n");
            free(cmd);
            break;
        }

        //printf("echo(%i):'%s'\n", strlen(cmd), cmd);
        PyRun_SimpleString(cmd);
        free(cmd);
    }

    // finalize the interpreter
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }

    //
    PyMem_RawFree(program);

    return 0;
}