From a06a27122b0a9087e9c64743940016c7e0176ce2 Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Tue, 10 Jan 2012 08:48:29 +0300 Subject: [PATCH] [build] Rewrite ginsh related sed scripts in python. This makes build scripts more portable and human readable. --- ginsh/Makefile.am | 10 +++---- ginsh/ginsh_fcn_help.py | 63 ++++++++++++++++++++++++++++++++++++++++ ginsh/ginsh_fcn_help.sed | 33 --------------------- ginsh/ginsh_op_help.py | 54 ++++++++++++++++++++++++++++++++++ ginsh/ginsh_op_help.sed | 18 ------------ 5 files changed, 122 insertions(+), 56 deletions(-) create mode 100755 ginsh/ginsh_fcn_help.py delete mode 100644 ginsh/ginsh_fcn_help.sed create mode 100755 ginsh/ginsh_op_help.py delete mode 100644 ginsh/ginsh_op_help.sed diff --git a/ginsh/Makefile.am b/ginsh/Makefile.am index 6789fc44..c4269f56 100644 --- a/ginsh/Makefile.am +++ b/ginsh/Makefile.am @@ -10,13 +10,13 @@ AM_YFLAGS = -d man_MANS = ginsh.1 CLEANFILES = ginsh_fcn_help.h ginsh_op_help.h -EXTRA_DIST = ginsh_parser.h ginsh_fcn_help.sed ginsh_op_help.sed +EXTRA_DIST = ginsh_parser.h ginsh_fcn_help.py ginsh_op_help.py # files created by sed scripts -ginsh_fcn_help.h: ginsh.1 $(srcdir)/ginsh_fcn_help.sed - sed -n -f $(srcdir)/ginsh_fcn_help.sed <$< >$@ +ginsh_fcn_help.h: ginsh.1.in $(srcdir)/ginsh_fcn_help.py + $(PYTHON) $(srcdir)/ginsh_fcn_help.py -o $@ $< -ginsh_op_help.h: ginsh.1 $(srcdir)/ginsh_op_help.sed - sed -n -f $(srcdir)/ginsh_op_help.sed <$< >$@ +ginsh_op_help.h: ginsh.1 $(srcdir)/ginsh_op_help.py + $(PYTHON) $(srcdir)/ginsh_op_help.py -o $@ $< ginsh_parser.o: ginsh_fcn_help.h ginsh_op_help.h ginsh_parser.h diff --git a/ginsh/ginsh_fcn_help.py b/ginsh/ginsh_fcn_help.py new file mode 100755 index 00000000..00f8f2d7 --- /dev/null +++ b/ginsh/ginsh_fcn_help.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# encoding: utf-8 +# Convert help for ginsh functions from man page to C source +import sys, re, optparse + +rxStart = re.compile('^.*GINSH_FCN_HELP_START$') +rxEnd = re.compile('^.*GINSH_FCN_HELP_END$') +fcnRx = re.compile('^[.]BI\s+') +hlpRx = re.compile('\\\\[-]') +codeFmt = 'insert_help("%s",\n"%s"\n);\n' + +def parseProto(pStr): + pStr = fcnRx.sub('', pStr) + pStr = re.sub('\s', '', pStr) + pStr = re.sub('"', '', pStr) + pStr = re.sub(',', ', ', pStr) + pStr = re.sub('\\[', ' [', pStr) + name = pStr.split('(')[0] + return name, pStr + +def extractHelp(inp, out): + name, proto, synopsis = None, None, None + seenStart = False + for l in inp: + l = l.strip() + if not seenStart: + if rxStart.match(l): + seenStart = True + continue + if rxEnd.match(l): + break + if fcnRx.match(l): + name, proto = parseProto(l) + elif hlpRx.match(l): + l = hlpRx.sub('', l).strip() + l = re.sub('"', "'", l) + synopsis = '%s"\n" - %s' % ( proto, l ) + elif l.lower() == '.br': + synopsis = synopsis or proto + out.write(codeFmt % ( name, synopsis )) + name, proto, synopsis = None, None, None + +def main(): + op = optparse.OptionParser() + op.add_option('-o', dest = 'outfile') + options, args = op.parse_args() + outfile = sys.stdout + infile = sys.stdin + if not options.outfile is None: + outfile = open(options.outfile, 'wt') + if len(args) >= 1: + infile = open(args[0]) + extractHelp(infile, outfile) + if infile != sys.stdin: + infile.close() + outfile.flush() + if outfile != sys.stdout: + outfile.close() + +if __name__ == '__main__': + main() + sys.exit(0) + diff --git a/ginsh/ginsh_fcn_help.sed b/ginsh/ginsh_fcn_help.sed deleted file mode 100644 index 0aa2f926..00000000 --- a/ginsh/ginsh_fcn_help.sed +++ /dev/null @@ -1,33 +0,0 @@ -# Convert help for ginsh functions from man page to C source -/GINSH_FCN_HELP_START/,/GINSH_FCN_HELP_END/{ - -# .BI lines contain the function synopsis -/\.BI/{ - -# extract function name -h -s/\.BI \(.*\)(.*/insert_help("\1",/p -g - -# extract synopsis -s/"//g -s/ , /,/g -s/\.BI /"/ -s/( /(/ -s/ )/)"/ -p -# handle multi-line synopsis -s/.br/);/p -} - -# \- lines contain the function description -/\\-/{ -s/"/'/g -s/\\-/" -/ -s/$/"/ -p -} - -# .br lines start the next function -/.br/s//);/p -} diff --git a/ginsh/ginsh_op_help.py b/ginsh/ginsh_op_help.py new file mode 100755 index 00000000..cdf63b01 --- /dev/null +++ b/ginsh/ginsh_op_help.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# encoding: utf-8 +# Convert help for ginsh operators from man page to C source +import sys, re, optparse + +rxStart = re.compile('^.*GINSH_OP_HELP_START$') +rxEnd = re.compile('^.*GINSH_OP_HELP_END$') +fcnRx = re.compile('^[.]B\s+') +minusRx = re.compile('\\\\[-]') +codeFmt = 'insert_help("operators", "%s\\t" "%s");\n' + +def extractHelp(inp, out): + sym, synopsis = None, None + seenStart = False + for l in inp: + l = l.strip() + if not seenStart: + if rxStart.match(l): + seenStart = True + continue + if rxEnd.match(l): + if sym is not None: + out.write(codeFmt % ( sym, synopsis )) + break + if fcnRx.match(l): + l = fcnRx.sub('', l) + sym = minusRx.sub('-', l) + elif l.lower() == '.tp': + out.write(codeFmt % ( sym, synopsis )) + sym, synopsis = None, None + else: + synopsis = l + +def main(): + op = optparse.OptionParser() + op.add_option('-o', dest = 'outfile') + options, args = op.parse_args() + outfile = sys.stdout + infile = sys.stdin + if not options.outfile is None: + outfile = open(options.outfile, 'wt') + if len(args) >= 1: + infile = open(args[0]) + extractHelp(infile, outfile) + if infile != sys.stdin: + infile.close() + outfile.flush() + if outfile != sys.stdout: + outfile.close() + +if __name__ == "__main__": + main() + sys.exit(0) + diff --git a/ginsh/ginsh_op_help.sed b/ginsh/ginsh_op_help.sed deleted file mode 100644 index 8ca265a0..00000000 --- a/ginsh/ginsh_op_help.sed +++ /dev/null @@ -1,18 +0,0 @@ -# Convert help for ginsh operators from man page to C source -/GINSH_OP_HELP_START/,/GINSH_OP_HELP_END/{ - -# .B lines contain the operator symbol -/.B/{ - -# extract operator symbol -s/.B \(.*\)/insert_help("operators","\1\\t"/ -s/\\-/-/g -p - -# next line contains description -n -s/^/"/ -s/$/");/ -p -} -} -- 2.44.0