[BACK]Return to echo.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/echo.c, Revision 1.22

1.22    ! vincent     1: /*     $OpenBSD: echo.c,v 1.21 2002/03/11 13:02:56 vincent Exp $       */
1.4       niklas      2:
1.1       deraadt     3: /*
1.3       millert     4:  *     Echo line reading and writing.
1.1       deraadt     5:  *
1.3       millert     6:  * Common routines for reading and writing characters in the echo line area
                      7:  * of the display screen. Used by the entire known universe.
1.1       deraadt     8:  */
1.3       millert     9:
                     10: #include "def.h"
                     11: #include "key.h"
                     12: #ifndef NO_MACRO
                     13: #include "macro.h"
                     14: #endif /* !NO_MACRO */
                     15:
1.16      art        16: #include "funmap.h"
                     17:
1.3       millert    18: #include <stdarg.h>
1.1       deraadt    19:
1.20      millert    20: static int     veread(const char *, char *buf, int, int, va_list);
                     21: static int     complt(int, int, char *, int);
                     22: static int     complt_list(int, int, char *, int);
                     23: static void    eformat(const char *, va_list);
                     24: static void    eputi(int, int);
                     25: static void    eputl(long, int);
1.21      vincent    26: static void    eputs(const char *);
1.20      millert    27: static void    eputc(char);
                     28: static LIST    *copy_list(LIST *);
1.1       deraadt    29:
1.14      mickey     30: int            epresf = FALSE;         /* stuff in echo line flag */
1.1       deraadt    31:
                     32: /*
                     33:  * Erase the echo line.
                     34:  */
1.10      art        35: void
1.21      vincent    36: eerase(void)
1.2       millert    37: {
1.1       deraadt    38:        ttcolor(CTEXT);
1.2       millert    39:        ttmove(nrow - 1, 0);
1.1       deraadt    40:        tteeol();
                     41:        ttflush();
                     42:        epresf = FALSE;
                     43: }
                     44:
                     45: /*
1.14      mickey     46:  * Ask a "yes" or "no" question.  Return ABORT if the user answers the
                     47:  * question with the abort ("^G") character.  Return FALSE for "no" and
                     48:  * TRUE for "yes".  No formatting services are available.  No newline
1.3       millert    49:  * required.
1.1       deraadt    50:  */
1.2       millert    51: int
1.21      vincent    52: eyorn(const char *sp)
1.2       millert    53: {
1.3       millert    54:        int      s;
1.1       deraadt    55:
                     56: #ifndef NO_MACRO
1.2       millert    57:        if (inmacro)
                     58:                return TRUE;
1.3       millert    59: #endif /* !NO_MACRO */
1.1       deraadt    60:        ewprintf("%s? (y or n) ", sp);
                     61:        for (;;) {
                     62:                s = getkey(FALSE);
1.2       millert    63:                if (s == 'y' || s == 'Y')
                     64:                        return TRUE;
                     65:                if (s == 'n' || s == 'N')
                     66:                        return FALSE;
                     67:                if (s == CCHR('G'))
                     68:                        return ctrlg(FFRAND, 1);
1.1       deraadt    69:                ewprintf("Please answer y or n.  %s? (y or n) ", sp);
                     70:        }
1.2       millert    71:        /* NOTREACHED */
1.1       deraadt    72: }
                     73:
                     74: /*
1.3       millert    75:  * Like eyorn, but for more important questions.  User must type all of
                     76:  * "yes" or "no" and the trainling newline.
1.1       deraadt    77:  */
1.2       millert    78: int
1.21      vincent    79: eyesno(const char *sp)
1.2       millert    80: {
1.3       millert    81:        int      s;
                     82:        char     buf[64];
1.1       deraadt    83:
                     84: #ifndef NO_MACRO
1.2       millert    85:        if (inmacro)
                     86:                return TRUE;
1.3       millert    87: #endif /* !NO_MACRO */
1.1       deraadt    88:        s = ereply("%s? (yes or no) ", buf, sizeof(buf), sp);
                     89:        for (;;) {
1.2       millert    90:                if (s == ABORT)
                     91:                        return ABORT;
1.1       deraadt    92:                if (s != FALSE) {
                     93: #ifndef NO_MACRO
                     94:                        if (macrodef) {
1.3       millert    95:                                LINE    *lp = maclcur;
1.1       deraadt    96:
1.2       millert    97:                                maclcur = lp->l_bp;
                     98:                                maclcur->l_fp = lp->l_fp;
1.3       millert    99:                                free((char *)lp);
1.1       deraadt   100:                        }
1.3       millert   101: #endif /* !NO_MACRO */
1.18      deraadt   102:                        if ((buf[0] == 'y' || buf[0] == 'Y') &&
                    103:                            (buf[1] == 'e' || buf[1] == 'E') &&
                    104:                            (buf[2] == 's' || buf[2] == 'S') &&
                    105:                            (buf[3] == '\0'))
1.2       millert   106:                                return TRUE;
1.18      deraadt   107:                        if ((buf[0] == 'n' || buf[0] == 'N') &&
                    108:                            (buf[1] == 'o' || buf[0] == 'O') &&
                    109:                            (buf[2] == '\0'))
1.2       millert   110:                                return FALSE;
1.1       deraadt   111:                }
                    112:                s = ereply("Please answer yes or no.  %s? (yes or no) ",
1.3       millert   113:                    buf, sizeof(buf), sp);
1.1       deraadt   114:        }
1.2       millert   115:        /* NOTREACHED */
1.1       deraadt   116: }
1.2       millert   117:
1.1       deraadt   118: /*
1.14      mickey    119:  * Write out a prompt and read back a reply.  The prompt is now written
                    120:  * out with full "ewprintf" formatting, although the arguments are in a
1.3       millert   121:  * rather strange place.  This is always a new message, there is no auto
1.1       deraadt   122:  * completion, and the return is echoed as such.
                    123:  */
