/* printcallers.c -- Print callers from a DLG Pro log file.
 *    USAGE: printcallers logfile [exclude_list...]
 *    EXAMPLE: printcallers DLGConfig:Text/Log.Mon "Sysop" "Not Logged In"
 *    COMPILE WITH: dcc [-DDEBUG] -o printcallers printcallers.c
 *    $Id: printcallers.c,v 1.1 92/12/03 03:40:54 vitas Exp Locker: vitas $
 *    Copyleft 1992 Vitas Povilaitis
 *    Source and binary is freely redistributable.
 */

#include <stdio.h>
#include <string.h>

#define BUFLEN 0x52

static char *version = { "$VER: printcallers 1.1 (03.12.1992) Copyleft 1992 Vitas Povilaitis $Revision: 1.1 $ $Date: 92/12/03 03:40:54 $" };

main(int argc, char *argv[])
{
	FILE *fd;
	char buf[BUFLEN];
	char *bufp = buf;
	int index;
#ifndef DEBUG
	char prevbuf[BUFLEN];
	char *prevbufp = prevbuf;
	int argindex;
	int exclude;

	*prevbuf = '\0';
#endif DEBUG

	if (argc < 2) {
		fprintf(stderr, "usage: %s logfile [exclude_list...]\n", argv[0]);
		return (10);
	}

	if ((fd = fopen(argv[1], "r")) == NULL) {
		fprintf(stderr, "%s: can't open %s\n", argv[0], argv[1]);
		return (10);
	}

	while (fread(bufp, 1, BUFLEN, fd) != 0){
#ifdef DEBUG
		for (index = 0; index < BUFLEN; index++)
			if (buf[index] >= ' ' && buf[index] <= '~')
				putchar(buf[index]);
			else
				putchar('.');
		putchar('\n');
#else DEBUG
		argindex = 2;
		exclude = 0;
		while (argindex < argc) {
			if (!strcmp(&buf[6], argv[argindex++]))
				exclude = 1;
		}
		if (!exclude && !strpbrk(&buf[6], "@!") && buf[6] != '\0' && strcmp(&buf[6], prevbufp)) {
			puts(&buf[6]);
			strcpy(prevbufp, &buf[6]);
		}
#endif DEBUG
	}

	fclose(fd);
	return (0);
}
