

/*                             FIX v1.4
                                  by
                             Dave Huntoon
                               03/12/86

Can be reached at
  SAPRROW RBBS
  Rochester, NY
  716-377-1113
*/

/*  LANGUAGE ==> AZTEC C II
                 Compile using "-tiny" switch for smaller .com size
*/


/*****************************************
*  Replaces TABS with <n> SPACES ==> -Tn *
*   adds <n> Nulls for each LF   ==> -Nn *
*   adds LF after CR             ==> -L  *
*   adds CR after LF             ==> -C  *
*   exchanges CR for LF          ==> -EC *
*   exchanges LF for CR          ==> -EL *
*   removes all LF's            ==>> -RL *
*   removes all CR's            ==>> -RC *
*   removes all NULLS           ==>> -RN *
******************************************/

#include "stdio.h"
#include "ctype.h"

#define TRUE   1
#define FALSE  0
#define ERROR  0
#define NULL   0
#define MAX    8
#define NMAX   9
#define MIN    1
#define NOM    8
#define NNOM   5
#define SP     0x20
#define LF     0x0a
#define CR     0x0d
#define TAB    0x09
#define BELL   0x07
#define CMD    '-'

/*  FIX will open a specified file and search for the selected
    character (CR, LF, TAB) and add or change as follows:

         -Tn   replaces TABS with spaces on <n> character boundaries
                  values can be from 1 to 8, nominal = 8

         -Nn   adds <n> NULLS after every LF for slow printers.
                  value can be from 1 to 9, nominal = 5

         -L    adds a line feed (LF) after every carriage return CR.

         -C    adds a CR after every LF

         -EC   exchanges CR for LF

         -EL   exchanges LF for CR

         -RL   removes all LF's

         -RC   removes all CR's

         -RN   removes all NULL's

    All changes will be written to <filename>.CNG, Drive can be selected.
*/

main(argc,argv)
int  argc  ;
char *argv[]  ;