1.2       millert   124: /* VARARGS */
                    125: int
                    126: ereply(const char *fmt, char *buf, int nbuf, ...)
1.1       deraadt   127: {
1.3       millert   128:        va_list  ap;
                    129:        int      i;
1.2       millert   130:        va_start(ap, nbuf);
                    131:        i = veread(fmt, buf, nbuf, EFNEW | EFCR, ap);
                    132:        va_end(ap);
1.1       deraadt   133:        return i;
                    134: }
                    135:
                    136: /*
1.14      mickey    137:  * This is the general "read input from the echo line" routine.  The basic
1.3       millert   138:  * idea is that the prompt string "prompt" is written to the echo line, and
1.14      mickey    139:  * a one line reply is read back into the supplied "buf" (with maximum
                    140:  * length "len"). The "flag" contains EFNEW (a new prompt), an EFFUNC
1.3       millert   141:  * (autocomplete), or EFCR (echo the carriage return as CR).
1.1       deraadt   142:  */
1.2       millert   143: /* VARARGS */
                    144: int
                    145: eread(const char *fmt, char *buf, int nbuf, int flag, ...)
1.1       deraadt   146: {
1.3       millert   147:        int      i;
                    148:        va_list  ap;
1.2       millert   149:        va_start(ap, flag);
                    150:        i = veread(fmt, buf, nbuf, flag, ap);
                    151:        va_end(ap);
1.1       deraadt   152:        return i;
                    153: }
                    154:
