/*
 *  pb98.c -- プリンタBIOSを使ってデータをプリンタに送る
 *
 *  revision history:
 *	0.0: Mar. 14, 1993 by Dai Ishijima
 *	0.1: Dec. 27, 1995
 */

#include <stdio.h>
#ifndef NO_STDLIB_H
#include <stdlib.h>
#endif
#include <time.h>
#include <dos.h>
#include "pb98.h"

#define YES 1
#define NO  0

/*
 *	intr. no., bios command codes
 */
#define PBIOS		0x1a	/* printer bios: INT 1AH */
#define PINIT		0x10    /* initialize printer bios */
#define PSEND		0x11    /* send character to printer */
#define PSTAT		0x12    /* get printer status */
#define PSEND_MASK	0x03
#define PSEND_OK	0x01
#define PSEND_TMOUT	0x02
#define PSTAT_MASK	0x01
#define PSTAT_OK	0x01


/* initialize printer bios */
void initpb(void)
{
    union REGS inregs, outregs;

    inregs.h.ah = PINIT;
    int86(PBIOS, &inregs, &outregs);
}


/* check printer status */
int isready(void)
{
    union REGS inregs, outregs;

    inregs.h.ah = PSTAT;
    int86(PBIOS, &inregs, &outregs);
    return(outregs.h.ah & PSEND_OK);
}


/* send a character to printer */
int sendchar(int ch)
{
    union REGS inregs, outregs;

    inregs.h.ah = PSEND;
    inregs.h.al = ch;
    int86(PBIOS, &inregs, &outregs);
    return(outregs.h.ah & PSEND_MASK);
}


/* putc(ch, PRINTER) */
void lputc(int ch)
{
    time_t t;
    static unsigned long count = 0;

    t = time(NULL);
    while (!isready()) {
	if (time(NULL) - t > TIMEOUT) {
	    fprintf(stderr, "lputc() timed out.  ");
	    fprintf(stderr, "sent %ld character(s).\n", count);
	    exit(1);
	}
    }
    sendchar(ch);
    ++count;
}


/* print strings *s */
void lputs(char *s)
{
    while (*s != '\0') {
	lputc(*s++);
    }
}
