summaryrefslogtreecommitdiff
path: root/usr/src/cmd/csh/sh.print.c
blob: 34cd721e9304e0f67de333fdeef85eda942ef347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved. The Berkeley Software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "sh.h"

void	p2dig_ull(unsigned long long);
void	p2dig_int(int);
void	flush(void);
void	Putchar(tchar);


/*
 * C Shell
 */

void
psecs_ull(unsigned long long l)
{
	unsigned long long i;

	i = l / 3600;
	if (i) {
		printf("%llu:", i);
		i = l % 3600;
		p2dig_ull(i / 60);
		goto minsec;
	}
	i = l;
	printf("%llu", i / 60);
minsec:
	i %= 60;
	printf(":");
	p2dig_ull(i);
}

void
psecs_int(int l)
{
	int i;

	i = l / 3600;
	if (i) {
		printf("%d:", i);
		i = l % 3600;
		p2dig_int(i / 60);
		goto minsec;
	}
	i = l;
	printf("%d", i / 60);
minsec:
	i %= 60;
	printf(":");
	p2dig_int(i);
}

void
p2dig_ull(unsigned long long i)
{
	printf("%llu%llu", i / 10, i % 10);
}

void
p2dig_int(int i)
{
	printf("%d%d", i / 10, i % 10);
}

char linbuf[128];
char *linp = linbuf;

#ifdef MBCHAR

/*
 * putbyte() send a byte to SHOUT.  No interpretation is done
 * except an un-QUOTE'd control character, which is displayed
 * as ^x.
 */
void
putbyte(int c)
{

	if ((c & QUOTE) == 0 && (c == 0177 || c < ' ' && c != '\t' &&
	    c != '\n')) {
		putbyte('^');
		if (c == 0177) {
			c = '?';
		} else {
			c |= 'A' - 1;
		}
	}
	c &= TRIM;
	*linp++ = c;

	if (c == '\n' || linp >= &linbuf[sizeof (linbuf) - 1 - MB_CUR_MAX]) {
		/* 'cause the next Putchar() call may overflow the buffer.  */
		flush();
	}
}

/*
 * Putchar(tc) does what putbyte(c) do for a byte c.
 * Note that putbyte(c) just send the byte c (provided c is not
 * a control character) as it is, while Putchar(tc) may expand the
 * character tc to some byte sequnce that represents the character
 * in EUC form.
 */
void
Putchar(tchar tc)
{
	int	n;

	if (isascii(tc&TRIM)) {
		putbyte((int)tc);
		return;
	}
	tc &= TRIM;
	n = wctomb(linp, tc);
	if (n == -1) {
		return;
	}
	linp += n;
	if (linp >= &linbuf[sizeof (linbuf) - 1 - MB_CUR_MAX]) {
		flush();
	}
}

#else	/* !MBCHAR */

/*
 * putbyte() send a byte to SHOUT.  No interpretation is done
 * except an un-QUOTE'd control character, which is displayed
 * as ^x.
 */
void
putbyte(int c)
{

	if ((c & QUOTE) == 0 && (c == 0177 || c < ' ' && c != '\t' &&
	    c != '\n')) {
		putbyte('^');
		if (c == 0177) {
			c = '?';
		} else {
			c |= 'A' - 1;
		}
	}
	c &= TRIM;
	*linp++ = c;
	if (c == '\n' || linp >= &linbuf[sizeof (linbuf) - 2]) {
		flush();
	}
}

/*
 * Putchar(tc) does what putbyte(c) do for a byte c.
 * For single-byte character only environment, there is no
 * difference between Putchar() and putbyte() though.
 */
void
Putchar(tchar tc)
{
	putbyte((int)tc);
}

#endif	/* !MBCHAR */

void
draino(void)
{
	linp = linbuf;
}

void
flush(void)
{
	int unit;
	int lmode;

	if (linp == linbuf) {
		return;
	}
	if (haderr) {
		unit = didfds ? 2 : SHDIAG;
	} else {
		unit = didfds ? 1 : SHOUT;
	}
#ifdef TIOCLGET
	if (didfds == 0 && ioctl(unit, TIOCLGET,  (char *)&lmode) == 0 &&
	    lmode&LFLUSHO) {
		lmode = LFLUSHO;
		(void) ioctl(unit, TIOCLBIC,  (char *)&lmode);
		(void) write(unit, "\n", 1);
	}
#endif
	(void) write(unit, linbuf, linp - linbuf);
	linp = linbuf;
}

/*
 * Should not be needed.
 */
void
write_string(char *s)
{
	int unit;
	/*
	 * First let's make it sure to flush out things.
	 */
	flush();

	if (haderr) {
		unit = didfds ? 2 : SHDIAG;
	} else {
		unit = didfds ? 1 : SHOUT;
	}

	(void) write(unit, s, strlen(s));
}