quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

mkhelp.pl (6060B)


      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 if($ARGV[0] eq "-c") {
     27     $c=1;
     28     shift @ARGV;
     29 }
     30 
     31 push @out, "          _   _ ____  _\n";
     32 push @out, "      ___| | | |  _ \\| |\n";
     33 push @out, "     / __| | | | |_) | |\n";
     34 push @out, "    | (__| |_| |  _ <| |___\n";
     35 push @out, "     \\___|\\___/|_| \\_\\_____|\n";
     36 
     37 while(<STDIN>) {
     38     my $line = $_;
     39     push @out, $line;
     40 }
     41 
     42 print <<HEAD
     43 /*
     44  * NEVER EVER edit this manually, fix the mkhelp.pl script instead!
     45  */
     46 #include "tool_hugehelp.h"
     47 #ifdef USE_MANUAL
     48 #include "tool_help.h"
     49 
     50 HEAD
     51     ;
     52 if($c) {
     53     # If compression requested, check that the Gzip module is available
     54     # or else disable compression
     55     $c = eval
     56     {
     57       require IO::Compress::Gzip;
     58       IO::Compress::Gzip->import();
     59       1;
     60     };
     61     print STDERR "Warning: compression requested but Gzip is not available\n" if(!$c)
     62 }
     63 
     64 if($c)
     65 {
     66     my $content = join("", @out);
     67     my $gzippedContent;
     68     IO::Compress::Gzip::gzip(
     69         \$content, \$gzippedContent, Level => 9, TextFlag => 1, Time=>0) or die "gzip failed:";
     70     $gzip = length($content);
     71     $gzipped = length($gzippedContent);
     72 
     73     print <<HEAD
     74 #include <zlib.h>
     75 #include <memdebug.h> /* keep this as LAST include */
     76 static const unsigned char hugehelpgz[] = {
     77   /* This mumbo-jumbo is the huge help text compressed with gzip.
     78      Thanks to this operation, the size of this data shrank from $gzip
     79      to $gzipped bytes. You can disable the use of compressed help
     80      texts by NOT passing -c to the mkhelp.pl tool. */
     81 HEAD
     82 ;
     83 
     84     my $c=0;
     85     for(split(//, $gzippedContent)) {
     86         my $num=ord($_);
     87         if(!($c % 12)) {
     88             print " ";
     89         }
     90         printf(" 0x%02x,", 0+$num);
     91         if(!(++$c % 12)) {
     92             print "\n";
     93         }
     94     }
     95     print "\n};\n";
     96 
     97     print <<EOF
     98 #define BUF_SIZE 0x10000
     99 static voidpf zalloc_func(voidpf opaque, unsigned int items, unsigned int size)
    100 {
    101   (void) opaque;
    102   /* not a typo, keep it calloc() */
    103   return (voidpf) calloc(items, size);
    104 }
    105 static void zfree_func(voidpf opaque, voidpf ptr)
    106 {
    107   (void) opaque;
    108   free(ptr);
    109 }
    110 
    111 #define HEADERLEN 10
    112 
    113 /* Decompress and send to stdout a gzip-compressed buffer */
    114 void hugehelp(void)
    115 {
    116   unsigned char *buf;
    117   int status;
    118   z_stream z;
    119 
    120   /* Make sure no gzip options are set */
    121   if(hugehelpgz[3] & 0xfe)
    122     return;
    123 
    124   memset(&z, 0, sizeof(z_stream));
    125   z.zalloc = (alloc_func)zalloc_func;
    126   z.zfree = (free_func)zfree_func;
    127   z.avail_in = (uInt)(sizeof(hugehelpgz) - HEADERLEN);
    128   z.next_in = (z_const Bytef *)hugehelpgz + HEADERLEN;
    129 
    130   if(inflateInit2(&z, -MAX_WBITS) != Z_OK)
    131     return;
    132 
    133   buf = malloc(BUF_SIZE);
    134   if(buf) {
    135     while(1) {
    136       z.avail_out = BUF_SIZE;
    137       z.next_out = buf;
    138       status = inflate(&z, Z_SYNC_FLUSH);
    139       if(status == Z_OK || status == Z_STREAM_END) {
    140         fwrite(buf, BUF_SIZE - z.avail_out, 1, stdout);
    141         if(status == Z_STREAM_END)
    142           break;
    143       }
    144       else
    145         break;    /* error */
    146     }
    147     free(buf);
    148   }
    149   inflateEnd(&z);
    150 }
    151 /* Show the help text for the 'arg' curl argument on stdout */
    152 void showhelp(const char *trigger, const char *arg, const char *endarg)
    153 {
    154   unsigned char *buf;
    155   int status;
    156   z_stream z;
    157   struct scan_ctx ctx;
    158   inithelpscan(&ctx, trigger, arg, endarg);
    159 
    160   /* Make sure no gzip options are set */
    161   if(hugehelpgz[3] & 0xfe)
    162     return;
    163 
    164   memset(&z, 0, sizeof(z_stream));
    165   z.zalloc = (alloc_func)zalloc_func;
    166   z.zfree = (free_func)zfree_func;
    167   z.avail_in = (uInt)(sizeof(hugehelpgz) - HEADERLEN);
    168   z.next_in = (z_const Bytef *)hugehelpgz + HEADERLEN;
    169 
    170   if(inflateInit2(&z, -MAX_WBITS) != Z_OK)
    171     return;
    172 
    173   buf = malloc(BUF_SIZE);
    174   if(buf) {
    175     while(1) {
    176       z.avail_out = BUF_SIZE;
    177       z.next_out = buf;
    178       status = inflate(&z, Z_SYNC_FLUSH);
    179       if(status == Z_OK || status == Z_STREAM_END) {
    180         size_t len = BUF_SIZE - z.avail_out;
    181         if(!helpscan(buf, len, &ctx))
    182           break;
    183         if(status == Z_STREAM_END)
    184           break;
    185       }
    186       else
    187         break;    /* error */
    188     }
    189     free(buf);
    190   }
    191   inflateEnd(&z);
    192 }
    193 EOF
    194     ;
    195 foot();
    196 exit;
    197 }
    198 else {
    199     print <<HEAD
    200 static const char * const curlman[] = {
    201 HEAD
    202         ;
    203 }
    204 
    205 my $blank;
    206 for my $n (@out) {
    207     chomp $n;
    208     $n =~ s/\\/\\\\/g;
    209     $n =~ s/\"/\\\"/g;
    210     $n =~ s/\t/\\t/g;
    211 
    212     if(!$n) {
    213         $blank++;
    214     }
    215     else {
    216         $n =~ s/        /\\t/g;
    217         printf("  \"%s%s\",\n", $blank?"\\n":"", $n);
    218         $blank = 0;
    219     }
    220 }
    221 
    222 print <<ENDLINE
    223   NULL
    224 };
    225 void hugehelp(void)
    226 {
    227   int i = 0;
    228   while(curlman[i])
    229     puts(curlman[i++]);
    230 }
    231 
    232 /* Show the help text for the 'arg' curl argument on stdout */
    233 void showhelp(const char *trigger, const char *arg, const char *endarg)
    234 {
    235   int i = 0;
    236   struct scan_ctx ctx;
    237   inithelpscan(&ctx, trigger, arg, endarg);
    238   while(curlman[i]) {
    239     size_t len = strlen(curlman[i]);
    240     if(!helpscan((const unsigned char *)curlman[i], len, &ctx) ||
    241        !helpscan((const unsigned char *)"\\n", 1, &ctx))
    242       break;
    243     i++;
    244   }
    245 }
    246 ENDLINE
    247     ;
    248 
    249 foot();
    250 
    251 sub foot {
    252     print <<FOOT
    253 #endif /* USE_MANUAL */
    254 FOOT
    255   ;
    256 }