mkhelp.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/local/bin/perl
  2. # Yeah, I know, probably 1000 other persons already wrote a script like
  3. # this, but I'll tell ya:
  4. # THEY DON'T FIT ME :-)
  5. # Get readme file as parameter:
  6. if($ARGV[0] eq "-c") {
  7. $c=1;
  8. shift @ARGV;
  9. }
  10. my $README = $ARGV[0];
  11. if($README eq "") {
  12. print "usage: mkreadme.pl [-c] <README> < manpage\n";
  13. exit;
  14. }
  15. push @out, " _ _ ____ _ \n";
  16. push @out, " Project ___| | | | _ \\| | \n";
  17. push @out, " / __| | | | |_) | | \n";
  18. push @out, " | (__| |_| | _ <| |___ \n";
  19. push @out, " \\___|\\___/|_| \\_\\_____|\n";
  20. my $olen=0;
  21. while (<STDIN>) {
  22. my $line = $_;
  23. # this should be removed:
  24. $line =~ s/(.|_)//g;
  25. if($line =~ /^([ \t]*\n|curl)/i) {
  26. # cut off headers and empty lines
  27. $wline++; # count number of cut off lines
  28. next;
  29. }
  30. my $text = $line;
  31. $text =~ s/^\s+//g; # cut off preceeding...
  32. $text =~ s/\s+$//g; # and trailing whitespaces
  33. $tlen = length($text);
  34. if($wline && ($olen == $tlen)) {
  35. # if the previous line with contents was exactly as long as
  36. # this line, then we ignore the newlines!
  37. # We do this magic because a header may abort a paragraph at
  38. # any line, but we don't want that to be noticed in the output
  39. # here
  40. $wline=0;
  41. }
  42. $olen = $tlen;
  43. if($wline) {
  44. # we only make one empty line max
  45. $wline = 0;
  46. push @out, "\n";
  47. }
  48. push @out, $line;
  49. }
  50. push @out, "\n"; # just an extra newline
  51. open(READ, "<$README") ||
  52. die "couldn't read the README infile $README";
  53. while(<READ>) {
  54. push @out, $_;
  55. }
  56. close(READ);
  57. # if compressed
  58. if($c) {
  59. my @test = `gzip --version 2>&1`;
  60. if($test[0] =~ /gzip/) {
  61. open(GZIP, ">dumpit") ||
  62. die "can't create the dumpit file, try without -c";
  63. binmode GZIP;
  64. for(@out) {
  65. print GZIP $_;
  66. $gzip += length($_);
  67. }
  68. close(GZIP);
  69. system("gzip --best --no-name dumpit");
  70. open(GZIP, "<dumpit.gz") ||
  71. die "can't read the dumpit.gz file, try without -c";
  72. binmode GZIP;
  73. while(<GZIP>) {
  74. push @gzip, $_;
  75. $gzipped += length($_);
  76. }
  77. close(GZIP);
  78. unlink("dumpit.gz");
  79. }
  80. else {
  81. # no gzip, no compression!
  82. undef $c;
  83. print STDERR "MEEEP: Couldn't find gzip, disable compression\n";
  84. }
  85. }
  86. $now = localtime;
  87. print <<HEAD
  88. /*
  89. * NEVER EVER edit this manually, fix the mkhelp.pl script instead!
  90. * Generation time: $now
  91. */
  92. #include "hugehelp.h"
  93. #include <stdio.h>
  94. HEAD
  95. ;
  96. if($c) {
  97. print <<HEAD
  98. #include <zlib.h>
  99. static const unsigned char hugehelpgz[] = {
  100. /* This mumbo-jumbo is the huge help text compressed with gzip.
  101. Thanks to this operation, the size of this data shrunk from $gzip
  102. to $gzipped bytes. You can disable the use of compressed help
  103. texts by NOT passing -c to the mkhelp.pl tool. */
  104. HEAD
  105. ;
  106. my $c=0;
  107. print " ";
  108. for(@gzip) {
  109. my @all=split(//, $_);
  110. for(@all) {
  111. my $num=ord($_);
  112. printf("0x%02x, ", 0+$num);
  113. if(++$c>11) {
  114. print "\n ";
  115. $c=0;
  116. }
  117. }
  118. }
  119. print "\n};\n";
  120. print <<EOF
  121. /* Decompress and send to stdout a gzip-compressed buffer */
  122. void hugehelp(void)
  123. {
  124. unsigned char buf[0x10000];
  125. int status,headerlen;
  126. z_stream z;
  127. /* Make sure no gzip options are set */
  128. if (hugehelpgz[3] & 0xfe)
  129. return;
  130. headerlen = 10;
  131. z.avail_in = sizeof(hugehelpgz) - headerlen;
  132. z.next_in = (unsigned char *)hugehelpgz + headerlen;
  133. z.zalloc = (alloc_func)Z_NULL;
  134. z.zfree = (free_func)Z_NULL;
  135. z.opaque = 0;
  136. if (inflateInit2(&z, -MAX_WBITS) != Z_OK)
  137. return;
  138. while(1) {
  139. z.avail_out = (int)sizeof(buf);
  140. z.next_out = buf;
  141. status = inflate(&z, Z_SYNC_FLUSH);
  142. if (status == Z_OK || status == Z_STREAM_END) {
  143. fwrite(buf, sizeof(buf) - z.avail_out, 1, stdout);
  144. if (status == Z_STREAM_END)
  145. break;
  146. }
  147. else
  148. break; /* Error */
  149. }
  150. inflateEnd(&z);
  151. }
  152. EOF
  153. ;
  154. exit;
  155. }
  156. else {
  157. print <<HEAD
  158. void hugehelp(void)
  159. {
  160. fputs(
  161. HEAD
  162. ;
  163. }
  164. $outsize=0;
  165. for(@out) {
  166. chop;
  167. $new = $_;
  168. $outsize += length($new)+1; # one for the newline
  169. $new =~ s/\\/\\\\/g;
  170. $new =~ s/\"/\\\"/g;
  171. # gcc 2.96 claims ISO C89 only is required to support 509 letter strings
  172. if($outsize > 500) {
  173. # terminate and make another fputs() call here
  174. print ", stdout);\n fputs(\n";
  175. $outsize=length($new)+1;
  176. }
  177. printf("\"%s\\n\"\n", $new);
  178. }
  179. print ", stdout) ;\n}\n"