4.2 distribution
[unix-history] / usr / src / sys / kern / subr_prf.c
CommitLineData
b11be056 1/* subr_prf.c 6.1 83/07/29 */
8cbb423c
BJ
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/seg.h"
6#include "../h/buf.h"
7#include "../h/conf.h"
0dc06be8 8#include "../h/reboot.h"
90f8d91f
BJ
9#include "../h/vm.h"
10#include "../h/msgbuf.h"
d11b28dc
RE
11#include "../h/dir.h"
12#include "../h/user.h"
961945a8 13#include "../h/proc.h"
d11b28dc 14#include "../h/tty.h"
96d38f03 15
e3d5bfde
SL
16#ifdef vax
17#include "../vax/mtpr.h"
18#endif
19
8cbb423c
BJ
20/*
21 * In case console is off,
22 * panicstr contains argument to last
23 * call to panic.
24 */
8cbb423c
BJ
25char *panicstr;
26
27/*
28 * Scaled down version of C Library printf.
b725a0ca
BJ
29 * Used to print diagnostic information directly on console tty.
30 * Since it is not interrupt driven, all system activities are
31 * suspended. Printf should not be used for chit-chat.
32 *
33 * One additional format: %b is supported to decode error registers.
34 * Usage is:
35 * printf("reg=%b\n", regval, "<base><arg>*");
36 * Where <base> is the output base expressed as a control character,
37 * e.g. \10 gives octal; \20 gives hex. Each arg is a sequence of
38 * characters, the first of which gives the bit number to be inspected
39 * (origin 1), and the next characters (up to a control character, i.e.
40 * a character <= 32), give the name of the register. Thus
41 * printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
42 * would produce output:
43 * reg=2<BITTWO,BITONE>
8cbb423c
BJ
44 */
45/*VARARGS1*/
46printf(fmt, x1)
b725a0ca
BJ
47 char *fmt;
48 unsigned x1;
96d38f03
BJ
49{
50
51 prf(fmt, &x1, 0);
52}
53
843267b1 54/*
b725a0ca
BJ
55 * Uprintf prints to the current user's terminal,
56 * guarantees not to sleep (so can be called by interrupt routines)
57 * and does no watermark checking - (so no verbose messages).
843267b1
BJ
58 */
59/*VARARGS1*/
60uprintf(fmt, x1)
b725a0ca 61 char *fmt;
843267b1 62 unsigned x1;
96d38f03
BJ
63{
64
843267b1 65 prf(fmt, &x1, 2);
96d38f03
BJ
66}
67
843267b1 68prf(fmt, adx, touser)
b725a0ca
BJ
69 register char *fmt;
70 register u_int *adx;
8cbb423c 71{
d5726689 72 register int b, c, i;
8cbb423c 73 char *s;
3c79e4ff 74 int any;
8cbb423c 75
8cbb423c 76loop:
843267b1 77 while ((c = *fmt++) != '%') {
8cbb423c
BJ
78 if(c == '\0')
79 return;
843267b1 80 putchar(c, touser);
8cbb423c 81 }
843267b1 82again:
8cbb423c 83 c = *fmt++;
b725a0ca 84 /* THIS CODE IS VAX DEPENDENT IN HANDLING %l? AND %c */
843267b1
BJ
85 switch (c) {
86
87 case 'l':
88 goto again;
89 case 'x': case 'X':
90 b = 16;
91 goto number;
92 case 'd': case 'D':
93 case 'u': /* what a joke */
94 b = 10;
95 goto number;
96 case 'o': case 'O':
97 b = 8;
98number:
a0eab615 99 printn((u_long)*adx, b, touser);
843267b1
BJ
100 break;
101 case 'c':
d5726689
BJ
102 b = *adx;
103 for (i = 24; i >= 0; i -= 8)
104 if (c = (b >> i) & 0x7f)
105 putchar(c, touser);
843267b1 106 break;
3c79e4ff
BJ
107 case 'b':
108 b = *adx++;
109 s = (char *)*adx;
a0eab615 110 printn((u_long)b, *s++, touser);
3c79e4ff
BJ
111 any = 0;
112 if (b) {
113 putchar('<', touser);
114 while (i = *s++) {
115 if (b & (1 << (i-1))) {
116 if (any)
117 putchar(',', touser);
118 any = 1;
119 for (; (c = *s) > 32; s++)
120 putchar(c, touser);
121 } else
122 for (; *s > 32; s++)
123 ;
124 }
1ce587f2
BJ
125 if (any)
126 putchar('>', touser);
3c79e4ff
BJ
127 }
128 break;
129
843267b1 130 case 's':
8cbb423c 131 s = (char *)*adx;
96d38f03 132 while (c = *s++)
843267b1
BJ
133 putchar(c, touser);
134 break;
2bb8d359
BJ
135
136 case '%':
137 putchar('%', touser);
138 break;
8cbb423c
BJ
139 }
140 adx++;
141 goto loop;
142}
143
b725a0ca
BJ
144/*
145 * Printn prints a number n in base b.
146 * We don't use recursion to avoid deep kernel stacks.
147 */
843267b1 148printn(n, b, touser)
a0eab615 149 u_long n;
8cbb423c 150{
d5726689 151 char prbuf[11];
843267b1 152 register char *cp;
8cbb423c 153
843267b1
BJ
154 if (b == 10 && (int)n < 0) {
155 putchar('-', touser);
156 n = (unsigned)(-(int)n);
8cbb423c 157 }
d5726689 158 cp = prbuf;
843267b1
BJ
159 do {
160 *cp++ = "0123456789abcdef"[n%b];
161 n /= b;
162 } while (n);
163 do
164 putchar(*--cp, touser);
d5726689 165 while (cp > prbuf);
8cbb423c
BJ
166}
167
168/*
0dc06be8 169 * Panic is called on unresolvable fatal errors.
b725a0ca
BJ
170 * It prints "panic: mesg", and then reboots.
171 * If we are called twice, then we avoid trying to
172 * sync the disks as this often leads to recursive panics.
8cbb423c
BJ
173 */
174panic(s)
b725a0ca 175 char *s;
8cbb423c 176{
68503f9a 177 int bootopt = RB_AUTOBOOT;
843267b1 178
68503f9a
BJ
179 if (panicstr)
180 bootopt |= RB_NOSYNC;
cddce008 181 else {
961945a8 182 panicstr = s;
961945a8 183 }
a9c526c1 184 printf("panic: %s\n", s);
b725a0ca 185 boot(RB_PANIC, bootopt);
8cbb423c
BJ
186}
187
fdd11a14
BJ
188/*
189 * Warn that a system table is full.
190 */
191tablefull(tab)
192 char *tab;
193{
194
195 printf("%s: table is full\n", tab);
196}
197
b725a0ca
BJ
198/*
199 * Hard error is the preface to plaintive error messages
fdd11a14 200 * about failing disk transfers.
b725a0ca 201 */
fdd11a14 202harderr(bp, cp)
3c79e4ff 203 struct buf *bp;
fdd11a14 204 char *cp;
8cbb423c
BJ
205{
206
fdd11a14
BJ
207 printf("%s%d%c: hard error sn%d ", cp,
208 dkunit(bp), 'a'+(minor(bp->b_dev)&07), bp->b_blkno);
8cbb423c 209}
b725a0ca 210
96d38f03 211/*
843267b1 212 * Print a character on console or users terminal.
96d38f03
BJ
213 * If destination is console then the last MSGBUFS characters
214 * are saved in msgbuf for inspection later.
215 */
49c84d3f 216/*ARGSUSED*/
843267b1
BJ
217putchar(c, touser)
218 register int c;
96d38f03 219{
96d38f03 220
843267b1
BJ
221 if (touser) {
222 register struct tty *tp = u.u_ttyp;
d11b28dc 223
c30cd845 224 if (tp && (tp->t_state&TS_CARR_ON)) {
843267b1
BJ
225 register s = spl6();
226 if (c == '\n')
2752c877
BJ
227 (void) ttyoutput('\r', tp);
228 (void) ttyoutput(c, tp);
d11b28dc
RE
229 ttstart(tp);
230 splx(s);
231 }
232 return;
233 }
cddce008 234 if (c != '\0' && c != '\r' && c != 0177
b4e32d36 235#ifdef vax
cddce008
BJ
236 && mfpr(MAPEN)
237#endif
238 ) {
90f8d91f 239 if (msgbuf.msg_magic != MSG_MAGIC) {
76db4195
SL
240 register int i;
241
90f8d91f
BJ
242 msgbuf.msg_bufx = 0;
243 msgbuf.msg_magic = MSG_MAGIC;
76db4195
SL
244 for (i=0; i < MSG_BSIZE; i++)
245 msgbuf.msg_bufc[i] = 0;
90f8d91f
BJ
246 }
247 if (msgbuf.msg_bufx < 0 || msgbuf.msg_bufx >= MSG_BSIZE)
248 msgbuf.msg_bufx = 0;
249 msgbuf.msg_bufc[msgbuf.msg_bufx++] = c;
96d38f03
BJ
250 }
251 if (c == 0)
252 return;
253 cnputc(c);
254}