]>
Commit | Line | Data |
---|---|---|
29ad6f68 DL |
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 | ||
2618a52e DL |
25 | /* This file is "exempt" from having |
26 | #include "config.h" | |
27 | * as the first include statement because Python.h also does environment | |
28 | * setup & these trample over each other. | |
29 | */ | |
30 | ||
29ad6f68 DL |
31 | #include <Python.h> |
32 | #include "structmember.h" | |
33 | #include <string.h> | |
34 | #include <stdlib.h> | |
35 | ||
36 | #include "command_graph.h" | |
37 | #include "clippy.h" | |
38 | ||
39 | struct wrap_graph; | |
d62a17ae | 40 | static PyObject *graph_to_pyobj(struct wrap_graph *graph, |
41 | struct graph_node *gn); | |
29ad6f68 DL |
42 | |
43 | /* | |
44 | * nodes are wrapped as follows: | |
45 | * - instances can only be acquired from a graph | |
46 | * - the same node will return the same wrapper object (they're buffered | |
47 | * through "idx") | |
48 | * - a reference is held onto the graph | |
49 | * - fields are copied for easy access with PyMemberDef | |
50 | */ | |
51 | struct wrap_graph_node { | |
52 | PyObject_HEAD | |
53 | ||
d62a17ae | 54 | bool allowrepeat; |
29ad6f68 DL |
55 | const char *type; |
56 | ||
57 | bool deprecated; | |
58 | bool hidden; | |
59 | const char *text; | |
60 | const char *desc; | |
61 | const char *varname; | |
62 | long long min, max; | |
63 | ||
64 | struct graph_node *node; | |
65 | struct wrap_graph *wgraph; | |
66 | size_t idx; | |
67 | }; | |
68 | ||
69 | /* | |
70 | * graphs are wrapped as follows: | |
71 | * - they can only be created by parsing a definition string | |
72 | * - there's a table here for the wrapped nodes (nodewrappers), indexed | |
73 | * by "idx" (corresponds to node's position in graph's table of nodes) | |
74 | * - graphs do NOT hold references to nodes (would be circular) | |
75 | */ | |
76 | struct wrap_graph { | |
77 | PyObject_HEAD | |
78 | ||
d62a17ae | 79 | char *definition; |
29ad6f68 DL |
80 | struct graph *graph; |
81 | struct wrap_graph_node **nodewrappers; | |
82 | }; | |
83 | ||
84 | static PyObject *refuse_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
85 | { | |
d62a17ae | 86 | PyErr_SetString(PyExc_ValueError, |
87 | "cannot create instances of this type"); | |
29ad6f68 DL |
88 | return NULL; |
89 | } | |
90 | ||
d62a17ae | 91 | #define member(name, type) \ |
92 | { \ | |
93 | (char *)#name, type, offsetof(struct wrap_graph_node, name), \ | |
94 | READONLY, (char *)#name " (" #type ")" \ | |
95 | } | |
29ad6f68 | 96 | static PyMemberDef members_graph_node[] = { |
d62a17ae | 97 | member(allowrepeat, T_BOOL), member(type, T_STRING), |
98 | member(deprecated, T_BOOL), member(hidden, T_BOOL), | |
99 | member(text, T_STRING), member(desc, T_STRING), | |
100 | member(min, T_LONGLONG), member(max, T_LONGLONG), | |
3e300703 | 101 | member(varname, T_STRING), {}, |
29ad6f68 DL |
102 | }; |
103 | #undef member | |
104 | ||
105 | /* | |
106 | * node.next() -- returns list of all "next" nodes. | |
107 | * this will include circles if the graph has them. | |
108 | */ | |
109 | static PyObject *graph_node_next(PyObject *self, PyObject *args) | |
110 | { | |
111 | struct wrap_graph_node *wrap = (struct wrap_graph_node *)self; | |
112 | PyObject *pylist; | |
113 | ||
114 | if (wrap->node->data | |
d62a17ae | 115 | && ((struct cmd_token *)wrap->node->data)->type == END_TKN) |
29ad6f68 DL |
116 | return PyList_New(0); |
117 | pylist = PyList_New(vector_active(wrap->node->to)); | |
118 | for (size_t i = 0; i < vector_active(wrap->node->to); i++) { | |
119 | struct graph_node *gn = vector_slot(wrap->node->to, i); | |
120 | PyList_SetItem(pylist, i, graph_to_pyobj(wrap->wgraph, gn)); | |
121 | } | |
122 | return pylist; | |
123 | }; | |
124 | ||
125 | /* | |
126 | * node.join() -- return FORK's JOIN node or None | |
127 | */ | |
128 | static PyObject *graph_node_join(PyObject *self, PyObject *args) | |
129 | { | |
130 | struct wrap_graph_node *wrap = (struct wrap_graph_node *)self; | |
131 | ||
132 | if (!wrap->node->data | |
d62a17ae | 133 | || ((struct cmd_token *)wrap->node->data)->type == END_TKN) |
29ad6f68 DL |
134 | Py_RETURN_NONE; |
135 | ||
136 | struct cmd_token *tok = wrap->node->data; | |
137 | if (tok->type != FORK_TKN) | |
138 | Py_RETURN_NONE; | |
139 | ||
140 | return graph_to_pyobj(wrap->wgraph, tok->forkjoin); | |
141 | }; | |
142 | ||
143 | static PyMethodDef methods_graph_node[] = { | |
144 | {"next", graph_node_next, METH_NOARGS, "outbound graph edge list"}, | |
145 | {"join", graph_node_join, METH_NOARGS, "outbound join node"}, | |
3e300703 | 146 | {}}; |
29ad6f68 DL |
147 | |
148 | static void graph_node_wrap_free(void *arg) | |
149 | { | |
150 | struct wrap_graph_node *wrap = arg; | |
151 | wrap->wgraph->nodewrappers[wrap->idx] = NULL; | |
152 | Py_DECREF(wrap->wgraph); | |
153 | } | |
154 | ||
155 | static PyTypeObject typeobj_graph_node = { | |
d62a17ae | 156 | PyVarObject_HEAD_INIT(NULL, 0).tp_name = "_clippy.GraphNode", |
157 | .tp_basicsize = sizeof(struct wrap_graph_node), | |
158 | .tp_flags = Py_TPFLAGS_DEFAULT, | |
159 | .tp_doc = "struct graph_node *", | |
160 | .tp_new = refuse_new, | |
161 | .tp_free = graph_node_wrap_free, | |
162 | .tp_members = members_graph_node, | |
163 | .tp_methods = methods_graph_node, | |
29ad6f68 DL |
164 | }; |
165 | ||
d62a17ae | 166 | static PyObject *graph_to_pyobj(struct wrap_graph *wgraph, |
167 | struct graph_node *gn) | |
29ad6f68 DL |
168 | { |
169 | struct wrap_graph_node *wrap; | |
170 | size_t i; | |
171 | ||
172 | for (i = 0; i < vector_active(wgraph->graph->nodes); i++) | |
173 | if (vector_slot(wgraph->graph->nodes, i) == gn) | |
174 | break; | |
175 | if (i == vector_active(wgraph->graph->nodes)) { | |
176 | PyErr_SetString(PyExc_ValueError, "cannot find node in graph"); | |
177 | return NULL; | |
178 | } | |
179 | if (wgraph->nodewrappers[i]) { | |
180 | PyObject *obj = (PyObject *)wgraph->nodewrappers[i]; | |
181 | Py_INCREF(obj); | |
182 | return obj; | |
183 | } | |
184 | ||
d62a17ae | 185 | wrap = (struct wrap_graph_node *)typeobj_graph_node.tp_alloc( |
186 | &typeobj_graph_node, 0); | |
29ad6f68 DL |
187 | if (!wrap) |
188 | return NULL; | |
189 | wgraph->nodewrappers[i] = wrap; | |
190 | Py_INCREF(wgraph); | |
191 | ||
192 | wrap->idx = i; | |
193 | wrap->wgraph = wgraph; | |
194 | wrap->node = gn; | |
195 | wrap->type = "NULL"; | |
196 | wrap->allowrepeat = false; | |
197 | if (gn->data) { | |
198 | struct cmd_token *tok = gn->data; | |
199 | switch (tok->type) { | |
200 | #define item(x) case x: wrap->type = #x; break; | |
d62a17ae | 201 | item(WORD_TKN) // words |
202 | item(VARIABLE_TKN) // almost anything | |
203 | item(RANGE_TKN) // integer range | |
204 | item(IPV4_TKN) // IPV4 addresses | |
205 | item(IPV4_PREFIX_TKN) // IPV4 network prefixes | |
206 | item(IPV6_TKN) // IPV6 prefixes | |
207 | item(IPV6_PREFIX_TKN) // IPV6 network prefixes | |
473196f6 QY |
208 | item(MAC_TKN) // MAC address |
209 | item(MAC_PREFIX_TKN) // MAC address with mask | |
d62a17ae | 210 | |
211 | /* plumbing types */ | |
212 | item(FORK_TKN) item(JOIN_TKN) item(START_TKN) | |
213 | item(END_TKN) default | |
9d303b37 | 214 | : wrap->type = "???"; |
29ad6f68 DL |
215 | } |
216 | ||
217 | wrap->deprecated = (tok->attr == CMD_ATTR_DEPRECATED); | |
218 | wrap->hidden = (tok->attr == CMD_ATTR_HIDDEN); | |
219 | wrap->text = tok->text; | |
220 | wrap->desc = tok->desc; | |
221 | wrap->varname = tok->varname; | |
222 | wrap->min = tok->min; | |
223 | wrap->max = tok->max; | |
224 | wrap->allowrepeat = tok->allowrepeat; | |
225 | } | |
226 | ||
227 | return (PyObject *)wrap; | |
228 | } | |
229 | ||
d62a17ae | 230 | #define member(name, type) \ |
231 | { \ | |
232 | (char *)#name, type, offsetof(struct wrap_graph, name), \ | |
233 | READONLY, (char *)#name " (" #type ")" \ | |
234 | } | |
29ad6f68 DL |
235 | static PyMemberDef members_graph[] = { |
236 | member(definition, T_STRING), | |
3e300703 | 237 | {}, |
29ad6f68 DL |
238 | }; |
239 | #undef member | |
240 | ||
241 | /* graph.first() - root node */ | |
242 | static PyObject *graph_first(PyObject *self, PyObject *args) | |
243 | { | |
244 | struct wrap_graph *gwrap = (struct wrap_graph *)self; | |
245 | struct graph_node *gn = vector_slot(gwrap->graph->nodes, 0); | |
246 | return graph_to_pyobj(gwrap, gn); | |
247 | }; | |
248 | ||
249 | static PyMethodDef methods_graph[] = { | |
250 | {"first", graph_first, METH_NOARGS, "first graph node"}, | |
3e300703 | 251 | {}}; |
29ad6f68 | 252 | |
d62a17ae | 253 | static PyObject *graph_parse(PyTypeObject *type, PyObject *args, |
254 | PyObject *kwds); | |
29ad6f68 DL |
255 | |
256 | static void graph_wrap_free(void *arg) | |
257 | { | |
258 | struct wrap_graph *wgraph = arg; | |
e871b669 DS |
259 | |
260 | graph_delete_graph(wgraph->graph); | |
29ad6f68 DL |
261 | free(wgraph->nodewrappers); |
262 | free(wgraph->definition); | |
263 | } | |
264 | ||
265 | static PyTypeObject typeobj_graph = { | |
d62a17ae | 266 | PyVarObject_HEAD_INIT(NULL, 0).tp_name = "_clippy.Graph", |
267 | .tp_basicsize = sizeof(struct wrap_graph), | |
268 | .tp_flags = Py_TPFLAGS_DEFAULT, | |
269 | .tp_doc = "struct graph *", | |
270 | .tp_new = graph_parse, | |
271 | .tp_free = graph_wrap_free, | |
272 | .tp_members = members_graph, | |
273 | .tp_methods = methods_graph, | |
29ad6f68 DL |
274 | }; |
275 | ||
276 | /* top call / entrypoint for python code */ | |
277 | static PyObject *graph_parse(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
278 | { | |
279 | const char *def, *doc = NULL; | |
280 | struct wrap_graph *gwrap; | |
d62a17ae | 281 | static const char *kwnames[] = {"cmddef", "doc", NULL}; |
29ad6f68 DL |
282 | |
283 | gwrap = (struct wrap_graph *)typeobj_graph.tp_alloc(&typeobj_graph, 0); | |
284 | if (!gwrap) | |
285 | return NULL; | |
286 | ||
d62a17ae | 287 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", (char **)kwnames, |
288 | &def, &doc)) | |
29ad6f68 DL |
289 | return NULL; |
290 | ||
d62a17ae | 291 | struct graph *graph = graph_new(); |
292 | struct cmd_token *token = cmd_token_new(START_TKN, 0, NULL, NULL); | |
293 | graph_new_node(graph, token, (void (*)(void *)) & cmd_token_del); | |
29ad6f68 | 294 | |
d62a17ae | 295 | struct cmd_element cmd = {.string = def, .doc = doc}; |
296 | cmd_graph_parse(graph, &cmd); | |
297 | cmd_graph_names(graph); | |
29ad6f68 DL |
298 | |
299 | gwrap->graph = graph; | |
300 | gwrap->definition = strdup(def); | |
301 | gwrap->nodewrappers = calloc(vector_active(graph->nodes), | |
d62a17ae | 302 | sizeof(gwrap->nodewrappers[0])); |
29ad6f68 DL |
303 | return (PyObject *)gwrap; |
304 | } | |
305 | ||
306 | static PyMethodDef clippy_methods[] = { | |
307 | {"parse", clippy_parse, METH_VARARGS, "Parse a C file"}, | |
d62a17ae | 308 | {NULL, NULL, 0, NULL}}; |
29ad6f68 DL |
309 | |
310 | #if PY_MAJOR_VERSION >= 3 | |
311 | static struct PyModuleDef pymoddef_clippy = { | |
312 | PyModuleDef_HEAD_INIT, | |
313 | "_clippy", | |
314 | NULL, /* docstring */ | |
315 | -1, | |
316 | clippy_methods, | |
317 | }; | |
318 | #define modcreate() PyModule_Create(&pymoddef_clippy) | |
319 | #define initret(val) return val; | |
320 | #else | |
321 | #define modcreate() Py_InitModule("_clippy", clippy_methods) | |
d62a17ae | 322 | #define initret(val) \ |
323 | do { \ | |
324 | if (!val) \ | |
325 | Py_FatalError("initialization failure"); \ | |
326 | return; \ | |
327 | } while (0) | |
29ad6f68 DL |
328 | #endif |
329 | ||
f54dd468 | 330 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
29ad6f68 DL |
331 | PyMODINIT_FUNC command_py_init(void) |
332 | { | |
d62a17ae | 333 | PyObject *pymod; |
29ad6f68 DL |
334 | |
335 | if (PyType_Ready(&typeobj_graph_node) < 0) | |
336 | initret(NULL); | |
337 | if (PyType_Ready(&typeobj_graph) < 0) | |
338 | initret(NULL); | |
339 | ||
340 | pymod = modcreate(); | |
341 | if (!pymod) | |
342 | initret(NULL); | |
343 | ||
344 | Py_INCREF(&typeobj_graph_node); | |
345 | PyModule_AddObject(pymod, "GraphNode", (PyObject *)&typeobj_graph_node); | |
346 | Py_INCREF(&typeobj_graph); | |
347 | PyModule_AddObject(pymod, "Graph", (PyObject *)&typeobj_graph); | |
348 | initret(pymod); | |
349 | } |