
/*  This routine is a utility to read a MAKEFILE used by the MAKE
    utility and recover, display, and print an alphabetic listing
    of all FUNCTIONS found in the source files.
*/


#include <exec/types.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <stdio.h>
#include <ctype.h>
#include <exec/memory.h>

char str[120], c[190], line[190], filename[190], lin[190], file[180];
int toggle, ll, a, p, mkin, vv;
char *x,*y,*z, *xx;
int per1,os,coln,per2,cs,LineCount,lparen,rparen,ss,mk;


/*************************/
void main()
{
   FILE *fb, *ib, *ob;

   if((fb = fopen("makefile","r")) == NULL) {
      printf("Error.. can't open MAKEFILE");
      exit(100);
   }
   if((ob = fopen(".FUNCT","w")) == NULL) {
      printf("Error.. can't open .FUNCT file");
      fclose(fb);
      exit(101);
   }
vv=0;

NEXTLINE:
   xx = fgets(c,190,fb);
   vv++;
   if (xx==NULL) { goto Main_Exit;}   /* end of makefile */
   ll=strlen(c);
   if (ll<5) {
      printf("\n%d Line too short",vv);
      goto NEXTLINE;
   }
                               /* if (c[0]==0x20) {goto NEXTLINE;} */
   per1 = os = coln = per2 = cs = 0;
   for (mkin=0;mkin < ll; mkin++) {
      if(c[mkin]==0x2e) {       /* period */
          if((per1==0)&&(c[mkin+1]==0x6f)) {   /* o */
                 per1=mkin;
                 os=mkin+1;
          }
          if((per1>0)&&(c[mkin+1]==0x63)&&(coln>0)) { /* c */
                 per2=mkin;
                 cs=mkin+1;
          }
       }
       if(c[mkin]==0x3a) {               /* colon */
          if((per1>0)&&(per2==0)) {
                 coln=mkin;
           }
       }
    }       /* end of FOR */
  if((per1==0)||(per2==0)||(coln==0)) {goto NEXTLINE;}
  if((per1>coln)||(per2<coln)) { goto NEXTLINE;}  /*UN- successful line */
     mkin=coln+1;
     while(c[mkin]==0x20) {mkin++;}
     per2+=2;
     c[per2]=0x00;
     strcpy(&file,&c[mkin]);
     printf("\nProcessing >%s<\n\n",file);

     if((ib=fopen(file,"r"))==NULL) {
         printf("\nError... cannot open >%s<\n",file);
         goto NEXTLINE;
     }
/* NOW TO READ THE SOURCE FILE */

   LineCount = 0;
   toggle = 0;
NEXTIN:

   lparen = rparen = 0;
   ss = mk = 999;              /* 1st space before fname */
   xx=fgets(c,190,ib);
   if(xx==NULL){
      fclose(ib);
      goto NEXTLINE;
   }

    LineCount++;
    mkin=strlen(c);
    if (mkin<4) {goto NEXTIN;}
    for( ;mkin>0;mkin--) {
        if((c[mkin]==0x2f)&&(c[mkin+1]==0x2a)){
           toggle=1;
           goto MOREFOR;
        }
        if((c[mkin]==0x2a)&&(c[mkin+1]==0x2f)){
           toggle=0;
           goto MOREFOR;
        }
        if (toggle == 1) {goto MOREFOR;}
        

        if((c[mkin]==0x0a)||(c[mkin]==0x0d)) {c[mkin]=0x00;}
        if((c[mkin]==0x3b)||(c[mkin]==0x7d)||(c[mkin]==0x26)||
           (c[mkin]==0x7c)||(c[mkin]==0x7b)) { goto NEXTIN;}
        if(c[mkin]==0x28) {lparen = mkin;}      /* left paren  */
        if(c[mkin]==0x29) {rparen = mkin;}      /* right paren */
        if ((c[mkin]==0x20)&&(lparen>0)&&(rparen>0)&&(mk!=999)&&(ss==999)){
               ss=mkin;
                  }
        if ((isalpha(c[mkin]))!=0) {
           if ((lparen>0)&&(rparen>0)) {
              mk=mkin;
           }
        }
MOREFOR:
   }       /* end of FOR */

   if((lparen==0)||(rparen<=lparen)) { goto NEXTIN;}   /* FAIL */
      if(ss==999) {
         ss=0;
       } else {
         ss++;
       }
       strcpy(line,&c[ss]);
       strupr(line);


/* START LOOKING FOR SPECIALS */

       if((stcpm(line,"IF(+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,"SWITCH(+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,"WHILE(+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,"DO(+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,"IF (+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,">+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,"<+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,"LONG)+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,"SIZEOF(+",z))!=0) {goto NEXTIN;}
       if((stcpm(line,"=+",z))!=0) {goto NEXTIN;}
   

       for(a=0;a<strlen(line);a++) {
           if(line[a]==0x2a) {goto NEXTIN;}
       }
       if(line[0]==0x20) {goto NEXTIN;}
       if(line[0]==0x28) {goto NEXTIN;}


       sprintf(str,"%-25.25s %-25.25s %4.4d %20.20s\n",
             line,&c[ss],LineCount,file);
       printf("%s",str);
       fputs(str,ob);
       goto NEXTIN;

Main_Exit:
      if(fb) { fclose(fb);}
      if(ob) { fclose(ob);}
      printf("\n\nEND OF write!!!\n\n");
      printf("\n%d Makefile lines processed!\n",vv);

      Execute("sort .FUNCT to .SUNCT",0,0);


   if((fb = fopen(".SUNCT","r")) == NULL) {
      printf("Error.. can't open MAKEFILE");
      exit(100);
   }
   if((ob = fopen("SORTED.FUNCTIONS","w")) == NULL) {
      printf("Error.. can't open OUTPUT file");
      fclose(fb);
      exit(101);
   }

SPINWRITE:   
   xx = fgets(c,190,fb);
   if (xx==NULL) { goto Final_Exit;}   /* end of makefile */
   strcpy(line,&c[26]);
   printf("%s",line);
   fputs(line,ob);
   goto SPINWRITE;

Final_Exit:
   fclose(fb);
   fclose(ob);

}







         

