/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: print.c 1.5 89/11/17 $@(#)sh:print.c	1.12"
/*
 * UNIX shell
 *
 */

#include	"defs.h"
#include	<sys/param.h>

#define		BUFLEN		256

static unsigned char	buffer[BUFLEN];
static int	index = 0;
unsigned char		numbuf[12];

extern void	prc_buff();
extern void	prs_buff();
extern void	prn_buff();
extern void	prs_cntl();
extern void	prn_buff();

/*
 * printing and io conversion
 */
prp()
{
	if ((flags & prompt) == 0 && cmdadr)
	{
		prs_cntl(cmdadr);
		prs(colon);
	}
}

prs(as)
unsigned char	*as;
{
	register unsigned char	*s;

	if (s = as)
		write(output, s, length(s) - 1);
}

prc(c)
unsigned char	c;
{
	if (c)
		write(output, &c, 1);
}

prt(t)
long	t;
{
	register int hr, min, sec;

	t += HZ / 2;
	t /= HZ;
	sec = t % 60;
	t /= 60;
	min = t % 60;

	if (hr = t / 60)
	{
		prn_buff(hr);
		prc_buff('h');
	}

	prn_buff(min);
	prc_buff('m');
	prn_buff(sec);
	prc_buff('s');
}

prn(n)
	int	n;
{
	itos(n);

	prs(numbuf);
}

itos(n)
{
	register unsigned char *abuf;
	register unsigned a, i;
	int pr, d;

	abuf = numbuf;

	pr = FALSE;
	a = n;
	for (i = 10000; i != 1; i /= 10)
	{
		if ((pr |= (d = a / i)))
			*abuf++ = d + '0';
		a %= i;
	}
	*abuf++ = a + '0';
	*abuf++ = 0;
}

stoi(icp)
unsigned char	*icp;
{
	register unsigned char	*cp = icp;
	register int	r = 0;
	register unsigned char	c;

	while ((c = *cp, digit(c)) && c && r >= 0)
	{
		r = r * 10 + c - '0';
		cp++;
	}
	if (r < 0 || cp == icp)
		failed(icp, gettxt("sh:13", badnum));
	else
		return(r);
}

prl(n)
long n;
{
	int i;

	i = 11;
	while (n > 0 && --i >= 0)
	{
		numbuf[i] = n % 10 + '0';
		n /= 10;
	}
	numbuf[11] = '\0';
	prs_buff(&numbuf[i]);
}

void
flushb()
{
	if (index)
	{
		buffer[index] = '\0';
		write(1, buffer, length(buffer) - 1);
		index = 0;
	}
}

void
prc_buff(c)
	unsigned char c;
{
	if (c)
	{
		if (index + 1 >= BUFLEN)
			flushb();

		buffer[index++] = c;
	}
	else
	{
		flushb();
		write(1, &c, 1);
	}
}

void
prs_buff(s)
	unsigned char *s;
{
	register int len = length(s) - 1;

	if (index + len >= BUFLEN)
		flushb();

	if (len >= BUFLEN)
		write(1, s, len);
	else
	{
		movstr(s, &buffer[index]);
		index += len;
	}
}


clear_buff()
{
	index = 0;
}

unsigned char *octal(c, ptr)
char c;
char *ptr;
{
	*ptr++ = '\\';
	*ptr++ = ((unsigned int)(c & 0377) >> 6) + '0';
	*ptr++ = (((unsigned int)c >> 3) & 07) + '0';
	*ptr++ = (c & 07) + '0';
	return((unsigned char *)ptr);
}

void
prs_cntl(s)
	unsigned char *s;
{
	register unsigned char *ptr = buffer;
	register unsigned char c;

	register int n;
	wchar_t l;
	unsigned char *olds = s;
	n = mbtowc(&l, s, MULTI_BYTE_MAX);

	while(n != 0)
	{
		
		/* translate a control character into a printable sequence */

		if(n < 0)
			ptr = octal(*s++, ptr);
		else { 
			c = l;
			s += n;
			if(!wisprint(c))
			{
				if(c < '\040' && c > 0)
				{
				/*
			 	 * assumes ASCII char
			 	 * translate a control character 
			 	 * into a printable sequence 
			 	 */
					*ptr++ = '^';
					*ptr++ = (c + 0100);	
				}
				else if (c == 0177) 
		{	/* '\0177' does not work */
					*ptr++ = '^';
					*ptr++ = '?';
				}
				else    /* unprintable 8-bit byte sequence 
				 	 * assumes all legal multibyte
				 	 * sequences are 
				 	 * printable 
				 	 */
					ptr = octal(*olds, ptr);
			}
			else
				while(n--)
					*ptr++ = *olds++;
		}
		if(ptr >= &buffer[BUFLEN-4]) {
			*ptr='\0';
			prs(buffer);
			ptr = buffer;
		}
		olds = s;
		n = mbtowc(&l, s, MULTI_BYTE_MAX);
	}

	*ptr = '\0';
	prs(buffer);
}


void
prn_buff(n)
	int	n;
{
	itos(n);

	prs_buff(numbuf);
}
