#include	<stdio.h>
#include	<ctype.h>

#define	MAXLINE	256
#define REG	register

main(argc,argv)
char **argv ;
{
  char	*infile = argv[1] ;
  char	*outfile = argv[2] ;
  char	inline[MAXLINE] ;
  char	outline[MAXLINE] ;
  char	*s ,
	*os ;
  char	ch ;
  FILE	*ifp ,
	*ofp ;
  int	i ;
  long	len ,
	outcount = 0 ,
	incount  = 0;

  if (argc != 3) {
    printf ("Usage index <infile> <outfile>") ;
    exit (1) ;
  }

  if (!(ifp = fopen (infile,"br"))) {
    printf ("unable to open infile %s\n",infile) ;
    exit (2) ;
  }

  if (!(ofp = fopen (outfile,"w"))) {
    printf ("unable to open outfile %s\n") ;
    fclose (ifp) ;
    exit(3) ;
  }

  while (len = fgets_ri(inline,MAXLINE,ifp)) {
    if ((i = findex (inline,"---")) >= 0) {
      *(os = outline) = ((s=inline)[i]) = '\0' ;
      while (ch = *s) {
	if (isupper(ch)) {
	  do {
	    ch = (*os++ = *s++) ;
	  } while (ch && ch != ' ' && ch != '\t') ;
	  s-- ;
	}
	s++ ;
      }
      os-- ;
      while (ch = *os && (ch == ' ' || ch == '\t')) os-- ;
      *os = '\0' ;

      if (*outline)  {
	fprintf (ofp,"%06ld %s\n",incount,outline) ;
	outcount += strlen(outline)+9 ;
      }
    }
    incount += len ;
  }
  fprintf (ofp,"%06ld\n",outcount+8) ;
  fclose(ifp) ;
  fclose(ofp) ;
}


findex (s,t)
REG char *t,*s;
{

  REG int i,j,k;
  REG char	ch;

  for (i=0;s[i] != '\0';i++) {
    for (j=i,k=0;t[k] != '\0' && s[j] == t[k]; j++,k++);
    if (t[k] == '\0') return (i);
  }
  return (-1);
}

/*******
  Im Unterschied zur Library_funktion fgets
  liefert fgets_ri die Laenge der Eingabezeile zurueck
*/
fgets_ri (s,ml,fp)
register char	*s;
register int	ml;
FILE	*fp;
{

  REG	int	ch,
		i=0;

  while (i++<ml) {
    if ((ch=getc(fp)) == EOF) {
      i--;
      break;
    }
    if ((*s++=ch) == '\n' || ch == '\r') break;
  }
  *s = '\0';
  return (i);
}
