]>
git.saurik.com Git - apple/security.git/blob - OSX/lib/generateErrStrings.pl
3 # Copyright (c) 2003-2004,2006,2008,2012,2014 Apple Inc. All Rights Reserved.
5 # @APPLE_LICENSE_HEADER_START@
7 # This file contains Original Code and/or Modifications of Original Code
8 # as defined in and that are subject to the Apple Public Source License
9 # Version 2.0 (the 'License'). You may not use this file except in
10 # compliance with the License. Please obtain a copy of the License at
11 # http://www.opensource.apple.com/apsl/ and read it before using this
14 # The Original Code and all software distributed under the License are
15 # distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 # EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 # INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 # Please see the License for the specific language governing rights and
20 # limitations under the License.
22 # @APPLE_LICENSE_HEADER_END@
24 # generateErrStrings.pl - create error strings files from the Security header files
28 # perl generateErrStrings.pl <GENDEBUGSTRS?> <NAME_OF_STRINGS_FILE> <input files>
30 # Currently supported files are SecBase.h, SecureTransport.h,cssmapple.h,
31 # cssmerr.h and Authorization.h. These are used by:
33 # void cssmPerror(const char *how, CSSM_RETURN error);
35 # which is in SecBase.cpp.
37 # Paths of input files:
39 # ./libsecurity_authorization/lib/Authorization.h
40 # ./libsecurity_cssm/lib/cssmapple.h
41 # ./libsecurity_cssm/lib/cssmerr.h
42 # ./libsecurity_keychain/lib/SecBase.h
43 # ./libsecurity_ssl/lib/SecureTransport.h
47 # perl generateErrStrings.pl "YES" "SecErrorMessages.strings" Authorization.h SecBase.h \
48 # cssmapple.h cssmerr.h SecureTransport.h
50 # Input to script: header file(s) containing enum declarations
51 # Output: C++ program with one cout statement per decl
53 # The input headers are scanned for enums containing error numbers and
54 # optional comments. Only certain prefixes for the identifiers in the
55 # enums are considered, to avoid non-error message type defines. See
56 # the line in the file with CSSM_ERRCODE for acceptable prefixes.
58 # There are three styles of comments that this script parses:
60 # Style A [see /System/Library/Frameworks/Security.framework/Headers/SecBase.h]:
62 # errSSLProtocol = -9800, /* SSL protocol error */
64 # Style B [see /System/Library/Frameworks/Security.framework/Headers/cssmapple.h]:
66 # /* a code signature match failed */
67 # CSSMERR_CSP_APPLE_SIGNATURE_MISMATCH = CSSM_CSP_PRIVATE_ERROR + 2,
69 # Style C [see /System/Library/Frameworks/Security.framework/Headers/cssmerr.h]:
71 # CSSM_CSSM_BASE_CSSM_ERROR =
72 # CSSM_CSSM_BASE_ERROR + CSSM_ERRORCODE_COMMON_EXTENT + 0x10,
73 # CSSMERR_CSSM_SCOPE_NOT_SUPPORTED = CSSM_CSSM_BASE_CSSM_ERROR + 1,
75 # Style A has the comment after the value. Style has the comment before the value,
76 # and Style C has no comment. In cases where both Style A and B apply, the
77 # comment at the end of the line is used.
79 # The final output after the generated Objective-C++ program is run looks like:
81 # /* errSSLProtocol */
82 # "-9800" = "SSL protocol error";
84 # /* errSSLNegotiation */
85 # "-9801" = "Cipher Suite negotiation failure";
87 # The appropriate byte order marker for UTF-16 is written to the start of the file.
88 # Note that the list of errors must be numerically unique across all input files,
89 # or the strings file will be invalid. Comments in "Style B" may span multiple lines.
90 # C++ style comments are not supported. Double quotes in a comment are hardened with
93 # The English versions of the error messages can be seen with:
95 # cat /System/Library/Frameworks/Security.framework/Resources/en.lproj/SecErrorMessages.strings
97 # find -H -X -x . -name "*.h" -print0 2>/dev/null | xargs -0 grep -ri err
98 # -----------------------------------------------------------------------------------
101 # - what should I make PROGNAME?
102 # - should I use a special call to make the temp file in the .mm file?
108 die "Usage: $0 <gendebug> <tmpdir> <.strings file> <list of headers>\n" if ($#ARGV < 3);
110 $GENDEBUGSTRINGS=$ARGV[0]; # If "YES", include all strings & don't localize
111 $TMPDIR=$ARGV[1]; # temporary directory for program compile, link, run
112 $TARGETSTR=$ARGV[2]; # path of .strings file, e.g.
113 # ${DERIVED_SRC}/en.lproj/SecErrorMessages.strings
114 @INPUTFILES=@ARGV[3 .. 9999]; # list of input files
116 $#INPUTFILES = $#ARGV - 3; # truncate to actual number of files
118 print "gend: $GENDEBUGSTRINGS, tmpdir: $TMPDIR, targetstr: $TARGETSTR\n";
119 open STRINGFILE
, "> $TARGETSTR" or die "can't open $TARGETSTR: $!";
121 binmode STRINGFILE
, ":encoding(UTF-16)";
123 # -----------------------------------------------------------------------------------
124 # Parse error headers and build array of all relevant lines
125 open(ERR
, "cat " . join(" ", @INPUTFILES) . "|") or die "Cannot open error header files";
126 $/="\};"; #We set the section termination string - very important
129 # -----------------------------------------------------------------------------------
134 # -----------------------------------------------------------------------------------
136 # -----------------------------------------------------------------------------------
140 # 3: Read input, process each line, output it.
141 while ( $line = <ERR
>)
143 ($enum) = ($line =~ /\n\s*(?:enum|CF_ENUM\(OSStatus\))\s*{\s*([^}]*)};/);
144 while ($enum ne '') #basic filter for badly formed enums
146 #Drop leading whitespace
149 ($leadingcomment) = ($enum =~ m
%^(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\
*+/)|(//.*)%);
150 if ($leadingcomment ne '')
152 $enum = substr($enum, length($leadingcomment));
153 $leadingcomment = substr($leadingcomment, 2); # drop leading "/*"
154 $leadingcomment = substr($leadingcomment, 0, -2); # drop trailing "*/"
155 $leadingcomment = cleanupComment
($leadingcomment);
157 next if ($enum eq ''); #basic filter for badly formed enums
159 # Check for C++ style comments at start of line
160 if ($enum =~ /\s*(\/\
/)/)
162 #Drop everything before the end of line
163 $enum =~ s/[^\n]*[\n]*//;
166 ($identifier) = ($enum =~ /\s*([_A-Za-z][_A-Za-z0-9]*)/);
167 #print "identifier: ", $identifier,"\n" if ($identifier ne '');
169 ($value) = ($enum =~ /\s*[_A-Za-z][_A-Za-z0-9]*\s*=\s*(-?[0-9]*),/);
170 #print "value: ", $value,"\n" if ($value ne '');
172 #Drop everything before the comma, end of line or trailing comment
173 $enum =~ s/[^,]*(,|\n|(\/\*))//;
175 # Now look for trailing comment. We only consider them
176 # trailing if they come before the end of the line
177 ($trailingcomment) = ($enum =~ /^[ \t]*\/\
*((.)*)?\
*\
//);
178 $trailingcomment = cleanupComment
($trailingcomment);
180 #Drop everything before the end of line
181 $enum =~ s/[^\n]*[\n]*//;
183 #print "lc:$leadingcomment, id:$identifier, v:$value, tc:$trailingcomment\n";
184 #print "===========================================\n";
186 writecomment
($leadingcomment, $identifier, $trailingcomment, $value);
193 # Leading comment, id, trailing comment
194 # To aid localizers, we will not output a line with no comment
197 # tmp << "/* errAuthorizationSuccess */\n\"" << errAuthorizationSuccess
198 # << "\" = \"The operation completed successfully.\"\n" << endl;
200 my($mylc,$myid,$mytc,$myvalue) = @_;
201 if ($myid =~ /(CSSM_ERRCODE|CSSMERR_|errSec|errCS|errAuth|errSSL)[_A-Za-z][_A-Za-z0-9]*/)
205 { $errormessage = $mytc; }
207 { $errormessage = $mylc; }
208 elsif ($GENDEBUGSTRINGS eq "YES")
209 { $errormessage = $myid; }
211 if ($errormessage ne '')
213 print "/* ", $myid, " */\n\"";
214 print $myvalue, "\" = \"";
215 print $errormessage, "\";\n\n";
222 my $comment = shift @_;
223 # print "A:",$comment,"\n";
226 $comment =~ s/\s\s+/ /g; # Squeeze multiple spaces to one
227 $comment =~ s/^\s+//; # Drop leading whitespace
228 $comment =~ s/\s+$//; # Drop trailing whitespace
229 $comment =~ s/[\"]/\\\"/g; # Replace double quotes with \" (backslash is sextupled to make it through regex and printf)
231 # print "B:",$comment,"\n";