mkhelp.pl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.haxx.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. ###########################################################################
  23. # Yeah, I know, probably 1000 other persons already wrote a script like
  24. # this, but I'll tell ya:
  25. # THEY DON'T FIT ME :-)
  26. # Get readme file as parameter:
  27. if($ARGV[0] eq "-c") {
  28. $c=1;
  29. shift @ARGV;
  30. }
  31. push @out, " _ _ ____ _\n";
  32. push @out, " Project ___| | | | _ \\| |\n";
  33. push @out, " / __| | | | |_) | |\n";
  34. push @out, " | (__| |_| | _ <| |___\n";
  35. push @out, " \\___|\\___/|_| \\_\\_____|\n";
  36. my $olen=0;
  37. while (<STDIN>) {
  38. my $line = $_;
  39. # this should be removed:
  40. $line =~ s/(.|_)//g;
  41. # remove trailing CR from line. msysgit checks out files as line+CRLF
  42. $line =~ s/\r$//;
  43. if($line =~ /^([ \t]*\n|curl)/i) {
  44. # cut off headers and empty lines
  45. $wline++; # count number of cut off lines
  46. next;
  47. }
  48. my $text = $line;
  49. $text =~ s/^\s+//g; # cut off preceding...
  50. $text =~ s/\s+$//g; # and trailing whitespaces
  51. $tlen = length($text);
  52. if($wline && ($olen == $tlen)) {
  53. # if the previous line with contents was exactly as long as
  54. # this line, then we ignore the newlines!
  55. # We do this magic because a header may abort a paragraph at
  56. # any line, but we don't want that to be noticed in the output
  57. # here
  58. $wline=0;
  59. }
  60. $olen = $tlen;
  61. if($wline) {
  62. # we only make one empty line max
  63. $wline = 0;
  64. push @out, "\n";
  65. }
  66. push @out, $line;
  67. }
  68. push @out, "\n"; # just an extra newline
  69. print <<HEAD
  70. /*
  71. * NEVER EVER edit this manually, fix the mkhelp.pl script instead!
  72. */
  73. #ifdef USE_MANUAL
  74. #include "tool_hugehelp.h"
  75. HEAD
  76. ;
  77. if($c) {
  78. # If compression requested, check that the Gzip module is available
  79. # or else disable compression
  80. $c = eval
  81. {
  82. require IO::Compress::Gzip;
  83. IO::Compress::Gzip->import();
  84. 1;
  85. };
  86. print STDERR "Warning: compression requested but Gzip is not available\n" if (!$c)
  87. }
  88. if($c)
  89. {
  90. my $content = join("", @out);
  91. my $gzippedContent;
  92. IO::Compress::Gzip::gzip(
  93. \$content, \$gzippedContent, Level => 9, TextFlag => 1, Time=>0) or die "gzip failed:";
  94. $gzip = length($content);
  95. $gzipped = length($gzippedContent);
  96. print <<HEAD
  97. #include <zlib.h>
  98. #include "memdebug.h" /* keep this as LAST include */
  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 shrank 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(split(//, $gzippedContent)) {
  109. my $num=ord($_);
  110. printf(" 0x%02x,", 0+$num);
  111. if(!(++$c % 12)) {
  112. print "\n ";
  113. }
  114. }
  115. print "\n};\n";
  116. print <<EOF
  117. #define BUF_SIZE 0x10000
  118. static voidpf zalloc_func(voidpf opaque, unsigned int items, unsigned int size)
  119. {
  120. (void) opaque;
  121. /* not a typo, keep it calloc() */
  122. return (voidpf) calloc(items, size);
  123. }
  124. static void zfree_func(voidpf opaque, voidpf ptr)
  125. {
  126. (void) opaque;
  127. free(ptr);
  128. }
  129. /* Decompress and send to stdout a gzip-compressed buffer */
  130. void hugehelp(void)
  131. {
  132. unsigned char* buf;
  133. int status,headerlen;
  134. z_stream z;
  135. /* Make sure no gzip options are set */
  136. if (hugehelpgz[3] & 0xfe)
  137. return;
  138. headerlen = 10;
  139. memset(&z, 0, sizeof(z_stream));
  140. z.zalloc = (alloc_func)zalloc_func;
  141. z.zfree = (free_func)zfree_func;
  142. z.avail_in = (unsigned int)(sizeof(hugehelpgz) - headerlen);
  143. z.next_in = (unsigned char *)hugehelpgz + headerlen;
  144. if (inflateInit2(&z, -MAX_WBITS) != Z_OK)
  145. return;
  146. buf = malloc(BUF_SIZE);
  147. if (buf) {
  148. while(1) {
  149. z.avail_out = BUF_SIZE;
  150. z.next_out = buf;
  151. status = inflate(&z, Z_SYNC_FLUSH);
  152. if (status == Z_OK || status == Z_STREAM_END) {
  153. fwrite(buf, BUF_SIZE - z.avail_out, 1, stdout);
  154. if (status == Z_STREAM_END)
  155. break;
  156. }
  157. else
  158. break; /* Error */
  159. }
  160. free(buf);
  161. }
  162. inflateEnd(&z);
  163. }
  164. EOF
  165. ;
  166. foot();
  167. exit;
  168. }
  169. else {
  170. print <<HEAD
  171. void hugehelp(void)
  172. {
  173. fputs(
  174. HEAD
  175. ;
  176. }
  177. $outsize=0;
  178. for(@out) {
  179. chop;
  180. $new = $_;
  181. $outsize += length($new)+1; # one for the newline
  182. $new =~ s/\\/\\\\/g;
  183. $new =~ s/\"/\\\"/g;
  184. # gcc 2.96 claims ISO C89 only is required to support 509 letter strings
  185. if($outsize > 500) {
  186. # terminate and make another fputs() call here
  187. print ", stdout);\n fputs(\n";
  188. $outsize=length($new)+1;
  189. }
  190. printf("\"%s\\n\"\n", $new);
  191. }
  192. print ", stdout) ;\n}\n";
  193. foot();
  194. sub foot {
  195. print <<FOOT
  196. #else /* !USE_MANUAL */
  197. /* built-in manual is disabled, blank function */
  198. #include "tool_hugehelp.h"
  199. void hugehelp(void) {}
  200. #endif /* USE_MANUAL */
  201. FOOT
  202. ;
  203. }