| 1 | /* subr_prf.c 4.16 81/03/17 */ |
| 2 | |
| 3 | #include "../h/param.h" |
| 4 | #include "../h/systm.h" |
| 5 | #include "../h/seg.h" |
| 6 | #include "../h/buf.h" |
| 7 | #include "../h/conf.h" |
| 8 | #include "../h/mtpr.h" |
| 9 | #include "../h/reboot.h" |
| 10 | #include "../h/vm.h" |
| 11 | #include "../h/msgbuf.h" |
| 12 | #include "../h/dir.h" |
| 13 | #include "../h/user.h" |
| 14 | #include "../h/tty.h" |
| 15 | |
| 16 | /* |
| 17 | * In case console is off, |
| 18 | * panicstr contains argument to last |
| 19 | * call to panic. |
| 20 | */ |
| 21 | char *panicstr; |
| 22 | |
| 23 | /* |
| 24 | * Scaled down version of C Library printf. |
| 25 | * Used to print diagnostic information directly on console tty. |
| 26 | * Since it is not interrupt driven, all system activities are |
| 27 | * suspended. Printf should not be used for chit-chat. |
| 28 | * |
| 29 | * One additional format: %b is supported to decode error registers. |
| 30 | * Usage is: |
| 31 | * printf("reg=%b\n", regval, "<base><arg>*"); |
| 32 | * Where <base> is the output base expressed as a control character, |
| 33 | * e.g. \10 gives octal; \20 gives hex. Each arg is a sequence of |
| 34 | * characters, the first of which gives the bit number to be inspected |
| 35 | * (origin 1), and the next characters (up to a control character, i.e. |
| 36 | * a character <= 32), give the name of the register. Thus |
| 37 | * printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); |
| 38 | * would produce output: |
| 39 | * reg=2<BITTWO,BITONE> |
| 40 | */ |
| 41 | /*VARARGS1*/ |
| 42 | printf(fmt, x1) |
| 43 | char *fmt; |
| 44 | unsigned x1; |
| 45 | { |
| 46 | |
| 47 | prf(fmt, &x1, 0); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Uprintf prints to the current user's terminal, |
| 52 | * guarantees not to sleep (so can be called by interrupt routines) |
| 53 | * and does no watermark checking - (so no verbose messages). |
| 54 | */ |
| 55 | /*VARARGS1*/ |
| 56 | uprintf(fmt, x1) |
| 57 | char *fmt; |
| 58 | unsigned x1; |
| 59 | { |
| 60 | |
| 61 | prf(fmt, &x1, 2); |
| 62 | } |
| 63 | |
| 64 | prf(fmt, adx, touser) |
| 65 | register char *fmt; |
| 66 | register u_int *adx; |
| 67 | { |
| 68 | register int b, c, i; |
| 69 | char *s; |
| 70 | int any; |
| 71 | |
| 72 | loop: |
| 73 | while ((c = *fmt++) != '%') { |
| 74 | if(c == '\0') |
| 75 | return; |
| 76 | putchar(c, touser); |
| 77 | } |
| 78 | again: |
| 79 | c = *fmt++; |
| 80 | /* THIS CODE IS VAX DEPENDENT IN HANDLING %l? AND %c */ |
| 81 | switch (c) { |
| 82 | |
| 83 | case 'l': |
| 84 | goto again; |
| 85 | case 'x': case 'X': |
| 86 | b = 16; |
| 87 | goto number; |
| 88 | case 'd': case 'D': |
| 89 | case 'u': /* what a joke */ |
| 90 | b = 10; |
| 91 | goto number; |
| 92 | case 'o': case 'O': |
| 93 | b = 8; |
| 94 | number: |
| 95 | printn((u_long)*adx, b, touser); |
| 96 | break; |
| 97 | case 'c': |
| 98 | b = *adx; |
| 99 | for (i = 24; i >= 0; i -= 8) |
| 100 | if (c = (b >> i) & 0x7f) |
| 101 | putchar(c, touser); |
| 102 | break; |
| 103 | case 'b': |
| 104 | b = *adx++; |
| 105 | s = (char *)*adx; |
| 106 | printn((u_long)b, *s++, touser); |
| 107 | any = 0; |
| 108 | if (b) { |
| 109 | putchar('<', touser); |
| 110 | while (i = *s++) { |
| 111 | if (b & (1 << (i-1))) { |
| 112 | if (any) |
| 113 | putchar(',', touser); |
| 114 | any = 1; |
| 115 | for (; (c = *s) > 32; s++) |
| 116 | putchar(c, touser); |
| 117 | } else |
| 118 | for (; *s > 32; s++) |
| 119 | ; |
| 120 | } |
| 121 | putchar('>', touser); |
| 122 | } |
| 123 | break; |
| 124 | |
| 125 | case 's': |
| 126 | s = (char *)*adx; |
| 127 | while (c = *s++) |
| 128 | putchar(c, touser); |
| 129 | break; |
| 130 | } |
| 131 | adx++; |
| 132 | goto loop; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Printn prints a number n in base b. |
| 137 | * We don't use recursion to avoid deep kernel stacks. |
| 138 | */ |
| 139 | printn(n, b, touser) |
| 140 | u_long n; |
| 141 | { |
| 142 | char prbuf[11]; |
| 143 | register char *cp; |
| 144 | |
| 145 | if (b == 10 && (int)n < 0) { |
| 146 | putchar('-', touser); |
| 147 | n = (unsigned)(-(int)n); |
| 148 | } |
| 149 | cp = prbuf; |
| 150 | do { |
| 151 | *cp++ = "0123456789abcdef"[n%b]; |
| 152 | n /= b; |
| 153 | } while (n); |
| 154 | do |
| 155 | putchar(*--cp, touser); |
| 156 | while (cp > prbuf); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Panic is called on unresolvable fatal errors. |
| 161 | * It prints "panic: mesg", and then reboots. |
| 162 | * If we are called twice, then we avoid trying to |
| 163 | * sync the disks as this often leads to recursive panics. |
| 164 | */ |
| 165 | panic(s) |
| 166 | char *s; |
| 167 | { |
| 168 | int bootopt = panicstr ? RB_AUTOBOOT : RB_AUTOBOOT|RB_NOSYNC; |
| 169 | |
| 170 | panicstr = s; |
| 171 | printf("panic: %s\n", s); |
| 172 | (void) spl0(); |
| 173 | boot(RB_PANIC, bootopt); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Warn that a system table is full. |
| 178 | */ |
| 179 | tablefull(tab) |
| 180 | char *tab; |
| 181 | { |
| 182 | |
| 183 | printf("%s: table is full\n", tab); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Hard error is the preface to plaintive error messages |
| 188 | * about failing disk transfers. |
| 189 | */ |
| 190 | harderr(bp, cp) |
| 191 | struct buf *bp; |
| 192 | char *cp; |
| 193 | { |
| 194 | |
| 195 | printf("%s%d%c: hard error sn%d ", cp, |
| 196 | dkunit(bp), 'a'+(minor(bp->b_dev)&07), bp->b_blkno); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Print a character on console or users terminal. |
| 201 | * If destination is console then the last MSGBUFS characters |
| 202 | * are saved in msgbuf for inspection later. |
| 203 | */ |
| 204 | /*ARGSUSED*/ |
| 205 | putchar(c, touser) |
| 206 | register int c; |
| 207 | { |
| 208 | |
| 209 | if (touser) { |
| 210 | register struct tty *tp = u.u_ttyp; |
| 211 | |
| 212 | if (tp && (tp->t_state&CARR_ON)) { |
| 213 | register s = spl6(); |
| 214 | if (c == '\n') |
| 215 | ttyoutput('\r', tp); |
| 216 | ttyoutput(c, tp); |
| 217 | ttstart(tp); |
| 218 | splx(s); |
| 219 | } |
| 220 | return; |
| 221 | } |
| 222 | if (c != '\0' && c != '\r' && c != 0177 && mfpr(MAPEN)) { |
| 223 | if (msgbuf.msg_magic != MSG_MAGIC) { |
| 224 | msgbuf.msg_bufx = 0; |
| 225 | msgbuf.msg_magic = MSG_MAGIC; |
| 226 | } |
| 227 | if (msgbuf.msg_bufx < 0 || msgbuf.msg_bufx >= MSG_BSIZE) |
| 228 | msgbuf.msg_bufx = 0; |
| 229 | msgbuf.msg_bufc[msgbuf.msg_bufx++] = c; |
| 230 | } |
| 231 | if (c == 0) |
| 232 | return; |
| 233 | cnputc(c); |
| 234 | } |