]> git.proxmox.com Git - mirror_frr.git/blob - lib/command_py.c
Merge pull request #701 from qlyoung/mt-safe-cancel
[mirror_frr.git] / lib / command_py.c
1 /*
2 * clippy (CLI preparator in python) wrapper for FRR command_graph
3 * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /* note: this wrapper is intended to be used as build-time helper. while
21 * it should be generally correct and proper, there may be the occasional
22 * memory leak or SEGV for things that haven't been well-tested.
23 */
24
25 #include <Python.h>
26 #include "structmember.h"
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "command_graph.h"
31 #include "clippy.h"
32
33 struct wrap_graph;
34 static PyObject *graph_to_pyobj(struct wrap_graph *graph, struct graph_node *gn);
35
36 /*
37 * nodes are wrapped as follows:
38 * - instances can only be acquired from a graph
39 * - the same node will return the same wrapper object (they're buffered
40 * through "idx")
41 * - a reference is held onto the graph
42 * - fields are copied for easy access with PyMemberDef
43 */
44 struct wrap_graph_node {
45 PyObject_HEAD
46
47 bool allowrepeat;
48 const char *type;
49
50 bool deprecated;
51 bool hidden;
52 const char *text;
53 const char *desc;
54 const char *varname;
55 long long min, max;
56
57 struct graph_node *node;
58 struct wrap_graph *wgraph;
59 size_t idx;
60 };
61
62 /*
63 * graphs are wrapped as follows:
64 * - they can only be created by parsing a definition string
65 * - there's a table here for the wrapped nodes (nodewrappers), indexed
66 * by "idx" (corresponds to node's position in graph's table of nodes)
67 * - graphs do NOT hold references to nodes (would be circular)
68 */
69 struct wrap_graph {
70 PyObject_HEAD
71
72 char *definition;
73 struct graph *graph;
74 struct wrap_graph_node **nodewrappers;
75 };
76
77 static PyObject *refuse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
78 {
79 PyErr_SetString(PyExc_ValueError, "cannot create instances of this type");
80 return NULL;
81 }
82
83 #define member(name, type) {(char *)#name, type, offsetof(struct wrap_graph_node, name), READONLY, \
84 (char *)#name " (" #type ")"}
85 static PyMemberDef members_graph_node[] = {
86 member(allowrepeat, T_BOOL),
87 member(type, T_STRING),
88 member(deprecated, T_BOOL),
89 member(hidden, T_BOOL),
90 member(text, T_STRING),
91 member(desc, T_STRING),
92 member(min, T_LONGLONG),
93 member(max, T_LONGLONG),
94 member(varname, T_STRING),
95 {},
96 };
97 #undef member
98
99 /*
100 * node.next() -- returns list of all "next" nodes.
101 * this will include circles if the graph has them.
102 */
103 static PyObject *graph_node_next(PyObject *self, PyObject *args)
104 {
105 struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
106 PyObject *pylist;
107
108 if (wrap->node->data
109 && ((struct cmd_token *)wrap->node->data)->type == END_TKN)
110 return PyList_New(0);
111 pylist = PyList_New(vector_active(wrap->node->to));
112 for (size_t i = 0; i < vector_active(wrap->node->to); i++) {
113 struct graph_node *gn = vector_slot(wrap->node->to, i);
114 PyList_SetItem(pylist, i, graph_to_pyobj(wrap->wgraph, gn));
115 }
116 return pylist;
117 };
118
119 /*
120 * node.join() -- return FORK's JOIN node or None
121 */
122 static PyObject *graph_node_join(PyObject *self, PyObject *args)
123 {
124 struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
125
126 if (!wrap->node->data
127 || ((struct cmd_token *)wrap->node->data)->type == END_TKN)
128 Py_RETURN_NONE;
129
130 struct cmd_token *tok = wrap->node->data;
131 if (tok->type != FORK_TKN)
132 Py_RETURN_NONE;
133
134 return graph_to_pyobj(wrap->wgraph, tok->forkjoin);
135 };
136
137 static PyMethodDef methods_graph_node[] = {
138 {"next", graph_node_next, METH_NOARGS, "outbound graph edge list"},
139 {"join", graph_node_join, METH_NOARGS, "outbound join node"},
140 {}
141 };
142
143 static void graph_node_wrap_free(void *arg)
144 {
145 struct wrap_graph_node *wrap = arg;
146 wrap->wgraph->nodewrappers[wrap->idx] = NULL;
147 Py_DECREF(wrap->wgraph);
148 }
149
150 static PyTypeObject typeobj_graph_node = {
151 PyVarObject_HEAD_INIT(NULL, 0)
152 .tp_name = "_clippy.GraphNode",
153 .tp_basicsize = sizeof(struct wrap_graph_node),
154 .tp_flags = Py_TPFLAGS_DEFAULT,
155 .tp_doc = "struct graph_node *",
156 .tp_new = refuse_new,
157 .tp_free = graph_node_wrap_free,
158 .tp_members = members_graph_node,
159 .tp_methods = methods_graph_node,
160 };
161
162 static PyObject *graph_to_pyobj(struct wrap_graph *wgraph, struct graph_node *gn)
163 {
164 struct wrap_graph_node *wrap;
165 size_t i;
166
167 for (i = 0; i < vector_active(wgraph->graph->nodes); i++)
168 if (vector_slot(wgraph->graph->nodes, i) == gn)
169 break;
170 if (i == vector_active(wgraph->graph->nodes)) {
171 PyErr_SetString(PyExc_ValueError, "cannot find node in graph");
172 return NULL;
173 }
174 if (wgraph->nodewrappers[i]) {
175 PyObject *obj = (PyObject *)wgraph->nodewrappers[i];
176 Py_INCREF(obj);
177 return obj;
178 }
179
180 wrap = (struct wrap_graph_node *)typeobj_graph_node.tp_alloc(&typeobj_graph_node, 0);
181 if (!wrap)
182 return NULL;
183 wgraph->nodewrappers[i] = wrap;
184 Py_INCREF(wgraph);
185
186 wrap->idx = i;
187 wrap->wgraph = wgraph;
188 wrap->node = gn;
189 wrap->type = "NULL";
190 wrap->allowrepeat = false;
191 if (gn->data) {
192 struct cmd_token *tok = gn->data;
193 switch (tok->type) {
194 #define item(x) case x: wrap->type = #x; break;
195 item(WORD_TKN) // words
196 item(VARIABLE_TKN) // almost anything
197 item(RANGE_TKN) // integer range
198 item(IPV4_TKN) // IPV4 addresses
199 item(IPV4_PREFIX_TKN) // IPV4 network prefixes
200 item(IPV6_TKN) // IPV6 prefixes
201 item(IPV6_PREFIX_TKN) // IPV6 network prefixes
202
203 /* plumbing types */
204 item(FORK_TKN)
205 item(JOIN_TKN)
206 item(START_TKN)
207 item(END_TKN)
208 default:
209 wrap->type = "???";
210 }
211
212 wrap->deprecated = (tok->attr == CMD_ATTR_DEPRECATED);
213 wrap->hidden = (tok->attr == CMD_ATTR_HIDDEN);
214 wrap->text = tok->text;
215 wrap->desc = tok->desc;
216 wrap->varname = tok->varname;
217 wrap->min = tok->min;
218 wrap->max = tok->max;
219 wrap->allowrepeat = tok->allowrepeat;
220 }
221
222 return (PyObject *)wrap;
223 }
224
225 #define member(name, type) {(char *)#name, type, offsetof(struct wrap_graph, name), READONLY, \
226 (char *)#name " (" #type ")"}
227 static PyMemberDef members_graph[] = {
228 member(definition, T_STRING),
229 {},
230 };
231 #undef member
232
233 /* graph.first() - root node */
234 static PyObject *graph_first(PyObject *self, PyObject *args)
235 {
236 struct wrap_graph *gwrap = (struct wrap_graph *)self;
237 struct graph_node *gn = vector_slot(gwrap->graph->nodes, 0);
238 return graph_to_pyobj(gwrap, gn);
239 };
240
241 static PyMethodDef methods_graph[] = {
242 {"first", graph_first, METH_NOARGS, "first graph node"},
243 {}
244 };
245
246 static PyObject *graph_parse(PyTypeObject *type, PyObject *args, PyObject *kwds);
247
248 static void graph_wrap_free(void *arg)
249 {
250 struct wrap_graph *wgraph = arg;
251 free(wgraph->nodewrappers);
252 free(wgraph->definition);
253 }
254
255 static PyTypeObject typeobj_graph = {
256 PyVarObject_HEAD_INIT(NULL, 0)
257 .tp_name = "_clippy.Graph",
258 .tp_basicsize = sizeof(struct wrap_graph),
259 .tp_flags = Py_TPFLAGS_DEFAULT,
260 .tp_doc = "struct graph *",
261 .tp_new = graph_parse,
262 .tp_free = graph_wrap_free,
263 .tp_members = members_graph,
264 .tp_methods = methods_graph,
265 };
266
267 /* top call / entrypoint for python code */
268 static PyObject *graph_parse(PyTypeObject *type, PyObject *args, PyObject *kwds)
269 {
270 const char *def, *doc = NULL;
271 struct wrap_graph *gwrap;
272 static const char *kwnames[] = { "cmddef", "doc", NULL };
273
274 gwrap = (struct wrap_graph *)typeobj_graph.tp_alloc(&typeobj_graph, 0);
275 if (!gwrap)
276 return NULL;
277
278 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", (char **)kwnames, &def, &doc))
279 return NULL;
280
281 struct graph *graph = graph_new ();
282 struct cmd_token *token = cmd_token_new (START_TKN, 0, NULL, NULL);
283 graph_new_node (graph, token, (void (*)(void *)) &cmd_token_del);
284
285 struct cmd_element cmd = { .string = def, .doc = doc };
286 cmd_graph_parse (graph, &cmd);
287 cmd_graph_names (graph);
288
289 gwrap->graph = graph;
290 gwrap->definition = strdup(def);
291 gwrap->nodewrappers = calloc(vector_active(graph->nodes),
292 sizeof (gwrap->nodewrappers[0]));
293 return (PyObject *)gwrap;
294 }
295
296 static PyMethodDef clippy_methods[] = {
297 {"parse", clippy_parse, METH_VARARGS, "Parse a C file"},
298 {NULL, NULL, 0, NULL}
299 };
300
301 #if PY_MAJOR_VERSION >= 3
302 static struct PyModuleDef pymoddef_clippy = {
303 PyModuleDef_HEAD_INIT,
304 "_clippy",
305 NULL, /* docstring */
306 -1,
307 clippy_methods,
308 };
309 #define modcreate() PyModule_Create(&pymoddef_clippy)
310 #define initret(val) return val;
311 #else
312 #define modcreate() Py_InitModule("_clippy", clippy_methods)
313 #define initret(val) do { \
314 if (!val) Py_FatalError("initialization failure"); \
315 return; } while (0)
316 #endif
317
318 PyMODINIT_FUNC command_py_init(void)
319 {
320 PyObject* pymod;
321
322 if (PyType_Ready(&typeobj_graph_node) < 0)
323 initret(NULL);
324 if (PyType_Ready(&typeobj_graph) < 0)
325 initret(NULL);
326
327 pymod = modcreate();
328 if (!pymod)
329 initret(NULL);
330
331 Py_INCREF(&typeobj_graph_node);
332 PyModule_AddObject(pymod, "GraphNode", (PyObject *)&typeobj_graph_node);
333 Py_INCREF(&typeobj_graph);
334 PyModule_AddObject(pymod, "Graph", (PyObject *)&typeobj_graph);
335 initret(pymod);
336 }