xref: /csrg-svn/sys/kern/subr_prf.c (revision 2386)
1*2386Swnj /*	subr_prf.c	4.8	02/08/81	*/
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 
2231Sbill char	*panicstr;
2331Sbill 
2431Sbill /*
2531Sbill  * Scaled down version of C Library printf.
2631Sbill  * Only %s %u %d (==%u) %o %x %D are recognized.
2731Sbill  * Used to print diagnostic information
2831Sbill  * directly on console tty.
2931Sbill  * Since it is not interrupt driven,
3031Sbill  * all system activities are pretty much
3131Sbill  * suspended.
3231Sbill  * Printf should not be used for chit-chat.
3331Sbill  */
3431Sbill /*VARARGS1*/
3531Sbill printf(fmt, x1)
3631Sbill register char *fmt;
3731Sbill unsigned x1;
3831Sbill {
39285Sbill 
40285Sbill 	prf(fmt, &x1, 0);
41285Sbill }
42285Sbill 
432377Swnj /*
442377Swnj  * print to the current users terminal,
452377Swnj  * guarantee not to sleep (so can be called by intr routine)
462377Swnj  * no watermark checking - so no verbose messages
472377Swnj  */
482377Swnj /*VARARGS1*/
492377Swnj uprintf(fmt, x1)
502377Swnj 	char	*fmt;
512377Swnj 	unsigned x1;
52285Sbill {
53285Sbill 
542377Swnj 	prf(fmt, &x1, 2);
55285Sbill }
56285Sbill 
572377Swnj /* THIS CODE IS VAX DEPENDENT */
582377Swnj prf(fmt, adx, touser)
59285Sbill register char *fmt;
602377Swnj register u_int *adx;
61285Sbill {
622377Swnj 	register int b, c;
6331Sbill 	char *s;
6431Sbill 
6531Sbill loop:
662377Swnj 	while ((c = *fmt++) != '%') {
6731Sbill 		if(c == '\0')
6831Sbill 			return;
692377Swnj 		putchar(c, touser);
7031Sbill 	}
712377Swnj again:
7231Sbill 	c = *fmt++;
732377Swnj 	switch (c) {
742377Swnj 
752377Swnj 	case 'l':
762377Swnj 		goto again;
772377Swnj 	case 'x': case 'X':
782377Swnj 		b = 16;
792377Swnj 		goto number;
802377Swnj 	case 'd': case 'D':
812377Swnj 	case 'u':		/* what a joke */
822377Swnj 		b = 10;
832377Swnj 		goto number;
842377Swnj 	case 'o': case 'O':
852377Swnj 		b = 8;
862377Swnj number:
872377Swnj 		printn(*adx, b, touser);
882377Swnj 		break;
892377Swnj 	case 'c':
902377Swnj 		c = *adx;
912377Swnj 		while (c) {
922377Swnj 			putchar(c&0x7f, touser);
932377Swnj 			c >>= 8;
942377Swnj 		}
952377Swnj 		break;
962377Swnj 	case 's':
9731Sbill 		s = (char *)*adx;
98285Sbill 		while (c = *s++)
992377Swnj 			putchar(c, touser);
1002377Swnj 		break;
10131Sbill 	}
10231Sbill 	adx++;
10331Sbill 	goto loop;
10431Sbill }
1052377Swnj /* END VAX DEPENDENT CODE */
10631Sbill 
1072377Swnj printn(n, b, touser)
1082377Swnj 	unsigned long n;
10931Sbill {
1102377Swnj 	char buf[11];
1112377Swnj 	register char *cp;
11231Sbill 
1132377Swnj 	if (b == 10 && (int)n < 0) {
1142377Swnj 		putchar('-', touser);
1152377Swnj 		n = (unsigned)(-(int)n);
11631Sbill 	}
1172377Swnj 	cp = buf;
1182377Swnj 	do {
1192377Swnj 		*cp++ = "0123456789abcdef"[n%b];
1202377Swnj 		n /= b;
1212377Swnj 	} while (n);
1222377Swnj 	do
1232377Swnj 		putchar(*--cp, touser);
1242377Swnj 	while (cp > buf);
12531Sbill }
12631Sbill 
12731Sbill /*
1281184Sbill  * Panic is called on unresolvable fatal errors.
1291184Sbill  * It syncs, prints "panic: mesg", and then reboots.
13031Sbill  */
13131Sbill panic(s)
13231Sbill char *s;
13331Sbill {
1342377Swnj 
13531Sbill 	panicstr = s;
13631Sbill 	printf("panic: %s\n", s);
1371787Sbill 	(void) spl0();
13831Sbill 	for(;;)
1391401Sbill 		boot(RB_PANIC, RB_AUTOBOOT);
14031Sbill }
14131Sbill 
14231Sbill /*
14331Sbill  * prdev prints a warning message of the
14431Sbill  * form "mesg on dev x/y".
14531Sbill  * x and y are the major and minor parts of
14631Sbill  * the device argument.
14731Sbill  */
14831Sbill prdev(str, dev)
1492377Swnj 	char *str;
1502377Swnj 	dev_t dev;
15131Sbill {
15231Sbill 
1532377Swnj 	printf("%s on dev %d/%d\n", str, major(dev), minor(dev));
15431Sbill }
15531Sbill 
15631Sbill /*
15731Sbill  * deverr prints a diagnostic from
15831Sbill  * a device driver.
15931Sbill  * It prints the device, block number,
16031Sbill  * and an octal word (usually some error
16131Sbill  * status register) passed as argument.
16231Sbill  */
16331Sbill deverror(bp, o1, o2)
1642377Swnj 	register struct buf *bp;
16531Sbill {
16631Sbill 
1672360Skre 	printf("bn=%d er=%x,%x", bp->b_blkno, o1,o2);
1682360Skre 	prdev("", bp->b_dev);
16931Sbill }
170285Sbill 
171285Sbill /*
1722377Swnj  * Print a character on console or users terminal.
173285Sbill  * If destination is console then the last MSGBUFS characters
174285Sbill  * are saved in msgbuf for inspection later.
175285Sbill  */
1761785Sbill /*ARGSUSED*/
1772377Swnj putchar(c, touser)
1782377Swnj 	register int c;
179285Sbill {
180285Sbill 
1812377Swnj 	if (touser) {
1822377Swnj 		register struct tty *tp = u.u_ttyp;
1832360Skre 
1842377Swnj 		if (tp && (tp->t_state&CARR_ON)) {
1852377Swnj 			register s = spl6();
1862377Swnj 			if (c == '\n')
1872377Swnj 				ttyoutput('\r', tp);
1882360Skre 			ttyoutput(c, tp);
1892360Skre 			ttstart(tp);
1902360Skre 			splx(s);
1912360Skre 		}
1922360Skre 		return;
1932360Skre 	}
194*2386Swnj 	if (c != '\0' && c != '\r' && c != 0177 && mfpr(MAPEN)) {
1952172Swnj 		if (msgbuf.msg_magic != MSG_MAGIC) {
1962172Swnj 			msgbuf.msg_bufx = 0;
1972172Swnj 			msgbuf.msg_magic = MSG_MAGIC;
1982172Swnj 		}
1992172Swnj 		if (msgbuf.msg_bufx < 0 || msgbuf.msg_bufx >= MSG_BSIZE)
2002172Swnj 			msgbuf.msg_bufx = 0;
2012172Swnj 		msgbuf.msg_bufc[msgbuf.msg_bufx++] = c;
202285Sbill 	}
203285Sbill 	if (c == 0)
204285Sbill 		return;
205285Sbill 	cnputc(c);
206285Sbill }
207