xref: /illumos-gate/usr/src/cmd/sh/print.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*39e7390aSna  * Common Development and Distribution License (the "License").
6*39e7390aSna  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*39e7390aSna  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*39e7390aSna  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*39e7390aSna /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*39e7390aSna /*	  All Rights Reserved  	*/
29*39e7390aSna 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * UNIX shell
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include	"defs.h"
367c478bd9Sstevel@tonic-gate #include	<sys/param.h>
377c478bd9Sstevel@tonic-gate #include	<locale.h>
387c478bd9Sstevel@tonic-gate #include	<wctype.h>	/* iswprint() */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define		BUFLEN		256
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate unsigned char numbuf[21];
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static unsigned char buffer[BUFLEN];
457c478bd9Sstevel@tonic-gate static unsigned char *bufp = buffer;
467c478bd9Sstevel@tonic-gate static int index = 0;
477c478bd9Sstevel@tonic-gate static int buffd = 1;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate void	prc_buff(unsigned char c);
507c478bd9Sstevel@tonic-gate void	prs_buff(unsigned char *s);
517c478bd9Sstevel@tonic-gate void	prn_buff(int n);
527c478bd9Sstevel@tonic-gate void	prs_cntl(unsigned char *s);
537c478bd9Sstevel@tonic-gate void	prs(unsigned char *as);
547c478bd9Sstevel@tonic-gate void	itos(int n);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * printing and io conversion
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate void
prp()607c478bd9Sstevel@tonic-gate prp()
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	if ((flags & prompt) == 0 && cmdadr) {
637c478bd9Sstevel@tonic-gate 		prs_cntl(cmdadr);
647c478bd9Sstevel@tonic-gate 		prs((unsigned char *)colon);
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void
prs(unsigned char * as)697c478bd9Sstevel@tonic-gate prs(unsigned char *as)
707c478bd9Sstevel@tonic-gate {
71*39e7390aSna 	if (as) {
72*39e7390aSna 		write(output, as, length(as) - 1);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate void
prc(unsigned char c)777c478bd9Sstevel@tonic-gate prc(unsigned char c)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	if (c) {
807c478bd9Sstevel@tonic-gate 		write(output, &c, 1);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate void
prwc(wchar_t c)857c478bd9Sstevel@tonic-gate prwc(wchar_t c)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	char	mb[MB_LEN_MAX + 1];
887c478bd9Sstevel@tonic-gate 	int	len;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (c == 0) {
917c478bd9Sstevel@tonic-gate 		return;
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 	if ((len = wctomb(mb, c)) < 0) {
947c478bd9Sstevel@tonic-gate 		mb[0] = (unsigned char)c;
957c478bd9Sstevel@tonic-gate 		len = 1;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	write(output, mb, len);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate void
prt(long t)1017c478bd9Sstevel@tonic-gate prt(long t)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	int hr, min, sec;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	t += HZ / 2;
1067c478bd9Sstevel@tonic-gate 	t /= HZ;
1077c478bd9Sstevel@tonic-gate 	sec = t % 60;
1087c478bd9Sstevel@tonic-gate 	t /= 60;
1097c478bd9Sstevel@tonic-gate 	min = t % 60;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if ((hr = t / 60) != 0) {
1127c478bd9Sstevel@tonic-gate 		prn_buff(hr);
1137c478bd9Sstevel@tonic-gate 		prc_buff('h');
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	prn_buff(min);
1177c478bd9Sstevel@tonic-gate 	prc_buff('m');
1187c478bd9Sstevel@tonic-gate 	prn_buff(sec);
1197c478bd9Sstevel@tonic-gate 	prc_buff('s');
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate void
prn(int n)1237c478bd9Sstevel@tonic-gate prn(int n)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	itos(n);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	prs(numbuf);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate void
itos(int n)1317c478bd9Sstevel@tonic-gate itos(int n)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	unsigned char buf[21];
1347c478bd9Sstevel@tonic-gate 	unsigned char *abuf = &buf[20];
1357c478bd9Sstevel@tonic-gate 	int d;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	*--abuf = (unsigned char)'\0';
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	do {
1407c478bd9Sstevel@tonic-gate 		 *--abuf = (unsigned char)('0' + n - 10 * (d = n / 10));
1417c478bd9Sstevel@tonic-gate 	} while ((n = d) != 0);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	strncpy(numbuf, abuf, sizeof (numbuf));
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate int
stoi(unsigned char * icp)1477c478bd9Sstevel@tonic-gate stoi(unsigned char *icp)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	unsigned char	*cp = icp;
1507c478bd9Sstevel@tonic-gate 	int	r = 0;
1517c478bd9Sstevel@tonic-gate 	unsigned char	c;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	while ((c = *cp, digit(c)) && c && r >= 0) {
1547c478bd9Sstevel@tonic-gate 		r = r * 10 + c - '0';
1557c478bd9Sstevel@tonic-gate 		cp++;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	if (r < 0 || cp == icp) {
1587c478bd9Sstevel@tonic-gate 		failed(icp, badnum);
159*39e7390aSna 		/* NOTREACHED */
1607c478bd9Sstevel@tonic-gate 	} else {
1617c478bd9Sstevel@tonic-gate 		return (r);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate int
ltos(long n)1667c478bd9Sstevel@tonic-gate ltos(long n)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	int i;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	numbuf[20] = '\0';
1717c478bd9Sstevel@tonic-gate 	for (i = 19; i >= 0; i--) {
1727c478bd9Sstevel@tonic-gate 		numbuf[i] = n % 10 + '0';
1737c478bd9Sstevel@tonic-gate 		if ((n /= 10) == 0) {
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	return (i);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
180*39e7390aSna static int
ulltos(u_longlong_t n)1817c478bd9Sstevel@tonic-gate ulltos(u_longlong_t n)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int i;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/* The max unsigned long long is 20 characters (+1 for '\0') */
1867c478bd9Sstevel@tonic-gate 	numbuf[20] = '\0';
1877c478bd9Sstevel@tonic-gate 	for (i = 19; i >= 0; i--) {
1887c478bd9Sstevel@tonic-gate 		numbuf[i] = n % 10 + '0';
1897c478bd9Sstevel@tonic-gate 		if ((n /= 10) == 0) {
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	return (i);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate void
flushb()1977c478bd9Sstevel@tonic-gate flushb()
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	if (index) {
2007c478bd9Sstevel@tonic-gate 		bufp[index] = '\0';
2017c478bd9Sstevel@tonic-gate 		write(buffd, bufp, length(bufp) - 1);
2027c478bd9Sstevel@tonic-gate 		index = 0;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate void
prc_buff(unsigned char c)2077c478bd9Sstevel@tonic-gate prc_buff(unsigned char c)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	if (c) {
2107c478bd9Sstevel@tonic-gate 		if (buffd != -1 && index + 1 >= BUFLEN) {
2117c478bd9Sstevel@tonic-gate 			flushb();
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		bufp[index++] = c;
2157c478bd9Sstevel@tonic-gate 	} else {
2167c478bd9Sstevel@tonic-gate 		flushb();
2177c478bd9Sstevel@tonic-gate 		write(buffd, &c, 1);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate void
prs_buff(unsigned char * s)2227c478bd9Sstevel@tonic-gate prs_buff(unsigned char *s)
2237c478bd9Sstevel@tonic-gate {
224*39e7390aSna 	int len = length(s) - 1;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (buffd != -1 && index + len >= BUFLEN) {
2277c478bd9Sstevel@tonic-gate 		flushb();
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (buffd != -1 && len >= BUFLEN) {
231*39e7390aSna 		write(buffd, s, len);
2327c478bd9Sstevel@tonic-gate 	} else {
233*39e7390aSna 		movstr(s, &bufp[index]);
2347c478bd9Sstevel@tonic-gate 		index += len;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
238*39e7390aSna static unsigned char *
octal(unsigned char c,unsigned char * ptr)2397c478bd9Sstevel@tonic-gate octal(unsigned char c, unsigned char *ptr)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	*ptr++ = '\\';
2427c478bd9Sstevel@tonic-gate 	*ptr++ = ((unsigned int)c >> 6) + '0';
2437c478bd9Sstevel@tonic-gate 	*ptr++ = (((unsigned int)c >> 3) & 07) + '0';
2447c478bd9Sstevel@tonic-gate 	*ptr++ = (c & 07) + '0';
2457c478bd9Sstevel@tonic-gate 	return (ptr);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate void
prs_cntl(unsigned char * s)2497c478bd9Sstevel@tonic-gate prs_cntl(unsigned char *s)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	int n;
2527c478bd9Sstevel@tonic-gate 	wchar_t wc;
2537c478bd9Sstevel@tonic-gate 	unsigned char *olds = s;
2547c478bd9Sstevel@tonic-gate 	unsigned char *ptr = bufp;
2557c478bd9Sstevel@tonic-gate 	wchar_t c;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if ((n = mbtowc(&wc, (const char *)s, MB_LEN_MAX)) <= 0) {
2587c478bd9Sstevel@tonic-gate 		n = 0;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	while (n != 0) {
2617c478bd9Sstevel@tonic-gate 		if (n < 0) {
2627c478bd9Sstevel@tonic-gate 			ptr = octal(*s++, ptr);
2637c478bd9Sstevel@tonic-gate 		} else {
2647c478bd9Sstevel@tonic-gate 			c = wc;
2657c478bd9Sstevel@tonic-gate 			s += n;
2667c478bd9Sstevel@tonic-gate 			if (!iswprint(c)) {
2677c478bd9Sstevel@tonic-gate 				if (c < '\040' && c > 0) {
2687c478bd9Sstevel@tonic-gate 					/*
2697c478bd9Sstevel@tonic-gate 					 * assumes ASCII char
2707c478bd9Sstevel@tonic-gate 					 * translate a control character
2717c478bd9Sstevel@tonic-gate 					 * into a printable sequence
2727c478bd9Sstevel@tonic-gate 					 */
2737c478bd9Sstevel@tonic-gate 					*ptr++ = '^';
2747c478bd9Sstevel@tonic-gate 					*ptr++ = (c + 0100);
2757c478bd9Sstevel@tonic-gate 				} else if (c == 0177) {
2767c478bd9Sstevel@tonic-gate 					/* '\0177' does not work */
2777c478bd9Sstevel@tonic-gate 					*ptr++ = '^';
2787c478bd9Sstevel@tonic-gate 					*ptr++ = '?';
2797c478bd9Sstevel@tonic-gate 				} else {
2807c478bd9Sstevel@tonic-gate 					/*
2817c478bd9Sstevel@tonic-gate 					 * unprintable 8-bit byte sequence
2827c478bd9Sstevel@tonic-gate 					 * assumes all legal multibyte
2837c478bd9Sstevel@tonic-gate 					 * sequences are
2847c478bd9Sstevel@tonic-gate 					 * printable
2857c478bd9Sstevel@tonic-gate 					 */
2867c478bd9Sstevel@tonic-gate 					ptr = octal(*olds, ptr);
2877c478bd9Sstevel@tonic-gate 				}
2887c478bd9Sstevel@tonic-gate 			} else {
2897c478bd9Sstevel@tonic-gate 				while (n--) {
2907c478bd9Sstevel@tonic-gate 					*ptr++ = *olds++;
2917c478bd9Sstevel@tonic-gate 				}
2927c478bd9Sstevel@tonic-gate 			}
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		if (buffd != -1 && ptr >= &bufp[BUFLEN-4]) {
2957c478bd9Sstevel@tonic-gate 			*ptr = '\0';
2967c478bd9Sstevel@tonic-gate 			prs(bufp);
2977c478bd9Sstevel@tonic-gate 			ptr = bufp;
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 		olds = s;
3007c478bd9Sstevel@tonic-gate 		if ((n = mbtowc(&wc, (const char *)s, MB_LEN_MAX)) <= 0) {
3017c478bd9Sstevel@tonic-gate 			n = 0;
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 	*ptr = '\0';
3057c478bd9Sstevel@tonic-gate 	prs(bufp);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate void
prull_buff(u_longlong_t lc)3097c478bd9Sstevel@tonic-gate prull_buff(u_longlong_t lc)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	prs_buff(&numbuf[ulltos(lc)]);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate void
prn_buff(int n)3157c478bd9Sstevel@tonic-gate prn_buff(int n)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	itos(n);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	prs_buff(numbuf);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate int
setb(int fd)3237c478bd9Sstevel@tonic-gate setb(int fd)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	int ofd;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if ((ofd = buffd) == -1) {
3287c478bd9Sstevel@tonic-gate 		if (bufp+index+1 >= brkend) {
3297c478bd9Sstevel@tonic-gate 			growstak(bufp+index+1);
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		if (bufp[index-1]) {
3327c478bd9Sstevel@tonic-gate 			bufp[index++] = 0;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		endstak(bufp+index);
3357c478bd9Sstevel@tonic-gate 	} else {
3367c478bd9Sstevel@tonic-gate 		flushb();
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 	if ((buffd = fd) == -1) {
3397c478bd9Sstevel@tonic-gate 		bufp = locstak();
3407c478bd9Sstevel@tonic-gate 	} else {
3417c478bd9Sstevel@tonic-gate 		bufp = buffer;
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	index = 0;
3447c478bd9Sstevel@tonic-gate 	return (ofd);
3457c478bd9Sstevel@tonic-gate }
346