def process_file(fn, ofd, dumpfd, all_defun, macros): errors = 0 filedata = clippy.parse(fn) cond_stack = [] for entry in filedata["data"]: 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) continue if entry["type"].startswith("DEFPY") or ( all_defun and entry["type"].startswith("DEFUN")): if len(entry["args"][0]) != 1: sys.stderr.write( "%s:%d: DEFPY function name not parseable (%r)\n" % (fn, entry["lineno"], entry["args"][0])) errors += 1 continue cmddef = entry["args"][2] cmddefx = [] for i in cmddef: 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)) errors += 1 cmddefx = None break if cmddefx is None: continue cmddef = "".join([i for i in cmddefx]) graph = clippy.Graph(cmddef) args = OrderedDict() always_args = set() 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)) always_args.add(token.varname) get_always_args(graph.first(), always_args) # print('-' * 76) # pprint(entry) # clippy.dump(graph) # pprint(args) params = {"cmddef": cmddef, "fnname": entry["args"][0][0]} argdefs = [] argdecls = [] arglist = [] argblocks = [] argassert = [] doc = [] canfail = 0 def do_add(handler, basename, varname, attr=""): 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"))) arglist.append(", %s%s" % (handler.deref, varname)) 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)) if attr == "": at = handler.argtype if not at.startswith("const "): at = ". . . " + at doc.append( "\t%-26s %s %s" % (at, "alw" if basename in always_args else "opt", varname)) for varname in args.keys(): handler = mix_handlers(args[varname]) # print(varname, handler) if handler is None: continue do_add(handler, varname, varname) code = handler.code.substitute({ "varname": varname }).replace("\n", "\n\t\t\t") if handler.canfail: canfail = 1 strblock = "" if not handler.drop_str: do_add( StringHandler(None), varname, "%s_str" % (varname), " __attribute__ ((unused))", ) strblock = "\n\t\t\t%s_str = argv[_i]->arg;" % (varname) argblocks.append( argblock.substitute({ "varname": varname, "strblock": strblock, "code": code })) 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)) params["cond_begin"] = "".join(cond_stack) params["cond_end"] = "".join(["#endif\n"] * len(cond_stack)) params["argdefs"] = "".join(argdefs) params["argdecls"] = "".join(argdecls) params["arglist"] = "".join(arglist) params["argblocks"] = "".join(argblocks) params["canfail"] = canfail params["nonempty"] = len(argblocks) params["argassert"] = "".join(argassert) ofd.write(templ.substitute(params)) return errors
def process_file(fn, ofd, dumpfd, all_defun): filedata = clippy.parse(fn) for entry in filedata['data']: if entry['type'].startswith('DEFPY') or (all_defun and entry['type'].startswith('DEFUN')): cmddef = entry['args'][2] cmddef = ''.join([i[1:-1] for i in cmddef]) graph = clippy.Graph(cmddef) args = OrderedDict() 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)) #print('-' * 76) #pprint(entry) #clippy.dump(graph) #pprint(args) params = { 'cmddef': cmddef, 'fnname': entry['args'][0][0] } argdefs = [] argdecls = [] arglist = [] argblocks = [] doc = [] canfail = 0 def do_add(handler, varname, attr = ''): 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'))) arglist.append(', %s%s' % (handler.deref, varname)) if attr == '': at = handler.argtype if not at.startswith('const '): at = '. . . ' + at doc.append('\t%-26s %s' % (at, varname)) for varname in args.keys(): handler = mix_handlers(args[varname]) #print(varname, handler) if handler is None: continue do_add(handler, varname) code = handler.code.substitute({'varname': varname}).replace('\n', '\n\t\t\t') if handler.canfail: canfail = 1 strblock = '' if not handler.drop_str: do_add(StringHandler(None), '%s_str' % (varname), ' __attribute__ ((unused))') strblock = '\n\t\t\t%s_str = argv[_i]->arg;' % (varname) argblocks.append(argblock.substitute({'varname': varname, 'strblock': strblock, 'code': code})) 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)) params['argdefs'] = ''.join(argdefs) params['argdecls'] = ''.join(argdecls) params['arglist'] = ''.join(arglist) params['argblocks'] = ''.join(argblocks) params['canfail'] = canfail params['nonempty'] = len(argblocks) ofd.write(templ.substitute(params))
def process_file(fn, ofd, dumpfd, all_defun, macros): errors = 0 filedata = clippy.parse(fn) for entry in filedata['data']: if entry['type'].startswith('DEFPY') or ( all_defun and entry['type'].startswith('DEFUN')): if len(entry['args'][0]) != 1: sys.stderr.write( '%s:%d: DEFPY function name not parseable (%r)\n' % (fn, entry['lineno'], entry['args'][0])) errors += 1 continue cmddef = entry['args'][2] cmddefx = [] for i in cmddef: 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)) errors += 1 cmddefx = None break if cmddefx is None: continue cmddef = ''.join([i for i in cmddefx]) graph = clippy.Graph(cmddef) args = OrderedDict() always_args = set() 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)) always_args.add(token.varname) get_always_args(graph.first(), always_args) #print('-' * 76) #pprint(entry) #clippy.dump(graph) #pprint(args) params = {'cmddef': cmddef, 'fnname': entry['args'][0][0]} argdefs = [] argdecls = [] arglist = [] argblocks = [] argassert = [] doc = [] canfail = 0 def do_add(handler, basename, varname, attr=''): 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'))) arglist.append(', %s%s' % (handler.deref, varname)) 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)) if attr == '': at = handler.argtype if not at.startswith('const '): at = '. . . ' + at doc.append( '\t%-26s %s %s' % (at, 'alw' if basename in always_args else 'opt', varname)) for varname in args.keys(): handler = mix_handlers(args[varname]) #print(varname, handler) if handler is None: continue do_add(handler, varname, varname) code = handler.code.substitute({ 'varname': varname }).replace('\n', '\n\t\t\t') if handler.canfail: canfail = 1 strblock = '' if not handler.drop_str: do_add(StringHandler(None), varname, '%s_str' % (varname), ' __attribute__ ((unused))') strblock = '\n\t\t\t%s_str = argv[_i]->arg;' % (varname) argblocks.append( argblock.substitute({ 'varname': varname, 'strblock': strblock, 'code': code })) 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)) params['argdefs'] = ''.join(argdefs) params['argdecls'] = ''.join(argdecls) params['arglist'] = ''.join(arglist) params['argblocks'] = ''.join(argblocks) params['canfail'] = canfail params['nonempty'] = len(argblocks) params['argassert'] = ''.join(argassert) ofd.write(templ.substitute(params)) return errors