xref: /csrg-svn/sys/kern/subr_prf.c (revision 2678)
1 /*	subr_prf.c	4.10	02/25/81	*/
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"
8 #include "../h/mtpr.h"
9 #include "../h/reboot.h"
10 #include "../h/vm.h"
11 #include "../h/msgbuf.h"
12 #include "../h/dir.h"
13 #include "../h/user.h"
14 #include "../h/tty.h"
15 
16 /*
17  * In case console is off,
18  * panicstr contains argument to last
19  * call to panic.
20  */
21 
22 char	*panicstr;
23 
24 /*
25  * Scaled down version of C Library printf.
26  * Only %s %u %d (==%u) %o %x %D are recognized.
27  * Used to print diagnostic information
28  * directly on console tty.
29  * Since it is not interrupt driven,
30  * all system activities are pretty much
31  * suspended.
32  * Printf should not be used for chit-chat.
33  */
34 /*VARARGS1*/
35 printf(fmt, x1)
36 register char *fmt;
37 unsigned x1;
38 {
39 
40 	prf(fmt, &x1, 0);
41 }
42 
43 /*
44  * print to the current users terminal,
45  * guarantee not to sleep (so can be called by intr routine)
46  * no watermark checking - so no verbose messages
47  */
48 /*VARARGS1*/
49 uprintf(fmt, x1)
50 	char	*fmt;
51 	unsigned x1;
52 {
53 
54 	prf(fmt, &x1, 2);
55 }
56 
57 /* THIS CODE IS VAX DEPENDENT */
58 prf(fmt, adx, touser)
59 register char *fmt;
60 register u_int *adx;
61 {
62 	register int b, c, i;
63 	char *s;
64 	int any;
65 
66 loop:
67 	while ((c = *fmt++) != '%') {
68 		if(c == '\0')
69 			return;
70 		putchar(c, touser);
71 	}
72 again:
73 	c = *fmt++;
74 	switch (c) {
75 
76 	case 'l':
77 		goto again;
78 	case 'x': case 'X':
79 		b = 16;
80 		goto number;
81 	case 'd': case 'D':
82 	case 'u':		/* what a joke */
83 		b = 10;
84 		goto number;
85 	case 'o': case 'O':
86 		b = 8;
87 number:
88 		printn(*adx, b, touser);
89 		break;
90 	case 'c':
91 		b = *adx;
92 		for (i = 24; i >= 0; i -= 8)
93 			if (c = (b >> i) & 0x7f)
94 				putchar(c, touser);
95 		break;
96 	case 'b':
97 		b = *adx++;
98 		s = (char *)*adx;
99 		printn(b, *s++, touser);
100 		any = 0;
101 		if (b) {
102 			putchar('<', touser);
103 			while (i = *s++) {
104 				if (b & (1 << (i-1))) {
105 					if (any)
106 						putchar(',', touser);
107 					any = 1;
108 					for (; (c = *s) > 32; s++)
109 						putchar(c, touser);
110 				} else
111 					for (; *s > 32; s++)
112 						;
113 			}
114 			putchar('>', touser);
115 		}
116 		break;
117 
118 	case 's':
119 		s = (char *)*adx;
120 		while (c = *s++)
121 			putchar(c, touser);
122 		break;
123 	}
124 	adx++;
125 	goto loop;
126 }
127 /* END VAX DEPENDENT CODE */
128 
129 printn(n, b, touser)
130 	unsigned long n;
131 {
132 	char prbuf[11];
133 	register char *cp;
134 
135 	if (b == 10 && (int)n < 0) {
136 		putchar('-', touser);
137 		n = (unsigned)(-(int)n);
138 	}
139 	cp = prbuf;
140 	do {
141 		*cp++ = "0123456789abcdef"[n%b];
142 		n /= b;
143 	} while (n);
144 	do
145 		putchar(*--cp, touser);
146 	while (cp > prbuf);
147 }
148 
149 /*
150  * Panic is called on unresolvable fatal errors.
151  * It syncs, prints "panic: mesg", and then reboots.
152  */
153 panic(s)
154 char *s;
155 {
156 
157 	panicstr = s;
158 	printf("panic: %s\n", s);
159 	(void) spl0();
160 	for(;;)
161 		boot(RB_PANIC, RB_AUTOBOOT);
162 }
163 
164 /*
165  * prdev prints a warning message of the
166  * form "mesg on dev x/y".
167  * x and y are the major and minor parts of
168  * the device argument.
169  */
170 prdev(str, dev)
171 	char *str;
172 	dev_t dev;
173 {
174 
175 	printf("%s on dev %d/%d\n", str, major(dev), minor(dev));
176 }
177 
178 harderr(bp)
179 	struct buf *bp;
180 {
181 
182 	printf("hard err bn %d ", bp->b_blkno);
183 }
184 /*
185  * Print a character on console or users terminal.
186  * If destination is console then the last MSGBUFS characters
187  * are saved in msgbuf for inspection later.
188  */
189 /*ARGSUSED*/
190 putchar(c, touser)
191 	register int c;
192 {
193 
194 	if (touser) {
195 		register struct tty *tp = u.u_ttyp;
196 
197 		if (tp && (tp->t_state&CARR_ON)) {
198 			register s = spl6();
199 			if (c == '\n')
200 				ttyoutput('\r', tp);
201 			ttyoutput(c, tp);
202 			ttstart(tp);
203 			splx(s);
204 		}
205 		return;
206 	}
207 	if (c != '\0' && c != '\r' && c != 0177 && mfpr(MAPEN)) {
208 		if (msgbuf.msg_magic != MSG_MAGIC) {
209 			msgbuf.msg_bufx = 0;
210 			msgbuf.msg_magic = MSG_MAGIC;
211 		}
212 		if (msgbuf.msg_bufx < 0 || msgbuf.msg_bufx >= MSG_BSIZE)
213 			msgbuf.msg_bufx = 0;
214 		msgbuf.msg_bufc[msgbuf.msg_bufx++] = c;
215 	}
216 	if (c == 0)
217 		return;
218 	cnputc(c);
219 }
220