/*--------------------------------------------------------------------------*
              __    __
             / /   /_/                      $RCSfile: ti.c,v $
            / /_  __                        $Release$
           / __/ / /                        $Revision: 1.3 $
          / /__ / /_                        $Date: 92/12/19 14:27:08 $
         /____//___/                        $Author: tf $
                                            $State: Exp $

           (c) Copyright 1992 Tobias Ferber, All Rights Reserved.

 *--------------------------------------------------------------------------*/

#include <exec/types.h>
#include <stdio.h>

static char rcs_id[]= "$Id: ti.c,v 1.3 92/12/19 14:27:08 tf Exp $";

#define BANNER "(c)Copyright 1992 by Tobias Ferber, All Rights Reserved\n" \
               "TI <textfile> [-w] [-t] [-q]"

#define crsr_on  "\233\40\160\r"
#define crsr_off "\233\60\40\160\r"

/* word seperators */

#define isws(c) ((c)==' ' || (c)=='\t' || (c)=='\n')

FILE *fp; /* global (for _abort) */

void _abort(void)
{ if(fp) fclose(fp);
  printf("^C\n**BREAK\n\n%s",crsr_on);
  exit(1);
}

char *howtouse[] = {

 "CHECKWIDTH", "-w", "<width>", "inform about lines with more than <width> characters",
 "TABSIZE",    "-t", "<size> ", "set tabsize to <size> (default: 8)",
 "BRIEF",      "-b", "       ", "stop after the first line of output",
 "FIND",       "-c", "<value>", "find characters with an ASCII value of <value>",

 NULL,NULL,NULL,NULL
};

main(int argc, char *argv[])
{
  BOOL badopt= FALSE,  /* bad option ? */
       verbose= TRUE;  /* display statistics */

  char *whoami= argv[0],     /* who am I */
       *fname= (char *)NULL; /* filename */

  long ts=8,    /* tab size */
       chw=0,   /* width to check */
       asc=-1;  /* char to find */

  onbreak(_abort);

  if(argc>1)
  { --argc;
    ++argv;
    while(argc>0 && !badopt)
    { char *arg=argv[0];
      if(isalpha(*arg)) /* convert options: AmigaDOS -> UNIX */
      { char **aopt= &howtouse[0];
        while(*aopt && strnicmp(arg,*aopt,strlen(*aopt)))
          aopt= &aopt[4];
        if(*aopt) arg= aopt[1];
      }
      if(*arg=='-')
      { arg++;
        switch(*arg)
        { case 'w': case 'W': /* CHECKWIDTH */
            if(arg[1]) chw= atol(&arg[1]);
            else
            { argv++;
              argc--;
              chw= atol(argv[0]);
            }
            break;
          case 't': case 'T': /* TABSIZE */
            if(arg[1]) ts= atol(&arg[1]);
            else
            { argv++;
              argc--;
              ts= atol(argv[0]);
            }
            break;
          case 'c': case 'C': /* FIND */
            if(arg[1]) asc= atol(&arg[1]);
            else
            { argv++;
              argc--;
              asc= atol(argv[0]);
            }
            break;
          case 'b': case 'B': /* BRIEF */
            verbose= FALSE;
            break;
          default:
            printf("%s: Bad option: -%c.\n",whoami,*arg);
            badopt=TRUE;
            break;
        }
      }
      else if(*arg=='?')
      { char **s= &howtouse[0];
        while(*s)
        { printf("%-10s or %-2s %s  %s\n",s[0],s[1],s[2],s[3]);
          s= &s[4];
        }
        exit(0);
      }
      else fname=arg;
      --argc;
      ++argv;
    }
    if(!badopt)
    { if(!fname)
      { printf("%s: Filename expected.\n",whoami);
        exit(5);
      }
      if(!(fp= fopen(fname,"r")))
      { printf("%s: No information for \"%s\": object not found\n",whoami,fname);
        exit(5);
      }
      printf(crsr_off);

      long b=0,  /* byte counter */
           l=0,  /* lines counter */
           w=0,  /* words counter */
           cl=0, /* chars per line counter */
           cm=0; /* max #of chars per line */

      BOOL goon= TRUE; /* don't stop now */

      char c,d='\0';
      for(;(c=fgetc(fp))!=EOF && !feof(fp) && goon;++b)
      {
        if(asc>=0 && c==(char)asc)
        { printf("%s: Line %ld \"%s\" contains character 0x%02x\n",whoami,l,fname,c);
          goon= verbose;
        }
        if(c=='\n'||c=='\f')
        { ++l;
          ++w;
          if(cl>cm) cm=cl;
          if(chw>0 && cl>chw)
          { printf("%s: Line %ld \"%s\" width %d > %ld\n",whoami,l,fname,cl,chw);
            goon= verbose;
          }
          cl=0L;
        }
        else
        { if(isws(c) && !isws(d)) ++w;
          if(c=='\t' && ts>1) cl= cl+ts-(cl%ts);
          else ++cl;
        }
        d=c;
      }
      if(verbose)
      { printf("file \"%s\" size: %ld bytes, %ld lines <= %ld chars, %ld words\n",
          fname,b,l,cm,w);
      }
      printf(crsr_on);
      if(fp) fclose(fp);
    }
  }
  else /* no args */
  { puts(rcs_id);
    puts(BANNER);
  }
  exit(0);
}