1.2       millert   155: static int
1.15      mickey    156: veread(const char *fp, char *buf, int nbuf, int flag, va_list ap)
1.2       millert   157: {
1.3       millert   158:        int      cpos;
                    159:        int      i;
                    160:        int      c;
1.1       deraadt   161:
                    162: #ifndef NO_MACRO
1.2       millert   163:        if (inmacro) {
                    164:                bcopy(maclcur->l_text, buf, maclcur->l_used);
                    165:                buf[maclcur->l_used] = '\0';
                    166:                maclcur = maclcur->l_fp;
                    167:                return TRUE;
1.1       deraadt   168:        }
1.3       millert   169: #endif /* !NO_MACRO */
1.1       deraadt   170:        cpos = 0;
1.2       millert   171:        if ((flag & EFNEW) != 0 || ttrow != nrow - 1) {
1.1       deraadt   172:                ttcolor(CTEXT);
1.2       millert   173:                ttmove(nrow - 1, 0);
1.1       deraadt   174:                epresf = TRUE;
                    175:        } else
                    176:                eputc(' ');
                    177:        eformat(fp, ap);
1.16      art       178:        if ((flag & EFDEF) != 0) {
                    179:                eputs(buf);
                    180:                cpos += strlen(buf);
                    181:        }
1.1       deraadt   182:        tteeol();
                    183:        ttflush();
                    184:        for (;;) {
                    185:                c = getkey(FALSE);
1.2       millert   186:                if ((flag & EFAUTO) != 0 && (c == ' ' || c == CCHR('I'))) {
1.1       deraadt   187:                        cpos += complt(flag, c, buf, cpos);
                    188:                        continue;
                    189:                }
1.2       millert   190:                if ((flag & EFAUTO) != 0 && c == '?') {
1.1       deraadt   191:                        complt_list(flag, c, buf, cpos);
                    192:                        continue;
                    193:                }
                    194:                switch (c) {
1.2       millert   195:                case CCHR('J'):
1.3       millert   196:                        c = CCHR('M');
1.22    ! vincent   197:                        /* FALLTHROUGH */
1.3       millert   198:                case CCHR('M'):                 /* return, done */
1.2       millert   199:                        if ((flag & EFFUNC) != 0) {
1.1       deraadt   200:                                if ((i = complt(flag, c, buf, cpos)) == 0)
                    201:                                        continue;
1.2       millert   202:                                if (i > 0)
                    203:                                        cpos += i;
1.1       deraadt   204:                        }
                    205:                        buf[cpos] = '\0';
1.2       millert   206:                        if ((flag & EFCR) != 0) {
1.1       deraadt   207:                                ttputc(CCHR('M'));
                    208:                                ttflush();
                    209:                        }
                    210: #ifndef NO_MACRO
1.2       millert   211:                        if (macrodef) {
1.3       millert   212:                                LINE    *lp;
1.1       deraadt   213:
1.2       millert   214:                                if ((lp = lalloc(cpos)) == NULL)
                    215:                                        return FALSE;
                    216:                                lp->l_fp = maclcur->l_fp;
                    217:                                maclcur->l_fp = lp;
                    218:                                lp->l_bp = maclcur;
                    219:                                maclcur = lp;
                    220:                                bcopy(buf, lp->l_text, cpos);
1.1       deraadt   221:                        }
1.3       millert   222: #endif /* !NO_MACRO */
1.1       deraadt   223:                        goto done;
1.3       millert   224:                case CCHR('G'):                 /* bell, abort */
1.1       deraadt   225:                        eputc(CCHR('G'));
1.10      art       226:                        (void)ctrlg(FFRAND, 0);
1.1       deraadt   227:                        ttflush();
                    228:                        return ABORT;
1.3       millert   229:                case CCHR('H'):                 /* rubout, erase */
1.14      mickey    230:                case CCHR('?'):
1.1       deraadt   231:                        if (cpos != 0) {
                    232:                                ttputc('\b');
                    233:                                ttputc(' ');
                    234:                                ttputc('\b');
                    235:                                --ttcol;
                    236:                                if (ISCTRL(buf[--cpos]) != FALSE) {
                    237:                                        ttputc('\b');
                    238:                                        ttputc(' ');
                    239:                                        ttputc('\b');
                    240:                                        --ttcol;
                    241:                                }
                    242:                                ttflush();
                    243:                        }
                    244:                        break;
1.3       millert   245:                case CCHR('X'):                 /* kill line */
                    246:                case CCHR('U'):
1.1       deraadt   247:                        while (cpos != 0) {
                    248:                                ttputc('\b');
                    249:                                ttputc(' ');
                    250:                                ttputc('\b');
                    251:                                --ttcol;
                    252:                                if (ISCTRL(buf[--cpos]) != FALSE) {
                    253:                                        ttputc('\b');
                    254:                                        ttputc(' ');
                    255:                                        ttputc('\b');
                    256:                                        --ttcol;
                    257:                                }
                    258:                        }
                    259:                        ttflush();
                    260:                        break;
1.3       millert   261:                case CCHR('W'):                 /* kill to beginning of word */
1.1       deraadt   262:                        while ((cpos > 0) && !ISWORD(buf[cpos - 1])) {
                    263:                                ttputc('\b');
                    264:                                ttputc(' ');
                    265:                                ttputc('\b');
                    266:                                --ttcol;
                    267:                                if (ISCTRL(buf[--cpos]) != FALSE) {
                    268:                                        ttputc('\b');
                    269:                                        ttputc(' ');
                    270:                                        ttputc('\b');
                    271:                                        --ttcol;
                    272:                                }
                    273:                        }
                    274:                        while ((cpos > 0) && ISWORD(buf[cpos - 1])) {
                    275:                                ttputc('\b');
                    276:                                ttputc(' ');
                    277:                                ttputc('\b');
                    278:                                --ttcol;
                    279:                                if (ISCTRL(buf[--cpos]) != FALSE) {
                    280:                                        ttputc('\b');
                    281:                                        ttputc(' ');
                    282:                                        ttputc('\b');
                    283:                                        --ttcol;
                    284:                                }
                    285:                        }
                    286:                        ttflush();
                    287:                        break;
1.2       millert   288:                case CCHR('\\'):
1.3       millert   289:                case CCHR('Q'):                 /* quote next */
                    290:                        c = getkey(FALSE);
1.22    ! vincent   291:                        /* FALLTHROUGH */
1.3       millert   292:                default:                        /* all the rest */
1.2       millert   293:                        if (cpos < nbuf - 1) {
1.3       millert   294:                                buf[cpos++] = (char)c;
                    295:                                eputc((char)c);
1.1       deraadt   296:                                ttflush();
                    297:                        }
                    298:                }
                    299:        }
1.7       art       300: done:
                    301:        return buf[0] != '\0';
1.1       deraadt   302: }
                    303:
                    304: /*
                    305:  * do completion on a list of objects.
                    306:  */
