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