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