[sheepdog] [PATCH v2 1/2] man: add script/gen_man.pl to generate manuals

MORITA Kazutaka morita.kazutaka at gmail.com
Thu Aug 15 04:44:59 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 man/Makefile.am    |   24 ++--------------
 man/dog.8.in       |    1 -
 man/sheep.8.in     |    1 -
 man/sheepfs.8.in   |    1 -
 script/Makefile.am |    2 +-
 script/gen_man.pl  |   79 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 82 insertions(+), 26 deletions(-)
 create mode 100755 script/gen_man.pl

diff --git a/man/Makefile.am b/man/Makefile.am
index 6d34cdd..d858dc2 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -8,31 +8,11 @@ endif
 
 EXTRA_DIST		= sheep.8.in dog.8.in sheepfs.8.in
 
-sheep.8.options = $(shell ../sheep/sheep -h | \
-	perl -ne 'print ".TP\n.BI $$1 \"\\fR, \\fP\" $$2\n$$3\n" if /^  ([^,]+), (\S+)\s+(.+)/' | \
-	sed 's/\\/\\\\\\/g' | \
-	sed 's/"/\\"/g' | \
-	perl -pe 's/\n/\\n/g')
-
-dog.8.options = $(shell ../dog/dog -h | \
-	perl -ne 'if (/^  (.+?)  \s+(.+)/) {system "../dog/dog $$1 -h"; print "Description:\n  $$2\n"}'| \
-	sed 's/Usage: dog \(.*\)/.TP\n.BI "\1"/g' | \
-	sed 's/\(^[a-zA-Z]*:\)/\n\1/g' | \
-	sed 's/\\/\\\\\\/g' | \
-	sed 's/"/\\"/g' | \
-	perl -pe 's/\n/\\n/g')
-
-sheepfs.8.options = $(shell ../sheepfs/sheepfs -h | \
-	perl -ne 'print ".TP\n.BI $$1 \"\\fR, \\fP\" $$2\n$$3\n" if /^  ([^,]+), (\S+)\s+(.+)/' | \
-	sed 's/\\/\\\\\\/g' | \
-	sed 's/"/\\"/g' | \
-	perl -pe 's/\n/\\n/g')
-
-%: %.in Makefile
+%.8: %.8.in Makefile $(top_srcdir)/script/gen_man.pl $(top_builddir)/%/$*
 	rm -f $@-t $@
 	@sed \
 		-e "s#@DATE@#`date '+%Y-%m-%d'`#g" \
-		-e "s#@OPTIONS@#${$@.options}#g" \
+		-e "s#@OPTIONS@#$(shell $(top_srcdir)/script/gen_man.pl $(top_builddir)/$*/$*)#g" \
 		$< > $@-t
 	mv $@-t $@
 
diff --git a/man/dog.8.in b/man/dog.8.in
index 9834a56..3e48cac 100644
--- a/man/dog.8.in
+++ b/man/dog.8.in
@@ -18,7 +18,6 @@ driver available in qemu(1).
 For more information, run 'dog <command> <subcommand> --help'.
 .SH COMMAND & SUBCOMMAND
 @OPTIONS@
-
 .SH DEPENDENCIES
 \fBSheepdog\fP requires QEMU 0.13.z or later and Corosync 1.y.z or 2.y.z.
 
diff --git a/man/sheep.8.in b/man/sheep.8.in
index 6f855c2..a40bf68 100644
--- a/man/sheep.8.in
+++ b/man/sheep.8.in
@@ -16,7 +16,6 @@ via dog(8).  QEMU virtual machines use the sheep daemon via a block
 driver available in qemu(1).
 .SH OPTIONS
 @OPTIONS@
-
 .SH PATH
 Proper LSB systems will store sheepdog files in /var/lib/sheepdog.  The init
 script uses this directory by default.  The directory must be on a filesystem
diff --git a/man/sheepfs.8.in b/man/sheepfs.8.in
index 0a3a3a1..190d97c 100644
--- a/man/sheepfs.8.in
+++ b/man/sheepfs.8.in
@@ -38,7 +38,6 @@ can be used as
 This file abstraction integrates well into kernel's pagecache.
 .SH OPTIONS
 @OPTIONS@
-
 .SH DEPENDENCIES
 \fBSheepdog\fP requires QEMU 0.13.z or later and Corosync 1.y.z.
 
diff --git a/script/Makefile.am b/script/Makefile.am
index 66ee428..8fccd53 100644
--- a/script/Makefile.am
+++ b/script/Makefile.am
@@ -2,7 +2,7 @@ MAINTAINERCLEANFILES    = Makefile.in
 
 EXTRA_DIST		= generic.in
 
-noinst_HEADERS		= bash_completion_dog checkarch.sh vditest
+noinst_HEADERS		= bash_completion_dog checkarch.sh vditest gen_man.pl
 
 target_INIT             = generic
 
diff --git a/script/gen_man.pl b/script/gen_man.pl
new file mode 100755
index 0000000..67d3b5e
--- /dev/null
+++ b/script/gen_man.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+#
+# Genrate sheepdog manuals from help messages
+#
+
+use strict;
+
+my ($cwd) = ($0 =~ m%^(.+/)%);
+my $program = $ARGV[0];
+
+## generator functions
+
+sub sheep {
+    my ($line) = @_;
+
+    if ($line =~ /^  ([^,]+), (\S+)\s+(.+)/) {
+	my ($opt, $longopt, $desc) = ($1, $2, $3);
+	print escape(header("$opt, $longopt") . "\n");
+	print escape("$desc\n");
+    }
+}
+
+sub dog {
+    my ($line) = @_;
+
+    if ($line =~ /^  (.+?)  \s+(.+)/) {
+	my ($cmd, $desc) = ($1, $2);
+	my $help = join '', `$program $cmd -h`;
+
+	$help =~ s/Usage: dog (.*)/header($1)/e;
+	$help =~ s/^([A-Z][ a-zA-Z]*:)/\n$1/mg;
+
+	print escape("$help\n");
+	print escape("Description:\n  $desc\n");
+    }
+}
+
+sub sheepfs {
+    my ($line) = @_;
+
+    if ($line =~ /^  ([^,]+), (\S+)\s+(.+)/) {
+	my ($opt, $longopt, $desc) = ($1, $2, $3);
+	print escape(header("$opt, $longopt") . "\n");
+	print escape("$desc\n");
+    }
+}
+
+## helper functions
+
+sub header {
+    my ($str) = @_;
+
+    return ".TP\n.BI \"$str\"";
+}
+
+sub escape {
+    my ($str) = @_;
+
+    $str =~ s/\t/  /g;
+    $str =~ s/\\/\\\\\\/g;
+    $str =~ s/"/\\"/g;
+    $str =~ s/#/\\#/g;
+    $str =~ s/\$/\\\$/g;
+    $str =~ s/\n/\\n/g;
+
+    return $str;
+}
+
+## main routine
+
+open IN, "$program -h |" or die "cannot find $program\n";
+my @help = <IN>;
+close IN;
+
+foreach my $help (@help) {
+    my ($func) = ($program =~ m#.*/(.+)#);
+    chomp($help);
+    eval "$func(\"$help\")";
+}
-- 
1.7.9.5




More information about the sheepdog mailing list