#include <stdio.h>
#include <ctype.h>
#include <string.h>
Go to the source code of this file.
Defines | |
#define | bufsize 5000 |
#define | is_alnum(c) (is_ascii(c) && isalnum(c)) |
#define | is_alpha(c) (is_ascii(c) && isalpha(c)) |
#define | is_ascii(c) 1 |
#define | is_space(c) (is_ascii(c) && isspace(c)) |
#define | isidchar(ch) (is_alnum(ch) || (ch) == '_') |
#define | isidfirstchar(ch) (is_alpha(ch) || (ch) == '_') |
Functions | |
int | convert1 (char *buf, FILE *out, int header, int convert_varargs) |
int | convert1 () |
int | free () |
int | main (int argc, argv) |
char * | malloc () |
char * | skipspace (char *p, int dir) |
char * | skipspace () |
int | test1 (char *buf) |
int | test1 () |
int | writeblanks (char *start, char *end) |
int | writeblanks () |
#define bufsize 5000 |
Referenced by main().
#define is_alnum | ( | c | ) | (is_ascii(c) && isalnum(c)) |
Definition at line 306 of file ansi2knr.c.
#define is_alpha | ( | c | ) | (is_ascii(c) && isalpha(c)) |
Definition at line 305 of file ansi2knr.c.
#define is_ascii | ( | c | ) | 1 |
Definition at line 299 of file ansi2knr.c.
#define is_space | ( | c | ) | (is_ascii(c) && isspace(c)) |
#define isidchar | ( | ch | ) | (is_alnum(ch) || (ch) == '_') |
#define isidfirstchar | ( | ch | ) | (is_alpha(ch) || (ch) == '_') |
int convert1 | ( | char * | buf, | |
FILE * | out, | |||
int | header, | |||
int | convert_varargs | |||
) |
Definition at line 528 of file ansi2knr.c.
References free(), isidchar, isidfirstchar, malloc(), skipspace(), and writeblanks().
00533 { char *endfn; 00534 register char *p; 00535 char **breaks; 00536 unsigned num_breaks = 2; /* for testing */ 00537 char **btop; 00538 char **bp; 00539 char **ap; 00540 char *vararg = 0; 00541 00542 /* Pre-ANSI implementations don't agree on whether strchr */ 00543 /* is called strchr or index, so we open-code it here. */ 00544 for ( endfn = buf; *(endfn++) != '('; ) 00545 ; 00546 top: p = endfn; 00547 breaks = (char **)malloc(sizeof(char *) * num_breaks * 2); 00548 if ( breaks == 0 ) 00549 { /* Couldn't allocate break table, give up */ 00550 fprintf(stderr, "Unable to allocate break table!\n"); 00551 fputs(buf, out); 00552 return -1; 00553 } 00554 btop = breaks + num_breaks * 2 - 2; 00555 bp = breaks; 00556 /* Parse the argument list */ 00557 do 00558 { int level = 0; 00559 char *lp = NULL; 00560 char *rp; 00561 char *end = NULL; 00562 00563 if ( bp >= btop ) 00564 { /* Filled up break table. */ 00565 /* Allocate a bigger one and start over. */ 00566 free((char *)breaks); 00567 num_breaks <<= 1; 00568 goto top; 00569 } 00570 *bp++ = p; 00571 /* Find the end of the argument */ 00572 for ( ; end == NULL; p++ ) 00573 { switch(*p) 00574 { 00575 case ',': 00576 if ( !level ) end = p; 00577 break; 00578 case '(': 00579 if ( !level ) lp = p; 00580 level++; 00581 break; 00582 case ')': 00583 if ( --level < 0 ) end = p; 00584 else rp = p; 00585 break; 00586 case '/': 00587 p = skipspace(p, 1) - 1; 00588 break; 00589 default: 00590 ; 00591 } 00592 } 00593 /* Erase any embedded prototype parameters. */ 00594 if ( lp ) 00595 writeblanks(lp + 1, rp); 00596 p--; /* back up over terminator */ 00597 /* Find the name being declared. */ 00598 /* This is complicated because of procedure and */ 00599 /* array modifiers. */ 00600 for ( ; ; ) 00601 { p = skipspace(p - 1, -1); 00602 switch ( *p ) 00603 { 00604 case ']': /* skip array dimension(s) */ 00605 case ')': /* skip procedure args OR name */ 00606 { int level = 1; 00607 while ( level ) 00608 switch ( *--p ) 00609 { 00610 case ']': case ')': level++; break; 00611 case '[': case '(': level--; break; 00612 case '/': p = skipspace(p, -1) + 1; break; 00613 default: ; 00614 } 00615 } 00616 if ( *p == '(' && *skipspace(p + 1, 1) == '*' ) 00617 { /* We found the name being declared */ 00618 while ( !isidfirstchar(*p) ) 00619 p = skipspace(p, 1) + 1; 00620 goto found; 00621 } 00622 break; 00623 default: 00624 goto found; 00625 } 00626 } 00627 found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' ) 00628 { if ( convert_varargs ) 00629 { *bp++ = "va_alist"; 00630 vararg = p-2; 00631 } 00632 else 00633 { p++; 00634 if ( bp == breaks + 1 ) /* sole argument */ 00635 writeblanks(breaks[0], p); 00636 else 00637 writeblanks(bp[-1] - 1, p); 00638 bp--; 00639 } 00640 } 00641 else 00642 { while ( isidchar(*p) ) p--; 00643 *bp++ = p+1; 00644 } 00645 p = end; 00646 } 00647 while ( *p++ == ',' ); 00648 *bp = p; 00649 /* Make a special check for 'void' arglist */ 00650 if ( bp == breaks+2 ) 00651 { p = skipspace(breaks[0], 1); 00652 if ( !strncmp(p, "void", 4) ) 00653 { p = skipspace(p+4, 1); 00654 if ( p == breaks[2] - 1 ) 00655 { bp = breaks; /* yup, pretend arglist is empty */ 00656 writeblanks(breaks[0], p + 1); 00657 } 00658 } 00659 } 00660 /* Put out the function name and left parenthesis. */ 00661 p = buf; 00662 while ( p != endfn ) putc(*p, out), p++; 00663 /* Put out the declaration. */ 00664 if ( header ) 00665 { fputs(");", out); 00666 for ( p = breaks[0]; *p; p++ ) 00667 if ( *p == '\r' || *p == '\n' ) 00668 putc(*p, out); 00669 } 00670 else 00671 { for ( ap = breaks+1; ap < bp; ap += 2 ) 00672 { p = *ap; 00673 while ( isidchar(*p) ) 00674 putc(*p, out), p++; 00675 if ( ap < bp - 1 ) 00676 fputs(", ", out); 00677 } 00678 fputs(") ", out); 00679 /* Put out the argument declarations */ 00680 for ( ap = breaks+2; ap <= bp; ap += 2 ) 00681 (*ap)[-1] = ';'; 00682 if ( vararg != 0 ) 00683 { *vararg = 0; 00684 fputs(breaks[0], out); /* any prior args */ 00685 fputs("va_dcl", out); /* the final arg */ 00686 fputs(bp[0], out); 00687 } 00688 else 00689 fputs(breaks[0], out); 00690 } 00691 free((char *)breaks); 00692 return 0; 00693 }
int convert1 | ( | ) |
Referenced by main().
int free | ( | ) |
Referenced by convert1(), jpeg_free_large(), jpeg_free_small(), jpeg_lib_exit(), jpeg_lib_init(), and main().
int main | ( | int | argc, | |
argv | ||||
) |
Definition at line 320 of file ansi2knr.c.
References bufsize, convert1(), free(), malloc(), skipspace(), and test1().
Referenced by jinit_d_main_controller(), make_funny_pointers(), METHODDEF(), process_data_context_main(), process_data_simple_main(), set_bottom_pointers(), set_wraparound_pointers(), and start_pass_main().
00323 { FILE *in, *out; 00324 #define bufsize 5000 /* arbitrary size */ 00325 char *buf; 00326 char *line; 00327 char *more; 00328 /* 00329 * In previous versions, ansi2knr recognized a --varargs switch. 00330 * If this switch was supplied, ansi2knr would attempt to convert 00331 * a ... argument to va_alist and va_dcl; if this switch was not 00332 * supplied, ansi2knr would simply drop any such arguments. 00333 * Now, ansi2knr always does this conversion, and we only 00334 * check for this switch for backward compatibility. 00335 */ 00336 int convert_varargs = 1; 00337 00338 if ( argc > 1 && argv[1][0] == '-' ) 00339 { if ( !strcmp(argv[1], "--varargs") ) 00340 { convert_varargs = 1; 00341 argc--; 00342 argv++; 00343 } 00344 else 00345 { fprintf(stderr, "Unrecognized switch: %s\n", argv[1]); 00346 exit(1); 00347 } 00348 } 00349 switch ( argc ) 00350 { 00351 default: 00352 printf("Usage: ansi2knr input_file [output_file]\n"); 00353 exit(0); 00354 case 2: 00355 out = stdout; 00356 break; 00357 case 3: 00358 out = fopen(argv[2], "w"); 00359 if ( out == NULL ) 00360 { fprintf(stderr, "Cannot open output file %s\n", argv[2]); 00361 exit(1); 00362 } 00363 } 00364 in = fopen(argv[1], "r"); 00365 if ( in == NULL ) 00366 { fprintf(stderr, "Cannot open input file %s\n", argv[1]); 00367 exit(1); 00368 } 00369 fprintf(out, "#line 1 \"%s\"\n", argv[1]); 00370 buf = malloc(bufsize); 00371 line = buf; 00372 while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL ) 00373 { 00374 test: line += strlen(line); 00375 switch ( test1(buf) ) 00376 { 00377 case 2: /* a function header */ 00378 convert1(buf, out, 1, convert_varargs); 00379 break; 00380 case 1: /* a function */ 00381 /* Check for a { at the start of the next line. */ 00382 more = ++line; 00383 f: if ( line >= buf + (bufsize - 1) ) /* overflow check */ 00384 goto wl; 00385 if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL ) 00386 goto wl; 00387 switch ( *skipspace(more, 1) ) 00388 { 00389 case '{': 00390 /* Definitely a function header. */ 00391 convert1(buf, out, 0, convert_varargs); 00392 fputs(more, out); 00393 break; 00394 case 0: 00395 /* The next line was blank or a comment: */ 00396 /* keep scanning for a non-comment. */ 00397 line += strlen(line); 00398 goto f; 00399 default: 00400 /* buf isn't a function header, but */ 00401 /* more might be. */ 00402 fputs(buf, out); 00403 strcpy(buf, more); 00404 line = buf; 00405 goto test; 00406 } 00407 break; 00408 case -1: /* maybe the start of a function */ 00409 if ( line != buf + (bufsize - 1) ) /* overflow check */ 00410 continue; 00411 /* falls through */ 00412 default: /* not a function */ 00413 wl: fputs(buf, out); 00414 break; 00415 } 00416 line = buf; 00417 } 00418 if ( line != buf ) 00419 fputs(buf, out); 00420 free(buf); 00421 fclose(out); 00422 fclose(in); 00423 return 0; 00424 }
char* malloc | ( | ) |
Referenced by convert1(), jpeg_get_large(), jpeg_get_small(), jpeg_lib_init(), and main().
char* skipspace | ( | char * | p, | |
int | dir | |||
) |
Definition at line 428 of file ansi2knr.c.
References is_space.
00431 { for ( ; ; ) 00432 { while ( is_space(*p) ) 00433 p += dir; 00434 if ( !(*p == '/' && p[dir] == '*') ) 00435 break; 00436 p += dir; p += dir; 00437 while ( !(*p == '*' && p[dir] == '/') ) 00438 { if ( *p == 0 ) 00439 return p; /* multi-line comment?? */ 00440 p += dir; 00441 } 00442 p += dir; p += dir; 00443 } 00444 return p; 00445 }
char* skipspace | ( | ) |
Referenced by convert1(), main(), and test1().
int test1 | ( | char * | buf | ) |
Definition at line 476 of file ansi2knr.c.
References isidchar, isidfirstchar, and skipspace().
00478 { register char *p = buf; 00479 char *bend; 00480 char *endfn; 00481 int contin; 00482 00483 if ( !isidfirstchar(*p) ) 00484 return 0; /* no name at left margin */ 00485 bend = skipspace(buf + strlen(buf) - 1, -1); 00486 switch ( *bend ) 00487 { 00488 case ';': contin = 0 /*2*/; break; 00489 case ')': contin = 1; break; 00490 case '{': return 0; /* not a function */ 00491 case '}': return 0; /* not a function */ 00492 default: contin = -1; 00493 } 00494 while ( isidchar(*p) ) 00495 p++; 00496 endfn = p; 00497 p = skipspace(p, 1); 00498 if ( *p++ != '(' ) 00499 return 0; /* not a function */ 00500 p = skipspace(p, 1); 00501 if ( *p == ')' ) 00502 return 0; /* no parameters */ 00503 /* Check that the apparent function name isn't a keyword. */ 00504 /* We only need to check for keywords that could be followed */ 00505 /* by a left parenthesis (which, unfortunately, is most of them). */ 00506 { static char *words[] = 00507 { "asm", "auto", "case", "char", "const", "double", 00508 "extern", "float", "for", "if", "int", "long", 00509 "register", "return", "short", "signed", "sizeof", 00510 "static", "switch", "typedef", "unsigned", 00511 "void", "volatile", "while", 0 00512 }; 00513 char **key = words; 00514 char *kp; 00515 int len = endfn - buf; 00516 00517 while ( (kp = *key) != 0 ) 00518 { if ( strlen(kp) == len && !strncmp(kp, buf, len) ) 00519 return 0; /* name is a keyword */ 00520 key++; 00521 } 00522 } 00523 return contin; 00524 }
int test1 | ( | ) |
Referenced by main().
int writeblanks | ( | char * | start, | |
char * | end | |||
) |
Definition at line 452 of file ansi2knr.c.
00455 { char *p; 00456 for ( p = start; p < end; p++ ) 00457 if ( *p != '\r' && *p != '\n' ) 00458 *p = ' '; 00459 return 0; 00460 }
int writeblanks | ( | ) |
Referenced by convert1().