/*
 * ffilt [file ...]
 */

#ifdef	DOCUMENTATION

title	ffilt	filtered file concatination
index		Concatinate files, keeping pathname indicators

synopsis

	ffilt [-options] [filename ...]

description

  Treats filename as a list of paths to document files.
  Produces an output file which concatinates these files
  but also inserts the original pathname every 20 lines
  and inserts breaks of 35 "-" characters between each
  file. This facilitates locating the original path of a
  "readme" file and hence the software being described.

	.lm -8;

	If no file names are given, ffilt reads from standard input.  All output
	is written to standard output.
diagnostics

	.lm +8
	.s.i -8;"file name": cannot open
	.s.i -8;"file name": illegal file name
	.lm -8

author

	Glenn Everhart

bugs

#endif
/*
 * Edit history
 * 0.0	03/04/1992 GCE	Creation
 */

char	*documentation[]  = {
"Ffilt reads files pointed at by each line of its input file,",
"concatinating them with pathname every 20 lines.",
"Usage:  ffilt [filename ...]",
"",
"If no file names are given, wc reads from standard input.  All output is",
"written to standard output.",
"",
0 };

#include <stdio.h>
#ifdef vms
#include		<ssdef.h>
#include		<stsdef.h>
#define	IO_SUCCESS	(SS$_NORMAL | STS$M_INHIB_MSG)
#define	IO_ERROR	SS$_ABORT
#endif
#define NEWLINE  10
/*
 * Note: IO_SUCCESS and IO_ERROR are defined in the Decus C stdio.h file
 */
#ifndef	IO_SUCCESS
#define	IO_SUCCESS	0
#endif
#ifndef	IO_ERROR
#define	IO_ERROR	1
#endif

int	dobytes = 0;		/* Show byte count	*/
int	dowords = 0;		/* Show word count	*/
int	dolines = 0;		/* Show line count	*/
int	ustyle	= 0;		/* Unix-style display	*/
long	twords	= 0;
long	tlines	= 0;
long	tbytes	= 0;
char	file_name[512];
char    filpat[512];

main(argc, argv)
char *argv[];
{
	int	i, nfiles;
	FILE	*fp;
	
	int		nopts,gotcha;

#ifdef vms
	argc = getredirection(argc,argv);
#endif

	if (argc > 1 && argv[1][0] == '?') {
		help();
		exit(IO_ERROR);
	}

	for (nopts = 0, i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {

			case 'b':
			case 'c':
				dobytes++;
				break;
			case 'l':
				dolines++;
				break;
			case 'u':
				ustyle++;
				break;
			case 'w':
				dowords++;
				break;
			default:
				usage();
				exit(IO_ERROR);
			}
			argv[i] = NULL;
			nopts++;
		}
	}

	if (dobytes == 0 && dowords == 0 && dolines == 0)
	{	dobytes++;
		dowords++;
		dolines++;
	}
	nfiles = 0;
	if ((argc-nopts) < 2) {
	    ++nfiles;
	    catf(stdin, NULL);
	}
	else {
#ifdef unix
	    for (i = 1; i < argc; ++i) {
		if (argv[i] == NULL)
			continue;
		if ((fp = fopen(argv[i], "r")) == NULL) {
		    perror(argv[i]);
		}
		else {
		    ++nfiles;
		    catf(fp, argv[i]);
		    fclose(fp);
		}
	    }
#else
	    for (i = 1; i < argc; ++i) {
		if (argv[i] == NULL)
			continue;
		if ((fp = fwild(argv[i], "r")) == NULL) {
		    perror(argv[i]);
		}
		else {
		    for (gotcha = 0; fnext(fp) != NULL; gotcha++) {
			++nfiles;
			fgetname(fp, file_name);
			catf(fp, file_name);
		    }
		    if (gotcha == 0)
			fprintf(stderr, "\"%s\": no matching files\n", argv[i]);
		}
	    }
#endif
	}
/*	if (nfiles > 1)
		output(tlines, twords, tbytes, "total");
*/
	exit(IO_SUCCESS);
}

catf(fp, filename)
FILE		*fp;			/* File pointer			*/
char		*filename;		/* File name string		*/
{
	int c, inword;
	long nfiles;
	long lines;
	long words;
	long bytes;
        long k,kk,kkk;
	int gotcha;
	long lincnt;
	FILE *fp2;

	lines = 0;
	words = 0;
	bytes = 0;
	inword = 0;
/* get a line from the initial file, then process it. */
	while((c = getc(fp)) != EOF) {
	lincnt = 0; /* lincnt is count of lines since start of file */
	kkk = 0;
	kk = 0;
/* get the rest of the pattern for a filename */
	  while (c != NEWLINE ){
	    if( kk > 500) break;
	    filpat[kk++] = c;
            c = getc(fp);
          }
/* skip if we have a null line */
	if (kk == 1) break;
	if (kk > 510) break;
	    filpat[kk] = 0; /* null terminate the name */
/* open the file we're getting to this time */
#ifdef unix
		if ((fp2 = fopen(filpat, "r")) == NULL) {
		    perror(filpat);
		}
		else {
		    ++nfiles;
		    zotf(fp2, filpat);
		    fclose(fp2);
		}
#else
		if ((fp2 = fwild(filpat[0], "r")) == NULL) {
		    perror(filpat);
		}
		else {
		    for (gotcha = 0; fnext(fp2) != NULL; gotcha++) {
			++nfiles;
			fgetname(fp2, filpat);
			zotf(fp2,filpat);
		    }
		    if (gotcha == 0)
			fprintf(stderr, "\"%s\": no matching files\n", filpat);
		}
#endif
/* now fp2 should point at the real file we're dealing with. Copy the file
   to stdout, counting newlines. After each 20th newline, emit filpat
   also. At the end of the file, emit a line of - characters at least 
   32 long. */
	}
	return(IO_SUCCESS);
}
/* Actual work of moving one file to stdout with appropriate counts is done here */
zotf(fp2, filename)
 FILE		*fp2;			/* File pointer			*/
 char		*filename;		/* File name string		*/
{
	int c, inword;
	long lines;
	long words;
	long bytes;
        long k,kk;
	long lincnt;
	FILE *fp3;

	lines = 0;
	words = 0;
	bytes = 0;
	inword = 0;
	lincnt = 0;
/* emit initial header */
	fprintf(stdout, "%s", "\n------------------------------------\n");
	fprintf(stdout, "%s\n",filename);
	while((c = getc(fp2)) != EOF) {
	 fputc(c,stdout);
	  if(c == NEWLINE) {
	   lincnt++;
	   if(lincnt > 20){
		lincnt = 0;
		fprintf(stdout, "%s,%s\n", " ===>>>",filename);
	     }	
	  }
/* end while loop */
	}
#ifndef unix
	fclose(fp2);
#endif
}


usage()
/*
 * Give simple-minded help
 */
{
	fprintf(stderr,
 "Usage:  ffilt [filename ...]\n"
		);
}

help()
/*
 * Give good help
 */
{
	char	**dp;

	for (dp = documentation; *dp; dp++)
		printf("%s\n", *dp);
}




