]> git.saurik.com Git - apple/security.git/blame - cdsa/cdsa_utilities/generator.pl
Security-30.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / generator.pl
CommitLineData
bac41a7b
A
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
15(${D}) = $SOURCEDIR =~ m@([/:])@; # guess directory delimiter
16sub macintosh() { return ${D} eq ':'; }
17
18if( macintosh() ){
19$TARGETDIR=$ARGV[2]; # directory for outputs
20}
21 else{
22$TARGETDIR=$ARGV[1];
23}
24
25$TABLES="$TARGETDIR${D}errorcodes.gen"; # error name tables
26
27$tabs = "\t\t\t"; # argument indentation (noncritical)
28$warning = "This file was automatically generated. Do not edit on penalty of futility!";
29
30
31#
32# Parse CSSM error header and build table of all named codes
33#
34open(ERR, "$SOURCEDIR${D}$ERR_H") or die "Cannot open $ERR_H: $^E";
35open(APPLE_ERR, "$SOURCEDIR${D}$APPLE_ERR_H") or die "Cannot open $APPLE_ERR_H: $^E";
36$/=undef; # big gulp mode
37$errors = <ERR> . <APPLE_ERR>;
38$errors =~ tr/\012/\015/ if macintosh;
39close(ERR); close(APPLE_ERR);
40
41@fullErrors = $errors =~ /^\s+CSSMERR_([A-Z_]+)/gm;
42@convertibles = $errors =~ /^\s+CSSM_ERRCODE_([A-Z_]+)\s*=\s*([0-9xXA-Fa-f]+)/gm;
43
44while ($name = shift @convertibles) {
45 $value = shift @convertibles or die;
46 $convErrors[hex $value] = $name;
47};
48
49#
50# Now we will generate the error name tables.
51#
52open(OUT, ">$TABLES") or die "Cannot write $TABLES: $^E";
53select OUT;
54
55print <<HDR;
56//
57// CSSM error code tables.
58// $warning
59//
60static const Mapping errorList[] = {
61HDR
62foreach $name (@fullErrors) {
63 print " { CSSMERR_$name, \"$name\" },\n";
64};
65print <<MID;
66};
67
68static const char * const convErrorList [] = {
69MID
70for ($n = 0; $n <= $#convErrors; $n++) {
71 if ($name = $convErrors[$n]) {
72 print " \"$name\",\n";
73 } else {
74 print " NULL,\n";
75 };
76};
77print "};\n";
78
79close(OUT);
80select(STDOUT);
81
82$nFull = $#fullErrors + 1;
83$nConv = $#convertibles + 1;
84print "$nFull errors available to error translation functions.\n";