2023-02-08 13:17:09 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2017-05-11 15:36:04 +02:00
|
|
|
# FRR CLI preprocessor (DEFPY)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 David Lamparter for NetDEF, Inc.
|
|
|
|
|
|
|
|
import clippy, traceback, sys, os
|
|
|
|
from collections import OrderedDict
|
|
|
|
from functools import reduce
|
|
|
|
from pprint import pprint
|
|
|
|
from string import Template
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
# the various handlers generate output C code for a particular type of
|
|
|
|
# CLI token, choosing the most useful output C type.
|
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class RenderHandler(object):
|
|
|
|
def __init__(self, token):
|
|
|
|
pass
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
def combine(self, other):
|
|
|
|
if type(self) == type(other):
|
|
|
|
return other
|
|
|
|
return StringHandler(None)
|
|
|
|
|
|
|
|
deref = ""
|
|
|
|
drop_str = False
|
2017-08-25 18:54:13 +02:00
|
|
|
canfail = True
|
2019-06-04 20:27:05 +02:00
|
|
|
canassert = False
|
2017-05-11 15:36:04 +02:00
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class StringHandler(RenderHandler):
|
|
|
|
argtype = "const char *"
|
|
|
|
decl = Template("const char *$varname = NULL;")
|
python: make DEFPY provide the text token of fixed parameters
In the CLI code, each cmd_token has both a "text" field, containing
the full token text (e.g. "unicast"), and an "arg" field,
containing the original text entered by the user (which might be
an abbreviation, like "uni" instead of "unicast").
To avoid the need to handle abbreviations, the recommended pattern
for DEFUN commands is to use the "text" value of fixed parameters
and the "arg" value of everything else.
Using DEFPY, however, the CLI parameters are automagically turned
into C variables which are initialized under the hood (so that
they're conveniently ready for use). The problem is that this
initialization was always using the "arg" value of the parameters,
which was leading to problems like these:
debian# show ipv6 route isi
Unknown route type
debian#
debian# conf t
debian(config)# router isis 1
debian(config-router)# redistribute ipv4 st level-1
% Configuration failed.
Invalid value "st" in "protocol" element.
YANG path: /frr-isisd:isis/instance[area-tag='1']/redistribute/ipv4[protocol='st']/protocol
To fix these problems (and probably others too), make DEFPY commands
auto-detect the type of the input parameters and use either the
"arg" or "text" value from the cmd_tokens accordingly.
Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
2019-01-19 20:24:09 +01:00
|
|
|
code = Template(
|
|
|
|
"$varname = (argv[_i]->type == WORD_TKN) ? argv[_i]->text : argv[_i]->arg;"
|
|
|
|
)
|
2017-05-11 15:36:04 +02:00
|
|
|
drop_str = True
|
2017-08-25 18:54:13 +02:00
|
|
|
canfail = False
|
2019-06-04 20:27:05 +02:00
|
|
|
canassert = True
|
2017-05-11 15:36:04 +02:00
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class LongHandler(RenderHandler):
|
|
|
|
argtype = "long"
|
|
|
|
decl = Template("long $varname = 0;")
|
|
|
|
code = Template(
|
|
|
|
"""\
|
|
|
|
char *_end;
|
|
|
|
$varname = strtol(argv[_i]->arg, &_end, 10);
|
|
|
|
_fail = (_end == argv[_i]->arg) || (*_end != '\\0');"""
|
|
|
|
)
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
|
2022-11-02 18:17:21 +01:00
|
|
|
class AsDotHandler(RenderHandler):
|
|
|
|
argtype = "as_t"
|
|
|
|
decl = Template("as_t $varname = 0;")
|
|
|
|
code = Template("_fail = !asn_str2asn(argv[_i]->arg, &$varname);")
|
|
|
|
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
# A.B.C.D/M (prefix_ipv4) and
|
|
|
|
# X:X::X:X/M (prefix_ipv6) are "compatible" and can merge into a
|
|
|
|
# struct prefix:
|
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class PrefixBase(RenderHandler):
|
|
|
|
def combine(self, other):
|
|
|
|
if type(self) == type(other):
|
|
|
|
return other
|
2017-08-11 18:53:06 +02:00
|
|
|
if isinstance(other, PrefixBase):
|
2017-05-11 15:36:04 +02:00
|
|
|
return PrefixGenHandler(None)
|
|
|
|
return StringHandler(None)
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
deref = "&"
|
2020-10-07 23:22:26 +02:00
|
|
|
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class Prefix4Handler(PrefixBase):
|
|
|
|
argtype = "const struct prefix_ipv4 *"
|
2018-12-01 16:49:45 +01:00
|
|
|
decl = Template("struct prefix_ipv4 $varname = { };")
|
2017-05-11 15:36:04 +02:00
|
|
|
code = Template("_fail = !str2prefix_ipv4(argv[_i]->arg, &$varname);")
|
2020-10-07 23:22:26 +02:00
|
|
|
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class Prefix6Handler(PrefixBase):
|
|
|
|
argtype = "const struct prefix_ipv6 *"
|
2018-12-01 16:49:45 +01:00
|
|
|
decl = Template("struct prefix_ipv6 $varname = { };")
|
2017-05-11 15:36:04 +02:00
|
|
|
code = Template("_fail = !str2prefix_ipv6(argv[_i]->arg, &$varname);")
|
2020-10-07 23:22:26 +02:00
|
|
|
|
|
|
|
|
2017-08-11 18:53:06 +02:00
|
|
|
class PrefixEthHandler(PrefixBase):
|
|
|
|
argtype = "struct prefix_eth *"
|
2018-12-01 16:49:45 +01:00
|
|
|
decl = Template("struct prefix_eth $varname = { };")
|
2017-08-11 18:53:06 +02:00
|
|
|
code = Template("_fail = !str2prefix_eth(argv[_i]->arg, &$varname);")
|
2020-10-07 23:22:26 +02:00
|
|
|
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class PrefixGenHandler(PrefixBase):
|
|
|
|
argtype = "const struct prefix *"
|
2018-12-01 16:49:45 +01:00
|
|
|
decl = Template("struct prefix $varname = { };")
|
2017-05-11 15:36:04 +02:00
|
|
|
code = Template("_fail = !str2prefix(argv[_i]->arg, &$varname);")
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
|
|
|
|
# same for IP addresses. result is union sockunion.
|
|
|
|
class IPBase(RenderHandler):
|
|
|
|
def combine(self, other):
|
|
|
|
if type(self) == type(other):
|
|
|
|
return other
|
|
|
|
if type(other) in [IP4Handler, IP6Handler, IPGenHandler]:
|
|
|
|
return IPGenHandler(None)
|
|
|
|
return StringHandler(None)
|
2020-10-07 23:22:26 +02:00
|
|
|
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class IP4Handler(IPBase):
|
|
|
|
argtype = "struct in_addr"
|
|
|
|
decl = Template("struct in_addr $varname = { INADDR_ANY };")
|
|
|
|
code = Template("_fail = !inet_aton(argv[_i]->arg, &$varname);")
|
2020-10-07 23:22:26 +02:00
|
|
|
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class IP6Handler(IPBase):
|
|
|
|
argtype = "struct in6_addr"
|
2018-12-01 16:49:45 +01:00
|
|
|
decl = Template("struct in6_addr $varname = {};")
|
2017-05-11 15:36:04 +02:00
|
|
|
code = Template("_fail = !inet_pton(AF_INET6, argv[_i]->arg, &$varname);")
|
2020-10-07 23:22:26 +02:00
|
|
|
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
class IPGenHandler(IPBase):
|
|
|
|
argtype = "const union sockunion *"
|
|
|
|
decl = Template(
|
|
|
|
"""union sockunion s__$varname = { .sa.sa_family = AF_UNSPEC }, *$varname = NULL;"""
|
|
|
|
)
|
|
|
|
code = Template(
|
|
|
|
"""\
|
|
|
|
if (argv[_i]->text[0] == 'X') {
|
|
|
|
s__$varname.sa.sa_family = AF_INET6;
|
|
|
|
_fail = !inet_pton(AF_INET6, argv[_i]->arg, &s__$varname.sin6.sin6_addr);
|
|
|
|
$varname = &s__$varname;
|
|
|
|
} else {
|
|
|
|
s__$varname.sa.sa_family = AF_INET;
|
|
|
|
_fail = !inet_aton(argv[_i]->arg, &s__$varname.sin.sin_addr);
|
|
|
|
$varname = &s__$varname;
|
|
|
|
}"""
|
|
|
|
)
|
2019-06-04 20:27:05 +02:00
|
|
|
canassert = True
|
2017-05-11 15:36:04 +02:00
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
def mix_handlers(handlers):
|
|
|
|
def combine(a, b):
|
|
|
|
if a is None:
|
|
|
|
return b
|
|
|
|
return a.combine(b)
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
return reduce(combine, handlers, None)
|
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
handlers = {
|
|
|
|
"WORD_TKN": StringHandler,
|
|
|
|
"VARIABLE_TKN": StringHandler,
|
|
|
|
"RANGE_TKN": LongHandler,
|
|
|
|
"IPV4_TKN": IP4Handler,
|
|
|
|
"IPV4_PREFIX_TKN": Prefix4Handler,
|
|
|
|
"IPV6_TKN": IP6Handler,
|
|
|
|
"IPV6_PREFIX_TKN": Prefix6Handler,
|
2017-08-11 18:53:06 +02:00
|
|
|
"MAC_TKN": PrefixEthHandler,
|
|
|
|
"MAC_PREFIX_TKN": PrefixEthHandler,
|
2022-11-02 18:17:21 +01:00
|
|
|
"ASNUM_TKN": AsDotHandler,
|
2017-05-11 15:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# core template invoked for each occurence of DEFPY.
|
2017-08-25 18:54:13 +02:00
|
|
|
#
|
|
|
|
# the "#if $..." bits are there to keep this template unified into one
|
|
|
|
# common form, without requiring a more advanced template engine (e.g.
|
|
|
|
# jinja2)
|
2017-05-11 15:36:04 +02:00
|
|
|
templ = Template(
|
2022-01-13 14:18:29 +01:00
|
|
|
"""$cond_begin/* $fnname => "$cmddef" */
|
2017-05-11 15:36:04 +02:00
|
|
|
DEFUN_CMD_FUNC_DECL($fnname)
|
|
|
|
#define funcdecl_$fnname static int ${fnname}_magic(\\
|
|
|
|
const struct cmd_element *self __attribute__ ((unused)),\\
|
|
|
|
struct vty *vty __attribute__ ((unused)),\\
|
|
|
|
int argc __attribute__ ((unused)),\\
|
|
|
|
struct cmd_token *argv[] __attribute__ ((unused))$argdefs)
|
|
|
|
funcdecl_$fnname;
|
|
|
|
DEFUN_CMD_FUNC_TEXT($fnname)
|
|
|
|
{
|
2017-08-25 18:54:13 +02:00
|
|
|
#if $nonempty /* anything to parse? */
|
2017-05-11 15:36:04 +02:00
|
|
|
int _i;
|
2017-08-25 18:54:13 +02:00
|
|
|
#if $canfail /* anything that can fail? */
|
2017-05-11 15:36:04 +02:00
|
|
|
unsigned _fail = 0, _failcnt = 0;
|
2017-08-25 18:54:13 +02:00
|
|
|
#endif
|
2017-05-11 15:36:04 +02:00
|
|
|
$argdecls
|
|
|
|
for (_i = 0; _i < argc; _i++) {
|
|
|
|
if (!argv[_i]->varname)
|
|
|
|
continue;
|
2017-08-25 18:54:13 +02:00
|
|
|
#if $canfail /* anything that can fail? */
|
|
|
|
_fail = 0;
|
|
|
|
#endif
|
|
|
|
$argblocks
|
|
|
|
#if $canfail /* anything that can fail? */
|
2017-05-11 15:36:04 +02:00
|
|
|
if (_fail)
|
2017-07-13 17:49:13 +02:00
|
|
|
vty_out (vty, "%% invalid input for %s: %s\\n",
|
2017-06-28 18:30:14 +02:00
|
|
|
argv[_i]->varname, argv[_i]->arg);
|
2017-05-11 15:36:04 +02:00
|
|
|
_failcnt += _fail;
|
2017-08-25 18:54:13 +02:00
|
|
|
#endif
|
2017-05-11 15:36:04 +02:00
|
|
|
}
|
2017-08-25 18:54:13 +02:00
|
|
|
#if $canfail /* anything that can fail? */
|
2017-05-11 15:36:04 +02:00
|
|
|
if (_failcnt)
|
|
|
|
return CMD_WARNING;
|
2017-08-25 18:54:13 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2019-06-04 20:27:05 +02:00
|
|
|
$argassert
|
2017-05-11 15:36:04 +02:00
|
|
|
return ${fnname}_magic(self, vty, argc, argv$arglist);
|
|
|
|
}
|
2022-01-13 14:18:29 +01:00
|
|
|
$cond_end
|
2020-10-07 23:22:26 +02:00
|
|
|
"""
|
2017-05-11 15:36:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# invoked for each named parameter
|
|
|
|
argblock = Template(
|
2020-10-07 23:22:26 +02:00
|
|
|
"""
|
2017-05-11 15:36:04 +02:00
|
|
|
if (!strcmp(argv[_i]->varname, \"$varname\")) {$strblock
|
|
|
|
$code
|
|
|
|
}"""
|
|
|
|
)
|
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2019-06-04 20:27:05 +02:00
|
|
|
def get_always_args(token, always_args, args=[], stack=[]):
|
|
|
|
if token in stack:
|
|
|
|
return
|
|
|
|
if token.type == "END_TKN":
|
|
|
|
for arg in list(always_args):
|
|
|
|
if arg not in args:
|
|
|
|
always_args.remove(arg)
|
|
|
|
return
|
|
|
|
|
|
|
|
stack = stack + [token]
|
|
|
|
if token.type in handlers and token.varname is not None:
|
|
|
|
args = args + [token.varname]
|
|
|
|
for nexttkn in token.next():
|
|
|
|
get_always_args(nexttkn, always_args, args, stack)
|
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2019-06-11 15:35:28 +02:00
|
|
|
class Macros(dict):
|
2022-01-16 21:43:55 +01:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self._loc = {}
|
|
|
|
|
2019-06-11 15:35:28 +02:00
|
|
|
def load(self, filename):
|
|
|
|
filedata = clippy.parse(filename)
|
|
|
|
for entry in filedata["data"]:
|
|
|
|
if entry["type"] != "PREPROC":
|
|
|
|
continue
|
2022-01-16 21:43:55 +01:00
|
|
|
self.load_preproc(filename, entry)
|
|
|
|
|
|
|
|
def setup(self, key, val, where="built-in"):
|
|
|
|
self[key] = val
|
|
|
|
self._loc[key] = (where, 0)
|
|
|
|
|
|
|
|
def load_preproc(self, filename, entry):
|
|
|
|
ppdir = entry["line"].lstrip().split(None, 1)
|
|
|
|
if ppdir[0] != "define" or len(ppdir) != 2:
|
|
|
|
return
|
|
|
|
ppdef = ppdir[1].split(None, 1)
|
|
|
|
name = ppdef[0]
|
|
|
|
if "(" in name:
|
|
|
|
return
|
|
|
|
val = ppdef[1] if len(ppdef) == 2 else ""
|
|
|
|
|
|
|
|
val = val.strip(" \t\n\\")
|
|
|
|
if self.get(name, val) != val:
|
|
|
|
sys.stderr.write(
|
|
|
|
"%s:%d: warning: macro %s redefined!\n"
|
|
|
|
% (
|
|
|
|
filename,
|
|
|
|
entry["lineno"],
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
sys.stderr.write(
|
|
|
|
"%s:%d: note: previously defined here\n"
|
|
|
|
% (
|
|
|
|
self._loc[name][0],
|
|
|
|
self._loc[name][1],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2019-06-11 15:35:28 +02:00
|
|
|
self[name] = val
|
2022-01-16 21:43:55 +01:00
|
|
|
self._loc[name] = (filename, entry["lineno"])
|
2019-06-11 15:35:28 +02:00
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2019-06-11 15:35:28 +02:00
|
|
|
def process_file(fn, ofd, dumpfd, all_defun, macros):
|
2019-06-04 17:07:57 +02:00
|
|
|
errors = 0
|
2017-05-11 15:36:04 +02:00
|
|
|
filedata = clippy.parse(fn)
|
|
|
|
|
2022-01-13 14:18:29 +01:00
|
|
|
cond_stack = []
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
for entry in filedata["data"]:
|
2022-01-13 14:18:29 +01:00
|
|
|
if entry["type"] == "PREPROC":
|
|
|
|
line = entry["line"].lstrip()
|
|
|
|
tokens = line.split(maxsplit=1)
|
|
|
|
line = "#" + line + "\n"
|
|
|
|
|
|
|
|
if not tokens:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tokens[0] in ["if", "ifdef", "ifndef"]:
|
|
|
|
cond_stack.append(line)
|
|
|
|
elif tokens[0] in ["elif", "else"]:
|
|
|
|
prev_line = cond_stack.pop(-1)
|
|
|
|
cond_stack.append(prev_line + line)
|
|
|
|
elif tokens[0] in ["endif"]:
|
|
|
|
cond_stack.pop(-1)
|
2022-01-16 21:43:55 +01:00
|
|
|
elif tokens[0] in ["define"]:
|
|
|
|
if not cond_stack:
|
|
|
|
macros.load_preproc(fn, entry)
|
|
|
|
elif len(cond_stack) == 1 and cond_stack[0] == "#ifdef CLIPPY\n":
|
|
|
|
macros.load_preproc(fn, entry)
|
2022-01-13 14:18:29 +01:00
|
|
|
continue
|
2018-03-29 19:01:06 +02:00
|
|
|
if entry["type"].startswith("DEFPY") or (
|
|
|
|
all_defun and entry["type"].startswith("DEFUN")
|
|
|
|
):
|
2019-06-04 17:07:57 +02:00
|
|
|
if len(entry["args"][0]) != 1:
|
|
|
|
sys.stderr.write(
|
|
|
|
"%s:%d: DEFPY function name not parseable (%r)\n"
|
|
|
|
% (fn, entry["lineno"], entry["args"][0])
|
2020-10-07 23:22:26 +02:00
|
|
|
)
|
2019-06-04 17:07:57 +02:00
|
|
|
errors += 1
|
|
|
|
continue
|
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
cmddef = entry["args"][2]
|
2019-06-11 15:35:28 +02:00
|
|
|
cmddefx = []
|
2019-06-04 17:07:57 +02:00
|
|
|
for i in cmddef:
|
2019-06-11 15:35:28 +02:00
|
|
|
while i in macros:
|
|
|
|
i = macros[i]
|
|
|
|
if i.startswith('"') and i.endswith('"'):
|
|
|
|
cmddefx.append(i[1:-1])
|
|
|
|
continue
|
|
|
|
|
|
|
|
sys.stderr.write(
|
|
|
|
"%s:%d: DEFPY command string not parseable (%r)\n"
|
|
|
|
% (fn, entry["lineno"], cmddef)
|
2020-10-07 23:22:26 +02:00
|
|
|
)
|
2019-06-11 15:35:28 +02:00
|
|
|
errors += 1
|
|
|
|
cmddefx = None
|
|
|
|
break
|
|
|
|
if cmddefx is None:
|
2019-06-04 17:07:57 +02:00
|
|
|
continue
|
2019-06-11 15:35:28 +02:00
|
|
|
cmddef = "".join([i for i in cmddefx])
|
2017-05-11 15:36:04 +02:00
|
|
|
|
|
|
|
graph = clippy.Graph(cmddef)
|
|
|
|
args = OrderedDict()
|
2019-06-04 20:27:05 +02:00
|
|
|
always_args = set()
|
2017-05-11 15:36:04 +02:00
|
|
|
for token, depth in clippy.graph_iterate(graph):
|
|
|
|
if token.type not in handlers:
|
|
|
|
continue
|
|
|
|
if token.varname is None:
|
|
|
|
continue
|
|
|
|
arg = args.setdefault(token.varname, [])
|
|
|
|
arg.append(handlers[token.type](token))
|
2019-06-04 20:27:05 +02:00
|
|
|
always_args.add(token.varname)
|
|
|
|
|
|
|
|
get_always_args(graph.first(), always_args)
|
2017-05-11 15:36:04 +02:00
|
|
|
|
|
|
|
# print('-' * 76)
|
|
|
|
# pprint(entry)
|
|
|
|
# clippy.dump(graph)
|
|
|
|
# pprint(args)
|
|
|
|
|
|
|
|
params = {"cmddef": cmddef, "fnname": entry["args"][0][0]}
|
|
|
|
argdefs = []
|
|
|
|
argdecls = []
|
|
|
|
arglist = []
|
|
|
|
argblocks = []
|
2019-06-04 20:27:05 +02:00
|
|
|
argassert = []
|
2017-05-11 15:36:04 +02:00
|
|
|
doc = []
|
2017-08-25 18:54:13 +02:00
|
|
|
canfail = 0
|
2017-05-11 15:36:04 +02:00
|
|
|
|
2019-06-04 20:27:05 +02:00
|
|
|
def do_add(handler, basename, varname, attr=""):
|
2017-05-11 15:36:04 +02:00
|
|
|
argdefs.append(",\\\n\t%s %s%s" % (handler.argtype, varname, attr))
|
|
|
|
argdecls.append(
|
|
|
|
"\t%s\n"
|
|
|
|
% (
|
|
|
|
handler.decl.substitute({"varname": varname}).replace(
|
|
|
|
"\n", "\n\t"
|
|
|
|
)
|
2020-10-07 23:22:26 +02:00
|
|
|
)
|
|
|
|
)
|
2017-05-11 15:36:04 +02:00
|
|
|
arglist.append(", %s%s" % (handler.deref, varname))
|
2019-06-04 20:27:05 +02:00
|
|
|
if basename in always_args and handler.canassert:
|
|
|
|
argassert.append(
|
|
|
|
"""\tif (!%s) {
|
|
|
|
\t\tvty_out(vty, "Internal CLI error [%%s]\\n", "%s");
|
|
|
|
\t\treturn CMD_WARNING;
|
|
|
|
\t}\n"""
|
|
|
|
% (varname, varname)
|
2020-10-07 23:22:26 +02:00
|
|
|
)
|
2017-05-11 15:36:04 +02:00
|
|
|
if attr == "":
|
|
|
|
at = handler.argtype
|
|
|
|
if not at.startswith("const "):
|
|
|
|
at = ". . . " + at
|
2019-06-04 20:27:05 +02:00
|
|
|
doc.append(
|
|
|
|
"\t%-26s %s %s"
|
|
|
|
% (at, "alw" if basename in always_args else "opt", varname)
|
2020-10-07 23:22:26 +02:00
|
|
|
)
|
2017-05-11 15:36:04 +02:00
|
|
|
|
|
|
|
for varname in args.keys():
|
|
|
|
handler = mix_handlers(args[varname])
|
|
|
|
# print(varname, handler)
|
|
|
|
if handler is None:
|
|
|
|
continue
|
2019-06-04 20:27:05 +02:00
|
|
|
do_add(handler, varname, varname)
|
2017-05-11 15:36:04 +02:00
|
|
|
code = handler.code.substitute({"varname": varname}).replace(
|
|
|
|
"\n", "\n\t\t\t"
|
|
|
|
)
|
2017-08-25 18:54:13 +02:00
|
|
|
if handler.canfail:
|
|
|
|
canfail = 1
|
2017-05-11 15:36:04 +02:00
|
|
|
strblock = ""
|
|
|
|
if not handler.drop_str:
|
2019-06-04 20:27:05 +02:00
|
|
|
do_add(
|
|
|
|
StringHandler(None),
|
|
|
|
varname,
|
|
|
|
"%s_str" % (varname),
|
|
|
|
" __attribute__ ((unused))",
|
|
|
|
)
|
2017-05-11 15:36:04 +02:00
|
|
|
strblock = "\n\t\t\t%s_str = argv[_i]->arg;" % (varname)
|
|
|
|
argblocks.append(
|
|
|
|
argblock.substitute(
|
|
|
|
{"varname": varname, "strblock": strblock, "code": code}
|
|
|
|
)
|
2020-10-07 23:22:26 +02:00
|
|
|
)
|
2017-05-11 15:36:04 +02:00
|
|
|
|
|
|
|
if dumpfd is not None:
|
|
|
|
if len(arglist) > 0:
|
|
|
|
dumpfd.write('"%s":\n%s\n\n' % (cmddef, "\n".join(doc)))
|
|
|
|
else:
|
|
|
|
dumpfd.write('"%s":\n\t---- no magic arguments ----\n\n' % (cmddef))
|
|
|
|
|
2022-01-13 14:18:29 +01:00
|
|
|
params["cond_begin"] = "".join(cond_stack)
|
|
|
|
params["cond_end"] = "".join(["#endif\n"] * len(cond_stack))
|
2017-05-11 15:36:04 +02:00
|
|
|
params["argdefs"] = "".join(argdefs)
|
|
|
|
params["argdecls"] = "".join(argdecls)
|
|
|
|
params["arglist"] = "".join(arglist)
|
|
|
|
params["argblocks"] = "".join(argblocks)
|
2017-08-25 18:54:13 +02:00
|
|
|
params["canfail"] = canfail
|
|
|
|
params["nonempty"] = len(argblocks)
|
2019-06-04 20:27:05 +02:00
|
|
|
params["argassert"] = "".join(argassert)
|
2017-05-11 15:36:04 +02:00
|
|
|
ofd.write(templ.substitute(params))
|
|
|
|
|
2019-06-04 17:07:57 +02:00
|
|
|
return errors
|
|
|
|
|
2020-10-07 23:22:26 +02:00
|
|
|
|
2017-05-11 15:36:04 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
argp = argparse.ArgumentParser(description="FRR CLI preprocessor in Python")
|
|
|
|
argp.add_argument(
|
|
|
|
"--all-defun",
|
|
|
|
action="store_const",
|
|
|
|
const=True,
|
|
|
|
help="process DEFUN() statements in addition to DEFPY()",
|
|
|
|
)
|
|
|
|
argp.add_argument(
|
|
|
|
"--show",
|
|
|
|
action="store_const",
|
|
|
|
const=True,
|
|
|
|
help="print out list of arguments and types for each definition",
|
|
|
|
)
|
|
|
|
argp.add_argument("-o", type=str, metavar="OUTFILE", help="output C file name")
|
|
|
|
argp.add_argument("cfile", type=str)
|
|
|
|
args = argp.parse_args()
|
|
|
|
|
|
|
|
dumpfd = None
|
|
|
|
if args.o is not None:
|
|
|
|
ofd = StringIO()
|
|
|
|
if args.show:
|
|
|
|
dumpfd = sys.stdout
|
|
|
|
else:
|
|
|
|
ofd = sys.stdout
|
|
|
|
if args.show:
|
|
|
|
dumpfd = sys.stderr
|
|
|
|
|
2019-07-03 14:53:32 +02:00
|
|
|
basepath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
2019-06-11 15:35:28 +02:00
|
|
|
macros = Macros()
|
|
|
|
macros.load("lib/route_types.h")
|
2019-07-03 14:53:32 +02:00
|
|
|
macros.load(os.path.join(basepath, "lib/command.h"))
|
2019-04-24 19:33:41 +02:00
|
|
|
macros.load(os.path.join(basepath, "bgpd/bgp_vty.h"))
|
2019-06-11 15:35:28 +02:00
|
|
|
# sigh :(
|
2022-01-16 21:43:55 +01:00
|
|
|
macros.setup("PROTO_REDIST_STR", "FRR_REDIST_STR_ISISD")
|
|
|
|
macros.setup("PROTO_IP_REDIST_STR", "FRR_IP_REDIST_STR_ISISD")
|
|
|
|
macros.setup("PROTO_IP6_REDIST_STR", "FRR_IP6_REDIST_STR_ISISD")
|
2019-06-11 15:35:28 +02:00
|
|
|
|
|
|
|
errors = process_file(args.cfile, ofd, dumpfd, args.all_defun, macros)
|
2019-06-04 17:07:57 +02:00
|
|
|
if errors != 0:
|
|
|
|
sys.exit(1)
|
2017-05-11 15:36:04 +02:00
|
|
|
|
|
|
|
if args.o is not None:
|
2018-08-16 00:03:58 +02:00
|
|
|
clippy.wrdiff(
|
|
|
|
args.o, ofd, [args.cfile, os.path.realpath(__file__), sys.executable]
|
|
|
|
)
|