Annotation of lucent/sys/src/boot/pc/console.c, revision 1.1

1.1     ! root        1: #include "u.h"
        !             2: #include "lib.h"
        !             3: #include "mem.h"
        !             4: #include "dat.h"
        !             5: #include "fns.h"
        !             6: #include "io.h"
        !             7: 
        !             8: static IOQ consiq;
        !             9: static IOQ consoq;
        !            10: 
        !            11: static void (*consputs)(IOQ*, char*, int) = cgaputs;
        !            12: 
        !            13: void
        !            14: consinit(void)
        !            15: {
        !            16:        char *p;
        !            17:        int baud, port;
        !            18: 
        !            19:        qinit(&consiq);
        !            20: 
        !            21:        if((p = getconf("console")) == 0 || strcmp(p, "cga") == 0){
        !            22:                consputs = cgaputs;
        !            23:                cgainit();
        !            24:                kbdinit();
        !            25:                return;
        !            26:        }
        !            27: 
        !            28:        qinit(&consoq);
        !            29:        consputs = uartputs;
        !            30: 
        !            31:        port = strtoul(p, 0, 0);
        !            32:        baud = 0;
        !            33:        if(p = getconf("baud"))
        !            34:                baud = strtoul(p, 0, 0);
        !            35:        if(baud == 0)
        !            36:                baud = 9600;
        !            37:        uartspecial(port, kbdchar, conschar, baud);
        !            38: }
        !            39: 
        !            40: void
        !            41: kbdchar(int c)
        !            42: {
        !            43:        c &= 0x7F;
        !            44:        if(c == 0x10)
        !            45:                panic("^p");
        !            46:        consiq.putc(&consiq, c);
        !            47: }
        !            48: 
        !            49: int
        !            50: conschar(void)
        !            51: {
        !            52:        return consoq.getc(&consoq);
        !            53: }
        !            54: 
        !            55: static int
        !            56: getline(char *buf, int size, int dotimeout)
        !            57: {
        !            58:        int c, i=0;
        !            59:        ulong start;
        !            60:        char echo;
        !            61: 
        !            62:        for (;;) {
        !            63:                start = m->ticks;
        !            64:                do{
        !            65:                        if(dotimeout && ((m->ticks - start) > 5*HZ))
        !            66:                                return -2;
        !            67:                        c = consiq.getc(&consiq);
        !            68:                }while(c == -1);
        !            69:                if(c == '\r')
        !            70:                        c = '\n';               /* turn carriage return into newline */
        !            71:                if(c == '\177')
        !            72:                        c = '\010';             /* turn delete into backspace */
        !            73:                if(c == '\025')
        !            74:                        echo = '\n';            /* echo ^U as a newline */
        !            75:                else
        !            76:                        echo = c;
        !            77:                (*consputs)(&consoq, &echo, 1);
        !            78: 
        !            79:                if(c == '\010'){
        !            80:                        if(i > 0)
        !            81:                                i--; /* bs deletes last character */
        !            82:                        continue;
        !            83:                }
        !            84:                /* a newline ends a line */
        !            85:                if (c == '\n')
        !            86:                        break;
        !            87:                /* ^U wipes out the line */
        !            88:                if (c =='\025')
        !            89:                        return -1;
        !            90:                if(i == size)
        !            91:                        return size;
        !            92:                buf[i++] = c;
        !            93:        }
        !            94:        buf[i] = 0;
        !            95:        return i;
        !            96: }
        !            97: 
        !            98: int
        !            99: getstr(char *prompt, char *buf, int size, char *def, int timeout)
        !           100: {
        !           101:        int len, isdefault;
        !           102: 
        !           103:        buf[0] = 0;
        !           104:        isdefault = (def && *def);
        !           105:        for (;;) {
        !           106:                if(isdefault)
        !           107:                        print("%s[default==%s]: ", prompt, def);
        !           108:                else
        !           109:                        print("%s: ", prompt);
        !           110:                len = getline(buf, size, timeout);
        !           111:                switch(len){
        !           112:                case -1:
        !           113:                        /* ^U typed */
        !           114:                        continue;
        !           115:                case -2:
        !           116:                        /* timeout, use default */
        !           117:                        (*consputs)(&consoq, "\n", 1);
        !           118:                        len = 0;
        !           119:                        break;
        !           120:                default:
        !           121:                        break;
        !           122:                }
        !           123:                if(len >= size){
        !           124:                        print("line too long\n");
        !           125:                        continue;
        !           126:                }
        !           127:                break;
        !           128:        }
        !           129:        if(len == 0 && isdefault)
        !           130:                strcpy(buf, def);
        !           131:        return 0;
        !           132: }
        !           133: 
        !           134: int
        !           135: sprint(char *s, char *fmt, ...)
        !           136: {
        !           137:        return donprint(s, s+PRINTSIZE, fmt, (&fmt+1)) - s;
        !           138: }
        !           139: 
        !           140: int
        !           141: print(char *fmt, ...)
        !           142: {
        !           143:        char buf[PRINTSIZE];
        !           144:        int n;
        !           145: 
        !           146:        if(consputs == 0)
        !           147:                return 0;
        !           148:        n = donprint(buf, buf+sizeof(buf), fmt, (&fmt+1)) - buf;
        !           149:        (*consputs)(&consoq, buf, n);
        !           150:        return n;
        !           151: }
        !           152: 
        !           153: void
        !           154: panic(char *fmt, ...)
        !           155: {
        !           156:        char buf[PRINTSIZE];
        !           157:        int n;
        !           158: 
        !           159:        if(consputs){
        !           160:                (*consputs)(&consoq, "panic: ", 7);
        !           161:                n = donprint(buf, buf+sizeof(buf), fmt, (&fmt+1)) - buf;
        !           162:                (*consputs)(&consoq, buf, n);
        !           163:                (*consputs)(&consoq, "\n", 1);
        !           164:        }
        !           165:        spllo();
        !           166:        i8042reset();
        !           167:        for(;;)
        !           168:                idle();
        !           169: }

unix.superglobalmegacorp.com

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