chiark / gitweb /
_keep_symbols_compat
[curl.git] / src / mkhelp.pl
1 #!/usr/bin/env perl
2 #***************************************************************************
3 #                                  _   _ ____  _
4 #  Project                     ___| | | |  _ \| |
5 #                             / __| | | | |_) | |
6 #                            | (__| |_| |  _ <| |___
7 #                             \___|\___/|_| \_\_____|
8 #
9 # Copyright (C) 1998 - 2014, 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.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: mkhelp.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     # remove trailing CR from line. msysgit checks out files as line+CRLF
58     $line =~ s/\r$//;
59
60     if($line =~ /^([ \t]*\n|curl)/i) {
61         # cut off headers and empty lines
62         $wline++; # count number of cut off lines
63         next;
64     }
65
66     my $text = $line;
67     $text =~ s/^\s+//g; # cut off preceding...
68     $text =~ s/\s+$//g; # and trailing whitespaces
69
70     $tlen = length($text);
71
72     if($wline && ($olen == $tlen)) {
73         # if the previous line with contents was exactly as long as
74         # this line, then we ignore the newlines!
75
76         # We do this magic because a header may abort a paragraph at
77         # any line, but we don't want that to be noticed in the output
78         # here
79         $wline=0;
80     }
81     $olen = $tlen;
82
83     if($wline) {
84         # we only make one empty line max
85         $wline = 0;
86         push @out, "\n";
87     }
88     push @out, $line;
89 }
90 push @out, "\n"; # just an extra newline
91
92 open(READ, "<$README") ||
93     die "couldn't read the README infile $README";
94
95 while(<READ>) {
96     my $line = $_;
97
98     # remove trailing CR from line. msysgit checks out files as line+CRLF
99     $line =~ s/\r$//;
100
101     push @out, $line;
102 }
103 close(READ);
104
105 $now = localtime;
106 print <<HEAD
107 /*
108  * NEVER EVER edit this manually, fix the mkhelp.pl script instead!
109  * Generation time: $now
110  */
111 #ifdef USE_MANUAL
112 #include "tool_hugehelp.h"
113 HEAD
114     ;
115 if($c) {
116     # If compression requested, check that the Gzip module is available
117     # or else disable compression
118     $c = eval
119     {
120       require IO::Compress::Gzip;
121       IO::Compress::Gzip->import();
122       1;
123     };
124     print STDERR "Warning: compression requested but Gzip is not available\n" if (!$c)
125 }
126
127 if($c)
128 {
129     my $content = join("", @out);
130     my $gzippedContent;
131     IO::Compress::Gzip::gzip(
132         \$content, \$gzippedContent, Level => 9, TextFlag => 1, Time=>0) or die "gzip failed:";
133     $gzip = length($content);
134     $gzipped = length($gzippedContent);
135
136     print <<HEAD
137 #include <zlib.h>
138 #include "memdebug.h" /* keep this as LAST include */
139 static const unsigned char hugehelpgz[] = {
140   /* This mumbo-jumbo is the huge help text compressed with gzip.
141      Thanks to this operation, the size of this data shrank from $gzip
142      to $gzipped bytes. You can disable the use of compressed help
143      texts by NOT passing -c to the mkhelp.pl tool. */
144 HEAD
145 ;
146
147     my $c=0;
148     print " ";
149     for(split(//, $gzippedContent)) {
150         my $num=ord($_);
151         printf(" 0x%02x,", 0+$num);
152         if(!(++$c % 12)) {
153             print "\n ";
154         }
155     }
156     print "\n};\n";
157
158     print <<EOF
159 #define BUF_SIZE 0x10000
160 static voidpf zalloc_func(voidpf opaque, unsigned int items, unsigned int size)
161 {
162   (void) opaque;
163   /* not a typo, keep it calloc() */
164   return (voidpf) calloc(items, size);
165 }
166 static void zfree_func(voidpf opaque, voidpf ptr)
167 {
168   (void) opaque;
169   free(ptr);
170 }
171 /* Decompress and send to stdout a gzip-compressed buffer */
172 void hugehelp(void)
173 {
174   unsigned char* buf;
175   int status,headerlen;
176   z_stream z;
177
178   /* Make sure no gzip options are set */
179   if (hugehelpgz[3] & 0xfe)
180     return;
181
182   headerlen = 10;
183   memset(&z, 0, sizeof(z_stream));
184   z.zalloc = (alloc_func)zalloc_func;
185   z.zfree = (free_func)zfree_func;
186   z.avail_in = (unsigned int)(sizeof(hugehelpgz) - headerlen);
187   z.next_in = (unsigned char *)hugehelpgz + headerlen;
188
189   if (inflateInit2(&z, -MAX_WBITS) != Z_OK)
190     return;
191
192   buf = malloc(BUF_SIZE);
193   if (buf) {
194     while(1) {
195       z.avail_out = BUF_SIZE;
196       z.next_out = buf;
197       status = inflate(&z, Z_SYNC_FLUSH);
198       if (status == Z_OK || status == Z_STREAM_END) {
199         fwrite(buf, BUF_SIZE - z.avail_out, 1, stdout);
200         if (status == Z_STREAM_END)
201           break;
202       }
203       else
204         break;    /* Error */
205     }
206     free(buf);
207   }
208   inflateEnd(&z);
209 }
210 EOF
211     ;
212 foot();
213 exit;
214 }
215 else {
216     print <<HEAD
217 void hugehelp(void)
218 {
219    fputs(
220 HEAD
221          ;
222 }
223
224 $outsize=0;
225 for(@out) {
226     chop;
227
228     $new = $_;
229
230     $outsize += length($new)+1; # one for the newline
231
232     $new =~ s/\\/\\\\/g;
233     $new =~ s/\"/\\\"/g;
234
235     # gcc 2.96 claims ISO C89 only is required to support 509 letter strings
236     if($outsize > 500) {
237         # terminate and make another fputs() call here
238         print ", stdout);\n fputs(\n";
239         $outsize=length($new)+1;
240     }
241     printf("\"%s\\n\"\n", $new);
242
243 }
244
245 print ", stdout) ;\n}\n";
246
247 foot();
248
249 sub foot {
250   print <<FOOT
251 #else /* !USE_MANUAL */
252 /* built-in manual is disabled, blank function */
253 #include "tool_hugehelp.h"
254 void hugehelp(void) {}
255 #endif /* USE_MANUAL */
256 FOOT
257   ;
258 }