]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/generator.pl
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / generator.pl
1 #!/usr/bin/perl
2 #
3 # generator.pl - derive various and sundry C++ code from the CDSA header files
4 #
5 # Usage:
6 # perl generator.pl input-directory output-directory
7 #
8 # Perry The Cynic, Fall 1999.
9 #
10 $ERR_H="cssmerr.h";
11 $APPLE_ERR_H="cssmapple.h";
12
13 $SOURCEDIR=$ARGV[0]; # directory with inputs
14 $TARGETDIR=$ARGV[1];
15
16 $TABLES="$TARGETDIR/errorcodes.gen"; # error name tables
17
18 $tabs = "\t\t\t"; # argument indentation (noncritical)
19 $warning = "This file was automatically generated. Do not edit on penalty of futility!";
20
21
22 #
23 # Parse CSSM error header and build table of all named codes
24 #
25 open(ERR, "$SOURCEDIR/$ERR_H") or die "Cannot open $ERR_H: $^E";
26 open(APPLE_ERR, "$SOURCEDIR/$APPLE_ERR_H") or die "Cannot open $APPLE_ERR_H: $^E";
27 $/=undef; # big gulp mode
28 $errors = <ERR> . <APPLE_ERR>;
29 close(ERR); close(APPLE_ERR);
30
31 @fullErrors = $errors =~ /^\s+CSSMERR_([A-Z_]+)/gm;
32 @convertibles = $errors =~ /^\s+CSSM_ERRCODE_([A-Z_]+)\s*=\s*([0-9xXA-Fa-f]+)/gm;
33
34 while ($name = shift @convertibles) {
35 $value = shift @convertibles or die;
36 $convErrors[hex $value] = $name;
37 };
38
39 #
40 # Now we will generate the error name tables.
41 #
42 open(OUT, ">$TABLES") or die "Cannot write $TABLES: $^E";
43 select OUT;
44
45 print <<HDR;
46 //
47 // CSSM error code tables.
48 // $warning
49 //
50 static const Mapping errorList[] = {
51 HDR
52 foreach $name (@fullErrors) {
53 print " { CSSMERR_$name, \"$name\" },\n";
54 };
55 print <<MID;
56 };
57
58 static const char * const convErrorList [] = {
59 MID
60 for ($n = 0; $n <= $#convErrors; $n++) {
61 if ($name = $convErrors[$n]) {
62 print " \"$name\",\n";
63 } else {
64 print " NULL,\n";
65 };
66 };
67 print "};\n";
68
69 close(OUT);
70 select(STDOUT);
71
72 $nFull = $#fullErrors + 1;
73 $nConv = $#convertibles + 1;
74 print "$nFull errors available to error translation functions.\n";