]>
git.saurik.com Git - apple/boot.git/blob - i386/util/sig.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright 1993 NeXT Computer, Inc.
27 * All rights reserved.
31 * Standalone Interface Generator
33 * Input file format: a series of lines of the form:
37 * <function declaration> <arg list> ;
40 * void *malloc(int len) len;
42 * Lines starting with '#' are passed through unchanged.
43 * Lines starting with '/' are ignored.
55 typedef enum {NO
=0, YES
=1} BOOL
;
56 #define strdup(str) ((char *)strcpy(malloc(strlen(str)+1),str))
75 int lineNumber
, outputNumber
;
79 char *stringToLower(char *string
)
81 char *new = strdup(string
);
89 char *stringToUpper(char *string
)
91 char *new = strdup(string
);
102 if (isalnum(c
) || c
== '_')
109 outputLine(char *decl
, char *function
, char *args
, char *arglist
)
111 if (which
== table
) {
112 static int struct_started
;
113 if (struct_started
== 0) {
114 fprintf(ofile
, "unsigned long (*%s_functions[])() = {\n",
118 fprintf(ofile
, "(unsigned long (*)())_%s,\t\t/* %d */\n", function
, outputNumber
);
122 fprintf(ofile
, "#define %s _%s\n",function
,function
);
125 if (which
== internal
) {
126 fprintf(ofile
, "extern %s _%s(%s);\n", decl
, function
, args
);
129 if (which
== external
) {
130 fprintf(ofile
, "#define %s_%s_FN %d\n",
131 moduleNameCaps
, function
, outputNumber
);
133 "static inline %s %s ( %s ) {\n", decl
, function
, args
);
135 "\treturn (%s)(*%s_FN[%d])(%s);\n",
136 decl
, moduleNameCaps
, outputNumber
, arglist
);
137 fprintf(ofile
, "}\n");
143 parseLine(char *line
)
145 char *paren
, *parenEnd
;
146 char *ident
, *identEnd
;
147 char *arglist
, *arglistEnd
;
150 paren
= strchr(line
, '(');
153 for (identEnd
= paren
- 1; !isIdentifier(*identEnd
); identEnd
--)
155 for (ident
= identEnd
; isIdentifier(*ident
); ident
--)
160 parenEnd
= strchr(paren
, ')');
161 if (parenEnd
== NULL
)
165 arglist
= parenEnd
+ 1;
166 while (isspace(*arglist
))
168 arglistEnd
= strchr(arglist
, ';');
169 if (arglistEnd
== NULL
)
173 function
= strdup(ident
);
175 outputLine(line
, function
, paren
, arglist
);
180 fprintf(stderr
, "Syntax error at line %d\n",lineNumber
);
185 getLineThru(FILE *file
, char *linebuf
, char stop
, int len
)
191 while (((c
= fgetc(file
)) != EOF
) && len
) {
195 if (c
== '\n') lineNumber
++;
212 skipWhitespace(FILE *file
)
216 while ((c
= fgetc(file
)) != EOF
&& isspace(c
))
217 if (c
== '\n') ++lineNumber
;
222 parseFile(FILE *file
)
227 line
= malloc(MAXLINE
+1);
230 skipWhitespace(file
);
232 while (!feof(file
)) {
234 if (c
== '#' || c
== '/') {
235 len
= getLineThru(file
, line
, '\n', MAXLINE
);
237 fprintf(ofile
, line
);
239 len
= getLineThru(file
, line
, ';', MAXLINE
);
242 skipWhitespace(file
);
248 main(int argc
, char **argv
)
254 char ofilename
[MAXPATHLEN
];
255 char *ifile
, *odir
= ".";
257 while ((c
= getopt(argc
, argv
, "d:n:")) != EOF
)
264 moduleNameCaps
= stringToUpper(moduleName
);
272 if ((ifile
= argv
[optind
]) != NULL
) {
273 file
= fopen(argv
[optind
], "r");
279 fprintf(stderr
,"No input file specified\n");
283 if (moduleName
== NULL
) {
286 newName
= strchr(ifile
, '/');
289 dot
= strchr(newName
, '.');
291 dot
= &newName
[strlen(newName
)];
293 moduleName
= (char *)malloc(len
+ 1);
294 strncpy(moduleName
, newName
, len
);
295 moduleName
[len
] = '\0';
296 moduleNameCaps
= stringToUpper(moduleName
);
299 for (which
= external
; which
<= defs
; which
++) {
303 sprintf(ofilename
, "%s/%s%s", odir
, moduleName
, osuffix
[which
]);
304 ofile
= fopen((const char *)ofilename
, "w");
306 fprintf(stderr
,"error opening output file %s\n",ofilename
);
310 if (which
== table
) {
311 fprintf(ofile
, "#define %s_TABLE 1\n", moduleNameCaps
);
312 fprintf(ofile
, "#import \"%s_internal.h\"\n",moduleName
);
315 if (which
== internal
) {
316 fprintf(ofile
, "#define %s_INTERNAL 1\n", moduleNameCaps
);
320 fprintf(ofile
, "#define %s_DEFS 1\n", moduleNameCaps
);
323 if (which
== external
) {
324 fprintf(ofile
, "#import \"memory.h\"\n");
325 fprintf(ofile
, "#define %s_EXTERNAL 1\n", moduleNameCaps
);
327 "#define %s_FN (*(unsigned long (***)())%s_TABLE_POINTER)\n\n",
328 moduleNameCaps
, moduleNameCaps
);
332 if (which
== table
) {
333 fprintf(ofile
, "};\n\n");