1*3101Swnj /* subr_prf.c 4.14 03/09/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 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: 95*3101Swnj 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; 106*3101Swnj 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) 140*3101Swnj 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; 1711787Sbill (void) spl0(); 1722781Swnj boot(RB_PANIC, bootopt); 17331Sbill } 17431Sbill 17531Sbill /* 1762941Swnj * Warn that a system table is full. 1772941Swnj */ 1782941Swnj tablefull(tab) 1792941Swnj char *tab; 1802941Swnj { 1812941Swnj 1822941Swnj printf("%s: table is full\n", tab); 1832941Swnj } 1842941Swnj 1852941Swnj /* 1862781Swnj * Hard error is the preface to plaintive error messages 1872941Swnj * about failing disk transfers. 1882781Swnj */ 1892941Swnj harderr(bp, cp) 1902678Swnj struct buf *bp; 1912941Swnj char *cp; 19231Sbill { 19331Sbill 1942941Swnj printf("%s%d%c: hard error sn%d ", cp, 1952941Swnj dkunit(bp), 'a'+(minor(bp->b_dev)&07), bp->b_blkno); 19631Sbill } 1972781Swnj 198285Sbill /* 1992377Swnj * Print a character on console or users terminal. 200285Sbill * If destination is console then the last MSGBUFS characters 201285Sbill * are saved in msgbuf for inspection later. 202285Sbill */ 2031785Sbill /*ARGSUSED*/ 2042377Swnj putchar(c, touser) 2052377Swnj register int c; 206285Sbill { 207285Sbill 2082377Swnj if (touser) { 2092377Swnj register struct tty *tp = u.u_ttyp; 2102360Skre 2112377Swnj if (tp && (tp->t_state&CARR_ON)) { 2122377Swnj register s = spl6(); 2132377Swnj if (c == '\n') 2142377Swnj ttyoutput('\r', tp); 2152360Skre ttyoutput(c, tp); 2162360Skre ttstart(tp); 2172360Skre splx(s); 2182360Skre } 2192360Skre return; 2202360Skre } 2212386Swnj if (c != '\0' && c != '\r' && c != 0177 && mfpr(MAPEN)) { 2222172Swnj if (msgbuf.msg_magic != MSG_MAGIC) { 2232172Swnj msgbuf.msg_bufx = 0; 2242172Swnj msgbuf.msg_magic = MSG_MAGIC; 2252172Swnj } 2262172Swnj if (msgbuf.msg_bufx < 0 || msgbuf.msg_bufx >= MSG_BSIZE) 2272172Swnj msgbuf.msg_bufx = 0; 2282172Swnj msgbuf.msg_bufc[msgbuf.msg_bufx++] = c; 229285Sbill } 230285Sbill if (c == 0) 231285Sbill return; 232285Sbill cnputc(c); 233285Sbill } 234