xref: /csrg-svn/sys/kern/subr_prf.c (revision 285)
1*285Sbill /*	subr_prf.c	3.3	06/22/80	*/
231Sbill 
331Sbill #include "../h/param.h"
431Sbill #include "../h/systm.h"
531Sbill #include "../h/seg.h"
631Sbill #include "../h/buf.h"
731Sbill #include "../h/conf.h"
8*285Sbill #include "../h/mtpr.h"
931Sbill 
10*285Sbill #ifdef TRACE
11*285Sbill #define	TRCBUFS	4096
12*285Sbill char	trcbuf[TRCBUFS];
13*285Sbill char	*trcbufp = trcbuf;
14*285Sbill int	trcwrap;
15*285Sbill int	trcprt = TRCBUFS;
16*285Sbill #endif
17*285Sbill 
1831Sbill /*
1931Sbill  * In case console is off,
2031Sbill  * panicstr contains argument to last
2131Sbill  * call to panic.
2231Sbill  */
2331Sbill 
2431Sbill char	*panicstr;
2531Sbill 
2631Sbill /*
2731Sbill  * Scaled down version of C Library printf.
2831Sbill  * Only %s %u %d (==%u) %o %x %D are recognized.
2931Sbill  * Used to print diagnostic information
3031Sbill  * directly on console tty.
3131Sbill  * Since it is not interrupt driven,
3231Sbill  * all system activities are pretty much
3331Sbill  * suspended.
3431Sbill  * Printf should not be used for chit-chat.
3531Sbill  */
3631Sbill /*VARARGS1*/
3731Sbill printf(fmt, x1)
3831Sbill register char *fmt;
3931Sbill unsigned x1;
4031Sbill {
41*285Sbill 
42*285Sbill 	prf(fmt, &x1, 0);
43*285Sbill }
44*285Sbill 
45*285Sbill #ifdef TRACE
46*285Sbill trace(fmt, x1)
47*285Sbill register char *fmt;
48*285Sbill unsigned x1;
49*285Sbill {
50*285Sbill 
51*285Sbill 	prf(fmt, &x1, 1);
52*285Sbill }
53*285Sbill 
54*285Sbill #endif
55*285Sbill 
56*285Sbill prf(fmt, adx, trace)
57*285Sbill register char *fmt;
58*285Sbill register unsigned int *adx;
59*285Sbill {
6031Sbill 	register c;
6131Sbill 	char *s;
6231Sbill 
6331Sbill loop:
6431Sbill 	while((c = *fmt++) != '%') {
6531Sbill 		if(c == '\0')
6631Sbill 			return;
67*285Sbill 		putchar(c, trace);
6831Sbill 	}
6931Sbill 	c = *fmt++;
70*285Sbill 	if (c == 'X')
71*285Sbill 		printx((long)*adx, trace);
72*285Sbill 	else if (c == 'd' || c == 'u' || c == 'o' || c == 'x')
73*285Sbill 		printn((long)*adx, c=='o'? 8: (c=='x'? 16:10), trace);
74*285Sbill 	else if (c == 's') {
7531Sbill 		s = (char *)*adx;
76*285Sbill 		while (c = *s++)
77*285Sbill #ifdef TRACE
78*285Sbill 			if (trace) {
79*285Sbill 				*trcbufp++ = c;
80*285Sbill 				if (trcbufp >= &trcbuf[TRCBUFS]) {
81*285Sbill 					trcbufp = trcbuf;
82*285Sbill 					trcwrap = 1;
83*285Sbill 				}
84*285Sbill 			} else
85*285Sbill #endif
86*285Sbill 				putchar(c, trace);
8731Sbill 	} else if (c == 'D') {
88*285Sbill 		printn(*(long *)adx, 10, trace);
8931Sbill 		adx += (sizeof(long) / sizeof(int)) - 1;
9031Sbill 	}
9131Sbill 	adx++;
9231Sbill 	goto loop;
9331Sbill }
9431Sbill 
95*285Sbill printx(x, trace)
9631Sbill long x;
9731Sbill {
9831Sbill 	int i;
9931Sbill 
10031Sbill 	for (i = 0; i < 8; i++)
101*285Sbill 		putchar("0123456789ABCDEF"[(x>>((7-i)*4))&0xf], trace);
10231Sbill }
10331Sbill 
10431Sbill /*
10531Sbill  * Print an unsigned integer in base b.
10631Sbill  */
107*285Sbill printn(n, b, trace)
10831Sbill long n;
10931Sbill {
11031Sbill 	register long a;
11131Sbill 
11231Sbill 	if (n<0) {	/* shouldn't happen */
113*285Sbill 		putchar('-', trace);
11431Sbill 		n = -n;
11531Sbill 	}
11631Sbill 	if(a = n/b)
117*285Sbill 		printn(a, b, trace);
118*285Sbill 	putchar("0123456789ABCDEF"[(int)(n%b)], trace);
11931Sbill }
12031Sbill 
12131Sbill /*
12231Sbill  * Panic is called on unresolvable
12331Sbill  * fatal errors.
12431Sbill  * It syncs, prints "panic: mesg" and
12531Sbill  * then loops.
12631Sbill  */
12731Sbill panic(s)
12831Sbill char *s;
12931Sbill {
13031Sbill 	panicstr = s;
13131Sbill 	update();
13231Sbill 	printf("panic: %s\n", s);
13331Sbill 	spl0();
13431Sbill 	for(;;)
13531Sbill 		;
13631Sbill }
13731Sbill 
13831Sbill /*
13931Sbill  * prdev prints a warning message of the
14031Sbill  * form "mesg on dev x/y".
14131Sbill  * x and y are the major and minor parts of
14231Sbill  * the device argument.
14331Sbill  */
14431Sbill prdev(str, dev)
14531Sbill char *str;
14631Sbill dev_t dev;
14731Sbill {
14831Sbill 
14931Sbill 	printf("%s on dev %u/%u\n", str, major(dev), minor(dev));
15031Sbill }
15131Sbill 
15231Sbill /*
15331Sbill  * deverr prints a diagnostic from
15431Sbill  * a device driver.
15531Sbill  * It prints the device, block number,
15631Sbill  * and an octal word (usually some error
15731Sbill  * status register) passed as argument.
15831Sbill  */
15931Sbill deverror(bp, o1, o2)
16031Sbill register struct buf *bp;
16131Sbill {
16231Sbill 
16331Sbill 	prdev("err", bp->b_dev);
16431Sbill 	printf("bn=%d er=%x,%x\n", bp->b_blkno, o1,o2);
16531Sbill }
166*285Sbill 
167*285Sbill #ifdef TRACE
168*285Sbill dumptrc()
169*285Sbill {
170*285Sbill 	register char *cp;
171*285Sbill 	register int pos, nch;
172*285Sbill 
173*285Sbill 	nch = trcprt;
174*285Sbill 	if (nch < 0 || nch > TRCBUFS)
175*285Sbill 		nch = TRCBUFS;
176*285Sbill 	pos = (trcbufp - trcbuf) - nch;
177*285Sbill 	if (pos < 0)
178*285Sbill 		if (trcwrap)
179*285Sbill 			pos += TRCBUFS;
180*285Sbill 		else {
181*285Sbill 			nch += pos;
182*285Sbill 			pos = 0;
183*285Sbill 		}
184*285Sbill 	for (cp = &trcbuf[pos]; nch > 0; nch--) {
185*285Sbill 		putchar(*cp++, 0);
186*285Sbill 		if (cp >= &trcbuf[TRCBUFS])
187*285Sbill 			cp = trcbuf;
188*285Sbill 	}
189*285Sbill }
190*285Sbill #else
191*285Sbill /*ARGSUSED*/
192*285Sbill dumptrc(nch)
193*285Sbill 	int nch;
194*285Sbill {
195*285Sbill 
196*285Sbill }
197*285Sbill #endif
198*285Sbill 
199*285Sbill char	*msgbufp = msgbuf;	/* Next saved printf character */
200*285Sbill /*
201*285Sbill  * Print a character on console or in internal trace buffer.
202*285Sbill  * If destination is console then the last MSGBUFS characters
203*285Sbill  * are saved in msgbuf for inspection later.
204*285Sbill  */
205*285Sbill putchar(c, trace)
206*285Sbill register c;
207*285Sbill {
208*285Sbill 	register s, timo;
209*285Sbill 
210*285Sbill #ifdef TRACE
211*285Sbill 	if (trace) {
212*285Sbill 		*trcbufp++ = c;
213*285Sbill 		if (trcbufp >= &trcbuf[TRCBUFS]) {
214*285Sbill 			trcbufp = trcbuf;
215*285Sbill 			trcwrap = 1;
216*285Sbill 		}
217*285Sbill 		return;
218*285Sbill 	}
219*285Sbill #endif
220*285Sbill 	if (c != '\0' && c != '\r' && c != 0177) {
221*285Sbill 		*msgbufp++ = c;
222*285Sbill 		if (msgbufp >= &msgbuf[MSGBUFS])
223*285Sbill 			msgbufp = msgbuf;
224*285Sbill 	}
225*285Sbill 	if (c == 0)
226*285Sbill 		return;
227*285Sbill 	cnputc(c);
228*285Sbill }
229