xref: /csrg-svn/sys/kern/subr_prf.c (revision 3736)
1*3736Sroot /*	subr_prf.c	4.17	81/05/11	*/
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"
8285Sbill #include "../h/mtpr.h"
91184Sbill #include "../h/reboot.h"
102172Swnj #include "../h/vm.h"
112172Swnj #include "../h/msgbuf.h"
122360Skre #include "../h/dir.h"
132360Skre #include "../h/user.h"
142360Skre #include "../h/tty.h"
1531Sbill 
1631Sbill /*
1731Sbill  * In case console is off,
1831Sbill  * panicstr contains argument to last
1931Sbill  * call to panic.
2031Sbill  */
2131Sbill char	*panicstr;
2231Sbill 
2331Sbill /*
2431Sbill  * Scaled down version of C Library printf.
252781Swnj  * Used to print diagnostic information directly on console tty.
262781Swnj  * Since it is not interrupt driven, all system activities are
272781Swnj  * suspended.  Printf should not be used for chit-chat.
282781Swnj  *
292781Swnj  * One additional format: %b is supported to decode error registers.
302781Swnj  * Usage is:
312781Swnj  *	printf("reg=%b\n", regval, "<base><arg>*");
322781Swnj  * Where <base> is the output base expressed as a control character,
332781Swnj  * e.g. \10 gives octal; \20 gives hex.  Each arg is a sequence of
342781Swnj  * characters, the first of which gives the bit number to be inspected
352781Swnj  * (origin 1), and the next characters (up to a control character, i.e.
362781Swnj  * a character <= 32), give the name of the register.  Thus
372781Swnj  *	printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
382781Swnj  * would produce output:
392781Swnj  *	reg=2<BITTWO,BITONE>
4031Sbill  */
4131Sbill /*VARARGS1*/
4231Sbill printf(fmt, x1)
432781Swnj 	char *fmt;
442781Swnj 	unsigned x1;
4531Sbill {
46285Sbill 
47285Sbill 	prf(fmt, &x1, 0);
48285Sbill }
49285Sbill 
502377Swnj /*
512781Swnj  * Uprintf prints to the current user's terminal,
522781Swnj  * guarantees not to sleep (so can be called by interrupt routines)
532781Swnj  * and does no watermark checking - (so no verbose messages).
542377Swnj  */
552377Swnj /*VARARGS1*/
562377Swnj uprintf(fmt, x1)
572781Swnj 	char *fmt;
582377Swnj 	unsigned x1;
59285Sbill {
60285Sbill 
612377Swnj 	prf(fmt, &x1, 2);
62285Sbill }
63285Sbill 
642377Swnj prf(fmt, adx, touser)
652781Swnj 	register char *fmt;
662781Swnj 	register u_int *adx;
67285Sbill {
682434Swnj 	register int b, c, i;
6931Sbill 	char *s;
702678Swnj 	int any;
7131Sbill 
7231Sbill loop:
732377Swnj 	while ((c = *fmt++) != '%') {
7431Sbill 		if(c == '\0')
7531Sbill 			return;
762377Swnj 		putchar(c, touser);
7731Sbill 	}
782377Swnj again:
7931Sbill 	c = *fmt++;
802781Swnj 	/* THIS CODE IS VAX DEPENDENT IN HANDLING %l? AND %c */
812377Swnj 	switch (c) {
822377Swnj 
832377Swnj 	case 'l':
842377Swnj 		goto again;
852377Swnj 	case 'x': case 'X':
862377Swnj 		b = 16;
872377Swnj 		goto number;
882377Swnj 	case 'd': case 'D':
892377Swnj 	case 'u':		/* what a joke */
902377Swnj 		b = 10;
912377Swnj 		goto number;
922377Swnj 	case 'o': case 'O':
932377Swnj 		b = 8;
942377Swnj number:
953101Swnj 		printn((u_long)*adx, b, touser);
962377Swnj 		break;
972377Swnj 	case 'c':
982434Swnj 		b = *adx;
992434Swnj 		for (i = 24; i >= 0; i -= 8)
1002434Swnj 			if (c = (b >> i) & 0x7f)
1012434Swnj 				putchar(c, touser);
1022377Swnj 		break;
1032678Swnj 	case 'b':
1042678Swnj 		b = *adx++;
1052678Swnj 		s = (char *)*adx;
1063101Swnj 		printn((u_long)b, *s++, touser);
1072678Swnj 		any = 0;
1082678Swnj 		if (b) {
1092678Swnj 			putchar('<', touser);
1102678Swnj 			while (i = *s++) {
1112678Swnj 				if (b & (1 << (i-1))) {
1122678Swnj 					if (any)
1132678Swnj 						putchar(',', touser);
1142678Swnj 					any = 1;
1152678Swnj 					for (; (c = *s) > 32; s++)
1162678Swnj 						putchar(c, touser);
1172678Swnj 				} else
1182678Swnj 					for (; *s > 32; s++)
1192678Swnj 						;
1202678Swnj 			}
1212678Swnj 			putchar('>', touser);
1222678Swnj 		}
1232678Swnj 		break;
1242678Swnj 
1252377Swnj 	case 's':
12631Sbill 		s = (char *)*adx;
127285Sbill 		while (c = *s++)
1282377Swnj 			putchar(c, touser);
1292377Swnj 		break;
130*3736Sroot 
131*3736Sroot 	case '%':
132*3736Sroot 		putchar('%', touser);
133*3736Sroot 		break;
13431Sbill 	}
13531Sbill 	adx++;
13631Sbill 	goto loop;
13731Sbill }
13831Sbill 
1392781Swnj /*
1402781Swnj  * Printn prints a number n in base b.
1412781Swnj  * We don't use recursion to avoid deep kernel stacks.
1422781Swnj  */
1432377Swnj printn(n, b, touser)
1443101Swnj 	u_long n;
14531Sbill {
1462434Swnj 	char prbuf[11];
1472377Swnj 	register char *cp;
14831Sbill 
1492377Swnj 	if (b == 10 && (int)n < 0) {
1502377Swnj 		putchar('-', touser);
1512377Swnj 		n = (unsigned)(-(int)n);
15231Sbill 	}
1532434Swnj 	cp = prbuf;
1542377Swnj 	do {
1552377Swnj 		*cp++ = "0123456789abcdef"[n%b];
1562377Swnj 		n /= b;
1572377Swnj 	} while (n);
1582377Swnj 	do
1592377Swnj 		putchar(*--cp, touser);
1602434Swnj 	while (cp > prbuf);
16131Sbill }
16231Sbill 
16331Sbill /*
1641184Sbill  * Panic is called on unresolvable fatal errors.
1652781Swnj  * It prints "panic: mesg", and then reboots.
1662781Swnj  * If we are called twice, then we avoid trying to
1672781Swnj  * sync the disks as this often leads to recursive panics.
16831Sbill  */
16931Sbill panic(s)
1702781Swnj 	char *s;
17131Sbill {
1722781Swnj 	int bootopt = panicstr ? RB_AUTOBOOT : RB_AUTOBOOT|RB_NOSYNC;
1732377Swnj 
17431Sbill 	panicstr = s;
1753285Swnj 	printf("panic: %s\n", s);
1761787Sbill 	(void) spl0();
1772781Swnj 	boot(RB_PANIC, bootopt);
17831Sbill }
17931Sbill 
18031Sbill /*
1812941Swnj  * Warn that a system table is full.
1822941Swnj  */
1832941Swnj tablefull(tab)
1842941Swnj 	char *tab;
1852941Swnj {
1862941Swnj 
1872941Swnj 	printf("%s: table is full\n", tab);
1882941Swnj }
1892941Swnj 
1902941Swnj /*
1912781Swnj  * Hard error is the preface to plaintive error messages
1922941Swnj  * about failing disk transfers.
1932781Swnj  */
1942941Swnj harderr(bp, cp)
1952678Swnj 	struct buf *bp;
1962941Swnj 	char *cp;
19731Sbill {
19831Sbill 
1992941Swnj 	printf("%s%d%c: hard error sn%d ", cp,
2002941Swnj 	    dkunit(bp), 'a'+(minor(bp->b_dev)&07), bp->b_blkno);
20131Sbill }
2022781Swnj 
203285Sbill /*
2042377Swnj  * Print a character on console or users terminal.
205285Sbill  * If destination is console then the last MSGBUFS characters
206285Sbill  * are saved in msgbuf for inspection later.
207285Sbill  */
2081785Sbill /*ARGSUSED*/
2092377Swnj putchar(c, touser)
2102377Swnj 	register int c;
211285Sbill {
212285Sbill 
2132377Swnj 	if (touser) {
2142377Swnj 		register struct tty *tp = u.u_ttyp;
2152360Skre 
2162377Swnj 		if (tp && (tp->t_state&CARR_ON)) {
2172377Swnj 			register s = spl6();
2182377Swnj 			if (c == '\n')
2192377Swnj 				ttyoutput('\r', tp);
2202360Skre 			ttyoutput(c, tp);
2212360Skre 			ttstart(tp);
2222360Skre 			splx(s);
2232360Skre 		}
2242360Skre 		return;
2252360Skre 	}
2262386Swnj 	if (c != '\0' && c != '\r' && c != 0177 && mfpr(MAPEN)) {
2272172Swnj 		if (msgbuf.msg_magic != MSG_MAGIC) {
2282172Swnj 			msgbuf.msg_bufx = 0;
2292172Swnj 			msgbuf.msg_magic = MSG_MAGIC;
2302172Swnj 		}
2312172Swnj 		if (msgbuf.msg_bufx < 0 || msgbuf.msg_bufx >= MSG_BSIZE)
2322172Swnj 			msgbuf.msg_bufx = 0;
2332172Swnj 		msgbuf.msg_bufc[msgbuf.msg_bufx++] = c;
234285Sbill 	}
235285Sbill 	if (c == 0)
236285Sbill 		return;
237285Sbill 	cnputc(c);
238285Sbill }
239