sh.print.c revision 69408
1/* $Header: /src/pub/tcsh/sh.print.c,v 3.20 2000/06/11 02:14:15 kim Exp $ */
2/*
3 * sh.print.c: Primitive Output routines.
4 */
5/*-
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37#include "sh.h"
38
39RCSID("$Id: sh.print.c,v 3.20 2000/06/11 02:14:15 kim Exp $")
40
41#include "ed.h"
42
43extern int Tty_eight_bit;
44extern int Tty_raw_mode;
45extern Char GettingInput;
46
47int     lbuffed = 1;		/* true if line buffered */
48
49static	void	p2dig	__P((int));
50
51/*
52 * C Shell
53 */
54
55#if defined(BSDLIMIT) || defined(RLIMIT_CPU)
56void
57psecs(l)
58    long    l;
59{
60    register int i;
61
62    i = (int) (l / 3600);
63    if (i) {
64	xprintf("%d:", i);
65	i = (int) (l % 3600);
66	p2dig(i / 60);
67	goto minsec;
68    }
69    i = (int) l;
70    xprintf("%d", i / 60);
71minsec:
72    i %= 60;
73    xprintf(":");
74    p2dig(i);
75}
76
77#endif
78
79void
80pcsecs(l)			/* PWP: print mm:ss.dd, l is in sec*100 */
81#ifdef BSDTIMES
82    long    l;
83#else /* BSDTIMES */
84# ifndef POSIX
85    time_t  l;
86# else /* POSIX */
87    clock_t l;
88# endif /* POSIX */
89#endif /* BSDTIMES */
90{
91    register int i;
92
93    i = (int) (l / 360000);
94    if (i) {
95	xprintf("%d:", i);
96	i = (int) ((l % 360000) / 100);
97	p2dig(i / 60);
98	goto minsec;
99    }
100    i = (int) (l / 100);
101    xprintf("%d", i / 60);
102minsec:
103    i %= 60;
104    xprintf(":");
105    p2dig(i);
106    xprintf(".");
107    p2dig((int) (l % 100));
108}
109
110static void
111p2dig(i)
112    register int i;
113{
114
115    xprintf("%d%d", i / 10, i % 10);
116}
117
118char    linbuf[2048];		/* was 128 */
119char   *linp = linbuf;
120bool    output_raw = 0;		/* PWP */
121bool    xlate_cr   = 0;		/* HE */
122
123void
124xputchar(c)
125    register int c;
126{
127    int     atr = 0;
128
129    atr |= c & ATTRIBUTES & TRIM;
130    c &= CHAR | QUOTE;
131    if (!output_raw && (c & QUOTE) == 0) {
132	if (Iscntrl(c)) {
133#ifdef COLORCAT
134	    if (c != '\t' && c != '\n' && !(adrof(STRcolorcat) && c=='\033') && (xlate_cr || c != '\r')) {
135#else
136	    if (c != '\t' && c != '\n' && (xlate_cr || c != '\r')) {
137#endif
138		xputchar('^' | atr);
139#ifdef IS_ASCII
140		if (c == ASCII)
141		    c = '?';
142		else
143		    c |= 0100;
144#else
145		if (c == CTL_ESC('\177'))
146		    c = '?';
147		else
148		    c =_toebcdic[_toascii[c]|0100];
149#endif
150
151	    }
152	}
153	else if (!Isprint(c)) {
154	    xputchar('\\' | atr);
155	    xputchar((((c >> 6) & 7) + '0') | atr);
156	    xputchar((((c >> 3) & 7) + '0') | atr);
157	    c = (c & 7) + '0';
158	}
159	(void) putraw(c | atr);
160    }
161    else {
162	c &= TRIM;
163	if (haderr ? (didfds ? is2atty : isdiagatty) :
164	    (didfds ? is1atty : isoutatty))
165	    SetAttributes(c | atr);
166	(void) putpure(c);
167    }
168    if (lbuffed && (c & CHAR) == '\n')
169	flush();
170}
171
172int
173putraw(c)
174    register int c;
175{
176    if (haderr ? (didfds ? is2atty : isdiagatty) :
177	(didfds ? is1atty : isoutatty)) {
178	if (Tty_eight_bit == -1)
179	    ed_set_tty_eight_bit();
180	if (!Tty_eight_bit && (c & META)) {
181	    c = (c & ~META) | STANDOUT;
182	}
183	SetAttributes(c);
184    }
185    return putpure(c);
186}
187
188int
189putpure(c)
190    register int c;
191{
192    c &= CHAR;
193
194    *linp++ = (char) c;
195    if (linp >= &linbuf[sizeof linbuf - 10])
196	flush();
197    return (1);
198}
199
200void
201drainoline()
202{
203    linp = linbuf;
204}
205
206void
207flush()
208{
209    int unit;
210    static int interrupted = 0;
211    size_t sz;
212
213    /* int lmode; */
214
215    if (linp == linbuf)
216	return;
217    if (GettingInput && !Tty_raw_mode && linp < &linbuf[sizeof linbuf - 10])
218	return;
219    if (interrupted) {
220	interrupted = 0;
221	linp = linbuf;		/* avoid resursion as stderror calls flush */
222	stderror(ERR_SILENT);
223    }
224    interrupted = 1;
225    if (haderr)
226	unit = didfds ? 2 : SHDIAG;
227    else
228	unit = didfds ? 1 : SHOUT;
229#ifdef COMMENT
230#ifdef TIOCLGET
231    if (didfds == 0 && ioctl(unit, TIOCLGET, (ioctl_t) & lmode) == 0 &&
232	lmode & LFLUSHO) {
233	lmode = LFLUSHO;
234	(void) ioctl(unit, TIOCLBIC, (ioclt_t) & lmode);
235	(void) write(unit, "\n", 1);
236    }
237#endif
238#endif
239    sz = (size_t) (linp - linbuf);
240    if (write(unit, linbuf, sz) == -1)
241	switch (errno) {
242#ifdef EIO
243	/* We lost our tty */
244	case EIO:
245#endif
246#ifdef ENXIO
247	/*
248	 * Deal with Digital Unix 4.0D bogocity, returning ENXIO when
249	 * we lose our tty.
250	 */
251	case ENXIO:
252#endif
253	/*
254	 * IRIX 6.4 bogocity?
255	 */
256#ifdef ENOTTY
257	case ENOTTY:
258#endif
259#ifdef EBADF
260	case EBADF:
261#endif
262#ifdef ESTALE
263	/*
264	 * Lost our file descriptor, exit (IRIS4D)
265	 */
266	case ESTALE:
267#endif
268	/*
269	 * Over our quota, writing the history file
270	 */
271#ifdef EDQUOT
272	case EDQUOT:
273#endif
274	/* Nothing to do, but die */
275	    xexit(1);
276	    break;
277	default:
278	    stderror(ERR_SILENT);
279	    break;
280	}
281
282    linp = linbuf;
283    interrupted = 0;
284}
285