1.2       millert   307: static int
1.21      vincent   308: complt(int flags, int c, char *buf, int cpos)
1.3       millert   309: {
                    310:        LIST    *lh, *lh2;
                    311:        LIST    *wholelist = NULL;
                    312:        int      i, nxtra, nhits, bxtra, msglen, nshown;
                    313:        int      wflag = FALSE;
                    314:        char    *msg;
                    315:
                    316:        lh = lh2 = NULL;
1.2       millert   317:
                    318:        if ((flags & EFFUNC) != 0) {
                    319:                buf[cpos] = '\0';
1.6       art       320:                wholelist = lh = complete_function_list(buf, c);
1.9       art       321:        } else if ((flags & EFBUF) != 0) {
1.6       art       322:                lh = &(bheadp->b_list);
1.9       art       323:        } else if ((flags & EFFILE) != 0) {
1.6       art       324:                buf[cpos] = '\0';
                    325:                wholelist = lh = make_file_list(buf);
                    326:        } else
                    327:                panic("broken complt call: flags");
                    328:
                    329:        if (c == ' ')
                    330:                wflag = TRUE;
                    331:        else if (c != '\t' && c != CCHR('M'))
                    332:                panic("broken complt call: c");
                    333:
                    334:        nhits = 0;
                    335:        nxtra = HUGE;
                    336:
                    337:        for (; lh != NULL; lh = lh->l_next) {
                    338:                if (memcmp(buf, lh->l_name, cpos) != 0)
                    339:                        continue;
1.2       millert   340:                if (nhits == 0)
1.6       art       341:                        lh2 = lh;
                    342:                ++nhits;
                    343:                if (lh->l_name[cpos] == '\0')
                    344:                        nxtra = -1;
1.3       millert   345:                else {
1.6       art       346:                        bxtra = getxtra(lh, lh2, cpos, wflag);
                    347:                        if (bxtra < nxtra)
                    348:                                nxtra = bxtra;
                    349:                        lh2 = lh;
                    350:                }
                    351:        }
                    352:        if (nhits == 0)
                    353:                msg = " [No match]";
                    354:        else if (nhits > 1 && nxtra == 0)
                    355:                msg = " [Ambiguous]";
                    356:        else {
                    357:                /*
                    358:                 * Being lazy - ought to check length, but all things
                    359:                 * autocompleted have known types/lengths.
                    360:                 */
                    361:                if (nxtra < 0 && nhits > 1 && c == ' ')
                    362:                        nxtra = 1;
                    363:                for (i = 0; i < nxtra; ++i) {
                    364:                        buf[cpos] = lh2->l_name[cpos];
                    365:                        eputc(buf[cpos++]);
1.1       deraadt   366:                }
1.6       art       367:                ttflush();
                    368:                free_file_list(wholelist);
                    369:                if (nxtra < 0 && c != CCHR('M'))
                    370:                        return 0;
                    371:                return nxtra;
1.1       deraadt   372:        }
1.3       millert   373:
1.2       millert   374:        /*
                    375:         * wholelist is null if we are doing buffers.  want to free lists
                    376:         * that were created for us, but not the buffer list!
                    377:         */
1.1       deraadt   378:        free_file_list(wholelist);
1.3       millert   379:
1.1       deraadt   380:        /* Set up backspaces, etc., being mindful of echo line limit */
                    381:        msglen = strlen(msg);
                    382:        nshown = (ttcol + msglen + 2 > ncol) ?
1.2       millert   383:                ncol - ttcol - 2 : msglen;
1.1       deraadt   384:        eputs(msg);
1.2       millert   385:        ttcol -= (i = nshown);  /* update ttcol!                 */
                    386:        while (i--)             /* move back before msg          */
1.1       deraadt   387:                ttputc('\b');
1.2       millert   388:        ttflush();              /* display to user               */
1.1       deraadt   389:        i = nshown;
1.3       millert   390:        while (i--)             /* blank out on next flush       */
1.1       deraadt   391:                eputc(' ');
1.2       millert   392:        ttcol -= (i = nshown);  /* update ttcol on BS's          */
1.1       deraadt   393:        while (i--)
1.2       millert   394:                ttputc('\b');   /* update ttcol again!           */
1.1       deraadt   395:        return 0;
                    396: }
                    397:
                    398: /*
                    399:  * do completion on a list of objects, listing instead of completing
                    400:  */
