makeman 4.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/perl

use strict;

## Copyright (C) Michael Still (mikal@stillhq.com)
## Released under the terms of the GNU GPL
##
## A script to make or install the manpages extracted by split-man
##
## Arguements: $1 -- the word "convert" or "install"
##             $2 -- the directory containing the SGML files for the manpages
##             $3 -- the filename which contained the sgmldoc output
##                     (I need this so I know which manpages to convert)

15
my($LISTING, $GENERATED, $INPUT, $OUTPUT, $front, $mode, $filename, $tmpdir);
16 17 18 19 20 21 22 23 24

if($ARGV[0] eq ""){
  die "Usage: makeman [convert | install] <dir> <file>\n";
}

if( ! -d "$ARGV[1]" ){
  die "Output directory \"$ARGV[1]\" does not exist\n";
}

25 26 27 28 29 30 31
if($ENV{"TMPDIR"} ne ""){
  $tmpdir = $ENV{"TMPDIR"};
}
else{
  $tmpdir = "/tmp";
}

32 33 34 35 36 37 38 39 40 41
if($ARGV[0] eq "convert"){
  open LISTING, "grep \"<refentrytitle>\" $ARGV[2] |";
  while(<LISTING>){
    s/<\/.*$//;
    s/^.*>//;
    s/\.sgml//;
    s/struct //;
    s/typedef //;

    chomp;
42 43 44 45 46 47 48 49 50
    $filename = $_;
    print "Processing $filename\n";

    # Open the input file to extract the front matter, generate the man page,
    # and open it, and the rearrange everything until it is happy
    open INPUT, "< $ARGV[1]/$filename.sgml";
    $front = "";
    $mode = 0;

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    # The modes used here are:
    #                                                         mode = 0
    # <!-- BEGINFRONTTAG -->
    # <!-- <bookinfo>                                         mode = 1
    # <!--   <legalnotice>                                    mode = 2
    # <!--     ...GPL or whatever...
    # <!--   </legalnotice>                                   mode = 4
    # <!-- </bookinfo>                                        mode = 3
    # <!-- ENDFRONTTAG -->
    #
    # ...doco...

    # I know that some of the if statements in this while loop are in a funny
    # order, but that is deliberate...
    while(<INPUT>){
66 67 68
      if($mode > 0){
	s/<!-- //;
	s/ -->//;
69 70 71
	s/<docinfo>//i;
	s<\/docinfo>//i;
	s/^[ \t]*//i;
72 73 74
      }

      if($mode == 2){
75
	if(/<para>/i){
76
	}
77
	elsif(/<\/para>/i){
78 79
	  $front = "$front.\\\" \n";
	}
80 81
	elsif(/<\/legalnotice>/i){
	  $mode = 4;
82 83 84 85 86 87 88 89 90
	}
	elsif(/^[ \t]*$/){
	}
	else{
	  $front = "$front.\\\"     $_";
	}
      }

      if($mode == 1){
91
	if(/<title>(.*)<\/title>/i){
92 93
	  $front = "$front.\\\" This documentation was generated from the book titled \"$1\", which is part of the Linux kernel source.\n.\\\" \n";
	}
94
	elsif(/<legalnotice>/i){
95 96 97 98
	  $front = "$front.\\\" This documentation comes with the following legal notice:\n.\\\" \n";
	  $mode = 2;
	}

99
	elsif(/<author>/i){
100 101
	  $front = "$front.\\\" Documentation by: ";
	}
102
	elsif(/<firstname>(.*)<\/firstname>/i){
103 104
	  $front = "$front$1 ";
	}
105
	elsif(/<surname>(.*)<\/surname>/i){
106 107
	  $front = "$front$1 ";
	}
108
	elsif(/<email>(.*)<\/email>/i){
109 110
	  $front = "$front($1)";
	}
111
	elsif(/\/author>/i){
112 113 114
	  $front = "$front\n";
	}

115
	elsif(/<copyright>/i){
116 117
	  $front = "$front.\\\" Documentation copyright: ";
	}
118
	elsif(/<holder>(.*)<\/holder>/i){
119 120
	  $front = "$front$1 ";
	}
121
	elsif(/<year>(.*)<\/year>/i){
122 123
	  $front = "$front$1 ";
	}
124
	elsif(/\/copyright>/i){
125 126 127 128
	  $front = "$front\n";
	}

	elsif(/^[ \t]*$/
129 130 131 132 133 134 135 136 137 138 139 140 141
	      || /<affiliation>/i
	      || /<\/affiliation>/i
	      || /<address>/i
	      || /<\/address>/i
	      || /<authorgroup>/i
	      || /<\/authorgroup>/i
	      || /<\/legalnotice>/i
              || /<date>/i
              || /<\/date>/i
              || /<edition>/i
              || /<\/edition>/i
	      || /<pubdate>/i
	      || /<\/pubdate>/i){
142 143 144 145 146 147
	}
	else{
	  print "Unknown tag in manpage conversion: $_";
	  }
      }

148 149 150 151 152 153 154 155 156 157
      if($mode == 0){
	if(/<bookinfo>/i){
	  $mode = 1;
	}
      }

      if($mode == 4){
	if(/<\/bookinfo>/i){
	  $mode = 3;
	}
158 159 160 161
      }
    }
    close INPUT;

162 163
    system("cd $ARGV[1]; docbook2man $filename.sgml; mv $filename.9 $tmpdir/$$.9\n");
    open GENERATED, "< $tmpdir/$$.9";
164 165 166 167 168 169 170 171 172 173 174
    open OUTPUT, "> $ARGV[1]/$filename.9";

    print OUTPUT "$front";
    print OUTPUT ".\\\" For comments on the formatting of this manpage, please contact Michael Still <mikal\@stillhq.com>\n\n";
    while(<GENERATED>){
      print OUTPUT "$_";
    }
    close OUTPUT;
    close GENERATED;

    system("gzip -f $ARGV[1]/$filename.9\n");
175
    unlink("$tmpdir/$$.9");
176 177 178 179 180 181 182 183 184 185
  }
}
elsif($ARGV[0] eq "install"){
  system("mkdir -p /usr/local/man/man9/; install $ARGV[1]/*.9.gz /usr/local/man/man9/");
}
else{
  die "Usage: makeman [convert | install] <dir> <file>\n";
}

print "Done\n";