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