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