Avoid memory leaks if options repeated.
[openssl.git] / apps / progs.pl
1 #! /usr/bin/env perl
2 # Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 # Generate progs.h file by looking for command mains in list of C files
10 # passed on the command line.
11
12 use strict;
13 use warnings;
14 use configdata qw/@disablables/;
15
16 my %commands = ();
17 my $cmdre = qr/^\s*int\s+([a-z_][a-z0-9_]*)_main\(\s*int\s+argc\s*,/;
18
19 foreach my $filename (@ARGV) {
20         open F, $filename or die "Coudn't open $_: $!\n";
21         foreach (grep /$cmdre/, <F>) {
22                 my @foo = /$cmdre/;
23                 $commands{$1} = 1;
24         }
25         close F;
26 }
27
28 @ARGV = sort keys %commands;
29
30 print <<'EOF';
31 /*
32  * WARNING: do not edit!
33  * Generated by apps/progs.pl
34  *
35  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
36  *
37  * Licensed under the OpenSSL license (the "License").  You may not use
38  * this file except in compliance with the License.  You can obtain a copy
39  * in the file LICENSE in the source distribution or at
40  * https://www.openssl.org/source/license.html
41  */
42
43 typedef enum FUNC_TYPE {
44     FT_none, FT_general, FT_md, FT_cipher, FT_pkey,
45     FT_md_alg, FT_cipher_alg
46 } FUNC_TYPE;
47
48 typedef struct function_st {
49     FUNC_TYPE type;
50     const char *name;
51     int (*func)(int argc,char *argv[]);
52     const OPTIONS *help;
53 } FUNCTION;
54
55 DEFINE_LHASH_OF(FUNCTION);
56
57 EOF
58
59 foreach (@ARGV) {
60         printf "extern int %s_main(int argc, char *argv[]);\n", $_;
61 }
62
63 print "\n";
64
65 foreach (@ARGV) {
66         printf "extern OPTIONS %s_options[];\n", $_;
67 }
68
69 print "\n#ifdef INCLUDE_FUNCTION_TABLE\n";
70 print "static FUNCTION functions[] = {\n";
71 my %cmd_disabler = (
72     ciphers  => "sock",
73     genrsa   => "rsa",
74     rsautl   => "rsa",
75     gendsa   => "dsa",
76     dsaparam => "dsa",
77     gendh    => "dh",
78     dhparam  => "dh",
79     ecparam  => "ec",
80     pkcs12   => "des",
81     );
82 foreach my $cmd (@ARGV) {
83         my $str="    { FT_general, \"$cmd\", ${cmd}_main, ${cmd}_options },\n";
84         if ($cmd =~ /^s_/) {
85                 print "#ifndef OPENSSL_NO_SOCK\n${str}#endif\n";
86         } elsif (grep { $cmd eq $_ } @disablables) {
87                 print "#ifndef OPENSSL_NO_".uc($cmd)."\n${str}#endif\n";
88         } elsif (my $disabler = $cmd_disabler{$cmd}) {
89                 print "#ifndef OPENSSL_NO_".uc($disabler)."\n${str}#endif\n";
90         } else {
91                 print $str;
92         }
93 }
94
95 my %md_disabler = (
96     blake2b512 => "blake2",
97     blake2s256 => "blake2",
98     );
99 foreach my $cmd (
100         "md2", "md4", "md5",
101         "gost",
102         "sha1", "sha224", "sha256", "sha384", "sha512",
103         "mdc2", "rmd160", "blake2b512", "blake2s256"
104 ) {
105         my $str = "    { FT_md, \"".$cmd."\", dgst_main},\n";
106         if (grep { $cmd eq $_ } @disablables) {
107                 print "#ifndef OPENSSL_NO_".uc($cmd)."\n${str}#endif\n";
108         } elsif (my $disabler = $md_disabler{$cmd}) {
109                 print "#ifndef OPENSSL_NO_".uc($disabler)."\n${str}#endif\n";
110         } else {
111                 print $str;
112         }
113 }
114
115 my %cipher_disabler = (
116     des3  => "des",
117     desx  => "des",
118     cast5 => "cast",
119     );
120 foreach my $cmd (
121         "aes-128-cbc", "aes-128-ecb",
122         "aes-192-cbc", "aes-192-ecb",
123         "aes-256-cbc", "aes-256-ecb",
124         "camellia-128-cbc", "camellia-128-ecb",
125         "camellia-192-cbc", "camellia-192-ecb",
126         "camellia-256-cbc", "camellia-256-ecb",
127         "base64", "zlib",
128         "des", "des3", "desx", "idea", "seed", "rc4", "rc4-40",
129         "rc2", "bf", "cast", "rc5",
130         "des-ecb", "des-ede",    "des-ede3",
131         "des-cbc", "des-ede-cbc","des-ede3-cbc",
132         "des-cfb", "des-ede-cfb","des-ede3-cfb",
133         "des-ofb", "des-ede-ofb","des-ede3-ofb",
134         "idea-cbc","idea-ecb",    "idea-cfb", "idea-ofb",
135         "seed-cbc","seed-ecb",    "seed-cfb", "seed-ofb",
136         "rc2-cbc", "rc2-ecb", "rc2-cfb","rc2-ofb", "rc2-64-cbc", "rc2-40-cbc",
137         "bf-cbc",  "bf-ecb",     "bf-cfb",   "bf-ofb",
138         "cast5-cbc","cast5-ecb", "cast5-cfb","cast5-ofb",
139         "cast-cbc", "rc5-cbc",   "rc5-ecb",  "rc5-cfb",  "rc5-ofb"
140 ) {
141         my $str="    { FT_cipher, \"$cmd\", enc_main, enc_options },\n";
142         (my $algo= $cmd) =~ s/-.*//g;
143         if ($cmd eq "zlib") {
144                 print "#ifdef ZLIB\n${str}#endif\n";
145         } elsif (grep { $algo eq $_ } @disablables) {
146                 print "#ifndef OPENSSL_NO_".uc($algo)."\n${str}#endif\n";
147         } elsif (my $disabler = $cipher_disabler{$algo}) {
148                 print "#ifndef OPENSSL_NO_".uc($disabler)."\n${str}#endif\n";
149         } else {
150                 print $str;
151         }
152 }
153
154 print "    { 0, NULL, NULL}\n};\n";
155 print "#endif\n";