1.2       millert   401: static int
1.21      vincent   402: complt_list(int flags, int c, char *buf, int cpos)
1.3       millert   403: {
                    404:        LIST    *lh, *lh2, *lh3;
                    405:        LIST    *wholelist = NULL;
                    406:        BUFFER  *bp;
                    407:        int      i, maxwidth, width;
                    408:        int      preflen = 0;
                    409:        int      oldrow = ttrow;
                    410:        int      oldcol = ttcol;
                    411:        int      oldhue = tthue;
1.17      art       412:        char     *linebuf;
1.21      vincent   413:        const char *cp;
1.3       millert   414:
                    415:        lh = NULL;
1.1       deraadt   416:
                    417:        ttflush();
                    418:
                    419:        /* the results are put into a help buffer */
1.2       millert   420:        bp = bfind("*help*", TRUE);
                    421:        if (bclear(bp) == FALSE)
                    422:                return FALSE;
                    423:
                    424:        {       /* this {} present for historical reasons */
                    425:
                    426:                /*
                    427:                 * first get the list of objects.  This list may contain only
                    428:                 * the ones that complete what has been typed, or may be the
                    429:                 * whole list of all objects of this type.  They are filtered
                    430:                 * later in any case.  Set wholelist if the list has been
                    431:                 * cons'ed up just for us, so we can free it later.  We have
                    432:                 * to copy the buffer list for this function even though we
                    433:                 * didn't for complt.  The sorting code does destructive
                    434:                 * changes to the list, which we don't want to happen to the
                    435:                 * main buffer list!
                    436:                 */
                    437:                if ((flags & EFBUF) != 0)
                    438:                        wholelist = lh = copy_list(&(bheadp->b_list));
                    439:                else if ((flags & EFFUNC) != 0) {
                    440:                        buf[cpos] = '\0';
                    441:                        wholelist = lh = complete_function_list(buf, c);
                    442:                } else if ((flags & EFFILE) != 0) {
                    443:                        buf[cpos] = '\0';
1.5       art       444:                        wholelist = lh = make_file_list(buf);
1.2       millert   445:                        /*
                    446:                         * We don't want to display stuff up to the / for file
                    447:                         * names preflen is the list of a prefix of what the
                    448:                         * user typed that should not be displayed.
                    449:                         */
                    450:                        cp = strrchr(buf, '/');
                    451:                        if (cp)
                    452:                                preflen = cp - buf + 1;
                    453:                } else
                    454:                        panic("broken complt call: flags");
1.1       deraadt   455:
                    456:
1.2       millert   457:                /*
                    458:                 * Sort the list, since users expect to see it in alphabetic
                    459:                 * order.
                    460:                 */
                    461:                lh2 = lh;
1.19      vincent   462:                while (lh2 != NULL) {
1.2       millert   463:                        lh3 = lh2->l_next;
1.19      vincent   464:                        while (lh3 != NULL) {
1.2       millert   465:                                if (strcmp(lh2->l_name, lh3->l_name) > 0) {
                    466:                                        cp = lh2->l_name;
                    467:                                        lh2->l_name = lh3->l_name;
                    468:                                        lh3->l_name = cp;
                    469:                                }
                    470:                                lh3 = lh3->l_next;
                    471:                        }
                    472:                        lh2 = lh2->l_next;
                    473:                }
1.1       deraadt   474:
                    475:                /*
1.2       millert   476:                 * First find max width of object to be displayed, so we can
                    477:                 * put several on a line.
1.1       deraadt   478:                 */
1.2       millert   479:                maxwidth = 0;
                    480:                lh2 = lh;
                    481:                while (lh2 != NULL) {
                    482:                        for (i = 0; i < cpos; ++i) {
                    483:                                if (buf[i] != lh2->l_name[i])
                    484:                                        break;
                    485:                        }
                    486:                        if (i == cpos) {
                    487:                                width = strlen(lh2->l_name);
                    488:                                if (width > maxwidth)
                    489:                                        maxwidth = width;
                    490:                        }
                    491:                        lh2 = lh2->l_next;
1.1       deraadt   492:                }
1.2       millert   493:                maxwidth += 1 - preflen;
                    494:
                    495:                /*
                    496:                 * Now do the display.  objects are written into linebuf until
                    497:                 * it fills, and then put into the help buffer.
                    498:                 */
1.17      art       499:                if ((linebuf = malloc((nrow + 1) * sizeof(char))) == NULL)
                    500:                        return FALSE;
1.2       millert   501:                width = 0;
1.19      vincent   502:
                    503:                /*
                    504:                 * We're going to strlcat() into the buffer, so it has to be
                    505:                 * NUL terminated
                    506:                 */
                    507:                linebuf[0] = '\0';
                    508:                for (lh2 = lh; lh2 != NULL; lh2 = lh2->l_next) {
1.2       millert   509:                        for (i = 0; i < cpos; ++i) {
                    510:                                if (buf[i] != lh2->l_name[i])
                    511:                                        break;
                    512:                        }
1.19      vincent   513:                        /* if we have a match */
1.2       millert   514:                        if (i == cpos) {
1.19      vincent   515:                                /* if it wraps */
1.2       millert   516:                                if ((width + maxwidth) > ncol) {
                    517:                                        addline(bp, linebuf);
1.19      vincent   518:                                        linebuf[0] = '\0';
1.2       millert   519:                                        width = 0;
1.19      vincent   520:                                }
                    521:                                strlcat(linebuf, lh2->l_name + preflen, nrow+1);                                i = strlen(lh2->l_name + preflen);
                    522:                                /* make all the objects nicely line up */
                    523:                                memset(linebuf + strlen(linebuf), ' ',
                    524:                                        maxwidth - i);
1.2       millert   525:                                width += maxwidth;
1.19      vincent   526:                                linebuf[width] = '\0';
1.2       millert   527:                        }
                    528:                }
                    529:                if (width > 0) {
                    530:                        addline(bp, linebuf);
1.1       deraadt   531:                }
1.17      art       532:                free(linebuf);
1.2       millert   533:        }
                    534:        /*
                    535:         * Note that we free lists only if they are put in wholelist lists
                    536:         * that were built just for us should be freed.  However when we use
                    537:         * the buffer list, obviously we don't want it freed.
1.1       deraadt   538:         */
                    539:        free_file_list(wholelist);
1.2       millert   540:        popbuftop(bp);          /* split the screen and put up the help
                    541:                                 * buffer */
                    542:        update();               /* needed to make the new stuff actually
                    543:                                 * appear */
                    544:        ttmove(oldrow, oldcol); /* update leaves cursor in arbitrary place */
                    545:        ttcolor(oldhue);        /* with arbitrary color */
1.1       deraadt   546:        ttflush();
                    547:        return 0;
                    548: }
                    549:
                    550: /*
1.3       millert   551:  * The "lp1" and "lp2" point to list structures.  The "cpos" is a horizontal
1.14      mickey    552:  * position in the name.  Return the longest block of characters that can be
                    553:  * autocompleted at this point.  Sometimes the two symbols are the same, but
1.3       millert   554:  * this is normal.
1.2       millert   555:  */
                    556: int
