Generate progs.h from a bunch of files instead of internal knowledge
[openssl.git] / apps / progs.pl
1 #!/usr/bin/perl
2 # Generate progs.h file by looking for command mains in list of C files
3 # passed on the command line.
4
5 use strict;
6 use warnings;
7
8 my %commands = ();
9 my $cmdre = qr/^\s*int\s+([a-z_][a-z0-9_]*)_main\(\s*int\s+argc\s*,/;
10
11 foreach my $filename (@ARGV) {
12         open F, $filename or die "Coudn't open $_: $!\n";
13         foreach (grep /$cmdre/, <F>) {
14                 my @foo = /$cmdre/;
15                 $commands{$1} = 1;
16         }
17         close F;
18 }
19
20 @ARGV = sort keys %commands;
21
22 print <<'EOF';
23 /*
24  * Automatically generated by progs.pl for openssl.c
25  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
26  * See the openssl.c for copyright details.
27  */
28
29 typedef enum FUNC_TYPE {
30     FT_none, FT_general, FT_md, FT_cipher, FT_pkey,
31     FT_md_alg, FT_cipher_alg
32 } FUNC_TYPE;
33
34 typedef struct function_st {
35     FUNC_TYPE type;
36     const char *name;
37     int (*func)(int argc,char *argv[]);
38     const OPTIONS *help;
39 } FUNCTION;
40
41 DEFINE_LHASH_OF(FUNCTION);
42
43 EOF
44
45 foreach (@ARGV) {
46         printf "extern int %s_main(int argc, char *argv[]);\n", $_;
47 }
48
49 print "\n";
50
51 foreach (@ARGV) {
52         printf "extern OPTIONS %s_options[];\n", $_;
53 }
54 print "\n#ifdef INCLUDE_FUNCTION_TABLE\n";
55 print "static FUNCTION functions[] = {\n";
56 foreach (@ARGV) {
57         my $str="    { FT_general, \"$_\", ${_}_main, ${_}_options },\n";
58         if (/^s_/ || /^ciphers$/) {
59                 print "#if !defined(OPENSSL_NO_SOCK)\n${str}#endif\n";
60         } elsif (/^engine$/) {
61                 print "#ifndef OPENSSL_NO_ENGINE\n${str}#endif\n";
62         } elsif (/^rsa$/ || /^genrsa$/ || /^rsautl$/) {
63                 print "#ifndef OPENSSL_NO_RSA\n${str}#endif\n";
64         } elsif (/^dsa$/ || /^gendsa$/ || /^dsaparam$/) {
65                 print "#ifndef OPENSSL_NO_DSA\n${str}#endif\n";
66         } elsif (/^ec$/ || /^ecparam$/) {
67                 print "#ifndef OPENSSL_NO_EC\n${str}#endif\n";
68         } elsif (/^dh$/ || /^gendh$/ || /^dhparam$/) {
69                 print "#ifndef OPENSSL_NO_DH\n${str}#endif\n";
70         } elsif (/^pkcs12$/) {
71                 print "#if !defined(OPENSSL_NO_DES)\n${str}#endif\n";
72         } elsif (/^cms$/) {
73                 print "#ifndef OPENSSL_NO_CMS\n${str}#endif\n";
74         } elsif (/^ocsp$/) {
75                 print "#ifndef OPENSSL_NO_OCSP\n${str}#endif\n";
76         } elsif (/^srp$/) {
77                 print "#ifndef OPENSSL_NO_SRP\n${str}#endif\n";
78         } else {
79                 print $str;
80         }
81 }
82
83 foreach (
84         "md2", "md4", "md5",
85         "md_ghost94",
86         "sha1", "sha224", "sha256", "sha384", "sha512",
87         "mdc2", "rmd160"
88 ) {
89         printf "#ifndef OPENSSL_NO_".uc($_)."\n" if ! /sha/;
90         printf "    { FT_md, \"".$_."\", dgst_main},\n";
91         printf "#endif\n" if ! /sha/;
92 }
93
94 foreach (
95         "aes-128-cbc", "aes-128-ecb",
96         "aes-192-cbc", "aes-192-ecb",
97         "aes-256-cbc", "aes-256-ecb",
98         "camellia-128-cbc", "camellia-128-ecb",
99         "camellia-192-cbc", "camellia-192-ecb",
100         "camellia-256-cbc", "camellia-256-ecb",
101         "base64", "zlib",
102         "des", "des3", "desx", "idea", "seed", "rc4", "rc4-40",
103         "rc2", "bf", "cast", "rc5",
104         "des-ecb", "des-ede",    "des-ede3",
105         "des-cbc", "des-ede-cbc","des-ede3-cbc",
106         "des-cfb", "des-ede-cfb","des-ede3-cfb",
107         "des-ofb", "des-ede-ofb","des-ede3-ofb",
108         "idea-cbc","idea-ecb",    "idea-cfb", "idea-ofb",
109         "seed-cbc","seed-ecb",    "seed-cfb", "seed-ofb",
110         "rc2-cbc", "rc2-ecb", "rc2-cfb","rc2-ofb", "rc2-64-cbc", "rc2-40-cbc",
111         "bf-cbc",  "bf-ecb",     "bf-cfb",   "bf-ofb",
112         "cast5-cbc","cast5-ecb", "cast5-cfb","cast5-ofb",
113         "cast-cbc", "rc5-cbc",   "rc5-ecb",  "rc5-cfb",  "rc5-ofb"
114 ) {
115         my $str="    { FT_cipher, \"$_\", enc_main, enc_options },\n";
116         if (/des/) {
117                 printf "#ifndef OPENSSL_NO_DES\n${str}#endif\n";
118         } elsif (/aes/) {
119                 printf "#ifndef OPENSSL_NO_AES\n${str}#endif\n";
120         } elsif (/camellia/) {
121                 printf "#ifndef OPENSSL_NO_CAMELLIA\n${str}#endif\n";
122         } elsif (/idea/) {
123                 printf "#ifndef OPENSSL_NO_IDEA\n${str}#endif\n";
124         } elsif (/seed/) {
125                 printf "#ifndef OPENSSL_NO_SEED\n${str}#endif\n";
126         } elsif (/rc4/) {
127                 printf "#ifndef OPENSSL_NO_RC4\n${str}#endif\n";
128         } elsif (/rc2/) {
129                 printf "#ifndef OPENSSL_NO_RC2\n${str}#endif\n";
130         } elsif (/bf/) {
131                 printf "#ifndef OPENSSL_NO_BF\n${str}#endif\n";
132         } elsif (/cast/) {
133                 printf "#ifndef OPENSSL_NO_CAST\n${str}#endif\n";
134         } elsif (/rc5/) {
135                 printf "#ifndef OPENSSL_NO_RC5\n${str}#endif\n";
136         } elsif (/zlib/) {
137                 printf "#ifdef ZLIB\n${str}#endif\n";
138         } else {
139                 print $str;
140         }
141 }
142
143 print "    { 0, NULL, NULL}\n};\n";
144 printf "#endif\n";