1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
/* cexport.c -- create header file of EXPORT'ed declarations from c files */
/*
* Author: Bert Bos <bert@w3.org>
* Created: before 1995
*
* C files are scanned for the keyword EXPORT. Any declaration that
* follows it is copied to a file with the extension .e. It works for
* typedefs, #defines, variables and functions, but only if ANSI
* prototypes are used. Macros are exported with EXPORTDEF(.)
*
* Examples:
*
* EXPORT typedef int * IntPtr -- export IntPtr
*
* EXPORT void walkTree(Tree t) -- export walkTree()
*
* #define max(a,b) ((a)>(b)?(a):(b))
* EXPORTDEF(max(a,b)) -- export max(a,b)
*
* Files are first piped through the C preprocessor cpp.
*
* Command line options:
* -c <cppcmd>: use <cppcmd> instead of cpp
* -e <extension>: use <extension> instead of '.e'
* other options are passed to cpp
*
* The program is not very smart about C syntax, but it doesn't have
* to be, as long as the input is correct ANSI C. If it is not, no
* warnings will be given (except possibly for unmatched braces,
* quotes and paretheses), but the output will not be correct C,
* either.
*
* TO DO: an option to check if the new .e file is different any
* existing one and to keep the old one in that case. (Useful to save
* unnecessary recompilations.)
*/
#include "config.h"
#include <stdio.h>
#if STDC_HEADERS
# include <string.h>
#else
# ifndef HAVE_STRCHR
# define strchr index
# define strrchr rindex
# endif
#endif
#include <ctype.h>
#include <stdlib.h>
#ifndef CPP
#define CPP "cc -E"
#endif
#define LINELEN BUFSIZ
static int err = 0; /* Global error counter */
static char *cppcmd = CPP;
static char *extension = ".e";
static FILE *in, *out;
static int eof;
static long lineno;
static char line[LINELEN];
static char *curname;
/***************************************************************************
* get_line -- read next line, return 0 if eof
***************************************************************************/
static int get_line()
{
static char buf[BUFSIZ];
char *s;
int i;
do {
if (eof)
return 0;
else if (! fgets(line, LINELEN, in)) {
eof = 1;
return 0;
} else if (line[0] != '#') {
lineno++;
return 1;
} else if (line[1] == ' ') {
i = 2; while (isspace(line[i])) i++;
if (! isdigit(line[i])) {
lineno++;
return 1;
} else {
lineno = strtol(line + i, &s, 0) - 1;
if (*(s+1) != '"') {
strcpy(buf, s + 1);
buf[strlen(buf)-1] = '\0';
} else {
strcpy(buf, s + 2);
for (i = 2; buf[i] != '"'; i++) ;
buf[i] = '\0';
}
if (buf[0]) curname = buf;
}
} else if (line[1] == 'l' && strncmp(line, "#line", 5) == 0) {
lineno = strtol(line + 5, &s, 0) - 1;
if (*(s+1) != '"') {
strcpy(buf, s + 1);
buf[strlen(buf)-1] = '\0';
} else {
strcpy(buf, s + 2);
for (i = 2; buf[i] != '"'; i++) ;
buf[i] = '\0';
}
if (buf[0]) curname = buf;
} else {
lineno++;
return 1;
}
} while (1);
}
/***************************************************************************
* exportdef -- copy a #define to output
***************************************************************************/
static void exportdef(i)
long i;
{
unsigned long len;
/*
* TO DO: encountering an end of file should produce a suitable error
* message: end of file in middle of macro definition.
*/
fputs("#define ", out); /* EXPORTDEF -> #define */
/* Unquote the following string */
for (i += 10; line[i] && line[i] != '"'; i++) ;
for (i++; line[i] && line[i] != '"'; i++) putc(line[i], out);
putc(' ', out);
fputs(line + i + 1, out); /* Write rest of line */
len = strlen(line); /* Continuation lines? */
while (len >= 2 && line[len-2] == '\\') {
if (! get_line()) break;
fputs(line, out);
len = strlen(line);
}
}
/***************************************************************************
* export -- copy next declaration to output
***************************************************************************/
static void export(i)
long *i;
{
int brace, paren, squote, dquote, comment, stop, is_typedef, start, is_enum,
is_extern, is_struct;
/*
* TO DO: End of file while any of the variables is still
* non-null is also an error.
*/
*i += 6; /* Skip "EXPORT" */
comment = 0;
squote = 0;
dquote = 0;
paren = 0;
brace = 0;
stop = 0;
is_typedef = 0;
is_enum = 0;
is_extern = 0;
is_struct = 0;
start = 1;
do {
switch (line[*i]) {
case '\\':
if (line[*i+1]) (*i)++; /* Skip next char */
break;
case '{':
if (!comment && !squote && !dquote && !paren) brace++;
break;
case '}':
if (!comment && !squote && !dquote && !paren) brace--;
if (brace < 0) {
fprintf(stderr, "%s:%ld: syntax error (too many '}'s)\n",
curname, lineno);
err++;
brace = 0;
}
break;
case '"':
if (!comment && !squote) dquote = !dquote;
break;
case '\'':
if (!comment && !dquote) squote = !squote;
break;
case '*':
if (!comment && !dquote && !squote && *i > 0 && line[*i-1] == '/')
comment = 1; /* Start of comment */
break;
case '/': /* Possible end of comment */
if (comment && *i > 0 && line[*i-1] == '*') comment = 0;
break;
case '(':
if (!comment && !dquote && !squote && !brace) paren++;
break;
case ')':
if (!comment && !dquote && !squote && !brace) {
paren--;
if (paren == 0 && !is_typedef) {
putc(')', out);
putc(';', out);
putc('\n', out);
stop = 1;
}
}
break;
case ';':
if (!comment && !dquote && !squote && !paren && !brace) {
putc(';', out);
putc('\n', out);
stop = 1;
}
break;
case '=':
if (!comment && !dquote && !squote && !brace && !paren) {
putc(';', out); /* End of variable decl. */
putc('\n', out);
stop = 1;
}
break;
case '\n':
if (dquote) {
fprintf(stderr,
"%s:%ld: syntax error (string didn't end)\n",
curname, lineno);
err++;
dquote = 0;
}
if (squote) {
fprintf(stderr,
"%s:%ld: syntax error (char const didn't end)\n",
curname, lineno);
err++;
squote = 0;
}
break;
case '\0':
if (! get_line()) stop = 1;
else *i = -1;
break;
case 't':
if (!comment && !squote && !dquote && paren == 0 && brace == 0
&& strncmp("typedef", &line[*i], 7) == 0)
is_typedef = 1;
break;
case 's':
if (!comment && !squote && !dquote && paren == 0 && brace == 0
&& strncmp("struct", &line[*i], 6) == 0)
is_struct = 1;
break;
case 'e':
if (!comment && !squote && !dquote && paren == 0 && brace == 0) {
if (strncmp("enum", &line[*i], 4) == 0) is_enum = 1;
else if (strncmp("extern", &line[*i], 6) == 0) is_extern = 1;
}
break;
}
if (! stop) {
if (*i >= 0) {
if (! start) {
putc(line[*i], out);
} else if (! isspace(line[*i])) {
if (! is_typedef && ! is_enum && ! is_extern && ! is_struct)
fputs("extern ", out);
putc(line[*i], out);
start = 0;
}
}
(*i)++;
}
} while (! stop);
}
/***************************************************************************
* process -- scan file and write exported declarations
***************************************************************************/
static void process(file, cpp)
char *file, *cpp;
{
char cmd[1024], *s, outname[1024];
int brace, paren, dquote, squote, comment;
long i;
strcpy(cmd, cppcmd); /* Build cpp command line */
strcat(cmd, cpp);
strcat(cmd, file ? file : "-");
eof = 0;
lineno = 0;
in = popen(cmd, "r"); /* Pipe file through cpp */
if (! in) { perror(cmd); err++; return; }
if (file) {
strcpy(outname, file); /* Construct output file */
s = strrchr(outname, '.'); /* Extension becomes .e */
if (! s) s = outname + strlen(outname);
strcpy(s, extension);
out = fopen(outname, "w");
if (! out) { perror(outname); err++; return; }
} else {
out = stdout; /* No file name, use stdout */
}
if (file) curname = file; else curname = "<stdin>";
/*
* If the word EXPORT is found and it is not inside a comment, between
* quotes, parentheses or braces, the export() function is called to copy
* the declaration to the out file. When the export() function ends, `line'
* may have changed, but `i' points to the last copied character.
*
* If the word EXPORTDEF is found at the start of a line and it
* is not inside a comment or between quotes, exportdef is called.
*/
comment = 0;
dquote = 0;
squote = 0;
paren = 0;
brace = 0;
while (get_line()) {
for (i = 0; line[i]; i++) {
switch (line[i]) {
case '\\':
if (line[i+1]) i++; /* Skip next char */
break;
case '{':
if (!comment && !dquote && !squote) brace++;
break;
case '}':
if (!comment && !dquote && !squote) brace--;
if (brace < 0) {
fprintf(stderr, "%s:%ld: syntax error (too many '}'s)\n",
curname, lineno);
err++;
brace = 0;
}
break;
case '(':
if (!comment && !dquote && !squote) paren++;
break;
case ')':
if (!comment && !dquote && !squote) paren--;
if (paren < 0) {
fprintf(stderr, "%s:%ld: syntax error (too many ')'s)\n",
curname, lineno);
err++;
paren = 0;
}
break;
case '\'':
if (!comment && !dquote) squote = !squote;
break;
case '"':
if (!comment && !squote) dquote = !dquote;
break;
case '\n':
if (dquote) {
fprintf(stderr,
"%s:%ld: syntax error (string didn't end)\n",
curname, lineno);
err++;
dquote = 0;
}
if (squote) {
fprintf(stderr,
"%s:%ld: syntax error (char const didn't end)\n",
curname, lineno);
err++;
squote = 0;
}
break;
case '*':
if (!comment && !dquote && !squote && i > 0 && line[i-1] == '/')
comment = 1; /* Start of comment */
break;
case '/': /* Possible end of comment */
if (comment && i > 0 && line[i-1] == '*') comment = 0;
break;
case 'E':
if (comment || dquote || squote || paren != 0 || brace != 0)
;
else if (strncmp(&line[i], "EXPORT", 6) == 0
&& (i == 0 || !isalnum(line[i-1]))
&& !isalnum(line[i+6]))
export(&i);
else if (strncmp(&line[i], "EXPORTDEF ", 10) == 0
&& (i == 0 || !isalnum(line[i-1]))) {
exportdef(i);
i = (long) strlen(line) - 1;
}
break;
}
}
}
if (comment) {
fprintf(stderr, "%s:%ld: syntax error (comment didn't end)\n",
curname, lineno);
err++;
}
if (dquote) {
fprintf(stderr, "%s:%ld: syntax error (string didn't end)\n",
curname, lineno);
err++;
}
if (squote) {
fprintf(stderr, "%s:%ld: syntax error (char const didn't end)\n",
curname, lineno);
err++;
}
if (file) fclose(out);
fclose(in);
}
static void usage(s)
char *s;
{
fprintf(stderr,
"Usage: %s {-Idir|-Dsym} [-h] [-c cppcmd] [-e ext] {file}\n",
s);
err++;
}
int main(argc, argv)
int argc;
char *argv[];
{
char cpp[BUFSIZ]; /* Max. cmd. line length */
int nfiles, i;
strcpy(cpp, " -D__export ");
nfiles = 0;
for (i = 1; i < argc; i++) {
if (!strncmp(argv[i], "-c", 2)) { /* Replace cpp command */
if (argv[i][2])
cppcmd = argv[i] + 2;
else
cppcmd = argv[++i];
} else if (!strncmp(argv[i], "-e", 2)) { /* Extension instead of .e */
if (argv[i][2])
extension = argv[i] + 2;
else
extension = argv[++i];
} else if (!strncmp(argv[i], "-h", 2)) { /* -h: help */
usage(argv[0]);
} else if (argv[i][0] == '-' || argv[i][0] == '+') {
strcat(cpp, argv[i]); /* Pass options to cpp */
strcat(cpp, " ");
} else { /* Not option, must be file */
nfiles++;
process(argv[i], cpp);
}
}
if (nfiles == 0) /* no arguments, use stdin */
process(NULL, cpp);
return err;
}
|