This makes build scripts more portable and human readable.
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
--- /dev/null
+#!/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)
+
+++ /dev/null
-# 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
-}
--- /dev/null
+#!/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)
+
+++ /dev/null
-# 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
-}
-}