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

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)
1.1.1.4 ! root       72:        const char      *s;
        !            73:        int             count;
1.1       root       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
1.1.1.4 ! root       94: db_delete(
        !            95:        int     n,
        !            96:        int     bwd)
1.1       root       97: {
1.1.1.4 ! root       98:        char *p;
1.1       root       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
1.1.1.4 ! root      114: db_delete_line(void)
1.1       root      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)
1.1.1.4 ! root      136: #endif /* DB_HISTORY_SIZE */
1.1.1.2   root      137: 
1.1       root      138: /* returns TRUE at end-of-line */
                    139: boolean_t
1.1.1.4 ! root      140: db_inputchar(int c)
1.1       root      141: {
                    142:        switch (c) {
                    143:            case CTRL('b'):
                    144:                /* back up one character */
                    145:                if (db_lc > db_lbuf_start) {
                    146:                    cnputc(BACKUP);
                    147:                    db_lc--;
                    148:                }
                    149:                break;
                    150:            case CTRL('f'):
                    151:                /* forward one character */
                    152:                if (db_lc < db_le) {
                    153:                    cnputc(*db_lc);
                    154:                    db_lc++;
                    155:                }
                    156:                break;
                    157:            case CTRL('a'):
                    158:                /* beginning of line */
                    159:                while (db_lc > db_lbuf_start) {
                    160:                    cnputc(BACKUP);
                    161:                    db_lc--;
                    162:                }
                    163:                break;
                    164:            case CTRL('e'):
                    165:                /* end of line */
                    166:                while (db_lc < db_le) {
                    167:                    cnputc(*db_lc);
                    168:                    db_lc++;
                    169:                }
                    170:                break;
                    171:            case CTRL('h'):
                    172:            case 0177:
                    173:                /* erase previous character */
                    174:                if (db_lc > db_lbuf_start)
                    175:                    db_delete(1, DEL_BWD);
                    176:                break;
                    177:            case CTRL('d'):
                    178:                /* erase next character */
                    179:                if (db_lc < db_le)
                    180:                    db_delete(1, DEL_FWD);
                    181:                break;
                    182:            case CTRL('k'):
                    183:                /* delete to end of line */
                    184:                if (db_lc < db_le)
                    185:                    db_delete(db_le - db_lc, DEL_FWD);
                    186:                break;
                    187:            case CTRL('u'):
                    188:                /* delete line */
                    189:                db_delete_line();
                    190:                break;
                    191:            case CTRL('t'):
                    192:                /* twiddle last 2 characters */
                    193:                if (db_lc >= db_lbuf_start + 2) {
                    194:                    c = db_lc[-2];
                    195:                    db_lc[-2] = db_lc[-1];
                    196:                    db_lc[-1] = c;
                    197:                    cnputc(BACKUP);
                    198:                    cnputc(BACKUP);
                    199:                    cnputc(db_lc[-2]);
                    200:                    cnputc(db_lc[-1]);
                    201:                }
                    202:                break;
                    203: #if DB_HISTORY_SIZE != 0
                    204:            case CTRL('p'):
                    205:                DEC_DB_CURR();
                    206:                while (db_history_curr != db_history_last) {
                    207:                        DEC_DB_CURR();
                    208:                        if (*db_history_curr == '\0')
                    209:                            break;
                    210:                }
                    211:                db_delete_line();
                    212:                if (db_history_curr == db_history_last) {
                    213:                        INC_DB_CURR();
                    214:                        db_le = db_lc = db_lbuf_start;
                    215:                } else {
1.1.1.4 ! root      216:                        char *p;
1.1       root      217:                        INC_DB_CURR();
                    218:                        for (p = db_history_curr, db_le = db_lbuf_start;
                    219:                             *p; ) {
                    220:                                *db_le++ = *p++;
                    221:                                if (p == db_history + db_history_size) {
                    222:                                        p = db_history;
                    223:                                }
                    224:                        }
                    225:                        db_lc = db_le;
                    226:                }
                    227:                db_putstring(db_lbuf_start, db_le - db_lbuf_start);
                    228:                break;
                    229:            case CTRL('n'):
                    230:                while (db_history_curr != db_history_last) {
                    231:                        if (*db_history_curr == '\0')
                    232:                            break;
                    233:                        INC_DB_CURR();
                    234:                }
                    235:                if (db_history_curr != db_history_last) {
                    236:                        INC_DB_CURR();
                    237:                        db_delete_line();
                    238:                        if (db_history_curr != db_history_last) {
1.1.1.4 ! root      239:                                char *p;
1.1       root      240:                                for (p = db_history_curr,
                    241:                                     db_le = db_lbuf_start; *p;) {
                    242:                                        *db_le++ = *p++;
                    243:                                        if (p == db_history +
                    244:                                            db_history_size) {
                    245:                                                p = db_history;
                    246:                                        }
                    247:                                }
                    248:                                db_lc = db_le;
                    249:                        }
                    250:                        db_putstring(db_lbuf_start, db_le - db_lbuf_start);
                    251:                }
                    252:                break;
1.1.1.4 ! root      253: #endif /* DB_HISTORY_SIZE */
1.1       root      254:            case CTRL('r'):
                    255:                db_putstring("^R\n", 3);
                    256:                if (db_le > db_lbuf_start) {
                    257:                        db_putstring(db_lbuf_start, db_le - db_lbuf_start);
                    258:                        db_putnchars(BACKUP, db_le - db_lc);
                    259:                }
                    260:                break;
                    261:            case '\n':
                    262:            case '\r':
                    263: #if DB_HISTORY_SIZE != 0
                    264:                /*
                    265:                 * Check whether current line is the same
                    266:                 * as previous saved line.  If it is, don`t
                    267:                 * save it.
                    268:                 */
                    269:                if (db_history_curr == db_history_prev) {
1.1.1.4 ! root      270:                        char *pp, *pc;
1.1       root      271: 
                    272:                        /*
                    273:                         * Is it the same?
                    274:                         */
                    275:                        for (pp = db_history_prev, pc = db_lbuf_start;
                    276:                             pc != db_le && *pp; ) {
                    277:                                if (*pp != *pc)
                    278:                                    break;
                    279:                                if (++pp == db_history + db_history_size) {
                    280:                                        pp = db_history;
                    281:                                }
                    282:                                pc++;
                    283:                        }
                    284:                        if (!*pp && pc == db_le) {
                    285:                                /*
                    286:                                 * Repeated previous line. Don`t save.
                    287:                                 */
                    288:                                db_history_curr = db_history_last;
                    289:                                *db_le++ = c;
                    290:                                return (TRUE);
                    291:                        }
                    292:                }
                    293:                if (db_le != db_lbuf_start) {
1.1.1.4 ! root      294:                        char *p;
1.1       root      295:                        db_history_prev = db_history_last;
                    296:                        for (p = db_lbuf_start; p != db_le; p++) {
                    297:                                *db_history_last++ = *p;
                    298:                                if (db_history_last == db_history +
                    299:                                    db_history_size) {
                    300:                                        db_history_last = db_history;
                    301:                                }
                    302:                        }
                    303:                        *db_history_last++ = '\0';
                    304:                }
                    305:                db_history_curr = db_history_last;
1.1.1.4 ! root      306: #endif /* DB_HISTORY_SIZE */
1.1       root      307:                *db_le++ = c;
                    308:                return (TRUE);
                    309:            default:
                    310:                if (db_le == db_lbuf_end) {
                    311:                    cnputc('\007');
                    312:                }
                    313:                else if (c >= ' ' && c <= '~') {
1.1.1.4 ! root      314:                    char *p;
1.1       root      315: 
                    316:                    for (p = db_le; p > db_lc; p--)
                    317:                        *p = *(p-1);
                    318:                    *db_lc++ = c;
                    319:                    db_le++;
                    320:                    cnputc(c);
                    321:                    db_putstring(db_lc, db_le - db_lc);
                    322:                    db_putnchars(BACKUP, db_le - db_lc);
                    323:                }
                    324:                break;
                    325:        }
                    326:        return (FALSE);
                    327: }
                    328: 
                    329: int
