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