xref: /onnv-gate/usr/src/cmd/csh/sh.print.c (revision 356:44e9075e1c86)
10Sstevel@tonic-gate /*
2*356Smuffin  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
70Sstevel@tonic-gate /*	  All Rights Reserved  	*/
80Sstevel@tonic-gate 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
110Sstevel@tonic-gate  * All rights reserved. The Berkeley Software License Agreement
120Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
130Sstevel@tonic-gate  */
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
160Sstevel@tonic-gate 
170Sstevel@tonic-gate #include "sh.h"
180Sstevel@tonic-gate 
19*356Smuffin void	p2dig_ull(unsigned long long);
20*356Smuffin void	p2dig_int(int);
21*356Smuffin void	flush(void);
22*356Smuffin void	Putchar(tchar);
23*356Smuffin 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * C Shell
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate void
psecs_ull(unsigned long long l)300Sstevel@tonic-gate psecs_ull(unsigned long long l)
310Sstevel@tonic-gate {
320Sstevel@tonic-gate 	unsigned long long i;
330Sstevel@tonic-gate 
340Sstevel@tonic-gate 	i = l / 3600;
350Sstevel@tonic-gate 	if (i) {
360Sstevel@tonic-gate 		printf("%llu:", i);
370Sstevel@tonic-gate 		i = l % 3600;
380Sstevel@tonic-gate 		p2dig_ull(i / 60);
390Sstevel@tonic-gate 		goto minsec;
400Sstevel@tonic-gate 	}
410Sstevel@tonic-gate 	i = l;
420Sstevel@tonic-gate 	printf("%llu", i / 60);
430Sstevel@tonic-gate minsec:
440Sstevel@tonic-gate 	i %= 60;
450Sstevel@tonic-gate 	printf(":");
460Sstevel@tonic-gate 	p2dig_ull(i);
470Sstevel@tonic-gate }
480Sstevel@tonic-gate 
490Sstevel@tonic-gate void
psecs_int(int l)500Sstevel@tonic-gate psecs_int(int l)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	int i;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	i = l / 3600;
550Sstevel@tonic-gate 	if (i) {
560Sstevel@tonic-gate 		printf("%d:", i);
570Sstevel@tonic-gate 		i = l % 3600;
580Sstevel@tonic-gate 		p2dig_int(i / 60);
590Sstevel@tonic-gate 		goto minsec;
600Sstevel@tonic-gate 	}
610Sstevel@tonic-gate 	i = l;
620Sstevel@tonic-gate 	printf("%d", i / 60);
630Sstevel@tonic-gate minsec:
640Sstevel@tonic-gate 	i %= 60;
650Sstevel@tonic-gate 	printf(":");
660Sstevel@tonic-gate 	p2dig_int(i);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate void
p2dig_ull(unsigned long long i)700Sstevel@tonic-gate p2dig_ull(unsigned long long i)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	printf("%llu%llu", i / 10, i % 10);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate void
p2dig_int(int i)760Sstevel@tonic-gate p2dig_int(int i)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	printf("%d%d", i / 10, i % 10);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate char linbuf[128];
820Sstevel@tonic-gate char *linp = linbuf;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #ifdef MBCHAR
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * putbyte() send a byte to SHOUT.  No interpretation is done
880Sstevel@tonic-gate  * except an un-QUOTE'd control character, which is displayed
890Sstevel@tonic-gate  * as ^x.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate void
putbyte(int c)920Sstevel@tonic-gate putbyte(int c)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if ((c & QUOTE) == 0 && (c == 0177 || c < ' ' && c != '\t' &&
960Sstevel@tonic-gate 	    c != '\n')) {
970Sstevel@tonic-gate 		putbyte('^');
980Sstevel@tonic-gate 		if (c == 0177) {
990Sstevel@tonic-gate 			c = '?';
1000Sstevel@tonic-gate 		} else {
1010Sstevel@tonic-gate 			c |= 'A' - 1;
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	c &= TRIM;
1050Sstevel@tonic-gate 	*linp++ = c;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (c == '\n' || linp >= &linbuf[sizeof (linbuf) - 1 - MB_CUR_MAX]) {
1080Sstevel@tonic-gate 		/* 'cause the next Putchar() call may overflow the buffer.  */
1090Sstevel@tonic-gate 		flush();
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * Putchar(tc) does what putbyte(c) do for a byte c.
1150Sstevel@tonic-gate  * Note that putbyte(c) just send the byte c (provided c is not
1160Sstevel@tonic-gate  * a control character) as it is, while Putchar(tc) may expand the
1170Sstevel@tonic-gate  * character tc to some byte sequnce that represents the character
1180Sstevel@tonic-gate  * in EUC form.
1190Sstevel@tonic-gate  */
120*356Smuffin void
Putchar(tchar tc)1210Sstevel@tonic-gate Putchar(tchar tc)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	int	n;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (isascii(tc&TRIM)) {
1260Sstevel@tonic-gate 		putbyte((int)tc);
1270Sstevel@tonic-gate 		return;
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	tc &= TRIM;
1300Sstevel@tonic-gate 	n = wctomb(linp, tc);
1310Sstevel@tonic-gate 	if (n == -1) {
1320Sstevel@tonic-gate 		return;
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 	linp += n;
1350Sstevel@tonic-gate 	if (linp >= &linbuf[sizeof (linbuf) - 1 - MB_CUR_MAX]) {
1360Sstevel@tonic-gate 		flush();
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #else	/* !MBCHAR */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * putbyte() send a byte to SHOUT.  No interpretation is done
1440Sstevel@tonic-gate  * except an un-QUOTE'd control character, which is displayed
1450Sstevel@tonic-gate  * as ^x.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate void
putbyte(int c)1480Sstevel@tonic-gate putbyte(int c)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if ((c & QUOTE) == 0 && (c == 0177 || c < ' ' && c != '\t' &&
1520Sstevel@tonic-gate 	    c != '\n')) {
1530Sstevel@tonic-gate 		putbyte('^');
1540Sstevel@tonic-gate 		if (c == 0177) {
1550Sstevel@tonic-gate 			c = '?';
1560Sstevel@tonic-gate 		} else {
1570Sstevel@tonic-gate 			c |= 'A' - 1;
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	c &= TRIM;
1610Sstevel@tonic-gate 	*linp++ = c;
1620Sstevel@tonic-gate 	if (c == '\n' || linp >= &linbuf[sizeof (linbuf) - 2]) {
1630Sstevel@tonic-gate 		flush();
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Putchar(tc) does what putbyte(c) do for a byte c.
1690Sstevel@tonic-gate  * For single-byte character only environment, there is no
1700Sstevel@tonic-gate  * difference between Putchar() and putbyte() though.
1710Sstevel@tonic-gate  */
172*356Smuffin void
Putchar(tchar tc)173*356Smuffin Putchar(tchar tc)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	putbyte((int)tc);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #endif	/* !MBCHAR */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate void
draino(void)181*356Smuffin draino(void)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	linp = linbuf;
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate void
flush(void)187*356Smuffin flush(void)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	int unit;
1900Sstevel@tonic-gate 	int lmode;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (linp == linbuf) {
1930Sstevel@tonic-gate 		return;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	if (haderr) {
1960Sstevel@tonic-gate 		unit = didfds ? 2 : SHDIAG;
1970Sstevel@tonic-gate 	} else {
1980Sstevel@tonic-gate 		unit = didfds ? 1 : SHOUT;
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate #ifdef TIOCLGET
2010Sstevel@tonic-gate 	if (didfds == 0 && ioctl(unit, TIOCLGET,  (char *)&lmode) == 0 &&
2020Sstevel@tonic-gate 	    lmode&LFLUSHO) {
2030Sstevel@tonic-gate 		lmode = LFLUSHO;
2040Sstevel@tonic-gate 		(void) ioctl(unit, TIOCLBIC,  (char *)&lmode);
2050Sstevel@tonic-gate 		(void) write(unit, "\n", 1);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate #endif
2080Sstevel@tonic-gate 	(void) write(unit, linbuf, linp - linbuf);
2090Sstevel@tonic-gate 	linp = linbuf;
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * Should not be needed.
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate void
write_string(char * s)2160Sstevel@tonic-gate write_string(char *s)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	int unit;
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * First let's make it sure to flush out things.
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	flush();
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if (haderr) {
2250Sstevel@tonic-gate 		unit = didfds ? 2 : SHDIAG;
2260Sstevel@tonic-gate 	} else {
2270Sstevel@tonic-gate 		unit = didfds ? 1 : SHOUT;
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	(void) write(unit, s, strlen(s));
2310Sstevel@tonic-gate }
232