]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/genErrorStrings/genErrorStrings.cpp
2 * genErrorStrings.cpp - parse supplied files, generate error table from
3 * them of the following form:
10 * ErrString errStrings[] = {
11 * { CSSMERR_CSSM_INTERNAL_ERROR, "CSSMERR_CSSM_INTERNAL_ERROR" },
13 * { CSSMERR_CSP_FUNCTION_FAILED, "CSSMERR_CSP_FUNCTION_FAILED" }
16 * The error table is written to stdout.
26 #define MAX_LINE_LEN 256
28 static void usage(char **argv
)
30 printf("usage: %s inFile [inFile...]\n", argv
[0]);
34 static void writePreamble(
38 fprintf(f
, " * This file autogenerated by genErrorStrings. Do not edit. \n");
39 fprintf(f
, " */\n\n");
40 fprintf(f
, "#include <Security/Security.h>\n\n");
41 fprintf(f
, "typedef struct {\n");
42 fprintf(f
, "\tCSSM_RETURN errCode;\n");
43 fprintf(f
, "\tconst char *errStr;\n");
44 fprintf(f
, "} ErrString;\n\n");
45 fprintf(f
, "static const ErrString errStrings[] = {\n");
48 static void writePostamble(
51 /* generate a null entry as terminator */
52 fprintf(f
, "\t{0, NULL}\n");
56 static void writeToken(
60 printf("\t{ %s,\"%s\"},\n", token
, token
);
63 /* skip whitespace (but not line terminators) */
64 static void skipWhite(
68 while(bytesLeft
!= 0) {
82 const char *&cp
, // IN/OUT
83 unsigned &bytesLeft
, // IN/OUT bytes left
87 char *endOfOut
= outp
+ MAX_LINE_LEN
- 2;
88 while(bytesLeft
!= 0) {
101 if(outp
== endOfOut
) {
102 printf("***getLine: line length exceeded!\n");
110 /* incoming line is NULL terminated even if it's empty */
111 static bool isLineEmpty(
126 /* process one file */
127 static void processFile(
128 const char *fileName
,
133 char lineBuf
[MAX_LINE_LEN
];
136 const char *endOfToken
;
137 char tokenBuf
[MAX_LINE_LEN
];
139 const char *lastSlash
= fileName
;
140 const char *nextSlash
;
142 while((nextSlash
= strchr(lastSlash
, '/')) != NULL
) {
143 lastSlash
= nextSlash
+ 1;
145 fprintf(f
, "\t/* Error codes from %s */\n", lastSlash
);
148 /* get one line, NULL terminated */
149 getLine(in
, inLen
, lineBuf
);
150 if(isLineEmpty(lineBuf
)) {
154 /* skip leading whitespace */
155 lineLen
= strlen(lineBuf
);
157 skipWhite(cp
, lineLen
);
160 if(strncmp((char *)cp
, "CSSMERR_", 8)) {
165 * cp is the start of the CSSMERR_ token
170 if(isalnum(*endOfToken
) || (*endOfToken
== '_')) {
179 /* endOfToken is one past the end of the CSSMERR_ token */
180 tokenLen
= endOfToken
- cp
;
181 memmove(tokenBuf
, cp
, tokenLen
);
182 tokenBuf
[tokenLen
] = '\0';
184 /* write the stuff */
185 writeToken(tokenBuf
, f
);
189 int main(int argc
, char **argv
)
191 unsigned char *inFile
;
199 writePreamble(stdout
);
200 writeToken("CSSM_OK", stdout
);
201 for(dex
=1; dex
<argc
; dex
++) {
202 if(readFile(argv
[dex
], &inFile
, &inFileLen
)) {
203 printf("***Error reading %s. Aborting.\n", argv
[dex
]);
206 processFile(argv
[dex
], (const char *)inFile
, inFileLen
, stdout
);
209 writePostamble(stdout
);