home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / GNUFIX.ZIP / GNUFIX.C < prev    next >
C/C++ Source or Header  |  1992-08-03  |  5KB  |  263 lines

  1.  
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #include "getopt.h"
  8. #include "xstring.h"
  9.  
  10. static char linebuffer[1024];    
  11. static char tempbuffer[1024];
  12.  
  13. #define MAX_KEYWORDS 2
  14. static int  replace_count[MAX_KEYWORDS] = { 0, 0 };
  15. static int  typedef_count = 0;
  16.  
  17. static char *keyword[MAX_KEYWORDS] = {
  18.    "APIENTRY ",
  19.    "EXPENTRY "
  20.    };
  21.  
  22. static char *infilename;
  23. static char *outfilename;
  24.  
  25. static FILE *infile;
  26. static FILE *outfile;
  27.  
  28. void
  29. strxblank( char *s )
  30. {
  31.    char *firstchar = NULL;
  32.    char *lastblank = NULL;
  33.    char *p = s;
  34.  
  35.    while ( *s != '\0' ) {
  36.       switch ( *s ) {
  37.      case '\n' :
  38.         s++;
  39.         continue;
  40.         break;
  41.      case ' ' :
  42.         if ( firstchar == NULL ) {        
  43.            s++;                
  44.            continue;
  45.            }
  46.         if ( lastblank == NULL )        
  47.            lastblank = p;
  48.         else {
  49.            s++;
  50.            continue;
  51.            }
  52.         break;
  53.      default :
  54.         if ( firstchar == NULL )
  55.            firstchar = p;
  56.         lastblank = NULL;
  57.         break;
  58.      }
  59.  
  60.       *p++ = *s++;
  61.       }
  62.  
  63.    if ( lastblank == p-1 )
  64.       p = lastblank;
  65.  
  66.    *p = '\0';        
  67. }
  68.  
  69. int
  70. substitute( char *the_key )
  71. {
  72.    char *p = tempbuffer;
  73.    char *s = linebuffer;
  74.    char *key;
  75.    char    *end;
  76.  
  77.        
  78.    key = strstr( linebuffer, the_key );
  79.    if ( key == NULL )
  80.       return FALSE;
  81.  
  82.        
  83.    end = strrchr(s,';');
  84.    if ( end == NULL ) {
  85.       printf("ERROR: located keyword with no terminating ;\n");
  86.       return FALSE;
  87.       }
  88.  
  89.    while ( s != key ) *p++ = *s++;    
  90.  
  91.    while ( *s != ' ' ) *p++ = *s++;    
  92.  
  93.    strcpy(p,"_FUNC");
  94.    p += 5;
  95.  
  96.    *p++ = '(';        
  97.    s++;            
  98.  
  99.        
  100.    while ( *s != ' ' && *s != '(' ) *p++ = *s++;
  101.  
  102.    *p++ = ',';        
  103.  
  104.    if ( *s == ' ' )    
  105.       s++;
  106.  
  107.    while ( s != end ) *p++ = *s++;
  108.  
  109.    *p++ = ')';        
  110.  
  111.    *p++ = *s++;        
  112.    *p = '\0';        
  113.  
  114.        
  115.    strcpy( linebuffer, tempbuffer );
  116.    return TRUE;
  117. }
  118.  
  119. void
  120. filter( FILE *input, FILE *output )
  121. {
  122.    int  comment_entry_flag = FALSE;
  123.    int  blank_flag = TRUE;
  124.    int  more_macro = FALSE;
  125.    int     ccount = 0;
  126.    int    linecount = 0;
  127.    int    eol = FALSE;
  128.    char    tline[2048];
  129.    char    line[1024];
  130.    int    macromoreflag = FALSE;
  131.    int  statementmoreflag = FALSE;
  132.    int    i;
  133.  
  134.    while ( !feof(input) ) {
  135.  
  136.       fgets(line,1024,input);
  137.       strxblank(line);
  138.  
  139.       if ( line[0] == '#' ) {
  140.  
  141.      if ( line[strlen(line)-1] == '\\' ) 
  142.         macromoreflag = TRUE;
  143.      else
  144.         macromoreflag = FALSE;
  145.         
  146.      fprintf(output,"%s\n",line);
  147.      for ( i = 0; i < MAX_KEYWORDS; i++ ) {
  148.         if ( strstr(line,keyword[i]) )
  149.            printf("WARNING: '%s' keyword found in MACRO\n",keyword[i]);
  150.         }
  151.      continue;
  152.          }
  153.  
  154.       if ( macromoreflag ) {
  155.      if ( line[strlen(line)-1] == '\\' )
  156.         macromoreflag = TRUE;
  157.      else
  158.         macromoreflag = FALSE;
  159.      for ( i = 0; i < MAX_KEYWORDS; i++ ) {
  160.         if ( strstr(line,keyword[i]) )
  161.            printf("WARNING: '%s' keyword found in MACRO\n",keyword[i]);
  162.         }
  163.      fprintf(output,"%s\n",line);
  164.      continue;
  165.          }
  166.  
  167.       if ( strrchr(line,'{') ) {
  168.      if ( strlen(linebuffer) != 0 ) {
  169.         fprintf(output,"%s\n",linebuffer);
  170.         linebuffer[0] = '\0';
  171.         }
  172.      fprintf(output,"%s\n",line);
  173.      continue;
  174.          }
  175.  
  176.       strcat(linebuffer,line);
  177.  
  178.       if ( strrchr(line,';') )
  179.      statementmoreflag = FALSE;
  180.       else
  181.      statementmoreflag = TRUE;
  182.  
  183.       if ( !statementmoreflag ) {
  184.      if ( !strstr(linebuffer,"typedef") ) {
  185.         for ( i = 0; i < MAX_KEYWORDS; i++ ) {
  186.            if ( substitute(keyword[i]) )
  187.           replace_count[i]++;
  188.            }
  189.         }    
  190.      else {
  191.         typedef_count++;
  192.             }
  193.      fprintf(output,"%s\n",linebuffer);
  194.      linebuffer[0] = '\0';
  195.          }
  196.  
  197.       } 
  198.  
  199. }
  200.  
  201. main(int argc, char *argv[], char *envp[])
  202. {
  203.    char c;
  204.    char *ptroption;
  205.    int  i;
  206.  
  207.    infilename = NULL;
  208.    outfilename = NULL;
  209.  
  210.    while ( (c = getopt(argc,argv,"i:o:",&ptroption)) != EOF) {
  211.       switch (c) {
  212.          case 'i' :
  213.         infilename = ptroption;
  214.             break;
  215.      case 'o' :
  216.         outfilename = ptroption;
  217.         break;
  218.          default :
  219.             return 2;
  220.       }
  221.    }
  222.  
  223.    if ( infilename == NULL ) {
  224.       printf("specify input file\n");
  225.       printf("gnufix -i filename -o filename\n");
  226.       exit(1);
  227.       }
  228.  
  229.    if ( outfilename == NULL ) {
  230.       printf("specify output file\n");
  231.       printf("gnufix -i filename -o filename\n");
  232.       exit(2);
  233.       }
  234.  
  235.    infile = fopen(infilename,"r");
  236.    if ( infile == NULL ) {
  237.       printf("can't find input file %s\n",infilename);
  238.       exit(3);
  239.       }
  240.  
  241.    outfile = fopen(outfilename, "w");
  242.    if ( outfile == NULL ) {
  243.       printf("can't create output file %s\n",outfilename);
  244.       exit(4);
  245.       }
  246.  
  247.    printf("gnu fixing: %s\n",infilename);
  248.  
  249.    filter(infile,outfile);
  250.  
  251.    if ( typedef_count )
  252.       printf("         %4d keyword(s) found in 'typedef'\n",typedef_count);
  253.    for ( i = 0; i < MAX_KEYWORDS; i++ ) {
  254.       if ( replace_count[i] )
  255.       printf("Replaced %4d '%s' keywords.\n",replace_count[i],keyword[i]);
  256.       }
  257.  
  258.    fflush(outfile);
  259.    fclose(infile);
  260.    fclose(outfile);
  261. }
  262.  
  263.