xref: /titanic_50/usr/src/cmd/sh/print.c (revision 1cb6af97c6f66f456d4f726ef056e1ebc0f73305)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1996, 1997 by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.12.6.1	*/
32 /*
33  * UNIX shell
34  *
35  */
36 
37 #include	"defs.h"
38 #include	<sys/param.h>
39 #include	<locale.h>
40 #include	<wctype.h>	/* iswprint() */
41 
42 #define		BUFLEN		256
43 
44 unsigned char numbuf[21];
45 
46 static unsigned char buffer[BUFLEN];
47 static unsigned char *bufp = buffer;
48 static int index = 0;
49 static int buffd = 1;
50 
51 void	prc_buff(unsigned char c);
52 void	prs_buff(unsigned char *s);
53 void	prn_buff(int n);
54 void	prs_cntl(unsigned char *s);
55 void	prs(unsigned char *as);
56 void	itos(int n);
57 
58 /*
59  * printing and io conversion
60  */
61 void
62 prp()
63 {
64 	if ((flags & prompt) == 0 && cmdadr) {
65 		prs_cntl(cmdadr);
66 		prs((unsigned char *)colon);
67 	}
68 }
69 
70 void
71 prs(unsigned char *as)
72 {
73 	char	*s;
74 
75 	if ((s = gettext((char *)as)) != 0) {
76 		write(output, s, length(s) - 1);
77 	}
78 }
79 
80 void
81 prc(unsigned char c)
82 {
83 	if (c) {
84 		write(output, &c, 1);
85 	}
86 }
87 
88 void
89 prwc(wchar_t c)
90 {
91 	char	mb[MB_LEN_MAX + 1];
92 	int	len;
93 
94 	if (c == 0) {
95 		return;
96 	}
97 	if ((len = wctomb(mb, c)) < 0) {
98 		mb[0] = (unsigned char)c;
99 		len = 1;
100 	}
101 	write(output, mb, len);
102 }
103 
104 void
105 prt(long t)
106 {
107 	int hr, min, sec;
108 
109 	t += HZ / 2;
110 	t /= HZ;
111 	sec = t % 60;
112 	t /= 60;
113 	min = t % 60;
114 
115 	if ((hr = t / 60) != 0) {
116 		prn_buff(hr);
117 		prc_buff('h');
118 	}
119 
120 	prn_buff(min);
121 	prc_buff('m');
122 	prn_buff(sec);
123 	prc_buff('s');
124 }
125 
126 void
127 prn(int n)
128 {
129 	itos(n);
130 
131 	prs(numbuf);
132 }
133 
134 void
135 itos(int n)
136 {
137 	unsigned char buf[21];
138 	unsigned char *abuf = &buf[20];
139 	int d;
140 
141 	*--abuf = (unsigned char)'\0';
142 
143 	do {
144 		 *--abuf = (unsigned char)('0' + n - 10 * (d = n / 10));
145 	} while ((n = d) != 0);
146 
147 	strncpy(numbuf, abuf, sizeof (numbuf));
148 }
149 
150 int
151 stoi(unsigned char *icp)
152 {
153 	unsigned char	*cp = icp;
154 	int	r = 0;
155 	unsigned char	c;
156 
157 	while ((c = *cp, digit(c)) && c && r >= 0) {
158 		r = r * 10 + c - '0';
159 		cp++;
160 	}
161 	if (r < 0 || cp == icp) {
162 		failed(icp, badnum);
163 	} else {
164 		return (r);
165 	}
166 }
167 
168 int
169 ltos(long n)
170 {
171 	int i;
172 
173 	numbuf[20] = '\0';
174 	for (i = 19; i >= 0; i--) {
175 		numbuf[i] = n % 10 + '0';
176 		if ((n /= 10) == 0) {
177 			break;
178 		}
179 	}
180 	return (i);
181 }
182 
183 void
184 prl(long n)
185 {
186 	int i;
187 	i = ltos(n);
188 	prs_buff(&numbuf[i]);
189 }
190 
191 
192 int
193 ulltos(u_longlong_t n)
194 {
195 	int i;
196 
197 	/* The max unsigned long long is 20 characters (+1 for '\0') */
198 	numbuf[20] = '\0';
199 	for (i = 19; i >= 0; i--) {
200 		numbuf[i] = n % 10 + '0';
201 		if ((n /= 10) == 0) {
202 			break;
203 		}
204 	}
205 	return (i);
206 }
207 
208 void
209 prull(u_longlong_t n)
210 {
211 	int i;
212 	i = ulltos(n);
213 	prs_buff(&numbuf[i]);
214 }
215 
216 void
217 flushb()
218 {
219 	if (index) {
220 		bufp[index] = '\0';
221 		write(buffd, bufp, length(bufp) - 1);
222 		index = 0;
223 	}
224 }
225 
226 void
227 prc_buff(unsigned char c)
228 {
229 	if (c) {
230 		if (buffd != -1 && index + 1 >= BUFLEN) {
231 			flushb();
232 		}
233 
234 		bufp[index++] = c;
235 	} else {
236 		flushb();
237 		write(buffd, &c, 1);
238 	}
239 }
240 
241 void
242 prs_buff(unsigned char *s)
243 {
244 	int len = length(gettext((char *)s)) - 1;
245 
246 	if (buffd != -1 && index + len >= BUFLEN) {
247 		flushb();
248 	}
249 
250 	if (buffd != -1 && len >= BUFLEN) {
251 		write(buffd, gettext((char *)s), len);
252 	} else {
253 		movstr(gettext((char *)s), &bufp[index]);
254 		index += len;
255 	}
256 }
257 
258 unsigned char *
259 octal(unsigned char c, unsigned char *ptr)
260 {
261 	*ptr++ = '\\';
262 	*ptr++ = ((unsigned int)c >> 6) + '0';
263 	*ptr++ = (((unsigned int)c >> 3) & 07) + '0';
264 	*ptr++ = (c & 07) + '0';
265 	return (ptr);
266 }
267 
268 void
269 prs_cntl(unsigned char *s)
270 {
271 	int n;
272 	wchar_t wc;
273 	unsigned char *olds = s;
274 	unsigned char *ptr = bufp;
275 	wchar_t c;
276 
277 	if ((n = mbtowc(&wc, (const char *)s, MB_LEN_MAX)) <= 0) {
278 		n = 0;
279 	}
280 	while (n != 0) {
281 		if (n < 0) {
282 			ptr = octal(*s++, ptr);
283 		} else {
284 			c = wc;
285 			s += n;
286 			if (!iswprint(c)) {
287 				if (c < '\040' && c > 0) {
288 					/*
289 					 * assumes ASCII char
290 					 * translate a control character
291 					 * into a printable sequence
292 					 */
293 					*ptr++ = '^';
294 					*ptr++ = (c + 0100);
295 				} else if (c == 0177) {
296 					/* '\0177' does not work */
297 					*ptr++ = '^';
298 					*ptr++ = '?';
299 				} else {
300 					/*
301 					 * unprintable 8-bit byte sequence
302 					 * assumes all legal multibyte
303 					 * sequences are
304 					 * printable
305 					 */
306 					ptr = octal(*olds, ptr);
307 				}
308 			} else {
309 				while (n--) {
310 					*ptr++ = *olds++;
311 				}
312 			}
313 		}
314 		if (buffd != -1 && ptr >= &bufp[BUFLEN-4]) {
315 			*ptr = '\0';
316 			prs(bufp);
317 			ptr = bufp;
318 		}
319 		olds = s;
320 		if ((n = mbtowc(&wc, (const char *)s, MB_LEN_MAX)) <= 0) {
321 			n = 0;
322 		}
323 	}
324 	*ptr = '\0';
325 	prs(bufp);
326 }
327 
328 void
329 prl_buff(long lc)
330 {
331 	prs_buff(&numbuf[ltos(lc)]);
332 }
333 
334 void
335 prull_buff(u_longlong_t lc)
336 {
337 	prs_buff(&numbuf[ulltos(lc)]);
338 }
339 
340 void
341 prn_buff(int n)
342 {
343 	itos(n);
344 
345 	prs_buff(numbuf);
346 }
347 
348 void
349 prsp_buff(int cnt)
350 {
351 	while (cnt--) {
352 		prc_buff(SPACE);
353 	}
354 }
355 
356 int
357 setb(int fd)
358 {
359 	int ofd;
360 
361 	if ((ofd = buffd) == -1) {
362 		if (bufp+index+1 >= brkend) {
363 			growstak(bufp+index+1);
364 		}
365 		if (bufp[index-1]) {
366 			bufp[index++] = 0;
367 		}
368 		endstak(bufp+index);
369 	} else {
370 		flushb();
371 	}
372 	if ((buffd = fd) == -1) {
373 		bufp = locstak();
374 	} else {
375 		bufp = buffer;
376 	}
377 	index = 0;
378 	return (ofd);
379 }
380