/* TERMLIB: Terminal independant database.
 *
 * Module: tputs
 *
 * Purpose: decode padding information
 *
 * Calling conventions: cp = string to be padded, affcnt = # of items
 *			affected (lines, characters, whatever),
 *			outc = routine to output 1 character.
 *
 * Returned values: none
 *
 * Notes
 *		cp has padding information ahead of it, in the form
 *	nnnTEXT or nnn*TEXT. nnn is the number of milliseconds to delay,
 *	and may be a decimal (nnn.mmm). If the asterisk is given, then
 *	the delay is multiplied by afcnt. The delay is produced by outputting
 *	a number of nulls (or other padding char) after printing the
 *	TEXT.
 *
 */
#include <stdio.h>
#include <ctype.h>
#include "termlib.h"

/* tputs.c (libtermlib.a)
 *
 */

long _bauds[16]={
	0,	50,	75,	110,
	134,	150,	200,	300,
	600,	1200,	1800,	2400,
	4800,	9600,	19200,	19200 };

tputs(cp, affcnt, outc)
char *cp;                                                 /* string to print */
int affcnt;                                      /* Number of lines affected */
int (*outc)();                              /* routine to output 1 character */
{
	long	frac,                    /* 10^(#digits after decimal point) */
		counter,                                           /* digits */
		atol();

	if(isdigit(*cp)) {
		counter = 0;
		frac = 1000;
		while(isdigit(*cp))
			counter = counter * 10L + (long)(*cp++ - '0');
		if(*cp=='.')
			while(isdigit(*++cp)) {
				counter = counter * 10L + (long)(*cp++ - '0');
				frac = frac * 10;
			}
		if(*cp!='*') {                 /* multiply by affected lines */
			if(affcnt>1) affcnt = 1;
		} 
		else
			cp++;

        /* Calculate number of characters for padding counter/frac ms delay */
		if(ospeed)
			counter = (counter * _bauds[ospeed] * (long)affcnt) / frac;

		while(*cp)                                  /* output string */
			(*outc)(*cp++);
		if(ospeed)
			while(counter--)            /* followed by pad characters */
				(*outc)(PC);
	} 
	else
		while(*cp)
			(*outc)(*cp++);
}