1.21      vincent   557: getxtra(LIST *lp1, LIST *lp2, int cpos, int wflag)
1.2       millert   558: {
1.3       millert   559:        int     i;
1.1       deraadt   560:
                    561:        i = cpos;
                    562:        for (;;) {
1.2       millert   563:                if (lp1->l_name[i] != lp2->l_name[i])
                    564:                        break;
                    565:                if (lp1->l_name[i] == '\0')
                    566:                        break;
1.1       deraadt   567:                ++i;
1.2       millert   568:                if (wflag && !ISWORD(lp1->l_name[i - 1]))
                    569:                        break;
1.1       deraadt   570:        }
                    571:        return (i - cpos);
                    572: }
                    573:
                    574: /*
1.14      mickey    575:  * Special "printf" for the echo line.  Each call to "ewprintf" starts a
                    576:  * new line in the echo area, and ends with an erase to end of the echo
                    577:  * line.  The formatting is done by a call to the standard formatting
1.3       millert   578:  * routine.
1.1       deraadt   579:  */
1.2       millert   580: /* VARARGS */
1.10      art       581: void
1.2       millert   582: ewprintf(const char *fmt, ...)
1.1       deraadt   583: {
1.3       millert   584:        va_list  ap;
1.1       deraadt   585:
                    586: #ifndef NO_MACRO
1.2       millert   587:        if (inmacro)
                    588:                return;
1.3       millert   589: #endif /* !NO_MACRO */
1.2       millert   590:        va_start(ap, fmt);
1.1       deraadt   591:        ttcolor(CTEXT);
1.2       millert   592:        ttmove(nrow - 1, 0);
                    593:        eformat(fmt, ap);
                    594:        va_end(ap);
1.1       deraadt   595:        tteeol();
                    596:        ttflush();
                    597:        epresf = TRUE;
                    598: }
                    599:
                    600: /*
1.14      mickey    601:  * Printf style formatting. This is called by both "ewprintf" and "ereply"
                    602:  * to provide formatting services to their clients.  The move to the start
                    603:  * of the echo line, and the erase to the end of the echo line, is done by
1.3       millert   604:  * the caller.
1.1       deraadt   605:  * Note: %c works, and prints the "name" of the character.
                    606:  * %k prints the name of a key (and takes no arguments).
                    607:  */
