[build] Rewrite ginsh related sed scripts in python.
authorAlexei Sheplyakov <Alexei.Sheplyakov@gmail.com>
Tue, 10 Jan 2012 05:48:29 +0000 (08:48 +0300)
committerAlexei Sheplyakov <Alexei.Sheplyakov@gmail.com>
Tue, 17 Jul 2012 04:49:55 +0000 (07:49 +0300)
This makes build scripts more portable and human readable.

ginsh/Makefile.am
ginsh/ginsh_fcn_help.py [new file with mode: 0755]
ginsh/ginsh_fcn_help.sed [deleted file]
ginsh/ginsh_op_help.py [new file with mode: 0755]
ginsh/ginsh_op_help.sed [deleted file]

index 6789fc4..c4269f5 100644 (file)
@@ -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 (executable)
index 0000000..00f8f2d
--- /dev/null
@@ -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 (file)
index 0aa2f92..0000000
+++ /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 (executable)
index 0000000..cdf63b0
--- /dev/null
@@ -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 (file)
index 8ca265a..0000000
+++ /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
-}
-}