{
 int   i,j,length, length1, temp,temp1,
      point,change,add,ch1,drive,exchange,remove  ;

 FILE  *f1, *f2, *fopen()  ;

 char  filenm[20], ch[3], ch2[3], name[20] ;


  printf("\n\n  FIX...Ver 1.4\n")  ;
  printf("\t  by Dave Huntoon 3/12/86\n\n")  ;

  if (argc==1 || argc != 3 )
     syntax()  ;

  strcpy(ch,argv[2])  ;

  if((ch[0] != CMD) || (strlen(ch) > 3))
      {
       printf("\n*** Command format error ***\n")  ;
       syntax()  ;
      }

  ch1=ch[1]  ;
 strcpy(filenm,argv[1])  ;
 printf("Input file  ==> %s\n",filenm)  ;

 j= point = drive = FALSE  ;

 printf("\nDrive [n] to write output to or <return> if same ==> ")  ;
 temp=getchar() ;

 if (iscntrl(temp)==0)
  {                       /* add drive specs to filename to write to */
   if (isalpha(temp)==0)
    {
     printf("\n\t*** Drive spec ERROR ***\n")  ;
     exit(0)  ;
    }
   drive=TRUE  ;
   temp1 = toupper(temp)  ;
   name[0]=temp1  ;
   name[1]=':'  ;
   point= 2 ;
  }                      /* end adding drive to name */

 j=-1  ;
    for(i=point; i<12; i++)
      {
       j = ++j  ;
       name[i]=filenm[j]  ;
       if(name[i] == ':' && drive == TRUE)
         i=(point-1)  ;

       if(name[i] == NULL)
         name[i] = '.'  ;

       if(name[i] == '.')
        {
         name[i+1]='C'  ;
         name[i+2]='N'  ;
         name[i+3]='G'  ;
         name[i+4]= NULL  ;
         break  ;
        }
      }

  printf("\nOutput file ==> %s\n",name)  ;

  if (((f1=fopen(argv[1],"r"))==NULL) || ((f2=fopen(name,"w"))==NULL))
   {
    printf("\nError opening file!\n")  ;
    exit(1)  ;
   }

       /*  Decide on command to invoke */

   exchange = remove = FALSE  ;

    switch(ch1)
    {
     case 'T':
     case 't':   {
                  ch2[0]=ch[2]  ;
                  ch2[1]=NULL  ;
                  length=atoi(ch2)  ;
                  if(length < MIN || length > MAX)
                    length = NOM  ;
                  printf("\nExchanging spaces for TABs on char boundary of %d\n",length)  ;
                  add=(char)SP  ;
                  change=(char)TAB  ;
                  break  ;
                 }

     case 'C':
     case 'c':  {
                 printf("\nAdding CR for each LF\n")  ;
                 add=(char)CR  ;
                 change=(char)LF  ;
                 break  ;
                }

     case 'L':
     case 'l':  {
                 printf("\nAdding LF for each CR\n")  ;
                 add=(char)LF  ;
                 change=(char)CR  ;
                 break  ;
                }
     case 'N':
     case 'n':   {
                  ch2[0]=ch[2]  ;
                  ch2[1]=NULL  ;
                  length=atoi(ch2)  ;
                  if(length < MIN || length > NMAX)
                    length = NNOM  ;
                  printf("\nAdding %d NULLs after each LF\n",length)  ;
                  add = NULL  ;
                  change = LF  ;
                  break  ;
                 }

      case 'E':
      case 'e':  {
                  if (ch[2] == 'c' || ch[2] == 'C')
                    {
                     printf ("\nExchanging CR for LF\n")  ;
                     add = CR  ;
                     change = LF  ;
                     exchange = TRUE  ;
                     break  ;
                    }
                  if (ch[2] == 'l' || ch[2] == 'L')
                    {
                     printf("\nExchanging LF for CR\n")  ;
                     add = LF  ;
                     change = CR  ;
                     exchange = TRUE  ;
                     break  ;
                    }
                 }

     case 'R':
     case 'r':  {
                 if (ch[2] == 'c' || ch[2] == 'C')
                   {
                    printf ("\nRemoving all CR's\n")  ;
                    change = CR  ;
                    remove = TRUE  ;
                    break  ;
                   }
                 if (ch[2] == 'l' || ch[2] == 'L')
                   {
                    printf("\nRemoving all LF's\n")  ;
                    change = LF  ;
                    remove = TRUE  ;
                    break  ;
                   }
                 if (ch[2] == 'n' || ch[2] == 'N')
                   {
                    printf("\nRemoving all NULL's\n")  ;
                    change = NULL  ;
                    remove = TRUE  ;
                    break  ;
                   }
                 }
    default:    {
                  printf("\nUnknown command ==>  %c\n\n",ch1)  ;
                  exit(0)  ;
                }
    }             /* end of command */


   /* Go Fix the file */

    j=0  ;     /* char counter */

    while((ch1=getc(f1)) != EOF)
      {
       if(j>=length || ch1==CR || ch1==LF)
         j=0  ;
       length1=length-j  ;

       j=j+1  ;

       if(ch1==CR || ch1==LF)
         j=0  ;

       if(ch1==change)
        {
          switch(ch1)
          {

            case TAB:    {
                          ch1=(char)SP  ;
                          j=0  ;
                          for(i=0; i<length1; i++)
                          putc(ch1,f2)  ;
                          break  ;
                          }
            case CR:
            case NULL:
                          {
                           if (remove)
                             break  ;

                           if(exchange)
                             {
                              putc(add,f2)  ;
                              break  ;
                             }

                           putc(change,f2)  ;
                           putc(add,f2)  ;
                           break  ;
                           }

             case LF:      {
                            if (remove)
                              break  ;

                            if (exchange)
                              {
                               putc(add,f2) ;
                               break  ;
                              }
                            putc(change,f2)  ;

                            if (add == CR)
                              putc(add,f2)  ;

                            if (add == NULL)
                              {
                               ch1=(char)NULL  ;
                               for (i=0; i<length; i++)
                               putc(ch1,f2)  ;
                              }
                            break  ;
                           }

          }
        }
    else putc(ch1,f2)  ;
   }

 fclose(f1)  ;
 fclose(f2)  ;


}  /* end of Fix */



syntax()

        /*  Incorrect syntax -- INFORM user!  */

    {
    printf("\nCorrect syntax is:\n\n")  ;
    printf("\tFIX <filename> -Tn   exchange SPACEs for TABs\n")  ;
    printf("\t                       n = 1 to 8 (character boundary)\n")  ;
    printf("\t               -Nn   add n NULLS for each LF\n")  ;
    printf("\t                       n = 1 to 9\n")  ;
    printf("\t               -L    adds LF after each CR\n")  ;
    printf("\t               -C    adds CR after each LF\n")  ;
    printf("\t               -EC   exchanges CR for LF\n")  ;
    printf("\t               -EL   exchanges LF for CR\n")  ;
    printf("\t               -RC   removes all CR's\n")  ;
    printf("\t               -RL   removes all LF's\n")  ;
    printf("\t               -RN   removes all NULL's\n")  ;
    printf("\n\t<filename>.CNG = file with changes.\n\n")  ;
    exit(0)  ;
    }  /* end of syntax */

