/************************************************************************
*                       - INFOFLEX System -
* This source code is CONFIDENTIAL and PROPRIETARY to INFOFLEX ,
* INC. . The information contained herein may not be used, copied or
* disclosed in whole or in part except as permitted by written agreement
* signed by an officer of INFOFLEX,INC..
*
* COPYRIGHT (c) 1986 INFOFLEX , INC., BURLINGAME, CALIFORNIA.
* ALL RIGHTS RESERVED.
*************************************************************************/


/*  This programs massages the output from cflow  */

#include <stdio.h>
#define YES 1
#define NO  0 
static char *getnextfile();
static char *getword();

static char inbuf[1000];
static char outbuf[1000];
static char g_word[1000];

void main( argc, argv)
int argc;
char *argv[];
{
    FILE *infile  = stdin;
    FILE *outfile = stdout;
    FILE *errorfile = stderr;
    int tabcount;
    char *ptr;

    for (;;) {
        if ( NULL == fgets(inbuf, sizeof( inbuf), infile) ) {
            break;
            }
        /* remove line feed */
        inbuf[strlen(inbuf) - 1] = '\0';

        ptr = inbuf;
        *outbuf = '\0';
        /* skip over line number count */
        if ( (char *)0 == (ptr = getword(ptr, g_word)) )
           break;
        /* count tabs */
        for (tabcount=0;*ptr == '\t'; ++tabcount, ++ptr);
        /*
          printf("firstword=%s, tabcount=%d\n", g_word, tabcount);
        */
        if (tabcount <= 0) {
           fprintf(errorfile, "%s\n", inbuf);
           /* strcpy(outbuf, inbuf); */
           continue;
           }
        --tabcount;
        memset(outbuf, ' ', tabcount * 4);
        outbuf[tabcount * 4] = '\0';
        if ( (char *)0 == (ptr = getword(ptr, g_word)) )
           break;
        strcat(outbuf, g_word);
        if (strcmp(g_word, "strcpy") == 0 ||
            strcmp(g_word, "strncpy") == 0 ||
            strcmp(g_word, "strcat") == 0 ||
            strcmp(g_word, "strncat") == 0 ||
            strcmp(g_word, "strcmp") == 0 ||
            strcmp(g_word, "strncmp") == 0 ||
            strcmp(g_word, "strlen") == 0 ||
            strcmp(g_word, "memset") == 0 ||
            strcmp(g_word, "memcpy") == 0 ||
            strcmp(g_word, "strcompress") == 0 ||
            strcmp(g_word, "loadmsg") == 0) {
            continue;
            }
        if ( (char *)0 == (ptr = getword(ptr, g_word)) )
           break;
        if (strcmp(g_word, ":") != 0) {
           strcat(outbuf, g_word);
           }
        for (;;) {
            if ( (char *)0 == (ptr = getword(ptr, g_word)) )
               break;
            if (strcmp(g_word, "<>") == 0)
               continue;
            if (g_word[0] != '<')
               continue;
            g_word[0] = '('; 
            strcat(g_word, ")");
            strcat(outbuf, g_word);
            break;
            }

        strcat(outbuf, "\n");
        if (EOF == fputs(outbuf, outfile)) {
           fprintf(errorfile, "fputs() error\n");
           }
        } /* end of for loop */


    fprintf(errorfile, "DONE\n");

} /* main */


static char *getword( ptr, word)
char *ptr;
char *word;
{
   char *ptrword = word;
   extern char *strchr();
#  ifdef DEBUG
   printf("start word = %s\n", ptr);
#  endif 

   /* skip leading spaces */
   for (;*ptr == ' ' || *ptr == '\t';++ptr);

   if (*ptr == ':') {
      ptrword[0] = *ptr;
      ptrword[1] = '\0';
#     ifdef DEBUG
      printf("word = %s\n", word); 
#     endif 
      return(++ptr);
      } 

#  ifdef DEBUG
   printf("next word = %s\n", ptr);
#  endif 

   /* copy until end of string */
   for (;*ptr != ' ' && *ptr != '\t' && *ptr != ':' && *ptr != '\0';)
      *ptrword++ = *ptr++;
   *ptrword = '\0'; /* terminate string */

#  ifdef DEBUG
   printf("word = %s\n", word); 
#  endif 

   if (*word == '\0')  {
        return( (char *)0);
        }
   return(ptr);
}


static int getfilelist()
{
    char command[ 100];
    char *filename;

    for ( ; ; ) {

       if ( NULL == (filename = getnextfile( "filelist") ) )
           break;

       strcpy( command, "sub ");
       strcat( command, "????");
       strcat( command, "  ");
       strcat( command, filename);
       strcat( command, " > junk");

       printf( command);
       printf( "\n");
       if ( 0 > system( command) ) {
          printf( "System call error\n");
          continue;
          }

       strcpy( command, "cp ");
       strcat( command, "junk ");
       strcat( command, filename);

       printf( command);
       printf( "\n");
       if ( 0 > system( command) ) {
          printf( "System call error\n");
          continue;
          }
       }

}



char *getnextfile( listname)
char *listname;
{
    static int firsttime = YES;
    static FILE *in;
    static char filename[100];


    if ( firsttime == YES) {
       firsttime = NO;
       in = fopen( listname,  "r");
       }

    if ( NULL == fgets( filename, sizeof( filename), in) ) {
      fclose( in);
      return( NULL);
      }

  /* strcompress( filename); */

  return( filename);

}

