split-man 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/perl
  2. use strict;
  3. ## Copyright (C) Michael Still (mikal@stillhq.com)
  4. ## Released under the terms of the GNU GPL
  5. ##
  6. ## Hoon through the specified DocBook SGML file, and split out the
  7. ## man pages. These can then be processed into groff format, and
  8. ## installed if desired...
  9. ##
  10. ## Arguements: $1 -- the name of the sgml file
  11. ## $2 -- the directory to put the generated SGML files in
  12. ## $3 -- kernel version
  13. my($SGML, $REF, $front, $refdata, $mode, $filename);
  14. if(($ARGV[0] eq "") || ($ARGV[1] eq "") || ($ARGV[2] eq "")){
  15. die "Usage: split-man <sgml file> <output dir> <kernel version>\n";
  16. }
  17. open SGML, "< $ARGV[0]" or die "Could not open input file \"$ARGV[0]\"\n";
  18. if( ! -d "$ARGV[1]" ){
  19. die "Output directory \"$ARGV[1]\" does not exist\n";
  20. }
  21. # Possible modes:
  22. # 0: Looking for input I care about
  23. # 1: Inside book front matter
  24. # 2: Inside a refentry
  25. # 3: Inside a refentry, and we know the filename
  26. $mode = 0;
  27. $refdata = "";
  28. $front = "";
  29. while(<SGML>){
  30. # Starting modes
  31. if(/<bookinfo>/ || /<docinfo>/){
  32. $mode = 1;
  33. }
  34. elsif(/<refentry>/){
  35. $mode = 2;
  36. }
  37. elsif(/<refentrytitle><phrase[^>]*>([^<]*)<.*$/){
  38. $mode = 3;
  39. $filename = $1;
  40. $filename =~ s/struct //;
  41. $filename =~ s/typedef //;
  42. print "Found manpage for $filename\n";
  43. open REF, "> $ARGV[1]/$filename.sgml" or
  44. die "Couldn't open output file \"$ARGV[1]/$filename.sgml\": $!\n";
  45. print REF <<EOF;
  46. <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
  47. <!-- BEGINFRONTTAG: The following is front matter for the parent book -->
  48. $front
  49. <!-- ENDFRONTTAG: End front matter -->
  50. $refdata
  51. EOF
  52. $refdata = "";
  53. }
  54. # Extraction
  55. if($mode == 1){
  56. chomp $_;
  57. $front = "$front<!-- $_ -->\n";
  58. }
  59. elsif($mode == 2){
  60. $refdata = "$refdata$_";
  61. }
  62. elsif($mode == 3){
  63. # There are some fixups which need to be applied
  64. if(/<\/refmeta>/){
  65. print REF "<manvolnum>9</manvolnum>\n";
  66. }
  67. if(/<\/refentry>/){
  68. print REF <<EOF;
  69. <refsect1><title>About this document</title>
  70. <para>
  71. This documentation was generated with kernel version $ARGV[2].
  72. </para>
  73. </refsect1>
  74. EOF
  75. }
  76. # For some reason, we title the synopsis twice in the main DocBook
  77. if(! /<title>Synopsis<\/title>/){
  78. if(/<refentrytitle>/){
  79. s/struct //;
  80. s/typedef //;
  81. }
  82. print REF "$_";
  83. }
  84. }
  85. # Ending modes
  86. if(/<\/bookinfo>/ || /<\/docinfo>/){
  87. $mode = 0;
  88. }
  89. elsif(/<\/refentry>/){
  90. $mode = 0;
  91. close REF;
  92. }
  93. }
  94. # And make sure we don't process this unnessesarily
  95. $ARGV[0] =~ s/\.sgml/.9/;
  96. `touch $ARGV[0]`;