Annotation of 3BSD/sys/stand/prf.c, revision 1.1.1.1

1.1       root        1: #include "../h/cons.h"
                      2: #include "../h/mtpr.h"
                      3: 
                      4: /*
                      5:  * Scaled down version of C Library printf.
                      6:  * Only %s %u %d (==%u) %o %x %D %c are recognized.
                      7:  * Used to print diagnostic information
                      8:  * directly on console tty.
                      9:  * Since it is not interrupt driven,
                     10:  * all system activities are pretty much
                     11:  * suspended.
                     12:  * Printf should not be used for chit-chat.
                     13:  */
                     14: /*VARARGS1*/
                     15: printf(fmt, x1)
                     16: register char *fmt;
                     17: unsigned x1;
                     18: {
                     19:        register c;
                     20:        register unsigned int *adx;
                     21:        char *s;
                     22: 
                     23:        adx = &x1;
                     24: loop:
                     25:        while((c = *fmt++) != '%') {
                     26:                if(c == '\0')
                     27:                        return;
                     28:                putchar(c);
                     29:        }
                     30:        c = *fmt++;
                     31:        if(c == 'X')
                     32:                printx((long)*adx);
                     33:        else if(c == 'd' || c == 'u' || c == 'o' || c == 'x')
                     34:                printn((long)*adx, c=='o'? 8: (c=='x'? 16:10));
                     35:        else if(c == 'c')
                     36:                putchar(*adx);
                     37:        else if(c == 's') {
                     38:                s = (char *)*adx;
                     39:                while(c = *s++)
                     40:                        putchar(c);
                     41:        } else if (c == 'D') {
                     42:                printn(*(long *)adx, 10);
                     43:                adx += (sizeof(long) / sizeof(int)) - 1;
                     44:        }
                     45:        adx++;
                     46:        goto loop;
                     47: }
                     48: 
                     49: printx(x)
                     50: long x;
                     51: {
                     52:        int i;
                     53: 
                     54:        for (i = 0; i < 8; i++)
                     55:                putchar("0123456789ABCDEF"[(x>>((7-i)*4))&0xf]);
                     56: }
                     57: 
                     58: /*
                     59:  * Print an unsigned integer in base b.
                     60:  */
                     61: printn(n, b)
                     62: long n;
                     63: {
                     64:        register long a;
                     65: 
                     66:        if (n<0) {      /* shouldn't happen */
                     67:                putchar('-');
                     68:                n = -n;
                     69:        }
                     70:        if(a = n/b)
                     71:                printn(a, b);
                     72:        putchar("0123456789ABCDEF"[(int)(n%b)]);
                     73: }
                     74: 
                     75: /*
                     76:  * Print a character on console.
                     77:  * Attempts to save and restore device
                     78:  * status.
                     79:  * If the switches are 0, all
                     80:  * printing is inhibited.
                     81:  *
                     82:  * Whether or not printing is inhibited,
                     83:  * the last MSGBUFS characters
                     84:  * are saved in msgbuf for inspection later.
                     85:  */
                     86: putchar(c)
                     87: register c;
                     88: {
                     89:        register s, timo;
                     90: 
                     91:        timo = 30000;
                     92:        /*
                     93:         * Try waiting for the console tty to come ready,
                     94:         * otherwise give up after a reasonable time.
                     95:         */
                     96:        while((mfpr(TXCS)&TXCS_RDY) == 0)
                     97:                if(--timo == 0)
                     98:                        break;
                     99:        if(c == 0)
                    100:                return;
                    101:        s = mfpr(TXCS);
                    102:        mtpr(TXCS,0);
                    103:        mtpr(TXDB, c&0xff);
                    104:        if(c == '\n')
                    105:                putchar('\r');
                    106:        putchar(0);
                    107:        mtpr(TXCS, s);
                    108: }
                    109: 
                    110: getchar()
                    111: {
                    112:        register c;
                    113: 
                    114:        while((mfpr(RXCS)&RXCS_DONE) == 0)
                    115:                ;
                    116:        c = mfpr(RXDB)&0177;
                    117:        if (c=='\r')
                    118:                c = '\n';
                    119:        putchar(c);
                    120:        return(c);
                    121: }
                    122: 
                    123: gets(buf)
                    124: char   *buf;
                    125: {
                    126: register char *lp;
                    127: register c;
                    128: 
                    129:        lp = buf;
                    130:        for (;;) {
                    131:                c = getchar() & 0177;
                    132:                if (c>='A' && c<='Z')
                    133:                        c -= 'A' - 'a';
                    134:                if (lp != buf && *(lp-1) == '\\') {
                    135:                        lp--;
                    136:                        if (c>='a' && c<='z') {
                    137:                                c += 'A' - 'a';
                    138:                                goto store;
                    139:                        }
                    140:                        switch ( c) {
                    141:                        case '(':
                    142:                                c = '{';
                    143:                                break;
                    144:                        case ')':
                    145:                                c = '}';
                    146:                                break;
                    147:                        case '!':
                    148:                                c = '|';
                    149:                                break;
                    150:                        case '^':
                    151:                                c = '~';
                    152:                                break;
                    153:                        case '\'':
                    154:                                c = '`';
                    155:                                break;
                    156:                        }
                    157:                }
                    158:        store:
                    159:                switch(c) {
                    160:                case '\n':
                    161:                case '\r':
                    162:                        c = '\n';
                    163:                        *lp++ = '\0';
                    164:                        return;
                    165:                case '\b':
                    166:                case '#':
                    167:                        lp--;
                    168:                        if (lp < buf)
                    169:                                lp = buf;
                    170:                        continue;
                    171:                case '@':
                    172:                        lp = buf;
                    173:                        putchar('\n');
                    174:                        continue;
                    175:                default:
                    176:                        *lp++ = c;
                    177:                }
                    178:        }
                    179: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.