1 | /* |
---|
2 | * neercs console-based window manager |
---|
3 | * Copyright (c) 2009 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: interpreter.c 3996 2009-11-22 12:41:45Z jylam $ |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | #include "config.h" |
---|
16 | |
---|
17 | #ifdef USE_PYTHON |
---|
18 | |
---|
19 | #include <Python.h> |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <string.h> |
---|
24 | #include <unistd.h> |
---|
25 | |
---|
26 | #include <fcntl.h> |
---|
27 | #include <signal.h> |
---|
28 | #include <sys/ioctl.h> |
---|
29 | #include <sys/socket.h> |
---|
30 | #include <sys/time.h> |
---|
31 | #include <time.h> |
---|
32 | #include <sys/wait.h> |
---|
33 | #include <sys/types.h> |
---|
34 | |
---|
35 | #include <caca.h> |
---|
36 | |
---|
37 | #include "neercs.h" |
---|
38 | #include "py_module.h" |
---|
39 | |
---|
40 | static int python_execute(struct screen_list *sl); |
---|
41 | char *getStringFromPyObject(PyObject * p); |
---|
42 | static char *getPythonError(void); |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | int python_command_handle_key(struct screen_list *screen_list, unsigned int c) |
---|
47 | { |
---|
48 | int ret = widget_ibox_handle_key(screen_list->interpreter_props.box, c); |
---|
49 | |
---|
50 | if (ret == INPUT_BOX_ESC) |
---|
51 | { |
---|
52 | widget_ibox_destroy(screen_list->interpreter_props.box); |
---|
53 | screen_list->interpreter_props.box = NULL; |
---|
54 | screen_list->modals.python_command = 0; |
---|
55 | } |
---|
56 | else if (ret == INPUT_BOX_RET) |
---|
57 | { |
---|
58 | python_execute(screen_list); |
---|
59 | } |
---|
60 | screen_list->changed = 1; |
---|
61 | |
---|
62 | return 1; |
---|
63 | } |
---|
64 | |
---|
65 | void draw_python_command(struct screen_list *screen_list) |
---|
66 | { |
---|
67 | if (!screen_list->interpreter_props.box) |
---|
68 | { |
---|
69 | screen_list->interpreter_props.box = |
---|
70 | widget_ibox_init(screen_list->cv, 65, 6); |
---|
71 | debug("py Init %p\n", screen_list->interpreter_props.box); |
---|
72 | } |
---|
73 | debug("py Drawing !\n"); |
---|
74 | widget_ibox_draw(screen_list->interpreter_props.box); |
---|
75 | } |
---|
76 | |
---|
77 | /* Actual Python interpreter stuff */ |
---|
78 | int python_init(struct screen_list *sl) |
---|
79 | { |
---|
80 | Py_Initialize(); |
---|
81 | initNeercsModule(sl); |
---|
82 | |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|
86 | int python_close(struct screen_list *sl) |
---|
87 | { |
---|
88 | widget_ibox_destroy(sl->interpreter_props.box); |
---|
89 | sl->interpreter_props.box = NULL; |
---|
90 | |
---|
91 | Py_Finalize(); |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | static int python_execute(struct screen_list *sl) |
---|
97 | { |
---|
98 | char *command = widget_ibox_get_text(sl->interpreter_props.box); |
---|
99 | if (!command || strlen(command) < 1) |
---|
100 | return -1; |
---|
101 | int err = 0; |
---|
102 | |
---|
103 | debug("py Executing '%s'\n", command); |
---|
104 | |
---|
105 | |
---|
106 | PyObject *pModule, *pName, *pFunc; |
---|
107 | |
---|
108 | /* Module from which to call the function */ |
---|
109 | pName = PyUnicode_FromString("neercs"); |
---|
110 | if (!pName) |
---|
111 | { |
---|
112 | widget_ibox_set_error(sl->interpreter_props.box, getPythonError()); |
---|
113 | err = 1; |
---|
114 | debug("py Error 1\n"); |
---|
115 | goto end; |
---|
116 | } |
---|
117 | |
---|
118 | pModule = PyImport_Import(pName); |
---|
119 | Py_DECREF(pName); |
---|
120 | |
---|
121 | if (pModule != NULL) |
---|
122 | { |
---|
123 | PyObject *dictionary = PyModule_GetDict(pModule); |
---|
124 | |
---|
125 | getExportedValues(dictionary); |
---|
126 | |
---|
127 | PyObject *o = |
---|
128 | PyRun_String(command, Py_single_input, |
---|
129 | dictionary, NULL); |
---|
130 | if (!o) |
---|
131 | { |
---|
132 | widget_ibox_set_error(sl->interpreter_props.box, getPythonError()); |
---|
133 | err = 1; |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | setExportedValues(dictionary); |
---|
138 | |
---|
139 | widget_ibox_set_msg(sl->interpreter_props.box, getStringFromPyObject(o)); |
---|
140 | err = 1; |
---|
141 | } |
---|
142 | goto end; |
---|
143 | |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | widget_ibox_set_error(sl->interpreter_props.box, getPythonError()); |
---|
148 | err = 1; |
---|
149 | goto end; |
---|
150 | } |
---|
151 | |
---|
152 | end: |
---|
153 | |
---|
154 | Py_XDECREF(pFunc); |
---|
155 | Py_DECREF(pModule); |
---|
156 | |
---|
157 | if (!err) |
---|
158 | { |
---|
159 | widget_ibox_destroy(sl->interpreter_props.box); |
---|
160 | sl->interpreter_props.box = NULL; |
---|
161 | sl->modals.python_command = 0; |
---|
162 | } |
---|
163 | sl->changed = 1; |
---|
164 | |
---|
165 | return 0; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | static char *getPythonError(void) |
---|
171 | { |
---|
172 | char *err; |
---|
173 | PyObject *type, *value, *traceback; |
---|
174 | PyErr_Fetch(&type, &value, &traceback); |
---|
175 | |
---|
176 | char *evalue = getStringFromPyObject(value); |
---|
177 | |
---|
178 | int r = asprintf(&err, "%s", evalue); |
---|
179 | (void)r; |
---|
180 | return err; |
---|
181 | } |
---|
182 | |
---|
183 | char *getStringFromPyObject(PyObject * p) |
---|
184 | { |
---|
185 | PyObject *str = PyObject_Repr(p); |
---|
186 | char *err = PyBytes_AS_STRING(PyUnicode_AsEncodedString(str, "utf-8", |
---|
187 | "Error ~")); |
---|
188 | |
---|
189 | char *ret = strdup(err); |
---|
190 | return ret; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | #endif |
---|