1.10      art       608: static void
1.15      mickey    609: eformat(const char *fp, va_list ap)
1.1       deraadt   610: {
1.12      mickey    611:        char    kname[NKNAME], *cp;
                    612:        int     c;
1.1       deraadt   613:
                    614:        while ((c = *fp++) != '\0') {
                    615:                if (c != '%')
                    616:                        eputc(c);
                    617:                else {
                    618:                        c = *fp++;
                    619:                        switch (c) {
                    620:                        case 'c':
1.12      mickey    621:                                keyname(kname, sizeof(kname), va_arg(ap, int));
1.1       deraadt   622:                                eputs(kname);
                    623:                                break;
                    624:
                    625:                        case 'k':
1.12      mickey    626:                                for (cp = kname, c = 0; c < key.k_count; c++) {
                    627:                                        if (c)
                    628:                                                *cp++ = ' ';
                    629:                                        cp = keyname(cp, sizeof(kname) -
                    630:                                            (cp - kname) - 1, key.k_chars[c]);
1.1       deraadt   631:                                }
                    632:                                eputs(kname);
                    633:                                break;
                    634:
                    635:                        case 'd':
1.2       millert   636:                                eputi(va_arg(ap, int), 10);
1.1       deraadt   637:                                break;
                    638:
                    639:                        case 'o':
1.2       millert   640:                                eputi(va_arg(ap, int), 8);
1.1       deraadt   641:                                break;
                    642:
                    643:                        case 's':
1.2       millert   644:                                eputs(va_arg(ap, char *));
1.1       deraadt   645:                                break;
                    646:
1.3       millert   647:                        case 'l':
                    648:                                /* explicit longword */
1.1       deraadt   649:                                c = *fp++;
1.2       millert   650:                                switch (c) {
1.1       deraadt   651:                                case 'd':
1.13      mickey    652:                                        eputl(va_arg(ap, long), 10);
1.1       deraadt   653:                                        break;
                    654:                                default:
                    655:                                        eputc(c);
                    656:                                        break;
                    657:                                }
                    658:                                break;
                    659:
                    660:                        default:
                    661:                                eputc(c);
                    662:                        }
                    663:                }
                    664:        }
                    665: }
                    666:
                    667: /*
                    668:  * Put integer, in radix "r".
                    669:  */
