1*3285Swnj /* subr_prf.c 4.16 81/03/17 */ 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; 13031Sbill } 13131Sbill adx++; 13231Sbill goto loop; 13331Sbill } 13431Sbill 1352781Swnj /* 1362781Swnj * Printn prints a number n in base b. 1372781Swnj * We don't use recursion to avoid deep kernel stacks. 1382781Swnj */ 1392377Swnj printn(n, b, touser) 1403101Swnj u_long n; 14131Sbill { 1422434Swnj char prbuf[11]; 1432377Swnj register char *cp; 14431Sbill 1452377Swnj if (b == 10 && (int)n < 0) { 1462377Swnj putchar('-', touser); 1472377Swnj n = (unsigned)(-(int)n); 14831Sbill } 1492434Swnj cp = prbuf; 1502377Swnj do { 1512377Swnj *cp++ = "0123456789abcdef"[n%b]; 1522377Swnj n /= b; 1532377Swnj } while (n); 1542377Swnj do 1552377Swnj putchar(*--cp, touser); 1562434Swnj while (cp > prbuf); 15731Sbill } 15831Sbill 15931Sbill /* 1601184Sbill * Panic is called on unresolvable fatal errors. 1612781Swnj * It prints "panic: mesg", and then reboots. 1622781Swnj * If we are called twice, then we avoid trying to 1632781Swnj * sync the disks as this often leads to recursive panics. 16431Sbill */ 16531Sbill panic(s) 1662781Swnj char *s; 16731Sbill { 1682781Swnj int bootopt = panicstr ? RB_AUTOBOOT : RB_AUTOBOOT|RB_NOSYNC; 1692377Swnj 17031Sbill panicstr = s; 171*3285Swnj printf("panic: %s\n", s); 1721787Sbill (void) spl0(); 1732781Swnj boot(RB_PANIC, bootopt); 17431Sbill } 17531Sbill 17631Sbill /* 1772941Swnj * Warn that a system table is full. 1782941Swnj */ 1792941Swnj tablefull(tab) 1802941Swnj char *tab; 1812941Swnj { 1822941Swnj 1832941Swnj printf("%s: table is full\n", tab); 1842941Swnj } 1852941Swnj 1862941Swnj /* 1872781Swnj * Hard error is the preface to plaintive error messages 1882941Swnj * about failing disk transfers. 1892781Swnj */ 1902941Swnj harderr(bp, cp) 1912678Swnj struct buf *bp; 1922941Swnj char *cp; 19331Sbill { 19431Sbill 1952941Swnj printf("%s%d%c: hard error sn%d ", cp, 1962941Swnj dkunit(bp), 'a'+(minor(bp->b_dev)&07), bp->b_blkno); 19731Sbill } 1982781Swnj 199285Sbill /* 2002377Swnj * Print a character on console or users terminal. 201285Sbill * If destination is console then the last MSGBUFS characters 202285Sbill * are saved in msgbuf for inspection later. 203285Sbill */ 2041785Sbill /*ARGSUSED*/ 2052377Swnj putchar(c, touser) 2062377Swnj register int c; 207285Sbill { 208285Sbill 2092377Swnj if (touser) { 2102377Swnj register struct tty *tp = u.u_ttyp; 2112360Skre 2122377Swnj if (tp && (tp->t_state&CARR_ON)) { 2132377Swnj register s = spl6(); 2142377Swnj if (c == '\n') 2152377Swnj ttyoutput('\r', tp); 2162360Skre ttyoutput(c, tp); 2172360Skre ttstart(tp); 2182360Skre splx(s); 2192360Skre } 2202360Skre return; 2212360Skre } 2222386Swnj if (c != '\0' && c != '\r' && c != 0177 && mfpr(MAPEN)) { 2232172Swnj if (msgbuf.msg_magic != MSG_MAGIC) { 2242172Swnj msgbuf.msg_bufx = 0; 2252172Swnj msgbuf.msg_magic = MSG_MAGIC; 2262172Swnj } 2272172Swnj if (msgbuf.msg_bufx < 0 || msgbuf.msg_bufx >= MSG_BSIZE) 2282172Swnj msgbuf.msg_bufx = 0; 2292172Swnj msgbuf.msg_bufc[msgbuf.msg_bufx++] = c; 230285Sbill } 231285Sbill if (c == 0) 232285Sbill return; 233285Sbill cnputc(c); 234285Sbill } 235