17c478bd9Sstevel@tonic-gate /*
2*6c02b4a4Smuffin * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley Software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #include "sh.h"
167c478bd9Sstevel@tonic-gate
17*6c02b4a4Smuffin void p2dig_ull(unsigned long long);
18*6c02b4a4Smuffin void p2dig_int(int);
19*6c02b4a4Smuffin void flush(void);
20*6c02b4a4Smuffin void Putchar(tchar);
21*6c02b4a4Smuffin
227c478bd9Sstevel@tonic-gate
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate * C Shell
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate void
psecs_ull(unsigned long long l)287c478bd9Sstevel@tonic-gate psecs_ull(unsigned long long l)
297c478bd9Sstevel@tonic-gate {
307c478bd9Sstevel@tonic-gate unsigned long long i;
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate i = l / 3600;
337c478bd9Sstevel@tonic-gate if (i) {
347c478bd9Sstevel@tonic-gate printf("%llu:", i);
357c478bd9Sstevel@tonic-gate i = l % 3600;
367c478bd9Sstevel@tonic-gate p2dig_ull(i / 60);
377c478bd9Sstevel@tonic-gate goto minsec;
387c478bd9Sstevel@tonic-gate }
397c478bd9Sstevel@tonic-gate i = l;
407c478bd9Sstevel@tonic-gate printf("%llu", i / 60);
417c478bd9Sstevel@tonic-gate minsec:
427c478bd9Sstevel@tonic-gate i %= 60;
437c478bd9Sstevel@tonic-gate printf(":");
447c478bd9Sstevel@tonic-gate p2dig_ull(i);
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate void
psecs_int(int l)487c478bd9Sstevel@tonic-gate psecs_int(int l)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate int i;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate i = l / 3600;
537c478bd9Sstevel@tonic-gate if (i) {
547c478bd9Sstevel@tonic-gate printf("%d:", i);
557c478bd9Sstevel@tonic-gate i = l % 3600;
567c478bd9Sstevel@tonic-gate p2dig_int(i / 60);
577c478bd9Sstevel@tonic-gate goto minsec;
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate i = l;
607c478bd9Sstevel@tonic-gate printf("%d", i / 60);
617c478bd9Sstevel@tonic-gate minsec:
627c478bd9Sstevel@tonic-gate i %= 60;
637c478bd9Sstevel@tonic-gate printf(":");
647c478bd9Sstevel@tonic-gate p2dig_int(i);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate void
p2dig_ull(unsigned long long i)687c478bd9Sstevel@tonic-gate p2dig_ull(unsigned long long i)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate printf("%llu%llu", i / 10, i % 10);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate void
p2dig_int(int i)747c478bd9Sstevel@tonic-gate p2dig_int(int i)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate printf("%d%d", i / 10, i % 10);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate char linbuf[128];
807c478bd9Sstevel@tonic-gate char *linp = linbuf;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate #ifdef MBCHAR
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * putbyte() send a byte to SHOUT. No interpretation is done
867c478bd9Sstevel@tonic-gate * except an un-QUOTE'd control character, which is displayed
877c478bd9Sstevel@tonic-gate * as ^x.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate void
putbyte(int c)907c478bd9Sstevel@tonic-gate putbyte(int c)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate if ((c & QUOTE) == 0 && (c == 0177 || c < ' ' && c != '\t' &&
947c478bd9Sstevel@tonic-gate c != '\n')) {
957c478bd9Sstevel@tonic-gate putbyte('^');
967c478bd9Sstevel@tonic-gate if (c == 0177) {
977c478bd9Sstevel@tonic-gate c = '?';
987c478bd9Sstevel@tonic-gate } else {
997c478bd9Sstevel@tonic-gate c |= 'A' - 1;
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate c &= TRIM;
1037c478bd9Sstevel@tonic-gate *linp++ = c;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if (c == '\n' || linp >= &linbuf[sizeof (linbuf) - 1 - MB_CUR_MAX]) {
1067c478bd9Sstevel@tonic-gate /* 'cause the next Putchar() call may overflow the buffer. */
1077c478bd9Sstevel@tonic-gate flush();
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * Putchar(tc) does what putbyte(c) do for a byte c.
1137c478bd9Sstevel@tonic-gate * Note that putbyte(c) just send the byte c (provided c is not
1147c478bd9Sstevel@tonic-gate * a control character) as it is, while Putchar(tc) may expand the
1157c478bd9Sstevel@tonic-gate * character tc to some byte sequnce that represents the character
1167c478bd9Sstevel@tonic-gate * in EUC form.
1177c478bd9Sstevel@tonic-gate */
118*6c02b4a4Smuffin void
Putchar(tchar tc)1197c478bd9Sstevel@tonic-gate Putchar(tchar tc)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate int n;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate if (isascii(tc&TRIM)) {
1247c478bd9Sstevel@tonic-gate putbyte((int)tc);
1257c478bd9Sstevel@tonic-gate return;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate tc &= TRIM;
1287c478bd9Sstevel@tonic-gate n = wctomb(linp, tc);
1297c478bd9Sstevel@tonic-gate if (n == -1) {
1307c478bd9Sstevel@tonic-gate return;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate linp += n;
1337c478bd9Sstevel@tonic-gate if (linp >= &linbuf[sizeof (linbuf) - 1 - MB_CUR_MAX]) {
1347c478bd9Sstevel@tonic-gate flush();
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate #else /* !MBCHAR */
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate * putbyte() send a byte to SHOUT. No interpretation is done
1427c478bd9Sstevel@tonic-gate * except an un-QUOTE'd control character, which is displayed
1437c478bd9Sstevel@tonic-gate * as ^x.
1447c478bd9Sstevel@tonic-gate */
1457c478bd9Sstevel@tonic-gate void
putbyte(int c)1467c478bd9Sstevel@tonic-gate putbyte(int c)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate if ((c & QUOTE) == 0 && (c == 0177 || c < ' ' && c != '\t' &&
1507c478bd9Sstevel@tonic-gate c != '\n')) {
1517c478bd9Sstevel@tonic-gate putbyte('^');
1527c478bd9Sstevel@tonic-gate if (c == 0177) {
1537c478bd9Sstevel@tonic-gate c = '?';
1547c478bd9Sstevel@tonic-gate } else {
1557c478bd9Sstevel@tonic-gate c |= 'A' - 1;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate c &= TRIM;
1597c478bd9Sstevel@tonic-gate *linp++ = c;
1607c478bd9Sstevel@tonic-gate if (c == '\n' || linp >= &linbuf[sizeof (linbuf) - 2]) {
1617c478bd9Sstevel@tonic-gate flush();
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * Putchar(tc) does what putbyte(c) do for a byte c.
1677c478bd9Sstevel@tonic-gate * For single-byte character only environment, there is no
1687c478bd9Sstevel@tonic-gate * difference between Putchar() and putbyte() though.
1697c478bd9Sstevel@tonic-gate */
170*6c02b4a4Smuffin void
Putchar(tchar tc)171*6c02b4a4Smuffin Putchar(tchar tc)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate putbyte((int)tc);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate #endif /* !MBCHAR */
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate void
draino(void)179*6c02b4a4Smuffin draino(void)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate linp = linbuf;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate void
flush(void)185*6c02b4a4Smuffin flush(void)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate int unit;
1887c478bd9Sstevel@tonic-gate int lmode;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate if (linp == linbuf) {
1917c478bd9Sstevel@tonic-gate return;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate if (haderr) {
1947c478bd9Sstevel@tonic-gate unit = didfds ? 2 : SHDIAG;
1957c478bd9Sstevel@tonic-gate } else {
1967c478bd9Sstevel@tonic-gate unit = didfds ? 1 : SHOUT;
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate #ifdef TIOCLGET
1997c478bd9Sstevel@tonic-gate if (didfds == 0 && ioctl(unit, TIOCLGET, (char *)&lmode) == 0 &&
2007c478bd9Sstevel@tonic-gate lmode&LFLUSHO) {
2017c478bd9Sstevel@tonic-gate lmode = LFLUSHO;
2027c478bd9Sstevel@tonic-gate (void) ioctl(unit, TIOCLBIC, (char *)&lmode);
2037c478bd9Sstevel@tonic-gate (void) write(unit, "\n", 1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate (void) write(unit, linbuf, linp - linbuf);
2077c478bd9Sstevel@tonic-gate linp = linbuf;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate * Should not be needed.
2127c478bd9Sstevel@tonic-gate */
2137c478bd9Sstevel@tonic-gate void
write_string(char * s)2147c478bd9Sstevel@tonic-gate write_string(char *s)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate int unit;
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * First let's make it sure to flush out things.
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate flush();
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate if (haderr) {
2237c478bd9Sstevel@tonic-gate unit = didfds ? 2 : SHDIAG;
2247c478bd9Sstevel@tonic-gate } else {
2257c478bd9Sstevel@tonic-gate unit = didfds ? 1 : SHOUT;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate (void) write(unit, s, strlen(s));
2297c478bd9Sstevel@tonic-gate }
230