zlib: enforce full initialization of our data space z_stream structs
[platform/upstream/curl.git] / src / mkhelp.pl
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
31 if($ARGV[0] eq "-c") {
32     $c=1;
33     shift @ARGV;
34 }
35
36 my $README = $ARGV[0];
37
38 if($README eq "") {
39     print "usage: mkreadme.pl [-c] <README> < manpage\n";
40     exit;
41 }
42
43
44 push @out, "                                  _   _ ____  _\n";
45 push @out, "  Project                     ___| | | |  _ \\| |\n";
46 push @out, "                             / __| | | | |_) | |\n";
47 push @out, "                            | (__| |_| |  _ <| |___\n";
48 push @out, "                             \\___|\\___/|_| \\_\\_____|\n";
49
50 my $olen=0;
51 while (<STDIN>) {
52     my $line = $_;
53
54     # this should be removed:
55     $line =~ s/(\b.|_\b)//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 }
87 push @out, "\n"; # just an extra newline
88
89 open(READ, "<$README") ||
90     die "couldn't read the README infile $README";
91
92 while(<READ>) {
93     push @out, $_;
94 }
95 close(READ);
96
97 # if compressed
98 if($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;
131 print <<HEAD
132 /*
133  * NEVER EVER edit this manually, fix the mkhelp.pl script instead!
134  * Generation time: $now
135  */
136 #include "setup.h"
137 #ifdef USE_MANUAL
138 #include "hugehelp.h"
139 HEAD
140     ;
141 if($c) {
142     print <<HEAD
143 #include <zlib.h>
144 static 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. */
149 HEAD
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
168 /* Decompress and send to stdout a gzip-compressed buffer */
169 void hugehelp(void)
170 {
171   unsigned char* buf;
172   int status,headerlen;
173   z_stream z;
174
175   /* Make sure no gzip options are set */
176   if (hugehelpgz[3] & 0xfe)
177     return;
178
179   headerlen = 10;
180   memset(&z, 0, sizeof(z_stream));
181   z.avail_in = (unsigned int)(sizeof(hugehelpgz) - headerlen);
182   z.next_in = (unsigned char *)hugehelpgz + headerlen;
183
184   if (inflateInit2(&z, -MAX_WBITS) != Z_OK)
185     return;
186
187   buf = malloc(BUF_SIZE);
188   if (buf) {
189     while(1) {
190       z.avail_out = BUF_SIZE;
191       z.next_out = buf;
192       status = inflate(&z, Z_SYNC_FLUSH);
193       if (status == Z_OK || status == Z_STREAM_END) {
194         fwrite(buf, BUF_SIZE - z.avail_out, 1, stdout);
195         if (status == Z_STREAM_END)
196           break;
197       }
198       else
199         break;    /* Error */
200     }
201     free(buf);
202   }
203   inflateEnd(&z);
204 }
205 EOF
206     ;
207 foot();
208 exit;
209 }
210 else {
211     print <<HEAD
212 void hugehelp(void)
213 {
214    fputs(
215 HEAD
216          ;
217 }
218
219 $outsize=0;
220 for(@out) {
221     chop;
222
223     $new = $_;
224
225     $outsize += length($new)+1; # one for the newline
226
227     $new =~ s/\\/\\\\/g;
228     $new =~ s/\"/\\\"/g;
229
230     # gcc 2.96 claims ISO C89 only is required to support 509 letter strings
231     if($outsize > 500) {
232         # terminate and make another fputs() call here
233         print ", stdout);\n fputs(\n";
234         $outsize=length($new)+1;
235     }
236     printf("\"%s\\n\"\n", $new);
237
238 }
239
240 print ", stdout) ;\n}\n";
241
242 foot();
243
244 sub foot {
245   print <<FOOT
246 #endif /* USE_MANUAL */
247 FOOT
248   ;
249 }