#! /usr/bin/perl -w sub die_arg() { print STDERR "usage: uogo2man PACKAGE PROGRAM SECTION FORMAT [opt SEC]...\n"; exit(2); } my $package=$ARGV[0] || die_arg; my $prog=$ARGV[1] || die_arg; my $secnum=$ARGV[2] || die_arg; my $format=$ARGV[3] || die_arg; my $html=1 if ($format eq "html"); $html=0 if ($format eq "man"); die "format must be html or man" unless (defined($html)); my $i; for ($i=0;$i<4;$i++) { shift(@ARGV); } my $cprog=$prog; $cprog =~ s%^.*/%%; my $datum=`date +%Y-%m-%d`; chomp($datum); sub die_no_help() { die "failed to get help from $prog: $!"; } my $secname; if (!$html) { if ($secnum == 1) { $secname="User Commands"; } elsif ($secnum == 2) { $secname="System Calls"; } elsif ($secnum == 3) { $secname="Library Functions"; } elsif ($secnum == 4) { $secname="Special Files"; } elsif ($secnum == 5) { $secname="File Formats"; } elsif ($secnum == 6) { $secname="Games"; } elsif ($secnum == 7) { $secname="Miscellaneous"; } elsif ($secnum == 8) { $secname="Administration Commands"; } elsif ($secnum == 9) { $secname="Kernel Internals"; } else { $secname="???"; } } if (open(I,") { chop; next if (/^#/); next if (/^$/); if (/^(\S*)\s+(.*)/) { $hl{$1}=$2; } elsif (/^(\S*)/) { $hl{$1}=""; } } close(I); } sub do_hl($) { my $x=shift; my $k; foreach $k (keys %hl) { if ($html) { if ($x !~ m%http://% && $x !~ m%$k(-\S+)*\@(\S+)%) { $x =~ s%(\b$k\b)%$k%g; } } else { $x =~ s/(\b$k\b)/\\fB$k\\fR/g; } } if ($html) { $x =~ s%<(\S+\@\S+)>%$1%g; $x =~ s%(http://\S+)%$1%g; } return $x; } sub copy_option_output($$$) { my $opt=shift; my $x=shift; my $dohl=shift; open(I,"$prog $opt|") || die_no_help; my $flag=0; my $inex=0; my $flag_dl=0; my $kill_usage=0; $kill_usage=1 if ($x eq "SYNOPSIS"); while () { $_=~ s%\\%\\\\%g; if ($kill_usage && /^usage:\s*(.*)/i) { $_=$1."\n"; } if (!$flag) { if ($html) { $n=$x; $n=~ s/\s/-/g; print "

$x


\n"; } else { print ".SH \"$x\"\n" unless ($flag); } } $flag=1; if ($inex && !/^ /) { $inex=0; if ($html) { print "\n"; } else { print ".fi\n"; } } if (/^ *\n$/) { if ($html) { print "

\n"; } else { print ".P\n"; } next; } if (/^ / && !$inex) { $inex=1; if ($html) { print "

\n";
      } else {
	print ".nf\n";
      }
    }
    $_=do_hl($_) if ($dohl);
    print "\\" if (/^'/ && !$html);
    print $_;
  }
  if ($inex) {
    if ($html) {
      print "
\n"; } else { print ".fi\n"; } } if ($flag_dl) { print "\n"; } close(I); } sub copy_manhelp_options_output($$$) { my $opt=shift; my $x=shift; my $dohl=shift; open(I,"$prog $opt|") || die_no_help; my $flag=0; my $flag_dl=0; my $in_indent=0; my $markword=""; while () { $_=~ s%\\%\\\\%g; if (!$flag) { if ($html) { $n=$x; $n=~ s/\s/-/g; print "

$x


\n"; } else { print ".SH \"$x\"\n" unless ($flag); } $flag=1; } if ($in_indent && !/^ /) { $in_indent=0; if ($html) { print "\n"; } else { print ".fi\n"; } } if (/^ / && !$in_indent) { $in_indent=1; if ($html) { print "
\n";
      } else {
	print ".nf\n";
      }
    }
    if (/^$/) {
      if ($html) {
	print "

\n"; } else { print ".br\n"; } next; } if (/^-/) { $markword=""; $markword=$1 if (/=(.*)\n/); if (!$flag_dl && $html) { print "

\n"; $flag_dl=1; } if ($html) { print "
\n"; } else { print ".TP\n"; } print $_; if ($html) { print "
\n"; # } else { # print ".br\n"; } next; } $_=do_hl($_) if ($dohl); if ($markword) { if ($html) { s%(\b$markword\b)%$markword%g; } else { s/(\b$markword\b)/\\fI$markword\\fR/g; } } if (!/^ /) { if ($html) { print "

$_

\n"; } else { print ".SS $_\n"; } next } s/^ //; print "\\" if (/^'/ && !$html); print $_; if (!$html) { print ".br\n"; } } if ($flag_dl) { print "
\n"; } if ($in_indent) { if ($html) { print "
\n"; } else { print ".fi\n"; } } close(I); } if ($html) { if (open(I,") { s/xxPACKAGExx/$package/g; s/xxPROGRAMxx/$cprog/g; print $_; } } } else { print ".TH $cprog $secnum $datum \"\" \"$secname\"\n"; } open(I,"$prog --manhelp-short-desc|") || die_no_help; my $description; while () { $description=$_; last; } close(I); die "no description" unless ($description); if ($html) { print "$cprog - $description\n"; print "

"; } else { print ".SH NAME\n"; print "$cprog \\- $description\n"; } copy_option_output("--manhelp-synopsis","SYNOPSIS",1); copy_option_output("--manhelp-long-desc","DESCRIPTION",1); copy_manhelp_options_output("--manhelp-options","OPTIONS",1); copy_option_output("--examples","EXAMPLES",1); while ($ARGV[0]) { die "bad number of arguments" unless ($ARGV[1]); copy_option_output($ARGV[0],$ARGV[1],1); shift(@ARGV); shift(@ARGV); } copy_option_output("--copyright","COPYRIGHT",1); copy_option_output("--author","AUTHOR",1); copy_option_output("--manhelp-tail","MORE INFORMATION",1); copy_option_output("--see-also","SEE ALSO",1);