#include <stdio.h>
#define NAME "ntype"
#define VERSION "2.0"

#define PAUSEWARN 30L

main(int argc,char *argv[])
{
	FILE *fi,*fo;
	int i;

	if (argc<2){
	printf("By Andrew Gaunt, AKA Quantum, #1@6300 - May be freely shared.\n\n");
	printf("usage: ntype file1 ... filen\n\n");
	printf("'P' may be used to pause screen, <space> aborts.\n");
	printf("\n[%s v%s]\n",NAME,VERSION);
	}

	for(i=1;i<argc;i++){
		type(argv[i],0);
	}
}


int type(f,i)
char *f;
int i;
{
	int c;
	FILE *fp;

	if ((fp = fopen(f, "r")) == NULL) {
		colour(6);
		printf("File %s not found.\n",f);
		colour(0);
		return(1);
	}
	else {
		/* type out until eof or key is hit */
		while ( (c=fgetc(fp)) != EOF ) {
			fputc(c,stdout);
			if ( (i==0) && mykbhit()){
				break;
			} 
		}
		purgkey();
		fclose(fp);
	}
	return(0);
}


mykbhit()
{
	long i,j;
	int c;
	static short pause,deadman;

	if(kbhit()){
		c=getch();
		switch (tolower(c)) {
			case 'p':
				if (pause==0){
					pause=1;
					deadman=0;
					time(&i);
					while (!kbhit()){
						time(&j);
						/* Warn user if paused more
than 10 seconds */
						if (j>i+PAUSEWARN){
							printf("\007[PAUSED]\b\b\b\b\b\b\b\b");
							deadman++;
							time(&i);
						}
						if(deadman>6){
							colour(6);
							printf("< Keyboard Timeout >\n");
							colour(0);
							exit(1);
						}
					}
				}
				else{
					pause=0;
				}
				return(0);

			case '\n':
			case ' ':
				if (pause==0)
					return(1);
			default:
				return(1);
		}
	}
}

/* if new is [0-7] then change colour */
colour(code)
short code;
{
	if (code<=7 && code>=0 ){
		printf("%c%d",'\003',code);

	}
}

purgkey()
{
	if (kbhit()){
		putchar('\n');
	}
	while(kbhit()) {
		getch();
	}
}

