mkhelp.pl 5.7 KB

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