xref: /onnv-gate/usr/src/cmd/sh/print.c (revision 2256:a9ca1c675600)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2256Sna195498  * Common Development and Distribution License (the "License").
6*2256Sna195498  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*2256Sna195498 
22*2256Sna195498 /*
23*2256Sna195498  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*2256Sna195498  * Use is subject to license terms.
25*2256Sna195498  */
26*2256Sna195498 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*2256Sna195498 #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * UNIX shell
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include	"defs.h"
370Sstevel@tonic-gate #include	<sys/param.h>
380Sstevel@tonic-gate #include	<locale.h>
390Sstevel@tonic-gate #include	<wctype.h>	/* iswprint() */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define		BUFLEN		256
420Sstevel@tonic-gate 
430Sstevel@tonic-gate unsigned char numbuf[21];
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static unsigned char buffer[BUFLEN];
460Sstevel@tonic-gate static unsigned char *bufp = buffer;
470Sstevel@tonic-gate static int index = 0;
480Sstevel@tonic-gate static int buffd = 1;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate void	prc_buff(unsigned char c);
510Sstevel@tonic-gate void	prs_buff(unsigned char *s);
520Sstevel@tonic-gate void	prn_buff(int n);
530Sstevel@tonic-gate void	prs_cntl(unsigned char *s);
540Sstevel@tonic-gate void	prs(unsigned char *as);
550Sstevel@tonic-gate void	itos(int n);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * printing and io conversion
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate void
prp()610Sstevel@tonic-gate prp()
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	if ((flags & prompt) == 0 && cmdadr) {
640Sstevel@tonic-gate 		prs_cntl(cmdadr);
650Sstevel@tonic-gate 		prs((unsigned char *)colon);
660Sstevel@tonic-gate 	}
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate void
prs(unsigned char * as)700Sstevel@tonic-gate prs(unsigned char *as)
710Sstevel@tonic-gate {
72*2256Sna195498 	if (as) {
73*2256Sna195498 		write(output, as, length(as) - 1);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate void
prc(unsigned char c)780Sstevel@tonic-gate prc(unsigned char c)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	if (c) {
810Sstevel@tonic-gate 		write(output, &c, 1);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate void
prwc(wchar_t c)860Sstevel@tonic-gate prwc(wchar_t c)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	char	mb[MB_LEN_MAX + 1];
890Sstevel@tonic-gate 	int	len;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (c == 0) {
920Sstevel@tonic-gate 		return;
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 	if ((len = wctomb(mb, c)) < 0) {
950Sstevel@tonic-gate 		mb[0] = (unsigned char)c;
960Sstevel@tonic-gate 		len = 1;
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 	write(output, mb, len);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate void
prt(long t)1020Sstevel@tonic-gate prt(long t)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	int hr, min, sec;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	t += HZ / 2;
1070Sstevel@tonic-gate 	t /= HZ;
1080Sstevel@tonic-gate 	sec = t % 60;
1090Sstevel@tonic-gate 	t /= 60;
1100Sstevel@tonic-gate 	min = t % 60;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if ((hr = t / 60) != 0) {
1130Sstevel@tonic-gate 		prn_buff(hr);
1140Sstevel@tonic-gate 		prc_buff('h');
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	prn_buff(min);
1180Sstevel@tonic-gate 	prc_buff('m');
1190Sstevel@tonic-gate 	prn_buff(sec);
1200Sstevel@tonic-gate 	prc_buff('s');
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate void
prn(int n)1240Sstevel@tonic-gate prn(int n)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	itos(n);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	prs(numbuf);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate void
itos(int n)1320Sstevel@tonic-gate itos(int n)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	unsigned char buf[21];
1350Sstevel@tonic-gate 	unsigned char *abuf = &buf[20];
1360Sstevel@tonic-gate 	int d;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	*--abuf = (unsigned char)'\0';
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	do {
1410Sstevel@tonic-gate 		 *--abuf = (unsigned char)('0' + n - 10 * (d = n / 10));
1420Sstevel@tonic-gate 	} while ((n = d) != 0);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	strncpy(numbuf, abuf, sizeof (numbuf));
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate int
stoi(unsigned char * icp)1480Sstevel@tonic-gate stoi(unsigned char *icp)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	unsigned char	*cp = icp;
1510Sstevel@tonic-gate 	int	r = 0;
1520Sstevel@tonic-gate 	unsigned char	c;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	while ((c = *cp, digit(c)) && c && r >= 0) {
1550Sstevel@tonic-gate 		r = r * 10 + c - '0';
1560Sstevel@tonic-gate 		cp++;
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	if (r < 0 || cp == icp) {
1590Sstevel@tonic-gate 		failed(icp, badnum);
160*2256Sna195498 		/* NOTREACHED */
1610Sstevel@tonic-gate 	} else {
1620Sstevel@tonic-gate 		return (r);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate int
ltos(long n)1670Sstevel@tonic-gate ltos(long n)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	int i;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	numbuf[20] = '\0';
1720Sstevel@tonic-gate 	for (i = 19; i >= 0; i--) {
1730Sstevel@tonic-gate 		numbuf[i] = n % 10 + '0';
1740Sstevel@tonic-gate 		if ((n /= 10) == 0) {
1750Sstevel@tonic-gate 			break;
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 	return (i);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
181*2256Sna195498 static int
ulltos(u_longlong_t n)1820Sstevel@tonic-gate ulltos(u_longlong_t n)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	int i;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/* The max unsigned long long is 20 characters (+1 for '\0') */
1870Sstevel@tonic-gate 	numbuf[20] = '\0';
1880Sstevel@tonic-gate 	for (i = 19; i >= 0; i--) {
1890Sstevel@tonic-gate 		numbuf[i] = n % 10 + '0';
1900Sstevel@tonic-gate 		if ((n /= 10) == 0) {
1910Sstevel@tonic-gate 			break;
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 	return (i);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate void
flushb()1980Sstevel@tonic-gate flushb()
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	if (index) {
2010Sstevel@tonic-gate 		bufp[index] = '\0';
2020Sstevel@tonic-gate 		write(buffd, bufp, length(bufp) - 1);
2030Sstevel@tonic-gate 		index = 0;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate void
prc_buff(unsigned char c)2080Sstevel@tonic-gate prc_buff(unsigned char c)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	if (c) {
2110Sstevel@tonic-gate 		if (buffd != -1 && index + 1 >= BUFLEN) {
2120Sstevel@tonic-gate 			flushb();
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 		bufp[index++] = c;
2160Sstevel@tonic-gate 	} else {
2170Sstevel@tonic-gate 		flushb();
2180Sstevel@tonic-gate 		write(buffd, &c, 1);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate void
prs_buff(unsigned char * s)2230Sstevel@tonic-gate prs_buff(unsigned char *s)
2240Sstevel@tonic-gate {
225*2256Sna195498 	int len = length(s) - 1;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (buffd != -1 && index + len >= BUFLEN) {
2280Sstevel@tonic-gate 		flushb();
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (buffd != -1 && len >= BUFLEN) {
232*2256Sna195498 		write(buffd, s, len);
2330Sstevel@tonic-gate 	} else {
234*2256Sna195498 		movstr(s, &bufp[index]);
2350Sstevel@tonic-gate 		index += len;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
239*2256Sna195498 static unsigned char *
octal(unsigned char c,unsigned char * ptr)2400Sstevel@tonic-gate octal(unsigned char c, unsigned char *ptr)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	*ptr++ = '\\';
2430Sstevel@tonic-gate 	*ptr++ = ((unsigned int)c >> 6) + '0';
2440Sstevel@tonic-gate 	*ptr++ = (((unsigned int)c >> 3) & 07) + '0';
2450Sstevel@tonic-gate 	*ptr++ = (c & 07) + '0';
2460Sstevel@tonic-gate 	return (ptr);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate void
prs_cntl(unsigned char * s)2500Sstevel@tonic-gate prs_cntl(unsigned char *s)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	int n;
2530Sstevel@tonic-gate 	wchar_t wc;
2540Sstevel@tonic-gate 	unsigned char *olds = s;
2550Sstevel@tonic-gate 	unsigned char *ptr = bufp;
2560Sstevel@tonic-gate 	wchar_t c;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if ((n = mbtowc(&wc, (const char *)s, MB_LEN_MAX)) <= 0) {
2590Sstevel@tonic-gate 		n = 0;
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 	while (n != 0) {
2620Sstevel@tonic-gate 		if (n < 0) {
2630Sstevel@tonic-gate 			ptr = octal(*s++, ptr);
2640Sstevel@tonic-gate 		} else {
2650Sstevel@tonic-gate 			c = wc;
2660Sstevel@tonic-gate 			s += n;
2670Sstevel@tonic-gate 			if (!iswprint(c)) {
2680Sstevel@tonic-gate 				if (c < '\040' && c > 0) {
2690Sstevel@tonic-gate 					/*
2700Sstevel@tonic-gate 					 * assumes ASCII char
2710Sstevel@tonic-gate 					 * translate a control character
2720Sstevel@tonic-gate 					 * into a printable sequence
2730Sstevel@tonic-gate 					 */
2740Sstevel@tonic-gate 					*ptr++ = '^';
2750Sstevel@tonic-gate 					*ptr++ = (c + 0100);
2760Sstevel@tonic-gate 				} else if (c == 0177) {
2770Sstevel@tonic-gate 					/* '\0177' does not work */
2780Sstevel@tonic-gate 					*ptr++ = '^';
2790Sstevel@tonic-gate 					*ptr++ = '?';
2800Sstevel@tonic-gate 				} else {
2810Sstevel@tonic-gate 					/*
2820Sstevel@tonic-gate 					 * unprintable 8-bit byte sequence
2830Sstevel@tonic-gate 					 * assumes all legal multibyte
2840Sstevel@tonic-gate 					 * sequences are
2850Sstevel@tonic-gate 					 * printable
2860Sstevel@tonic-gate 					 */
2870Sstevel@tonic-gate 					ptr = octal(*olds, ptr);
2880Sstevel@tonic-gate 				}
2890Sstevel@tonic-gate 			} else {
2900Sstevel@tonic-gate 				while (n--) {
2910Sstevel@tonic-gate 					*ptr++ = *olds++;
2920Sstevel@tonic-gate 				}
2930Sstevel@tonic-gate 			}
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 		if (buffd != -1 && ptr >= &bufp[BUFLEN-4]) {
2960Sstevel@tonic-gate 			*ptr = '\0';
2970Sstevel@tonic-gate 			prs(bufp);
2980Sstevel@tonic-gate 			ptr = bufp;
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 		olds = s;
3010Sstevel@tonic-gate 		if ((n = mbtowc(&wc, (const char *)s, MB_LEN_MAX)) <= 0) {
3020Sstevel@tonic-gate 			n = 0;
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	*ptr = '\0';
3060Sstevel@tonic-gate 	prs(bufp);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate void
prull_buff(u_longlong_t lc)3100Sstevel@tonic-gate prull_buff(u_longlong_t lc)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	prs_buff(&numbuf[ulltos(lc)]);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate void
prn_buff(int n)3160Sstevel@tonic-gate prn_buff(int n)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	itos(n);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	prs_buff(numbuf);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate int
setb(int fd)3240Sstevel@tonic-gate setb(int fd)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	int ofd;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if ((ofd = buffd) == -1) {
3290Sstevel@tonic-gate 		if (bufp+index+1 >= brkend) {
3300Sstevel@tonic-gate 			growstak(bufp+index+1);
3310Sstevel@tonic-gate 		}
3320Sstevel@tonic-gate 		if (bufp[index-1]) {
3330Sstevel@tonic-gate 			bufp[index++] = 0;
3340Sstevel@tonic-gate 		}
3350Sstevel@tonic-gate 		endstak(bufp+index);
3360Sstevel@tonic-gate 	} else {
3370Sstevel@tonic-gate 		flushb();
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	if ((buffd = fd) == -1) {
3400Sstevel@tonic-gate 		bufp = locstak();
3410Sstevel@tonic-gate 	} else {
3420Sstevel@tonic-gate 		bufp = buffer;
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 	index = 0;
3450Sstevel@tonic-gate 	return (ofd);
3460Sstevel@tonic-gate }
347