Annotation of Gnu-Mach/ddb/db_input.c, revision 1.1.1.3

1.1.1.2   root        1: /*
1.1       root        2:  * Mach Operating System
                      3:  * Copyright (c) 1992,1991,1990 Carnegie Mellon University
                      4:  * All Rights Reserved.
1.1.1.2   root        5:  *
1.1       root        6:  * Permission to use, copy, modify and distribute this software and its
                      7:  * documentation is hereby granted, provided that both the copyright
                      8:  * notice and this permission notice appear in all copies of the
                      9:  * software, derivative works or modified versions, and any portions
                     10:  * thereof, and that both notices appear in supporting documentation.
1.1.1.2   root       11:  *
1.1       root       12:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     13:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
                     14:  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1.1.1.2   root       15:  *
1.1       root       16:  * Carnegie Mellon requests users of this software to return to
1.1.1.2   root       17:  *
1.1       root       18:  *  Software Distribution Coordinator  or  [email protected]
                     19:  *  School of Computer Science
                     20:  *  Carnegie Mellon University
                     21:  *  Pittsburgh PA 15213-3890
1.1.1.2   root       22:  *
1.1       root       23:  * any improvements or extensions that they make and grant Carnegie Mellon
                     24:  * the rights to redistribute these changes.
                     25:  */
                     26: /*
                     27:  *     Author: David B. Golub, Carnegie Mellon University
                     28:  *     Date:   7/90
                     29:  */
1.1.1.3 ! root       30: 
1.1       root       31: #if MACH_KDB
                     32: 
                     33: #include <mach/boolean.h>
                     34: #include <machine/db_machdep.h>
1.1.1.3 ! root       35: #include <device/cons.h>
        !            36: #include <ddb/db_command.h>
        !            37: #include <ddb/db_input.h>
1.1       root       38: #include <ddb/db_output.h>
                     39: 
                     40: #ifndef DB_HISTORY_SIZE
                     41: #define DB_HISTORY_SIZE 4000
1.1.1.2   root       42: #endif /* DB_HISTORY_SIZE */
1.1       root       43: 
                     44: /*
                     45:  * Character input and editing.
                     46:  */
                     47: 
                     48: /*
                     49:  * We don't track output position while editing input,
                     50:  * since input always ends with a new-line.  We just
                     51:  * reset the line position at the end.
                     52:  */
                     53: char * db_lbuf_start;  /* start of input line buffer */
                     54: char * db_lbuf_end;    /* end of input line buffer */
                     55: char * db_lc;          /* current character */
                     56: char * db_le;          /* one past last character */
                     57: #if DB_HISTORY_SIZE != 0
                     58: char    db_history[DB_HISTORY_SIZE];   /* start of history buffer */
                     59: int     db_history_size = DB_HISTORY_SIZE;/* size of history buffer */
                     60: char *  db_history_curr = db_history;  /* start of current line */
                     61: char *  db_history_last = db_history;  /* start of last line */
                     62: char *  db_history_prev = (char *) 0;  /* start of previous line */
                     63: #endif
1.1.1.2   root       64: 
1.1       root       65: #define        CTRL(c)         ((c) & 0x1f)
                     66: #define        isspace(c)      ((c) == ' ' || (c) == '\t')
                     67: #define        BLANK           ' '
                     68: #define        BACKUP          '\b'
                     69: 
                     70: void
                     71: db_putstring(s, count)
                     72:        char    *s;
                     73:        int     count;
                     74: {
                     75:        while (--count >= 0)
                     76:            cnputc(*s++);
                     77: }
                     78: 
                     79: void
                     80: db_putnchars(c, count)
                     81:        int     c;
                     82:        int     count;
                     83: {
                     84:        while (--count >= 0)
                     85:            cnputc(c);
                     86: }
                     87: 
                     88: /*
                     89:  * Delete N characters, forward or backward
                     90:  */
                     91: #define        DEL_FWD         0
                     92: #define        DEL_BWD         1
                     93: void
                     94: db_delete(n, bwd)
                     95:        int     n;
                     96:        int     bwd;
                     97: {
                     98:        register char *p;
                     99: 
                    100:        if (bwd) {
                    101:            db_lc -= n;
                    102:            db_putnchars(BACKUP, n);
                    103:        }
                    104:        for (p = db_lc; p < db_le-n; p++) {
                    105:            *p = *(p+n);
                    106:            cnputc(*p);
                    107:        }
                    108:        db_putnchars(BLANK, n);
                    109:        db_putnchars(BACKUP, db_le - db_lc);
                    110:        db_le -= n;
                    111: }
                    112: 
                    113: void
                    114: db_delete_line()
                    115: {
                    116:        db_delete(db_le - db_lc, DEL_FWD);
                    117:        db_delete(db_lc - db_lbuf_start, DEL_BWD);
                    118:        db_le = db_lc = db_lbuf_start;
                    119: }
                    120: 
                    121: #if DB_HISTORY_SIZE != 0
                    122: #define INC_DB_CURR() \
                    123:     do { \
                    124:             db_history_curr++; \
                    125:             if (db_history_curr > \
                    126:                 db_history + db_history_size - 1) \
                    127:                     db_history_curr = db_history; \
                    128:        } while (0)
                    129: #define DEC_DB_CURR() \
                    130:     do { \
                    131:             db_history_curr--; \
                    132:             if (db_history_curr < db_history) \
                    133:                 db_history_curr = db_history + \
                    134:                 db_history_size - 1; \
                    135:        } while (0)
                    136: #endif
