xref: /titanic_52/usr/src/cmd/csh/sh.print.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley Software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include "sh.h"
18 
19 void p2dig_ull(unsigned long long i);
20 void p2dig_int(int i);
21 void flush();
22 
23 /*
24  * C Shell
25  */
26 
27 void
28 psecs_ull(unsigned long long l)
29 {
30 	unsigned long long i;
31 
32 	i = l / 3600;
33 	if (i) {
34 		printf("%llu:", i);
35 		i = l % 3600;
36 		p2dig_ull(i / 60);
37 		goto minsec;
38 	}
39 	i = l;
40 	printf("%llu", i / 60);
41 minsec:
42 	i %= 60;
43 	printf(":");
44 	p2dig_ull(i);
45 }
46 
47 void
48 psecs_int(int l)
49 {
50 	int i;
51 
52 	i = l / 3600;
53 	if (i) {
54 		printf("%d:", i);
55 		i = l % 3600;
56 		p2dig_int(i / 60);
57 		goto minsec;
58 	}
59 	i = l;
60 	printf("%d", i / 60);
61 minsec:
62 	i %= 60;
63 	printf(":");
64 	p2dig_int(i);
65 }
66 
67 void
68 p2dig_ull(unsigned long long i)
69 {
70 	printf("%llu%llu", i / 10, i % 10);
71 }
72 
73 void
74 p2dig_int(int i)
75 {
76 	printf("%d%d", i / 10, i % 10);
77 }
78 
79 char linbuf[128];
80 char *linp = linbuf;
81 
82 #ifdef MBCHAR
83 
84 /*
85  * putbyte() send a byte to SHOUT.  No interpretation is done
86  * except an un-QUOTE'd control character, which is displayed
87  * as ^x.
88  */
89 void
90 putbyte(int c)
91 {
92 
93 	if ((c & QUOTE) == 0 && (c == 0177 || c < ' ' && c != '\t' &&
94 	    c != '\n')) {
95 		putbyte('^');
96 		if (c == 0177) {
97 			c = '?';
98 		} else {
99 			c |= 'A' - 1;
100 		}
101 	}
102 	c &= TRIM;
103 	*linp++ = c;
104 
105 	if (c == '\n' || linp >= &linbuf[sizeof (linbuf) - 1 - MB_CUR_MAX]) {
106 		/* 'cause the next Putchar() call may overflow the buffer.  */
107 		flush();
108 	}
109 }
110 
111 /*
112  * Putchar(tc) does what putbyte(c) do for a byte c.
113  * Note that putbyte(c) just send the byte c (provided c is not
114  * a control character) as it is, while Putchar(tc) may expand the
115  * character tc to some byte sequnce that represents the character
116  * in EUC form.
117  */
118 Putchar(tchar tc)
119 {
120 	int	n;
121 
122 	if (isascii(tc&TRIM)) {
123 		putbyte((int)tc);
124 		return;
125 	}
126 	tc &= TRIM;
127 	n = wctomb(linp, tc);
128 	if (n == -1) {
129 		return;
130 	}
131 	linp += n;
132 	if (linp >= &linbuf[sizeof (linbuf) - 1 - MB_CUR_MAX]) {
133 		flush();
134 	}
135 }
136 
137 #else	/* !MBCHAR */
138 
139 /*
140  * putbyte() send a byte to SHOUT.  No interpretation is done
141  * except an un-QUOTE'd control character, which is displayed
142  * as ^x.
143  */
144 void
145 putbyte(int c)
146 {
147 
148 	if ((c & QUOTE) == 0 && (c == 0177 || c < ' ' && c != '\t' &&
149 	    c != '\n')) {
150 		putbyte('^');
151 		if (c == 0177) {
152 			c = '?';
153 		} else {
154 			c |= 'A' - 1;
155 		}
156 	}
157 	c &= TRIM;
158 	*linp++ = c;
159 	if (c == '\n' || linp >= &linbuf[sizeof (linbuf) - 2]) {
160 		flush();
161 	}
162 }
163 
164 /*
165  * Putchar(tc) does what putbyte(c) do for a byte c.
166  * For single-byte character only environment, there is no
167  * difference between Putchar() and putbyte() though.
168  */
169 Putchar(tc)
170 	tchar tc;
171 {
172 	putbyte((int)tc);
173 }
174 
175 #endif	/* !MBCHAR */
176 
177 void
178 draino()
179 {
180 	linp = linbuf;
181 }
182 
183 void
184 flush()
185 {
186 	int unit;
187 	int lmode;
188 
189 	if (linp == linbuf) {
190 		return;
191 	}
192 	if (haderr) {
193 		unit = didfds ? 2 : SHDIAG;
194 	} else {
195 		unit = didfds ? 1 : SHOUT;
196 	}
197 #ifdef TIOCLGET
198 	if (didfds == 0 && ioctl(unit, TIOCLGET,  (char *)&lmode) == 0 &&
199 	    lmode&LFLUSHO) {
200 		lmode = LFLUSHO;
201 		(void) ioctl(unit, TIOCLBIC,  (char *)&lmode);
202 		(void) write(unit, "\n", 1);
203 	}
204 #endif
205 	(void) write(unit, linbuf, linp - linbuf);
206 	linp = linbuf;
207 }
208 
209 /*
210  * Should not be needed.
211  */
212 void
213 write_string(char *s)
214 {
215 	int unit;
216 	/*
217 	 * First let's make it sure to flush out things.
218 	 */
219 	flush();
220 
221 	if (haderr) {
222 		unit = didfds ? 2 : SHDIAG;
223 	} else {
224 		unit = didfds ? 1 : SHOUT;
225 	}
226 
227 	(void) write(unit, s, strlen(s));
228 }
229