#include <stdio.h>
#include <local.h>

static char *Program[] = { "tp.c - type files  01-16-88",
			   "S. Leoce, V1.0 R1.0 va@psj" };

static char *Usage = "usage: tp [/chnpHLn /Sn /Fn] file [file ... ]";

#define max(x, y)  ((x) > (y) ? (x) : (y))

#include "cmdline.c"

#define	MAXLINE	255
#define NONE	0
#define DEFAULT	24
#define TOP	0

main(argc, argv)
int argc;
char *argv[];
{
	auto char bufr[BUFSIZ];
	auto short count = FALSE, header = FALSE, number = FALSE;
	auto short LineOption = FALSE, pause = FALSE;
	register long Number = NONE;
	auto int Lines = DEFAULT;	/* default lines  */
	auto int space = NONE;  /* default space lines    */
	auto int First = TOP;   /* top is default of file */
	FILE *fp;
	char line [MAXLINE];	/* maximum input line     */

	opterr = FALSE;
	while ((LineOption = getopt(argc, argv, "chnpHF:L:S:")) != EOF)
		switch(LineOption) {
		case '?':
			fprintf(stderr,"tp: illegal option %c\n",badopt);
			fprintf(stderr,"tp: %s\n", Usage);
			_exit(0x04);
		case 'c':
			count = TRUE;
			break;
		case 'h':
			header = TRUE;
			break;
		case 'n':
			number = TRUE;
			break;
		case 'p':
			pause = TRUE;
			break;
		case 'S':
			space = max(atoi(optarg), 0);
			break;
		case 'L':
			if((Lines = max(atoi(optarg), 0)) == 0)
				_exit(fprintf(stderr,"tp: pause count must "
					"be greater zero\n") + 0x10);
			break;
		case 'F':
			if((First = max(atoi(optarg), 0)) == 0)
				_exit(fprintf(stderr,"tp: first line must "
					"be greater zero\n") + 0x12);
			break;
		case 'H':
			_exit(help() + 0x00);
		}

	setbuf(stdout, bufr);	
	if (argv[optind] == NULL) {	/* goto filter mode, 1 time only */
		register int left = Lines, first = First;

		while (--first > 0 && fgets(line, MAXLINE, stdin) != NULL)
			Number++;

		while (fgets(line, MAXLINE, stdin) != NULL) {
			auto int i = EOF;

			Number++;
			if (pause && --left == 0) {
				fflush(stdout);
				fprintf(stderr,"\r--More--");
				if (feof(stdin) || toupper(getch()) == 'Q')
					_exit(0x00);
				fprintf(stderr,"\r        \r");
				left = Lines;
			}
			if (number)
				printf("%ld: ", Number);
			printf("%s", line);
			for (i=1; i <= space; i++)
				putchar('\n');
		}
		if (count)
			printf("\n\n%ld Lines, %d Typed\n",Number,Number-First);
		fflush (stdout);
		_exit (0x00);
	}
	else	/* read the files from the command line */
	{
		while(argv[optind] != NULL) {
			if((fp = fopen(argv[optind++], "r")) == NULL) {
				fprintf(stderr,"tp: can't open %s",
					argv[optind-1]);
				perror(" - Reason");
			}
			else
			{
				register int left = Lines;
				register int first = First;
				
				if(setvbuf(fp, NULL, _IOFBF, 4K) != 0)
					fprintf(stderr,"tp: low core - input"
						" unbuffered\n");
				Number = 0;
				if (header)
					printf("\n---------------- %s\n",
						argv[optind-1]);
				else
					putchar('\n');
				while(--first > 0 && fgets(line,MAXLINE,fp)
					!= NULL)  Number++;

				while(fgets(line,MAXLINE,fp) != NULL) {
					auto short i;

					Number++;
					if (pause && --left == 0) {
						fflush(stdout);
						fprintf(stderr,"\r--More--");
						if (feof(stdin) || toupper(getch()) == 'Q')
							_exit(0x00);
						fprintf(stderr,"\r        \r");
						left = Lines;
					}
					if (number)
						printf("%ld: ",Number);
					printf("%s",line);
					for (i=1; i <= space; i++)
						putchar('\n');
				}
				if (First > Number)
					fprintf(stderr,"tp: first line specified "
						"larger than file length\n");
				fclose(fp);	/* close input file */
				if (count)
					printf("\n\n%ld Lines, %d Typed\n",Number,Number-First);
				fflush(stdout);	/* clean the buffer */
			}
		}
	}
	_exit (0x00);
}

help()
{
	fprintf(stderr, "TP Help\n\nOptions:\n");
	fprintf(stderr,"\t/c  - count total lines        ( default OFF )\n"
		       "\t/h  - header before output     ( default OFF )\n"
		       "\t/n  - number output lines      ( default OFF )\n"
		       "\t/p  - pause before scroll      ( default OFF )\n"
	 	       "\t/H  - show options help        ( default OFF )\n"
		       "\t/Ln - use n lines per page     ( default 24  )\n"
		       "\t/Sn - space n lines in output  ( default 0   )\n"
		       "\t/Fn - first output line is n   ( default 0   )\n");
	fprintf(stderr,"\n\tn an integer, greater than zero\n"
		       "\tq or Q at the --More-- prompt exits\n");

	return (0);
}