1.1.1.4 ! root      330: db_readline(
        !           331:        char *  lstart,
        !           332:        int     lsize)
1.1       root      333: {
                    334:        db_force_whitespace();  /* synch output position */
                    335: 
                    336:        db_lbuf_start = lstart;
                    337:        db_lbuf_end   = lstart + lsize - 1;
                    338:        db_lc = lstart;
                    339:        db_le = lstart;
                    340: 
                    341:        while (!db_inputchar(cngetc()))
                    342:            continue;
                    343: 
                    344:        db_putchar('\n');       /* synch output position */
                    345: 
                    346:        *db_le = 0;
                    347:        return (db_le - db_lbuf_start);
                    348: }
                    349: 
                    350: void
1.1.1.4 ! root      351: db_check_interrupt(void)
1.1       root      352: {
1.1.1.4 ! root      353:        int     c;
1.1       root      354: 
                    355:        c = cnmaygetc();
                    356:        switch (c) {
                    357:            case -1:            /* no character */
                    358:                return;
                    359: 
                    360:            case CTRL('c'):
                    361:                db_error((char *)0);
                    362:                /*NOTREACHED*/
                    363: 
                    364:            case CTRL('s'):
                    365:                do {
                    366:                    c = cnmaygetc();
                    367:                    if (c == CTRL('c'))
                    368:                        db_error((char *)0);
                    369:                } while (c != CTRL('q'));
                    370:                break;
                    371: 
                    372:            default:
                    373:                /* drop on floor */
                    374:                break;
                    375:        }
                    376: }
                    377: 
1.1.1.2   root      378: #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.