/* This program reads a C file and adds type's to function prototypes. e.g.
int xyz(int x);
	becomes
int xyz(int x);
*/

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

#define true 1
#define false 0

/*--------------------------------------------------------------------------*/
main(int argc, char *argv[])
{
  int i;
  char buff[90];
  for (i=1;i<argc;i++) {
    do_file(argv[i]);
    printf("Just done {%s} Continue?",argv[i]);
  }
}
do_file(char *inname)
{
	FILE *fin,*fout;
	char inbuff[3200];
	char outname[100],outbak[100];
	int sl,i,j,k;
	char *s;

	strcpy(outname,inname);

	fout = fopen("addtype.tmp","w");
	if (fout==NULL) perror("open error2 "), exit(1);
	fin = fopen(inname,"r");
	if (fin == NULL) { perror("Unable to open file") ;  exit(2); }
	if (fout == NULL) { perror("Unable to open file") ;  exit(2); }
	for (;!feof(fin);) {
	        inbuff[0] = 0;
		if (fgets(inbuff,32000,fin)==NULL) break;
		do_line(inbuff);
		fputs(inbuff,fout);
	}
	fclose(fin);
	fclose(fout);
	strcpy(outbak,outname);
	strcat(outbak,".bak");
	rename(outname,outbak);
	rename("addtype.tmp",outname);
}
do_line(char *ss)
{
	int bk=0;
	static char buff[200];
	char *s;
	s = ss;
	if (!isalpha(*s)) return;
	s++;
	for (;*s!=NULL;s++) {
		if (*s=='(') goto search_right;
		if (isspace(*s)) return;
	}
	return;
search_right:
	for (;*s!=NULL;s++) {
		if (*s=='(') bk++;
		if (*s==')') if (bk==1) {
			if (*(++s)==';') goto add_int;
			else return;
		} else bk--;
	}
	return;
add_int:
	strcpy(buff,"int ");
	strcat(buff,ss);
	strcpy(ss,buff);
	printf("%s",buff);
}
















