2 * clippy (CLI preparator in python) wrapper for FRR command_graph
3 * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
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)
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
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
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.
25 /* This file is "exempt" from having
27 * as the first include statement because Python.h also does environment
28 * setup & these trample over each other.
35 #include "structmember.h"
39 #include "command_graph.h"
43 static PyObject
*graph_to_pyobj(struct wrap_graph
*graph
,
44 struct graph_node
*gn
);
47 * nodes are wrapped as follows:
48 * - instances can only be acquired from a graph
49 * - the same node will return the same wrapper object (they're buffered
51 * - a reference is held onto the graph
52 * - fields are copied for easy access with PyMemberDef
54 struct wrap_graph_node
{
67 struct graph_node
*node
;
68 struct wrap_graph
*wgraph
;
73 * graphs are wrapped as follows:
74 * - they can only be created by parsing a definition string
75 * - there's a table here for the wrapped nodes (nodewrappers), indexed
76 * by "idx" (corresponds to node's position in graph's table of nodes)
77 * - graphs do NOT hold references to nodes (would be circular)
84 struct wrap_graph_node
**nodewrappers
;
87 static PyObject
*refuse_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
89 PyErr_SetString(PyExc_ValueError
,
90 "cannot create instances of this type");
94 #define member(name, type) \
96 (char *)#name, type, offsetof(struct wrap_graph_node, name), \
97 READONLY, (char *)#name " (" #type ")" \
99 static PyMemberDef members_graph_node
[] = {
100 member(allowrepeat
, T_BOOL
), member(type
, T_STRING
),
101 member(deprecated
, T_BOOL
), member(hidden
, T_BOOL
),
102 member(text
, T_STRING
), member(desc
, T_STRING
),
103 member(min
, T_LONGLONG
), member(max
, T_LONGLONG
),
104 member(varname
, T_STRING
), {},
109 * node.next() -- returns list of all "next" nodes.
110 * this will include circles if the graph has them.
112 static PyObject
*graph_node_next(PyObject
*self
, PyObject
*args
)
114 struct wrap_graph_node
*wrap
= (struct wrap_graph_node
*)self
;
118 && ((struct cmd_token
*)wrap
->node
->data
)->type
== END_TKN
)
119 return PyList_New(0);
120 pylist
= PyList_New(vector_active(wrap
->node
->to
));
121 for (size_t i
= 0; i
< vector_active(wrap
->node
->to
); i
++) {
122 struct graph_node
*gn
= vector_slot(wrap
->node
->to
, i
);
123 PyList_SetItem(pylist
, i
, graph_to_pyobj(wrap
->wgraph
, gn
));
129 * node.join() -- return FORK's JOIN node or None
131 static PyObject
*graph_node_join(PyObject
*self
, PyObject
*args
)
133 struct wrap_graph_node
*wrap
= (struct wrap_graph_node
*)self
;
135 if (!wrap
->node
->data
136 || ((struct cmd_token
*)wrap
->node
->data
)->type
== END_TKN
)
139 struct cmd_token
*tok
= wrap
->node
->data
;
140 if (tok
->type
!= FORK_TKN
)
143 return graph_to_pyobj(wrap
->wgraph
, tok
->forkjoin
);
146 static PyMethodDef methods_graph_node
[] = {
147 {"next", graph_node_next
, METH_NOARGS
, "outbound graph edge list"},
148 {"join", graph_node_join
, METH_NOARGS
, "outbound join node"},
151 static void graph_node_wrap_free(void *arg
)
153 struct wrap_graph_node
*wrap
= arg
;
154 wrap
->wgraph
->nodewrappers
[wrap
->idx
] = NULL
;
155 Py_DECREF(wrap
->wgraph
);
158 static PyTypeObject typeobj_graph_node
= {
159 PyVarObject_HEAD_INIT(NULL
, 0).tp_name
= "_clippy.GraphNode",
160 .tp_basicsize
= sizeof(struct wrap_graph_node
),
161 .tp_flags
= Py_TPFLAGS_DEFAULT
,
162 .tp_doc
= "struct graph_node *",
163 .tp_new
= refuse_new
,
164 .tp_free
= graph_node_wrap_free
,
165 .tp_members
= members_graph_node
,
166 .tp_methods
= methods_graph_node
,
169 static PyObject
*graph_to_pyobj(struct wrap_graph
*wgraph
,
170 struct graph_node
*gn
)
172 struct wrap_graph_node
*wrap
;
175 for (i
= 0; i
< vector_active(wgraph
->graph
->nodes
); i
++)
176 if (vector_slot(wgraph
->graph
->nodes
, i
) == gn
)
178 if (i
== vector_active(wgraph
->graph
->nodes
)) {
179 PyErr_SetString(PyExc_ValueError
, "cannot find node in graph");
182 if (wgraph
->nodewrappers
[i
]) {
183 PyObject
*obj
= (PyObject
*)wgraph
->nodewrappers
[i
];
188 wrap
= (struct wrap_graph_node
*)typeobj_graph_node
.tp_alloc(
189 &typeobj_graph_node
, 0);
192 wgraph
->nodewrappers
[i
] = wrap
;
196 wrap
->wgraph
= wgraph
;
199 wrap
->allowrepeat
= false;
201 struct cmd_token
*tok
= gn
->data
;
206 break /* no semicolon */
208 item(WORD_TKN
); // words
209 item(VARIABLE_TKN
); // almost anything
210 item(RANGE_TKN
); // integer range
211 item(IPV4_TKN
); // IPV4 addresses
212 item(IPV4_PREFIX_TKN
); // IPV4 network prefixes
213 item(IPV6_TKN
); // IPV6 prefixes
214 item(IPV6_PREFIX_TKN
); // IPV6 network prefixes
215 item(MAC_TKN
); // MAC address
216 item(MAC_PREFIX_TKN
); // MAC address with mask
229 wrap
->deprecated
= (tok
->attr
== CMD_ATTR_DEPRECATED
);
230 wrap
->hidden
= (tok
->attr
== CMD_ATTR_HIDDEN
);
231 wrap
->text
= tok
->text
;
232 wrap
->desc
= tok
->desc
;
233 wrap
->varname
= tok
->varname
;
234 wrap
->min
= tok
->min
;
235 wrap
->max
= tok
->max
;
236 wrap
->allowrepeat
= tok
->allowrepeat
;
239 return (PyObject
*)wrap
;
242 #define member(name, type) \
244 (char *)#name, type, offsetof(struct wrap_graph, name), \
245 READONLY, (char *)#name " (" #type ")" \
247 static PyMemberDef members_graph
[] = {
248 member(definition
, T_STRING
),
253 /* graph.first() - root node */
254 static PyObject
*graph_first(PyObject
*self
, PyObject
*args
)
256 struct wrap_graph
*gwrap
= (struct wrap_graph
*)self
;
257 struct graph_node
*gn
= vector_slot(gwrap
->graph
->nodes
, 0);
258 return graph_to_pyobj(gwrap
, gn
);
261 static PyMethodDef methods_graph
[] = {
262 {"first", graph_first
, METH_NOARGS
, "first graph node"},
265 static PyObject
*graph_parse(PyTypeObject
*type
, PyObject
*args
,
268 static void graph_wrap_free(void *arg
)
270 struct wrap_graph
*wgraph
= arg
;
272 graph_delete_graph(wgraph
->graph
);
273 free(wgraph
->nodewrappers
);
274 free(wgraph
->definition
);
277 static PyTypeObject typeobj_graph
= {
278 PyVarObject_HEAD_INIT(NULL
, 0).tp_name
= "_clippy.Graph",
279 .tp_basicsize
= sizeof(struct wrap_graph
),
280 .tp_flags
= Py_TPFLAGS_DEFAULT
,
281 .tp_doc
= "struct graph *",
282 .tp_new
= graph_parse
,
283 .tp_free
= graph_wrap_free
,
284 .tp_members
= members_graph
,
285 .tp_methods
= methods_graph
,
288 /* top call / entrypoint for python code */
289 static PyObject
*graph_parse(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
291 const char *def
, *doc
= NULL
;
292 struct wrap_graph
*gwrap
;
293 static const char *kwnames
[] = {"cmddef", "doc", NULL
};
295 gwrap
= (struct wrap_graph
*)typeobj_graph
.tp_alloc(&typeobj_graph
, 0);
299 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "s|s", (char **)kwnames
,
303 struct graph
*graph
= graph_new();
304 struct cmd_token
*token
= cmd_token_new(START_TKN
, 0, NULL
, NULL
);
305 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
307 struct cmd_element cmd
= {.string
= def
, .doc
= doc
};
308 cmd_graph_parse(graph
, &cmd
);
309 cmd_graph_names(graph
);
311 gwrap
->graph
= graph
;
312 gwrap
->definition
= strdup(def
);
313 gwrap
->nodewrappers
= calloc(vector_active(graph
->nodes
),
314 sizeof(gwrap
->nodewrappers
[0]));
315 return (PyObject
*)gwrap
;
318 static PyMethodDef clippy_methods
[] = {
319 {"parse", clippy_parse
, METH_VARARGS
, "Parse a C file"},
320 {NULL
, NULL
, 0, NULL
}};
322 #if PY_MAJOR_VERSION >= 3
323 static struct PyModuleDef pymoddef_clippy
= {
324 PyModuleDef_HEAD_INIT
,
326 NULL
, /* docstring */
330 #define modcreate() PyModule_Create(&pymoddef_clippy)
331 #define initret(val) return val;
333 #define modcreate() Py_InitModule("_clippy", clippy_methods)
334 #define initret(val) \
337 Py_FatalError("initialization failure"); \
342 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
343 PyMODINIT_FUNC
command_py_init(void)
347 if (PyType_Ready(&typeobj_graph_node
) < 0)
349 if (PyType_Ready(&typeobj_graph
) < 0)
356 Py_INCREF(&typeobj_graph_node
);
357 PyModule_AddObject(pymod
, "GraphNode", (PyObject
*)&typeobj_graph_node
);
358 Py_INCREF(&typeobj_graph
);
359 PyModule_AddObject(pymod
, "Graph", (PyObject
*)&typeobj_graph
);
360 if (!elf_py_init(pymod
))