/* Copyright (C) 1991 David Strout

This file is part of RevScanner.

RevScanner is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

RevScanner is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RCS; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

Report problems and direct all questions to:

    dstrout@darpa.mil

*/
/*
$Id: RevScanner.m,v 1.11 1991/12/20 01:45:40 dstrout Beta $
$Log: RevScanner.m,v $
# Revision 1.11  1991/12/20  01:45:40  dstrout
# Added GNU License
#
# Revision 1.10  1991/12/19  20:58:23  dstrout
# Added test to see if thePanel is hooked up.
#
# Revision 1.9  1991/12/19  20:19:48  dstrout
# Fixed Multiple-Copys-of-info bug.
#
# Revision 1.8  1991/12/19  19:43:24  dstrout
# Added makeKeyAndOrderFront message.
# Changed rcsmap to a .h file
#
#
*/
#import "RevScanner.h"
#import "rcsbase.h"
#import "rcsmap.h"
#import <stdlib.h>
#import <stdio.h>
#import <appkit/defaults.h>		/*Gives us NXArgv */
#import <appkit/ScrollView.h>	/*For dealing with the Scrolling Text from */
#import <appkit/Text.h>			/*	IB's palette */

static char rcsid[]="$Id: RevScanner.m,v 1.11 1991/12/20 01:45:40 dstrout Beta $";

static int match P((FILE*));
static char info[BUFSIZ];

@implementation RevScanner

- scan:sender
{
	FILE *fp;
	register int c;
	
	if((fp = fopen(NXArgv[0], "r")) != 0){
	c = 0;
	while (c != EOF) {
		if (c == KDELIM) {
			if ((c = match(fp))){
		continue;
		}
	}
	c = getc(fp);
   };
	[[[[theScrollingText docView] selectAll:self] delete:self] setText:info];
	*info= '\0';
	}
	if(thePanel != nil) [thePanel makeKeyAndOrderFront:self];
    return self;
}


@end


	static int
match(fp)   /* group substring between two KDELIM's; then do pattern match */
   register FILE *fp;
{
   char line[BUFSIZ];
   char tmp[BUFSIZ];
   register int c;
   register char * tp;

   tp = line;
   while ((c = getc(fp)) != VDELIM)
      switch (ctab[c]) {
	 case LETTER: case Letter:
	    *tp++ = c;
	    if (tp < line+sizeof(line)-4)
	       break;
	    /* fall into */
	 default:
	    return c ? c : '\n'/* anything but 0 or KDELIM or EOF */;
      }
   *tp++ = c;
   if ((c = getc(fp)) != ' ')
      return c ? c : '\n';
   *tp++ = c;
   while( (c = getc(fp)) != KDELIM ) {
      switch (ctab[c]) {
	 default:
	    *tp++ = c;
	    if (tp < line+sizeof(line)-2)
	       break;
	    /* fall into */
	 case EOFILE: case NEWLN: case UNKN:
	    return c ? c : '\n';
      }
   }
   if (tp[-1] != ' ')
      return tp[-1];
   *tp++ = c;     /*append trailing KDELIM*/
   *tp   = '\0';
   VOID sprintf(tmp, "     %c%s\n", KDELIM, line);
   strcat(info, tmp);
   return 0;
}
