home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Professional
/
OS2PRO194.ISO
/
os2
/
patches
/
toolkt20
/
gnufix.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-03
|
5KB
|
263 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.h"
#include "xstring.h"
static char linebuffer[1024];
static char tempbuffer[1024];
#define MAX_KEYWORDS 2
static int replace_count[MAX_KEYWORDS] = { 0, 0 };
static int typedef_count = 0;
static char *keyword[MAX_KEYWORDS] = {
"APIENTRY ",
"EXPENTRY "
};
static char *infilename;
static char *outfilename;
static FILE *infile;
static FILE *outfile;
void
strxblank( char *s )
{
char *firstchar = NULL;
char *lastblank = NULL;
char *p = s;
while ( *s != '\0' ) {
switch ( *s ) {
case '\n' :
s++;
continue;
break;
case ' ' :
if ( firstchar == NULL ) {
s++;
continue;
}
if ( lastblank == NULL )
lastblank = p;
else {
s++;
continue;
}
break;
default :
if ( firstchar == NULL )
firstchar = p;
lastblank = NULL;
break;
}
*p++ = *s++;
}
if ( lastblank == p-1 )
p = lastblank;
*p = '\0';
}
int
substitute( char *the_key )
{
char *p = tempbuffer;
char *s = linebuffer;
char *key;
char *end;
key = strstr( linebuffer, the_key );
if ( key == NULL )
return FALSE;
end = strrchr(s,';');
if ( end == NULL ) {
printf("ERROR: located keyword with no terminating ;\n");
return FALSE;
}
while ( s != key ) *p++ = *s++;
while ( *s != ' ' ) *p++ = *s++;
strcpy(p,"_FUNC");
p += 5;
*p++ = '(';
s++;
while ( *s != ' ' && *s != '(' ) *p++ = *s++;
*p++ = ',';
if ( *s == ' ' )
s++;
while ( s != end ) *p++ = *s++;
*p++ = ')';
*p++ = *s++;
*p = '\0';
strcpy( linebuffer, tempbuffer );
return TRUE;
}
void
filter( FILE *input, FILE *output )
{
int comment_entry_flag = FALSE;
int blank_flag = TRUE;
int more_macro = FALSE;
int ccount = 0;
int linecount = 0;
int eol = FALSE;
char tline[2048];
char line[1024];
int macromoreflag = FALSE;
int statementmoreflag = FALSE;
int i;
while ( !feof(input) ) {
fgets(line,1024,input);
strxblank(line);
if ( line[0] == '#' ) {
if ( line[strlen(line)-1] == '\\' )
macromoreflag = TRUE;
else
macromoreflag = FALSE;
fprintf(output,"%s\n",line);
for ( i = 0; i < MAX_KEYWORDS; i++ ) {
if ( strstr(line,keyword[i]) )
printf("WARNING: '%s' keyword found in MACRO\n",keyword[i]);
}
continue;
}
if ( macromoreflag ) {
if ( line[strlen(line)-1] == '\\' )
macromoreflag = TRUE;
else
macromoreflag = FALSE;
for ( i = 0; i < MAX_KEYWORDS; i++ ) {
if ( strstr(line,keyword[i]) )
printf("WARNING: '%s' keyword found in MACRO\n",keyword[i]);
}
fprintf(output,"%s\n",line);
continue;
}
if ( strrchr(line,'{') ) {
if ( strlen(linebuffer) != 0 ) {
fprintf(output,"%s\n",linebuffer);
linebuffer[0] = '\0';
}
fprintf(output,"%s\n",line);
continue;
}
strcat(linebuffer,line);
if ( strrchr(line,';') )
statementmoreflag = FALSE;
else
statementmoreflag = TRUE;
if ( !statementmoreflag ) {
if ( !strstr(linebuffer,"typedef") ) {
for ( i = 0; i < MAX_KEYWORDS; i++ ) {
if ( substitute(keyword[i]) )
replace_count[i]++;
}
}
else {
typedef_count++;
}
fprintf(output,"%s\n",linebuffer);
linebuffer[0] = '\0';
}
}
}
main(int argc, char *argv[], char *envp[])
{
char c;
char *ptroption;
int i;
infilename = NULL;
outfilename = NULL;
while ( (c = getopt(argc,argv,"i:o:",&ptroption)) != EOF) {
switch (c) {
case 'i' :
infilename = ptroption;
break;
case 'o' :
outfilename = ptroption;
break;
default :
return 2;
}
}
if ( infilename == NULL ) {
printf("specify input file\n");
printf("gnufix -i filename -o filename\n");
exit(1);
}
if ( outfilename == NULL ) {
printf("specify output file\n");
printf("gnufix -i filename -o filename\n");
exit(2);
}
infile = fopen(infilename,"r");
if ( infile == NULL ) {
printf("can't find input file %s\n",infilename);
exit(3);
}
outfile = fopen(outfilename, "w");
if ( outfile == NULL ) {
printf("can't create output file %s\n",outfilename);
exit(4);
}
printf("gnu fixing: %s\n",infilename);
filter(infile,outfile);
if ( typedef_count )
printf(" %4d keyword(s) found in 'typedef'\n",typedef_count);
for ( i = 0; i < MAX_KEYWORDS; i++ ) {
if ( replace_count[i] )
printf("Replaced %4d '%s' keywords.\n",replace_count[i],keyword[i]);
}
fflush(outfile);
fclose(infile);
fclose(outfile);
}