/*
 * tstlpt.c  10 Nov 83  Craig Milo Rogers at USC/ISI
 *
 *	This program tests lpt_pkg.c, the line printer port I/O package.
 */

#define M_TSTLPT		/* For debugging. */

#include "stdio.h"

#include "truth.h"
#include "beauty.h"

#define BUFLEN 2048		/* LPT1: output buffer length. */
static char lptbuf[BUFLEN];	/* LPT1: output buffer. */

static char *pattern = "The quick brown fox jumps over the lazy dog.\r\n";

#define UNIT 1			/* Deal only with LPT1: */


main(argc, argv)
int argc;			/* Number of command arguments. */
char *argv[];			/* Pointers to argument strings. */
{
    char *cp;			/* Output character pointer. */
    int cnt;			/* Output free count. */
    int stat;			/* Printer status. */
    int i;			/* Counters. */

    printf("Initializing the interrupt package.\n");
    int_ini();

    printf("Partial initialization.\n");
    lpt_ini(UNIT, lptbuf, BUFLEN, FALSE);

    printf("Terminating LPT service.\n");
    lpt_trm(UNIT);

    printf("Complete initialization.\n");
    lpt_ini(UNIT, lptbuf, BUFLEN, TRUE);

    printf("Checking output free count.\n");
    if ((cnt = lpt_ocnt(UNIT)) != BUFLEN) {
	printf("Output free count mismatch: lpt_ocnt() = %d, BUFLEN = %d.\n",
	    cnt, BUFLEN);
    }

    printf("Getting printer status.\n");
    stat = lpt_stat(UNIT);
    printf("Printer status = %02x.\n", stat);

    printf("Printing test message on printer.\n");
    for (i = 0; i < 100; i++) {		/* Print 100 lines: */
	printf(".");			/* One feedback dot per line. */
	for (cp = pattern; *cp; cp++) {
	    while (!lpt_putc(UNIT, *cp)) {
		kbhit();
	    }
	}
    }
    printf("\n");			/* Terminate the feedback line. */

    printf("Waiting for buffer to flush.\n");
    while(lpt_ocnt(UNIT) != BUFLEN) {
	kbhit();
    }

    printf("Advance paper and sound alarm.\n");
    lpt_putc(UNIT, 014);
    lpt_putc(UNIT, 007);

    printf("Waiting for buffer to flush.\n");
    while(lpt_ocnt(UNIT) != BUFLEN) {
	kbhit();
    }

    printf("Terminating LPT service.\n");
    lpt_trm(UNIT);

    printf("Terminating interrupt service.\n");
    int_trm();

    printf("Done.\n");
}
