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