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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* clippy (CLI preparator in python) wrapper for FRR command_graph
* Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
*/
/* note: this wrapper is intended to be used as build-time helper. while
* it should be generally correct and proper, there may be the occasional
* memory leak or SEGV for things that haven't been well-tested.
*/
/* This file is "exempt" from having
#include "config.h"
* as the first include statement because Python.h also does environment
* setup & these trample over each other.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Python.h>
#include "structmember.h"
#include <string.h>
#include <stdlib.h>
#include "command_graph.h"
#include "clippy.h"
struct wrap_graph;
static PyObject *graph_to_pyobj(struct wrap_graph *graph,
struct graph_node *gn);
static PyObject *graph_to_pyobj_idx(struct wrap_graph *wgraph, size_t i);
/*
* nodes are wrapped as follows:
* - instances can only be acquired from a graph
* - the same node will return the same wrapper object (they're buffered
* through "idx")
* - a reference is held onto the graph
* - fields are copied for easy access with PyMemberDef
*/
struct wrap_graph_node {
PyObject_HEAD
bool allowrepeat;
const char *type;
struct graph_node *node;
struct wrap_graph *wgraph;
size_t idx;
};
/*
* graphs are wrapped as follows:
* - they can only be created by parsing a definition string
* - there's a table here for the wrapped nodes (nodewrappers), indexed
* by "idx" (corresponds to node's position in graph's table of nodes)
* - graphs do NOT hold references to nodes (would be circular)
*/
struct wrap_graph {
PyObject_HEAD
char *definition;
struct graph *graph;
size_t n_nodewrappers;
struct wrap_graph_node **nodewrappers;
};
static PyObject *refuse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyErr_SetString(PyExc_ValueError,
"cannot create instances of this type");
return NULL;
}
#define member(name, type) \
{ \
(char *)#name, type, offsetof(struct wrap_graph_node, name), \
READONLY, (char *)#name " (" #type ")" \
}
static PyMemberDef members_graph_node[] = {
/* clang-format off */
member(type, T_STRING),
member(idx, T_ULONG),
{},
/* clang-format on */
};
#undef member
static PyObject *graph_node_get_str(PyObject *self, void *poffset)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
void *offset = (char *)wrap->node->data + (ptrdiff_t)poffset;
const char *val = *(const char **)offset;
if (!val)
Py_RETURN_NONE;
return PyUnicode_FromString(val);
}
static PyObject *graph_node_get_bool(PyObject *self, void *poffset)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
void *offset = (char *)wrap->node->data + (ptrdiff_t)poffset;
bool val = *(bool *)offset;
return PyBool_FromLong(val);
}
static PyObject *graph_node_get_ll(PyObject *self, void *poffset)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
void *offset = (char *)wrap->node->data + (ptrdiff_t)poffset;
long long val = *(long long *)offset;
return PyLong_FromLongLong(val);
}
static PyObject *graph_node_get_u8(PyObject *self, void *poffset)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
void *offset = (char *)wrap->node->data + (ptrdiff_t)poffset;
uint8_t val = *(uint8_t *)offset;
return PyLong_FromUnsignedLong(val);
}
/* clang-format off */
#define member(name, variant) \
{ \
(char *)#name, \
graph_node_get_##variant, \
NULL, \
(char *)#name " (" #variant ")", \
(void *)offsetof(struct cmd_token, name), \
}
/* clang-format on */
static PyGetSetDef getset_graph_node[] = {
/* clang-format off */
member(attr, u8),
member(allowrepeat, bool),
member(varname_src, u8),
member(text, str),
member(desc, str),
member(min, ll),
member(max, ll),
member(varname, str),
{},
/* clang-format on */
};
#undef member
/*
* node.next() -- returns list of all "next" nodes.
* this will include circles if the graph has them.
*/
static PyObject *graph_node_next(PyObject *self, PyObject *args)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
PyObject *pylist;
if (wrap->node->data &&
((struct cmd_token *)wrap->node->data)->type == CMD_ELEMENT_TKN)
return PyList_New(0);
pylist = PyList_New(vector_active(wrap->node->to));
for (size_t i = 0; i < vector_active(wrap->node->to); i++) {
struct graph_node *gn = vector_slot(wrap->node->to, i);
PyList_SetItem(pylist, i, graph_to_pyobj(wrap->wgraph, gn));
}
return pylist;
};
static PyObject *graph_node_prev(PyObject *self, PyObject *args)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
PyObject *pylist;
if (wrap->node->data &&
((struct cmd_token *)wrap->node->data)->type == START_TKN)
return PyList_New(0);
pylist = PyList_New(vector_active(wrap->node->from));
for (size_t i = 0; i < vector_active(wrap->node->from); i++) {
struct graph_node *gn = vector_slot(wrap->node->from, i);
PyList_SetItem(pylist, i, graph_to_pyobj(wrap->wgraph, gn));
}
return pylist;
};
/*
* node.join() -- return FORK's JOIN node or None
*/
static PyObject *graph_node_join(PyObject *self, PyObject *args)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
struct cmd_token *tok;
if (!wrap->node->data
|| ((struct cmd_token *)wrap->node->data)->type == END_TKN)
Py_RETURN_NONE;
tok = wrap->node->data;
if (tok->type != FORK_TKN)
Py_RETURN_NONE;
return graph_to_pyobj(wrap->wgraph, tok->forkjoin);
};
static PyObject *graph_node_fork(PyObject *self, PyObject *args)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)self;
struct cmd_token *tok;
if (!wrap->node->data ||
((struct cmd_token *)wrap->node->data)->type == END_TKN)
Py_RETURN_NONE;
tok = wrap->node->data;
if (tok->type != JOIN_TKN)
Py_RETURN_NONE;
return graph_to_pyobj(wrap->wgraph, tok->forkjoin);
};
static PyMethodDef methods_graph_node[] = {
{ "next", graph_node_next, METH_NOARGS, "outbound graph edge list" },
{ "prev", graph_node_prev, METH_NOARGS, "inbound graph edge list" },
{ "join", graph_node_join, METH_NOARGS, "outbound join node" },
{ "fork", graph_node_fork, METH_NOARGS, "inbound fork node" },
{}
};
static void graph_node_wrap_free(void *arg)
{
struct wrap_graph_node *wrap = arg;
assert(wrap->idx < wrap->wgraph->n_nodewrappers);
wrap->wgraph->nodewrappers[wrap->idx] = NULL;
Py_DECREF(wrap->wgraph);
}
static PyObject *repr_graph_node(PyObject *arg)
{
struct wrap_graph_node *wrap = (struct wrap_graph_node *)arg;
return PyUnicode_FromFormat("<_clippy.GraphNode %p [%zu] %s>",
wrap->node, wrap->idx, wrap->type);
}
static PyTypeObject typeobj_graph_node = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "_clippy.GraphNode",
.tp_basicsize = sizeof(struct wrap_graph_node),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "struct graph_node *",
.tp_new = refuse_new,
.tp_free = graph_node_wrap_free,
.tp_members = members_graph_node,
.tp_getset = getset_graph_node,
.tp_methods = methods_graph_node,
.tp_repr = repr_graph_node,
};
static PyObject *graph_to_pyobj(struct wrap_graph *wgraph,
struct graph_node *gn)
{
size_t i;
for (i = 0; i < vector_active(wgraph->graph->nodes); i++)
if (vector_slot(wgraph->graph->nodes, i) == gn)
break;
if (i == vector_active(wgraph->graph->nodes)) {
PyErr_SetString(PyExc_ValueError, "cannot find node in graph");
return NULL;
}
return graph_to_pyobj_idx(wgraph, i);
}
static PyObject *graph_to_pyobj_idx(struct wrap_graph *wgraph, size_t i)
{
struct wrap_graph_node *wrap;
struct graph_node *gn = vector_slot(wgraph->graph->nodes, i);
if (i >= wgraph->n_nodewrappers) {
wgraph->nodewrappers =
realloc(wgraph->nodewrappers,
(i + 1) * sizeof(wgraph->nodewrappers[0]));
memset(wgraph->nodewrappers + wgraph->n_nodewrappers, 0,
sizeof(wgraph->nodewrappers[0]) *
(i + 1 - wgraph->n_nodewrappers));
wgraph->n_nodewrappers = i + 1;
}
if (wgraph->nodewrappers[i]) {
PyObject *obj = (PyObject *)wgraph->nodewrappers[i];
Py_INCREF(obj);
return obj;
}
wrap = (struct wrap_graph_node *)typeobj_graph_node.tp_alloc(
&typeobj_graph_node, 0);
if (!wrap)
return NULL;
wgraph->nodewrappers[i] = wrap;
Py_INCREF(wgraph);
wrap->idx = i;
wrap->wgraph = wgraph;
wrap->node = gn;
wrap->type = "NULL";
wrap->allowrepeat = false;
if (gn->data) {
struct cmd_token *tok = gn->data;
switch (tok->type) {
#define item(x) \
case x: \
wrap->type = #x; \
break /* no semicolon */
item(WORD_TKN); // words
item(VARIABLE_TKN); // almost anything
item(RANGE_TKN); // integer range
item(IPV4_TKN); // IPV4 addresses
item(IPV4_PREFIX_TKN); // IPV4 network prefixes
item(IPV6_TKN); // IPV6 prefixes
item(IPV6_PREFIX_TKN); // IPV6 network prefixes
item(MAC_TKN); // MAC address
item(MAC_PREFIX_TKN); // MAC address with mask
item(ASNUM_TKN); // ASNUM
/* plumbing types */
item(FORK_TKN);
item(JOIN_TKN);
item(START_TKN);
item(END_TKN);
item(NEG_ONLY_TKN);
item(CMD_ELEMENT_TKN);
#undef item
default:
wrap->type = "???";
}
}
return (PyObject *)wrap;
}
#define member(name, type) \
{ \
(char *)#name, type, offsetof(struct wrap_graph, name), \
READONLY, (char *)#name " (" #type ")" \
}
static PyMemberDef members_graph[] = {
member(definition, T_STRING),
{},
};
#undef member
/* graph.first() - root node */
static PyObject *graph_first(PyObject *self, PyObject *args)
{
struct wrap_graph *gwrap = (struct wrap_graph *)self;
struct graph_node *gn = vector_slot(gwrap->graph->nodes, 0);
return graph_to_pyobj(gwrap, gn);
};
static PyObject *graph_merge(PyObject *self, PyObject *args);
static PyMethodDef methods_graph[] = {
{ "first", graph_first, METH_NOARGS, "first graph node" },
{ "merge", graph_merge, METH_VARARGS, "merge graphs" },
{}
};
static PyObject *graph_parse(PyTypeObject *type, PyObject *args,
PyObject *kwds);
static void graph_wrap_free(void *arg)
{
struct wrap_graph *wgraph = arg;
graph_delete_graph(wgraph->graph);
free(wgraph->nodewrappers);
free(wgraph->definition);
}
static Py_ssize_t graph_length(PyObject *self)
{
struct wrap_graph *gwrap = (struct wrap_graph *)self;
return vector_active(gwrap->graph->nodes);
}
static PyObject *graph_item(PyObject *self, Py_ssize_t idx)
{
struct wrap_graph *gwrap = (struct wrap_graph *)self;
if (idx >= vector_active(gwrap->graph->nodes))
return PyErr_Format(PyExc_IndexError,
"index %zd past graph size %u", idx,
vector_active(gwrap->graph->nodes));
return graph_to_pyobj_idx(gwrap, idx);
}
static PySequenceMethods seq_graph = {
.sq_length = graph_length,
.sq_item = graph_item,
};
static PyTypeObject typeobj_graph = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "_clippy.Graph",
.tp_basicsize = sizeof(struct wrap_graph),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "struct graph *",
.tp_new = graph_parse,
.tp_free = graph_wrap_free,
.tp_members = members_graph,
.tp_methods = methods_graph,
.tp_as_sequence = &seq_graph,
};
static PyObject *graph_merge(PyObject *self, PyObject *args)
{
PyObject *py_other;
struct wrap_graph *gwrap = (struct wrap_graph *)self;
struct wrap_graph *gother;
if (!PyArg_ParseTuple(args, "O!", &typeobj_graph, &py_other))
return NULL;
gother = (struct wrap_graph *)py_other;
cmd_graph_merge(gwrap->graph, gother->graph, +1);
Py_RETURN_NONE;
}
/* top call / entrypoint for python code */
static PyObject *graph_parse(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
const char *def, *doc = NULL, *name = NULL;
struct wrap_graph *gwrap;
static const char *const kwnames[] = { "cmddef", "doc", "name", NULL };
gwrap = (struct wrap_graph *)typeobj_graph.tp_alloc(&typeobj_graph, 0);
if (!gwrap)
return NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "z|ss", (char **)kwnames,
&def, &doc, &name))
return NULL;
struct graph *graph = graph_new();
struct cmd_token *token = cmd_token_new(START_TKN, 0, NULL, NULL);
graph_new_node(graph, token, (void (*)(void *)) & cmd_token_del);
if (def) {
struct cmd_element cmd = { .string = def, .doc = doc };
struct graph_node *last;
cmd_graph_parse(graph, &cmd);
cmd_graph_names(graph);
last = vector_slot(graph->nodes,
vector_active(graph->nodes) - 1);
assert(last->data == &cmd);
last->data = cmd_token_new(CMD_ELEMENT_TKN, 0, name, def);
last->del = (void (*)(void *))cmd_token_del;
gwrap->definition = strdup(def);
} else {
gwrap->definition = strdup("NULL");
}
gwrap->graph = graph;
return (PyObject *)gwrap;
}
static PyMethodDef clippy_methods[] = {
{"parse", clippy_parse, METH_VARARGS, "Parse a C file"},
{NULL, NULL, 0, NULL}};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef pymoddef_clippy = {
PyModuleDef_HEAD_INIT,
"_clippy",
NULL, /* docstring */
-1,
clippy_methods,
};
#define modcreate() PyModule_Create(&pymoddef_clippy)
#define initret(val) return val;
#else
#define modcreate() Py_InitModule("_clippy", clippy_methods)
#define initret(val) \
do { \
if (!val) \
Py_FatalError("initialization failure"); \
return; \
} while (0)
#endif
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
PyMODINIT_FUNC command_py_init(void)
{
PyObject *pymod;
if (PyType_Ready(&typeobj_graph_node) < 0)
initret(NULL);
if (PyType_Ready(&typeobj_graph) < 0)
initret(NULL);
pymod = modcreate();
if (!pymod)
initret(NULL);
if (PyModule_AddIntMacro(pymod, CMD_ATTR_YANG)
|| PyModule_AddIntMacro(pymod, CMD_ATTR_HIDDEN)
|| PyModule_AddIntMacro(pymod, CMD_ATTR_DEPRECATED)
|| PyModule_AddIntMacro(pymod, CMD_ATTR_NOSH))
initret(NULL);
Py_INCREF(&typeobj_graph_node);
PyModule_AddObject(pymod, "GraphNode", (PyObject *)&typeobj_graph_node);
Py_INCREF(&typeobj_graph);
PyModule_AddObject(pymod, "Graph", (PyObject *)&typeobj_graph);
if (!elf_py_init(pymod))
initret(NULL);
initret(pymod);
}
|