1.1.1.2   root      137: 
1.1       root      138: /* returns TRUE at end-of-line */
                    139: boolean_t
                    140: db_inputchar(c)
                    141:        int     c;
                    142: {
                    143:        switch (c) {
                    144:            case CTRL('b'):
                    145:                /* back up one character */
                    146:                if (db_lc > db_lbuf_start) {
                    147:                    cnputc(BACKUP);
                    148:                    db_lc--;
                    149:                }
                    150:                break;
                    151:            case CTRL('f'):
                    152:                /* forward one character */
                    153:                if (db_lc < db_le) {
                    154:                    cnputc(*db_lc);
                    155:                    db_lc++;
                    156:                }
                    157:                break;
                    158:            case CTRL('a'):
                    159:                /* beginning of line */
                    160:                while (db_lc > db_lbuf_start) {
                    161:                    cnputc(BACKUP);
                    162:                    db_lc--;
                    163:                }
                    164:                break;
                    165:            case CTRL('e'):
                    166:                /* end of line */
                    167:                while (db_lc < db_le) {
                    168:                    cnputc(*db_lc);
                    169:                    db_lc++;
                    170:                }
                    171:                break;
                    172:            case CTRL('h'):
                    173:            case 0177:
                    174:                /* erase previous character */
                    175:                if (db_lc > db_lbuf_start)
                    176:                    db_delete(1, DEL_BWD);
                    177:                break;
                    178:            case CTRL('d'):
                    179:                /* erase next character */
                    180:                if (db_lc < db_le)
                    181:                    db_delete(1, DEL_FWD);
                    182:                break;
                    183:            case CTRL('k'):
                    184:                /* delete to end of line */
                    185:                if (db_lc < db_le)
                    186:                    db_delete(db_le - db_lc, DEL_FWD);
                    187:                break;
                    188:            case CTRL('u'):
                    189:                /* delete line */
                    190:                db_delete_line();
                    191:                break;
                    192:            case CTRL('t'):
                    193:                /* twiddle last 2 characters */
                    194:                if (db_lc >= db_lbuf_start + 2) {
                    195:                    c = db_lc[-2];
                    196:                    db_lc[-2] = db_lc[-1];
                    197:                    db_lc[-1] = c;
                    198:                    cnputc(BACKUP);
                    199:                    cnputc(BACKUP);
                    200:                    cnputc(db_lc[-2]);
                    201:                    cnputc(db_lc[-1]);
                    202:                }
                    203:                break;
                    204: #if DB_HISTORY_SIZE != 0
                    205:            case CTRL('p'):
                    206:                DEC_DB_CURR();
                    207:                while (db_history_curr != db_history_last) {
                    208:                        DEC_DB_CURR();
                    209:                        if (*db_history_curr == '\0')
                    210:                            break;
                    211:                }
                    212:                db_delete_line();
                    213:                if (db_history_curr == db_history_last) {
                    214:                        INC_DB_CURR();
                    215:                        db_le = db_lc = db_lbuf_start;
                    216:                } else {
                    217:                        register char *p;
                    218:                        INC_DB_CURR();
                    219:                        for (p = db_history_curr, db_le = db_lbuf_start;
                    220:                             *p; ) {
                    221:                                *db_le++ = *p++;
                    222:                                if (p == db_history + db_history_size) {
                    223:                                        p = db_history;
                    224:                                }
                    225:                        }
                    226:                        db_lc = db_le;
                    227:                }
                    228:                db_putstring(db_lbuf_start, db_le - db_lbuf_start);
                    229:                break;
                    230:            case CTRL('n'):
                    231:                while (db_history_curr != db_history_last) {
                    232:                        if (*db_history_curr == '\0')
                    233:                            break;
                    234:                        INC_DB_CURR();
                    235:                }
                    236:                if (db_history_curr != db_history_last) {
                    237:                        INC_DB_CURR();
                    238:                        db_delete_line();
                    239:                        if (db_history_curr != db_history_last) {
                    240:                                register char *p;
                    241:                                for (p = db_history_curr,
                    242:                                     db_le = db_lbuf_start; *p;) {
                    243:                                        *db_le++ = *p++;
                    244:                                        if (p == db_history +
                    245:                                            db_history_size) {
                    246:                                                p = db_history;
                    247:                                        }
                    248:                                }
                    249:                                db_lc = db_le;
                    250:                        }
                    251:                        db_putstring(db_lbuf_start, db_le - db_lbuf_start);
                    252:                }
                    253:                break;
                    254: #endif
                    255:            case CTRL('r'):
                    256:                db_putstring("^R\n", 3);
                    257:                if (db_le > db_lbuf_start) {
                    258:                        db_putstring(db_lbuf_start, db_le - db_lbuf_start);
                    259:                        db_putnchars(BACKUP, db_le - db_lc);
                    260:                }
                    261:                break;
                    262:            case '\n':
                    263:            case '\r':
                    264: #if DB_HISTORY_SIZE != 0
                    265:                /*
                    266:                 * Check whether current line is the same
                    267:                 * as previous saved line.  If it is, don`t
                    268:                 * save it.
                    269:                 */
                    270:                if (db_history_curr == db_history_prev) {
                    271:                        register char *pp, *pc;
                    272: 
                    273:                        /*
                    274:                         * Is it the same?
                    275:                         */
                    276:                        for (pp = db_history_prev, pc = db_lbuf_start;
                    277:                             pc != db_le && *pp; ) {
                    278:                                if (*pp != *pc)
                    279:                                    break;
                    280:                                if (++pp == db_history + db_history_size) {
                    281:                                        pp = db_history;
                    282:                                }
                    283:                                pc++;
                    284:                        }
                    285:                        if (!*pp && pc == db_le) {
                    286:                                /*
                    287:                                 * Repeated previous line. Don`t save.
                    288:                                 */
                    289:                                db_history_curr = db_history_last;
                    290:                                *db_le++ = c;
                    291:                                return (TRUE);
                    292:                        }
                    293:                }
                    294:                if (db_le != db_lbuf_start) {
                    295:                        register char *p;
                    296:                        db_history_prev = db_history_last;
                    297:                        for (p = db_lbuf_start; p != db_le; p++) {
                    298:                                *db_history_last++ = *p;
                    299:                                if (db_history_last == db_history +
                    300:                                    db_history_size) {
                    301:                                        db_history_last = db_history;
                    302:                                }
                    303:                        }
                    304:                        *db_history_last++ = '\0';
                    305:                }
                    306:                db_history_curr = db_history_last;
                    307: #endif
                    308:                *db_le++ = c;
                    309:                return (TRUE);
                    310:            default:
                    311:                if (db_le == db_lbuf_end) {
                    312:                    cnputc('\007');
                    313:                }
                    314:                else if (c >= ' ' && c <= '~') {
                    315:                    register char *p;
                    316: 
                    317:                    for (p = db_le; p > db_lc; p--)
                    318:                        *p = *(p-1);
                    319:                    *db_lc++ = c;
                    320:                    db_le++;
                    321:                    cnputc(c);
                    322:                    db_putstring(db_lc, db_le - db_lc);
                    323:                    db_putnchars(BACKUP, db_le - db_lc);
                    324:                }
                    325:                break;
                    326:        }
                    327:        return (FALSE);
                    328: }
                    329: 
                    330: int
                    331: db_readline(lstart, lsize)
                    332:        char *  lstart;
                    333:        int     lsize;
                    334: {
                    335:        db_force_whitespace();  /* synch output position */
                    336: 
                    337:        db_lbuf_start = lstart;
                    338:        db_lbuf_end   = lstart + lsize - 1;
                    339:        db_lc = lstart;
                    340:        db_le = lstart;
                    341: 
                    342:        while (!db_inputchar(cngetc()))
                    343:            continue;
                    344: 
                    345:        db_putchar('\n');       /* synch output position */
                    346: 
                    347:        *db_le = 0;
                    348:        return (db_le - db_lbuf_start);
                    349: }
                    350: 
                    351: void
                    352: db_check_interrupt()
                    353: {
                    354:        register int    c;
                    355: 
                    356:        c = cnmaygetc();
                    357:        switch (c) {
                    358:            case -1:            /* no character */
                    359:                return;
                    360: 
                    361:            case CTRL('c'):
                    362:                db_error((char *)0);
                    363:                /*NOTREACHED*/
                    364: 
                    365:            case CTRL('s'):
                    366:                do {
                    367:                    c = cnmaygetc();
                    368:                    if (c == CTRL('c'))
                    369:                        db_error((char *)0);
                    370:                } while (c != CTRL('q'));
                    371:                break;
                    372: 
                    373:            default:
                    374:                /* drop on floor */
                    375:                break;
                    376:        }
                    377: }
                    378: 
1.1.1.2   root      379: #endif /* MACH_KDB */

unix.superglobalmegacorp.com

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