source: trunk/third/tcsh/sh.print.c @ 12039

Revision 12039, 5.7 KB checked in by danw, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12038, which included commits to RCS files with non-trunk default branches.
Line 
1/* $Header: /afs/dev.mit.edu/source/repository/third/tcsh/sh.print.c,v 1.1.1.2 1998-10-03 21:10:05 danw 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 1.1.1.2 1998-10-03 21:10:05 danw 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            if (c != '\t' && c != '\n' && (xlate_cr || c != '\r')) {
134                xputchar('^' | atr);
135                if (c == ASCII)
136                    c = '?';
137                else
138                    c |= 0100;
139            }
140        }
141        else if (!Isprint(c)) {
142            xputchar('\\' | atr);
143            xputchar((((c >> 6) & 7) + '0') | atr);
144            xputchar((((c >> 3) & 7) + '0') | atr);
145            c = (c & 7) + '0';
146        }
147        (void) putraw(c | atr);
148    }
149    else {
150        c &= TRIM;
151        if (haderr ? (didfds ? is2atty : isdiagatty) :
152            (didfds ? is1atty : isoutatty))
153            SetAttributes(c | atr);
154        (void) putpure(c);
155    }
156    if (lbuffed && (c & CHAR) == '\n')
157        flush();
158}
159
160int
161putraw(c)
162    register int c;
163{
164    if (haderr ? (didfds ? is2atty : isdiagatty) :
165        (didfds ? is1atty : isoutatty)) {
166        if (Tty_eight_bit == -1)
167            ed_set_tty_eight_bit();
168        if (!Tty_eight_bit && (c & META)) {
169            c = (c & ~META) | STANDOUT;
170        }
171        SetAttributes(c);
172    }
173    return putpure(c);
174}
175
176int
177putpure(c)
178    register int c;
179{
180    c &= CHAR;
181
182    *linp++ = (char) c;
183    if (linp >= &linbuf[sizeof linbuf - 10])
184        flush();
185    return (1);
186}
187
188void
189draino()
190{
191    linp = linbuf;
192}
193
194void
195flush()
196{
197    int unit;
198    static int interrupted = 0;
199    size_t sz;
200
201    /* int lmode; */
202
203    if (linp == linbuf)
204        return;
205    if (GettingInput && !Tty_raw_mode && linp < &linbuf[sizeof linbuf - 10])
206        return;
207    if (interrupted) {
208        interrupted = 0;
209        linp = linbuf;          /* avoid resursion as stderror calls flush */
210        stderror(ERR_SILENT);
211    }
212    interrupted = 1;
213    if (haderr)
214        unit = didfds ? 2 : SHDIAG;
215    else
216        unit = didfds ? 1 : SHOUT;
217#ifdef COMMENT
218#ifdef TIOCLGET
219    if (didfds == 0 && ioctl(unit, TIOCLGET, (ioctl_t) & lmode) == 0 &&
220        lmode & LFLUSHO) {
221        lmode = LFLUSHO;
222        (void) ioctl(unit, TIOCLBIC, (ioclt_t) & lmode);
223        (void) write(unit, "\n", 1);
224    }
225#endif
226#endif
227    sz = (size_t) (linp - linbuf);
228    if (write(unit, linbuf, sz) == -1)
229        switch (errno) {
230#ifdef EIO
231        /* We lost our tty */
232        case EIO:
233#endif
234#ifdef ENXIO
235        /*
236         * Deal with Digital Unix 4.0D bogocity, returning ENXIO when
237         * we lose our tty.
238         */
239        case ENXIO:
240#endif
241        /*
242         * IRIX 6.4 bogocity?
243         */
244#ifdef ENOTTY
245        case ENOTTY:
246#endif
247#ifdef EBADF
248        case EBADF:
249#endif
250        /* Nothing to do, but die */
251            xexit(1);
252            break;
253        default:
254            stderror(ERR_SILENT);
255            break;
256        }
257
258    linp = linbuf;
259    interrupted = 0;
260}
Note: See TracBrowser for help on using the repository browser.