2 * sh.print.c: Primitive Output routines.
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 extern int Tty_eight_bit
;
37 int lbuffed
= 1; /* true if line buffered */
39 static void p2dig (unsigned int);
45 #if defined(BSDLIMIT) || defined(RLIMIT_CPU)
47 psecs(unsigned long l
)
59 xprintf("%d", i
/ 60);
68 void /* PWP: print mm:ss.dd, l is in sec*100 */
70 pcsecs(unsigned long l
)
81 i
= (int) (l
/ 360000);
84 i
= (int) ((l
% 360000) / 100);
89 xprintf("%d", i
/ 60);
95 p2dig((int) (l
% 100));
102 xprintf("%u%u", i
/ 10, i
% 10);
105 char linbuf
[2048]; /* was 128 */
107 int output_raw
= 0; /* PWP */
108 int xlate_cr
= 0; /* HE */
110 /* For cleanup_push() */
112 output_raw_restore(void *xorig
)
124 char buf
[MB_LEN_MAX
];
127 len
= one_wctomb(buf
, c
& CHAR
);
128 for (i
= 0; i
< len
; i
++)
129 putraw((unsigned char)buf
[i
] | (c
& ~CHAR
));
135 char buf
[MB_LEN_MAX
];
138 len
= one_wctomb(buf
, c
& CHAR
);
139 for (i
= 0; i
< len
; i
++)
140 xputchar((unsigned char)buf
[i
] | (c
& ~CHAR
));
149 atr
= c
& ATTRIBUTES
& TRIM
;
151 if (!output_raw
&& (c
& QUOTE
) == 0) {
152 if (iscntrl(c
) && (ASC(c
) < 0x80 || MB_CUR_MAX
== 1)) {
153 if (c
!= '\t' && c
!= '\n'
155 && !(adrof(STRcolorcat
) && c
== CTL_ESC('\033'))
157 && (xlate_cr
|| c
!= '\r'))
160 if (c
== CTL_ESC('\177'))
163 /* Note: for IS_ASCII, this compiles to: c = c | 0100 */
164 c
= CTL_ESC(ASC(c
)|0100);
167 else if (!isprint(c
) && (ASC(c
) < 0x80 || MB_CUR_MAX
== 1)) {
168 xputchar('\\' | atr
);
169 xputchar((((c
>> 6) & 7) + '0') | atr
);
170 xputchar((((c
>> 3) & 7) + '0') | atr
);
173 (void) putraw(c
| atr
);
177 if (haderr
? (didfds
? is2atty
: isdiagatty
) :
178 (didfds
? is1atty
: isoutatty
))
179 SetAttributes(c
| atr
);
182 if (lbuffed
&& (c
& CHAR
) == '\n')
189 if (haderr
? (didfds
? is2atty
: isdiagatty
) :
190 (didfds
? is1atty
: isoutatty
)) {
191 if (Tty_eight_bit
== -1)
192 ed_set_tty_eight_bit();
193 if (!Tty_eight_bit
&& (c
& META
)) {
194 c
= (c
& ~META
) | STANDOUT
;
207 if (linp
>= &linbuf
[sizeof linbuf
- 10])
221 int unit
, oldexitset
= exitset
;
222 static int interrupted
= 0;
228 if (GettingInput
&& !Tty_raw_mode
&& linp
< &linbuf
[sizeof linbuf
- 10])
230 if (handle_interrupt
)
235 linp
= linbuf
; /* avoid recursion as stderror calls flush */
236 if (handle_interrupt
)
239 stderror(ERR_SILENT
);
243 unit
= didfds
? 2 : SHDIAG
;
245 unit
= didfds
? 1 : SHOUT
;
248 if (didfds
== 0 && ioctl(unit
, TIOCLGET
, (ioctl_t
) & lmode
) == 0 &&
251 (void) ioctl(unit
, TIOCLBIC
, (ioclt_t
) & lmode
);
252 (void) xwrite(unit
, "\n", 1);
256 if (xwrite(unit
, linbuf
, linp
- linbuf
) == -1)
259 /* We lost our tty */
264 * Deal with Digital Unix 4.0D bogocity, returning ENXIO when
280 * Lost our file descriptor, exit (IRIS4D)
286 * Deal with SoFS bogocity: returns ENOENT instead of ESTALE.
291 * Over our quota, writing the history file
296 /* Nothing to do, but die */
297 if (handle_interrupt
== 0)
301 if (handle_interrupt
)
304 stderror(ERR_SILENT
);
308 exitset
= oldexitset
;