8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sh / print.c
blobee10bb670ac25e8f9518c850596c69c4a8eae546
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 * UNIX shell
36 #include "defs.h"
37 #include <sys/param.h>
38 #include <locale.h>
39 #include <wctype.h> /* iswprint() */
41 #define BUFLEN 256
43 unsigned char numbuf[21];
45 static unsigned char buffer[BUFLEN];
46 static unsigned char *bufp = buffer;
47 static int index = 0;
48 static int buffd = 1;
50 void prc_buff(unsigned char c);
51 void prs_buff(unsigned char *s);
52 void prn_buff(int n);
53 void prs_cntl(unsigned char *s);
54 void prs(unsigned char *as);
55 void itos(int n);
58 * printing and io conversion
60 void
61 prp()
63 if ((flags & prompt) == 0 && cmdadr) {
64 prs_cntl(cmdadr);
65 prs((unsigned char *)colon);
69 void
70 prs(unsigned char *as)
72 if (as) {
73 write(output, as, length(as) - 1);
77 void
78 prc(unsigned char c)
80 if (c) {
81 write(output, &c, 1);
85 void
86 prwc(wchar_t c)
88 char mb[MB_LEN_MAX + 1];
89 int len;
91 if (c == 0) {
92 return;
94 if ((len = wctomb(mb, c)) < 0) {
95 mb[0] = (unsigned char)c;
96 len = 1;
98 write(output, mb, len);
101 void
102 prt(long t)
104 int hr, min, sec;
106 t += HZ / 2;
107 t /= HZ;
108 sec = t % 60;
109 t /= 60;
110 min = t % 60;
112 if ((hr = t / 60) != 0) {
113 prn_buff(hr);
114 prc_buff('h');
117 prn_buff(min);
118 prc_buff('m');
119 prn_buff(sec);
120 prc_buff('s');
123 void
124 prn(int n)
126 itos(n);
128 prs(numbuf);
131 void
132 itos(int n)
134 unsigned char buf[21];
135 unsigned char *abuf = &buf[20];
136 int d;
138 *--abuf = (unsigned char)'\0';
140 do {
141 *--abuf = (unsigned char)('0' + n - 10 * (d = n / 10));
142 } while ((n = d) != 0);
144 strncpy(numbuf, abuf, sizeof (numbuf));
148 stoi(unsigned char *icp)
150 unsigned char *cp = icp;
151 int r = 0;
152 unsigned char c;
154 while ((c = *cp, digit(c)) && c && r >= 0) {
155 r = r * 10 + c - '0';
156 cp++;
158 if (r < 0 || cp == icp) {
159 failed(icp, badnum);
160 /* NOTREACHED */
161 } else {
162 return (r);
167 ltos(long n)
169 int i;
171 numbuf[20] = '\0';
172 for (i = 19; i >= 0; i--) {
173 numbuf[i] = n % 10 + '0';
174 if ((n /= 10) == 0) {
175 break;
178 return (i);
181 static int
182 ulltos(u_longlong_t n)
184 int i;
186 /* The max unsigned long long is 20 characters (+1 for '\0') */
187 numbuf[20] = '\0';
188 for (i = 19; i >= 0; i--) {
189 numbuf[i] = n % 10 + '0';
190 if ((n /= 10) == 0) {
191 break;
194 return (i);
197 void
198 flushb()
200 if (index) {
201 bufp[index] = '\0';
202 write(buffd, bufp, length(bufp) - 1);
203 index = 0;
207 void
208 prc_buff(unsigned char c)
210 if (c) {
211 if (buffd != -1 && index + 1 >= BUFLEN) {
212 flushb();
215 bufp[index++] = c;
216 } else {
217 flushb();
218 write(buffd, &c, 1);
222 void
223 prs_buff(unsigned char *s)
225 int len = length(s) - 1;
227 if (buffd != -1 && index + len >= BUFLEN) {
228 flushb();
231 if (buffd != -1 && len >= BUFLEN) {
232 write(buffd, s, len);
233 } else {
234 movstr(s, &bufp[index]);
235 index += len;
239 static unsigned char *
240 octal(unsigned char c, unsigned char *ptr)
242 *ptr++ = '\\';
243 *ptr++ = ((unsigned int)c >> 6) + '0';
244 *ptr++ = (((unsigned int)c >> 3) & 07) + '0';
245 *ptr++ = (c & 07) + '0';
246 return (ptr);
249 void
250 prs_cntl(unsigned char *s)
252 int n;
253 wchar_t wc;
254 unsigned char *olds = s;
255 unsigned char *ptr = bufp;
256 wchar_t c;
258 if ((n = mbtowc(&wc, (const char *)s, MB_LEN_MAX)) <= 0) {
259 n = 0;
261 while (n != 0) {
262 if (n < 0) {
263 ptr = octal(*s++, ptr);
264 } else {
265 c = wc;
266 s += n;
267 if (!iswprint(c)) {
268 if (c < '\040' && c > 0) {
270 * assumes ASCII char
271 * translate a control character
272 * into a printable sequence
274 *ptr++ = '^';
275 *ptr++ = (c + 0100);
276 } else if (c == 0177) {
277 /* '\0177' does not work */
278 *ptr++ = '^';
279 *ptr++ = '?';
280 } else {
282 * unprintable 8-bit byte sequence
283 * assumes all legal multibyte
284 * sequences are
285 * printable
287 ptr = octal(*olds, ptr);
289 } else {
290 while (n--) {
291 *ptr++ = *olds++;
295 if (buffd != -1 && ptr >= &bufp[BUFLEN-4]) {
296 *ptr = '\0';
297 prs(bufp);
298 ptr = bufp;
300 olds = s;
301 if ((n = mbtowc(&wc, (const char *)s, MB_LEN_MAX)) <= 0) {
302 n = 0;
305 *ptr = '\0';
306 prs(bufp);
309 void
310 prull_buff(u_longlong_t lc)
312 prs_buff(&numbuf[ulltos(lc)]);
315 void
316 prn_buff(int n)
318 itos(n);
320 prs_buff(numbuf);
324 setb(int fd)
326 int ofd;
328 if ((ofd = buffd) == -1) {
329 if (bufp+index+1 >= brkend) {
330 growstak(bufp+index+1);
332 if (bufp[index-1]) {
333 bufp[index++] = 0;
335 endstak(bufp+index);
336 } else {
337 flushb();
339 if ((buffd = fd) == -1) {
340 bufp = locstak();
341 } else {
342 bufp = buffer;
344 index = 0;
345 return (ofd);