[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.7

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