1: /*
   2:  * Copyright (c) 1982, 1986 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  *
   6:  *	@(#)prf.c	7.1 (Berkeley) 6/5/86
   7:  */
   8: 
   9: #include "../h/param.h"
  10: 
  11: #include "../vax/mtpr.h"
  12: #include "../vax/cons.h"
  13: 
  14: /*
  15:  * Scaled down version of C Library printf.
  16:  * Used to print diagnostic information directly on console tty.
  17:  * Since it is not interrupt driven, all system activities are
  18:  * suspended.  Printf should not be used for chit-chat.
  19:  *
  20:  * One additional format: %b is supported to decode error registers.
  21:  * Usage is:
  22:  *	printf("reg=%b\n", regval, "<base><arg>*");
  23:  * Where <base> is the output base expressed as a control character,
  24:  * e.g. \10 gives octal; \20 gives hex.  Each arg is a sequence of
  25:  * characters, the first of which gives the bit number to be inspected
  26:  * (origin 1), and the next characters (up to a control character, i.e.
  27:  * a character <= 32), give the name of the register.  Thus
  28:  *	printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
  29:  * would produce output:
  30:  *	reg=2<BITTWO,BITONE>
  31:  */
  32: /*VARARGS1*/
  33: printf(fmt, x1)
  34:     char *fmt;
  35:     unsigned x1;
  36: {
  37: 
  38:     prf(fmt, &x1);
  39: }
  40: 
  41: prf(fmt, adx)
  42:     register char *fmt;
  43:     register u_int *adx;
  44: {
  45:     register int b, c, i;
  46:     char *s;
  47:     int any;
  48: 
  49: loop:
  50:     while ((c = *fmt++) != '%') {
  51:         if(c == '\0')
  52:             return;
  53:         putchar(c);
  54:     }
  55: again:
  56:     c = *fmt++;
  57:     /* THIS CODE IS VAX DEPENDENT IN HANDLING %l? AND %c */
  58:     switch (c) {
  59: 
  60:     case 'l':
  61:         goto again;
  62:     case 'x': case 'X':
  63:         b = 16;
  64:         goto number;
  65:     case 'd': case 'D':
  66:     case 'u':       /* what a joke */
  67:         b = 10;
  68:         goto number;
  69:     case 'o': case 'O':
  70:         b = 8;
  71: number:
  72:         printn((u_long)*adx, b);
  73:         break;
  74:     case 'c':
  75:         b = *adx;
  76:         for (i = 24; i >= 0; i -= 8)
  77:             if (c = (b >> i) & 0x7f)
  78:                 putchar(c);
  79:         break;
  80:     case 'b':
  81:         b = *adx++;
  82:         s = (char *)*adx;
  83:         printn((u_long)b, *s++);
  84:         any = 0;
  85:         if (b) {
  86:             while (i = *s++) {
  87:                 if (b & (1 << (i-1))) {
  88:                     putchar(any? ',' : '<');
  89:                     any = 1;
  90:                     for (; (c = *s) > 32; s++)
  91:                         putchar(c);
  92:                 } else
  93:                     for (; *s > 32; s++)
  94:                         ;
  95:             }
  96:             if (any)
  97:                 putchar('>');
  98:         }
  99:         break;
 100: 
 101:     case 's':
 102:         s = (char *)*adx;
 103:         while (c = *s++)
 104:             putchar(c);
 105:         break;
 106:     }
 107:     adx++;
 108:     goto loop;
 109: }
 110: 
 111: /*
 112:  * Printn prints a number n in base b.
 113:  * We don't use recursion to avoid deep kernel stacks.
 114:  */
 115: printn(n, b)
 116:     u_long n;
 117: {
 118:     char prbuf[11];
 119:     register char *cp;
 120: 
 121:     if (b == 10 && (int)n < 0) {
 122:         putchar('-');
 123:         n = (unsigned)(-(int)n);
 124:     }
 125:     cp = prbuf;
 126:     do {
 127:         *cp++ = "0123456789abcdef"[n%b];
 128:         n /= b;
 129:     } while (n);
 130:     do
 131:         putchar(*--cp);
 132:     while (cp > prbuf);
 133: }
 134: 
 135: /*
 136:  * Print a character on console.
 137:  */
 138: putchar(c)
 139:     register c;
 140: {
 141:     register s, timo;
 142: 
 143:     timo = 30000;
 144:     /*
 145: 	 * Try waiting for the console tty to come ready,
 146: 	 * otherwise give up after a reasonable time.
 147: 	 */
 148:     while((mfpr(TXCS)&TXCS_RDY) == 0)
 149:         if(--timo == 0)
 150:             break;
 151:     if(c == 0)
 152:         return;
 153:     s = mfpr(TXCS);
 154:     mtpr(TXCS,0);
 155:     mtpr(TXDB, c&0xff);
 156:     if(c == '\n')
 157:         putchar('\r');
 158:     putchar(0);
 159:     mtpr(TXCS, s);
 160: }
 161: 
 162: getchar()
 163: {
 164:     register c;
 165: 
 166:     while((mfpr(RXCS)&RXCS_DONE) == 0)
 167:         ;
 168:     c = mfpr(RXDB)&0177;
 169:     if (c=='\r')
 170:         c = '\n';
 171:     putchar(c);
 172:     return(c);
 173: }
 174: 
 175: gets(buf)
 176:     char *buf;
 177: {
 178:     register char *lp;
 179:     register c;
 180: 
 181:     lp = buf;
 182:     for (;;) {
 183:         c = getchar() & 0177;
 184:         switch(c) {
 185:         case '\n':
 186:         case '\r':
 187:             c = '\n';
 188:             *lp++ = '\0';
 189:             return;
 190:         case '\b':
 191:             if (lp > buf) {
 192:                 lp--;
 193:                 putchar(' ');
 194:                 putchar('\b');
 195:             }
 196:             continue;
 197:         case '#':
 198:         case '\177':
 199:             lp--;
 200:             if (lp < buf)
 201:                 lp = buf;
 202:             continue;
 203:         case '@':
 204:         case 'u'&037:
 205:             lp = buf;
 206:             putchar('\n');
 207:             continue;
 208:         default:
 209:             *lp++ = c;
 210:         }
 211:     }
 212: }

Defined functions

getchar defined in line 162; used 3 times
prf defined in line 41; used 1 times
  • in line 38
printf defined in line 33; used 869 times
printn defined in line 115; used 2 times
putchar defined in line 138; used 24 times
Last modified: 1986-06-05
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 927
Valid CSS Valid XHTML 1.0 Strict