1.10      art       670: static void
1.21      vincent   671: eputi(int i, int r)
1.1       deraadt   672: {
1.3       millert   673:        int      q;
1.1       deraadt   674:
1.2       millert   675:        if (i < 0) {
                    676:                eputc('-');
                    677:                i = -i;
1.1       deraadt   678:        }
1.2       millert   679:        if ((q = i / r) != 0)
1.1       deraadt   680:                eputi(q, r);
1.2       millert   681:        eputc(i % r + '0');
1.1       deraadt   682: }
                    683:
                    684: /*
                    685:  * Put long, in radix "r".
                    686:  */
1.10      art       687: static void
1.21      vincent   688: eputl(long l, int r)
1.1       deraadt   689: {
1.3       millert   690:        long     q;
1.1       deraadt   691:
1.2       millert   692:        if (l < 0) {
                    693:                eputc('-');
                    694:                l = -l;
1.1       deraadt   695:        }
1.2       millert   696:        if ((q = l / r) != 0)
1.1       deraadt   697:                eputl(q, r);
1.3       millert   698:        eputc((int)(l % r) + '0');
1.1       deraadt   699: }
                    700:
                    701: /*
                    702:  * Put string.
                    703:  */
1.10      art       704: static void
1.21      vincent   705: eputs(const char *s)
1.1       deraadt   706: {
1.3       millert   707:        int      c;
1.1       deraadt   708:
                    709:        while ((c = *s++) != '\0')
                    710:                eputc(c);
                    711: }
                    712:
                    713: /*
1.14      mickey    714:  * Put character.  Watch for control characters, and for the line getting
1.3       millert   715:  * too long.
1.1       deraadt   716:  */
1.10      art       717: static void
1.21      vincent   718: eputc(char c)
1.1       deraadt   719: {
1.2       millert   720:        if (ttcol + 2 < ncol) {
1.1       deraadt   721:                if (ISCTRL(c)) {
                    722:                        eputc('^');
                    723:                        c = CCHR(c);
                    724:                }
                    725:                ttputc(c);
                    726:                ++ttcol;
                    727:        }
                    728: }
                    729:
1.10      art       730: void
1.21      vincent   731: free_file_list(LIST *lp)
1.1       deraadt   732: {
1.3       millert   733:        LIST    *next;
1.2       millert   734:
                    735:        while (lp) {
                    736:                next = lp->l_next;
                    737:                free(lp);
                    738:                lp = next;
                    739:        }
1.1       deraadt   740: }
                    741:
1.2       millert   742: static LIST *
1.21      vincent   743: copy_list(LIST *lp)
1.2       millert   744: {
1.3       millert   745:        LIST    *current, *last;
1.2       millert   746:
                    747:        last = NULL;
                    748:        while (lp) {
1.3       millert   749:                current = (LIST *)malloc(sizeof(LIST));
1.2       millert   750:                current->l_next = last;
                    751:                current->l_name = lp->l_name;
1.3       millert   752:                last = (LIST *)current;
1.2       millert   753:                lp = lp->l_next;
                    754:        }
                    755:        return (last);
1.1       deraadt   756: }