xref: /curl/src/mkhelp.pl (revision e55db0c8)
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, "      ___| | | |  _ \\| |\n";
40push @out, "     / __| | | | |_) | |\n";
41push @out, "    | (__| |_| |  _ <| |___\n";
42push @out, "     \\___|\\___/|_| \\_\\_____|\n";
43
44my $olen=0;
45while (<STDIN>) {
46    my $line = $_;
47    push @out, $line;
48}
49
50print <<HEAD
51/*
52 * NEVER EVER edit this manually, fix the mkhelp.pl script instead!
53 */
54#ifdef USE_MANUAL
55#include "tool_hugehelp.h"
56HEAD
57    ;
58if($c) {
59    # If compression requested, check that the Gzip module is available
60    # or else disable compression
61    $c = eval
62    {
63      require IO::Compress::Gzip;
64      IO::Compress::Gzip->import();
65      1;
66    };
67    print STDERR "Warning: compression requested but Gzip is not available\n" if (!$c)
68}
69
70if($c)
71{
72    my $content = join("", @out);
73    my $gzippedContent;
74    IO::Compress::Gzip::gzip(
75        \$content, \$gzippedContent, Level => 9, TextFlag => 1, Time=>0) or die "gzip failed:";
76    $gzip = length($content);
77    $gzipped = length($gzippedContent);
78
79    print <<HEAD
80#include <zlib.h>
81#include "memdebug.h" /* keep this as LAST include */
82static const unsigned char hugehelpgz[] = {
83  /* This mumbo-jumbo is the huge help text compressed with gzip.
84     Thanks to this operation, the size of this data shrank from $gzip
85     to $gzipped bytes. You can disable the use of compressed help
86     texts by NOT passing -c to the mkhelp.pl tool. */
87HEAD
88;
89
90    my $c=0;
91    print " ";
92    for(split(//, $gzippedContent)) {
93        my $num=ord($_);
94        printf(" 0x%02x,", 0+$num);
95        if(!(++$c % 12)) {
96            print "\n ";
97        }
98    }
99    print "\n};\n";
100
101    print <<EOF
102#define BUF_SIZE 0x10000
103static voidpf zalloc_func(voidpf opaque, unsigned int items, unsigned int size)
104{
105  (void) opaque;
106  /* not a typo, keep it calloc() */
107  return (voidpf) calloc(items, size);
108}
109static void zfree_func(voidpf opaque, voidpf ptr)
110{
111  (void) opaque;
112  free(ptr);
113}
114/* Decompress and send to stdout a gzip-compressed buffer */
115void hugehelp(void)
116{
117  unsigned char *buf;
118  int status, headerlen;
119  z_stream z;
120
121  /* Make sure no gzip options are set */
122  if(hugehelpgz[3] & 0xfe)
123    return;
124
125  headerlen = 10;
126  memset(&z, 0, sizeof(z_stream));
127  z.zalloc = (alloc_func)zalloc_func;
128  z.zfree = (free_func)zfree_func;
129  z.avail_in = (unsigned int)(sizeof(hugehelpgz) - headerlen);
130  z.next_in = (unsigned char *)hugehelpgz + headerlen;
131
132  if(inflateInit2(&z, -MAX_WBITS) != Z_OK)
133    return;
134
135  buf = malloc(BUF_SIZE);
136  if(buf) {
137    while(1) {
138      z.avail_out = BUF_SIZE;
139      z.next_out = buf;
140      status = inflate(&z, Z_SYNC_FLUSH);
141      if(status == Z_OK || status == Z_STREAM_END) {
142        fwrite(buf, BUF_SIZE - z.avail_out, 1, stdout);
143        if(status == Z_STREAM_END)
144          break;
145      }
146      else
147        break;    /* Error */
148    }
149    free(buf);
150  }
151  inflateEnd(&z);
152}
153EOF
154    ;
155foot();
156exit;
157}
158else {
159    print <<HEAD
160static const char * const curlman[] = {
161HEAD
162        ;
163}
164
165my $blank;
166for my $n (@out) {
167    chomp $n;
168    $n =~ s/\\/\\\\/g;
169    $n =~ s/\"/\\\"/g;
170
171    if(!$n) {
172        $blank++;
173    }
174    else {
175        $n =~ s/        /\\t/g;
176        printf("  \"%s%s\",\n", $blank?"\\n":"", $n);
177        $blank = 0;
178    }
179}
180
181print <<ENDLINE
182  NULL
183};
184void hugehelp(void)
185{
186  int i = 0;
187  while(curlman[i])
188    puts(curlman[i++]);
189}
190ENDLINE
191    ;
192
193foot();
194
195sub foot {
196    print <<FOOT
197#endif /* USE_MANUAL */
198FOOT
199  ;
200}
201