xref: /386bsd/usr/src/bin/csh/print.c (revision a2142627)
1 /*-
2  * Copyright (c) 1980, 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)print.c	5.11 (Berkeley) 6/8/91";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 #include <unistd.h>
40 #if __STDC__
41 # include <stdarg.h>
42 #else
43 # include <varargs.h>
44 #endif
45 
46 #include "csh.h"
47 #include "extern.h"
48 
49 extern int Tty_eight_bit;
50 extern int Tty_raw_mode;
51 extern Char GettingInput;
52 
53 int     lbuffed = 1;		/* true if line buffered */
54 
55 static void	p2dig __P((int));
56 
57 void
psecs(l)58 psecs(l)
59     long    l;
60 {
61     register int i;
62 
63     i = l / 3600;
64     if (i) {
65 	xprintf("%d:", i);
66 	i = l % 3600;
67 	p2dig(i / 60);
68 	goto minsec;
69     }
70     i = l;
71     xprintf("%d", i / 60);
72 minsec:
73     i %= 60;
74     xprintf(":");
75     p2dig(i);
76 }
77 
78 void
pcsecs(l)79 pcsecs(l)			/* PWP: print mm:ss.dd, l is in sec*100 */
80     long    l;
81 {
82     register int i;
83 
84     i = l / 360000;
85     if (i) {
86 	xprintf("%d:", i);
87 	i = (l % 360000) / 100;
88 	p2dig(i / 60);
89 	goto minsec;
90     }
91     i = l / 100;
92     xprintf("%d", i / 60);
93 minsec:
94     i %= 60;
95     xprintf(":");
96     p2dig(i);
97     xprintf(".");
98     p2dig((int) (l % 100));
99 }
100 
101 static void
p2dig(i)102 p2dig(i)
103     register int i;
104 {
105 
106     xprintf("%d%d", i / 10, i % 10);
107 }
108 
109 char    linbuf[2048];
110 char   *linp = linbuf;
111 bool    output_raw = 0;		/* PWP */
112 
113 void
xputchar(c)114 xputchar(c)
115     register int c;
116 {
117     c &= CHAR | QUOTE;
118     if (!output_raw && (c & QUOTE) == 0) {
119 	if (Iscntrl(c)) {
120 	    if (c != '\t' && c != '\n' && c != '\r') {
121 		xputchar('^');
122 		if (c == ASCII)
123 		    c = '?';
124 		else
125 		    c |= 0100;
126 	    }
127 	}
128 	else if (!Isprint(c)) {
129 	    xputchar('\\');
130 	    xputchar((((c >> 6) & 7) + '0'));
131 	    xputchar((((c >> 3) & 7) + '0'));
132 	    c = (c & 7) + '0';
133 	}
134 	(void) putraw(c);
135     }
136     else {
137 	c &= TRIM;
138 	(void) putpure(c);
139     }
140     if (lbuffed && (c & CHAR) == '\n')
141 	flush();
142 }
143 
144 int
putraw(c)145 putraw(c)
146     register int c;
147 {
148     return putpure(c);
149 }
150 
151 int
putpure(c)152 putpure(c)
153     register int c;
154 {
155     c &= CHAR;
156 
157     *linp++ = c;
158     if (linp >= &linbuf[sizeof linbuf - 10])
159 	flush();
160     return (1);
161 }
162 
163 void
draino()164 draino()
165 {
166     linp = linbuf;
167 }
168 
169 void
flush()170 flush()
171 {
172     register int unit;
173 
174     /* int lmode; */
175 
176     if (linp == linbuf)
177 	return;
178 #ifdef notdef
179     if (linp < &linbuf[sizeof linbuf - 10])
180 	return;
181 #endif
182     if (haderr)
183 	unit = didfds ? 2 : SHDIAG;
184     else
185 	unit = didfds ? 1 : SHOUT;
186     (void) write(unit, linbuf, (size_t) (linp - linbuf));
187     linp = linbuf;
188 }
189