[BACK]Return to readline.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libedit

Annotation of src/lib/libedit/readline.c, Revision 1.114

1.114   ! christos    1: /*     $NetBSD: readline.c,v 1.113 2014/10/18 08:33:23 snj Exp $       */
1.1       christos    2:
                      3: /*-
                      4:  * Copyright (c) 1997 The NetBSD Foundation, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Jaromir Dolecek.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31:
1.20      christos   32: #include "config.h"
1.1       christos   33: #if !defined(lint) && !defined(SCCSID)
1.114   ! christos   34: __RCSID("$NetBSD: readline.c,v 1.113 2014/10/18 08:33:23 snj Exp $");
1.1       christos   35: #endif /* not lint && not SCCSID */
                     36:
                     37: #include <sys/types.h>
                     38: #include <sys/stat.h>
                     39: #include <stdio.h>
                     40: #include <dirent.h>
                     41: #include <string.h>
                     42: #include <pwd.h>
                     43: #include <ctype.h>
                     44: #include <stdlib.h>
                     45: #include <unistd.h>
                     46: #include <limits.h>
1.38      christos   47: #include <errno.h>
                     48: #include <fcntl.h>
1.70      christos   49: #include <setjmp.h>
1.35      christos   50: #include <vis.h>
1.97      christos   51:
1.79      sketch     52: #include "readline/readline.h"
1.45      christos   53: #include "el.h"
                     54: #include "fcns.h"              /* for EL_NUM_FCNS */
1.1       christos   55: #include "histedit.h"
1.53      dsl        56: #include "filecomplete.h"
1.1       christos   57:
1.68      christos   58: void rl_prep_terminal(int);
                     59: void rl_deprep_terminal(void);
                     60:
1.1       christos   61: /* for rl_complete() */
1.40      christos   62: #define TAB            '\r'
1.1       christos   63:
                     64: /* see comment at the #ifdef for sense of this */
1.40      christos   65: /* #define GDB_411_HACK */
1.1       christos   66:
                     67: /* readline compatibility stuff - look at readline sources/documentation */
                     68: /* to see what these variables mean */
1.11      lukem      69: const char *rl_library_version = "EditLine wrapper";
1.85      christos   70: int rl_readline_version = RL_READLINE_VERSION;
1.20      christos   71: static char empty[] = { '\0' };
                     72: static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };
                     73: static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
                     74:     '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
                     75: char *rl_readline_name = empty;
1.11      lukem      76: FILE *rl_instream = NULL;
                     77: FILE *rl_outstream = NULL;
                     78: int rl_point = 0;
                     79: int rl_end = 0;
                     80: char *rl_line_buffer = NULL;
1.55      agc        81: VCPFunction *rl_linefunc = NULL;
1.38      christos   82: int rl_done = 0;
                     83: VFunction *rl_event_hook = NULL;
1.70      christos   84: KEYMAP_ENTRY_ARRAY emacs_standard_keymap,
                     85:     emacs_meta_keymap,
                     86:     emacs_ctlx_keymap;
1.108     christos   87: /*
                     88:  * The following is not implemented; we always catch signals in the
                     89:  * libedit fashion: set handlers on entry to el_gets() and clear them
                     90:  * on the way out. This simplistic approach works for most cases; if
                     91:  * it does not work for your application, please let us know.
                     92:  */
                     93: int rl_catch_signals = 1;
                     94: int rl_catch_sigwinch = 1;
1.11      lukem      95:
                     96: int history_base = 1;          /* probably never subject to change */
                     97: int history_length = 0;
                     98: int max_input_history = 0;
                     99: char history_expansion_char = '!';
                    100: char history_subst_char = '^';
1.20      christos  101: char *history_no_expand_chars = expand_chars;
1.11      lukem     102: Function *history_inhibit_expansion_function = NULL;
1.40      christos  103: char *history_arg_extract(int start, int end, const char *str);
1.11      lukem     104:
                    105: int rl_inhibit_completion = 0;
                    106: int rl_attempted_completion_over = 0;
1.20      christos  107: char *rl_basic_word_break_characters = break_chars;
1.11      lukem     108: char *rl_completer_word_break_characters = NULL;
                    109: char *rl_completer_quote_characters = NULL;
1.34      christos  110: Function *rl_completion_entry_function = NULL;
1.103     christos  111: char *(*rl_completion_word_break_hook)(void) = NULL;
1.11      lukem     112: CPPFunction *rl_attempted_completion_function = NULL;
1.32      christos  113: Function *rl_pre_input_hook = NULL;
                    114: Function *rl_startup1_hook = NULL;
1.72      christos  115: int (*rl_getc_function)(FILE *) = NULL;
1.32      christos  116: char *rl_terminal_name = NULL;
                    117: int rl_already_prompted = 0;
                    118: int rl_filename_completion_desired = 0;
                    119: int rl_ignore_completion_duplicates = 0;
1.70      christos  120: int readline_echoing_p = 1;
                    121: int _rl_print_completions_horizontally = 0;
1.32      christos  122: VFunction *rl_redisplay_function = NULL;
1.33      christos  123: Function *rl_startup_hook = NULL;
1.34      christos  124: VFunction *rl_completion_display_matches_hook = NULL;
1.68      christos  125: VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal;
                    126: VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal;
1.85      christos  127: KEYMAP_ENTRY_ARRAY emacs_meta_keymap;
1.1       christos  128:
1.12      jdolecek  129: /*
1.32      christos  130:  * The current prompt string.
                    131:  */
                    132: char *rl_prompt = NULL;
                    133: /*
1.12      jdolecek  134:  * This is set to character indicating type of completion being done by
                    135:  * rl_complete_internal(); this is available for application completion
                    136:  * functions.
                    137:  */
                    138: int rl_completion_type = 0;
                    139:
                    140: /*
                    141:  * If more than this number of items results from query for possible
                    142:  * completions, we ask user if they are sure to really display the list.
                    143:  */
                    144: int rl_completion_query_items = 100;
                    145:
                    146: /*
1.15      jdolecek  147:  * List of characters which are word break characters, but should be left
                    148:  * in the parsed text when it is passed to the completion function.
                    149:  * Shell uses this to help determine what kind of completing to do.
1.12      jdolecek  150:  */
1.53      dsl       151: char *rl_special_prefixes = NULL;
1.15      jdolecek  152:
                    153: /*
                    154:  * This is the character appended to the completed words if at the end of
                    155:  * the line. Default is ' ' (a space).
                    156:  */
                    157: int rl_completion_append_character = ' ';
                    158:
                    159: /* stuff below is used internally by libedit for readline emulation */
                    160:
1.92      christos  161: static History *h = NULL;
1.1       christos  162: static EditLine *e = NULL;
1.35      christos  163: static Function *map[256];
1.70      christos  164: static jmp_buf topbuf;
1.1       christos  165:
                    166: /* internal functions */
1.11      lukem     167: static unsigned char    _el_rl_complete(EditLine *, int);
1.46      christos  168: static unsigned char    _el_rl_tstp(EditLine *, int);
1.11      lukem     169: static char            *_get_prompt(EditLine *);
1.59      christos  170: static int              _getc_function(EditLine *, char *);
1.11      lukem     171: static HIST_ENTRY      *_move_history(int);
1.40      christos  172: static int              _history_expand_command(const char *, size_t, size_t,
                    173:     char **);
1.11      lukem     174: static char            *_rl_compat_sub(const char *, const char *,
1.40      christos  175:     const char *, int);
1.38      christos  176: static int              _rl_event_read_char(EditLine *, char *);
1.49      christos  177: static void             _rl_update_pos(void);
1.1       christos  178:
1.5       christos  179:
                    180: /* ARGSUSED */
1.1       christos  181: static char *
1.30      christos  182: _get_prompt(EditLine *el __attribute__((__unused__)))
1.1       christos  183: {
1.32      christos  184:        rl_already_prompted = 1;
1.96      christos  185:        return rl_prompt;
1.1       christos  186: }
                    187:
1.11      lukem     188:
1.1       christos  189: /*
                    190:  * generic function for moving around history
                    191:  */
1.2       christos  192: static HIST_ENTRY *
1.11      lukem     193: _move_history(int op)
1.1       christos  194: {
1.92      christos  195:        HistEvent ev;
1.1       christos  196:        static HIST_ENTRY rl_he;
                    197:
1.92      christos  198:        if (history(h, &ev, op) != 0)
1.96      christos  199:                return NULL;
1.1       christos  200:
1.92      christos  201:        rl_he.line = ev.str;
1.40      christos  202:        rl_he.data = NULL;
1.1       christos  203:
1.96      christos  204:        return &rl_he;
1.1       christos  205: }
                    206:
                    207:
1.7       simonb    208: /*
1.59      christos  209:  * read one key from user defined input function
                    210:  */
                    211: static int
1.61      christos  212: /*ARGSUSED*/
1.97      christos  213: _getc_function(EditLine *el __attribute__((__unused__)), char *c)
1.59      christos  214: {
                    215:        int i;
                    216:
1.72      christos  217:        i = (*rl_getc_function)(NULL);
1.59      christos  218:        if (i == -1)
                    219:                return 0;
1.99      christos  220:        *c = (char)i;
1.59      christos  221:        return 1;
                    222: }
                    223:
1.91      christos  224: static void
                    225: _resize_fun(EditLine *el, void *a)
                    226: {
                    227:        const LineInfo *li;
                    228:        char **ap = a;
                    229:
                    230:        li = el_line(el);
                    231:        /* a cheesy way to get rid of const cast. */
1.98      christos  232:        *ap = memchr(li->buffer, *li->buffer, (size_t)1);
1.91      christos  233: }
                    234:
1.85      christos  235: static const char *
                    236: _default_history_file(void)
                    237: {
                    238:        struct passwd *p;
1.109     christos  239:        static char *path;
                    240:        size_t len;
1.85      christos  241:
1.109     christos  242:        if (path)
1.85      christos  243:                return path;
1.109     christos  244:
1.85      christos  245:        if ((p = getpwuid(getuid())) == NULL)
                    246:                return NULL;
1.109     christos  247:
                    248:        len = strlen(p->pw_dir) + sizeof("/.history");
                    249:        if ((path = malloc(len)) == NULL)
                    250:                return NULL;
                    251:
                    252:        (void)snprintf(path, len, "%s/.history", p->pw_dir);
1.85      christos  253:        return path;
                    254: }
1.59      christos  255:
                    256: /*
1.7       simonb    257:  * READLINE compatibility stuff
1.1       christos  258:  */
                    259:
                    260: /*
1.78      christos  261:  * Set the prompt
                    262:  */
                    263: int
                    264: rl_set_prompt(const char *prompt)
                    265: {
1.82      christos  266:        char *p;
                    267:
1.78      christos  268:        if (!prompt)
                    269:                prompt = "";
                    270:        if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0)
                    271:                return 0;
                    272:        if (rl_prompt)
1.95      christos  273:                el_free(rl_prompt);
1.78      christos  274:        rl_prompt = strdup(prompt);
1.82      christos  275:        if (rl_prompt == NULL)
                    276:                return -1;
                    277:
                    278:        while ((p = strchr(rl_prompt, RL_PROMPT_END_IGNORE)) != NULL)
                    279:                *p = RL_PROMPT_START_IGNORE;
                    280:
                    281:        return 0;
1.78      christos  282: }
                    283:
                    284: /*
1.1       christos  285:  * initialize rl compat stuff
                    286:  */
                    287: int
1.11      lukem     288: rl_initialize(void)
1.1       christos  289: {
1.92      christos  290:        HistEvent ev;
1.18      christos  291:        int editmode = 1;
                    292:        struct termios t;
1.1       christos  293:
                    294:        if (e != NULL)
                    295:                el_end(e);
                    296:        if (h != NULL)
1.92      christos  297:                history_end(h);
1.1       christos  298:
                    299:        if (!rl_instream)
                    300:                rl_instream = stdin;
                    301:        if (!rl_outstream)
                    302:                rl_outstream = stdout;
1.18      christos  303:
                    304:        /*
                    305:         * See if we don't really want to run the editor
                    306:         */
                    307:        if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0)
                    308:                editmode = 0;
                    309:
1.4       christos  310:        e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);
1.18      christos  311:
                    312:        if (!editmode)
1.92      christos  313:                el_set(e, EL_EDITMODE, 0);
1.1       christos  314:
1.92      christos  315:        h = history_init();
1.1       christos  316:        if (!e || !h)
1.96      christos  317:                return -1;
1.1       christos  318:
1.92      christos  319:        history(h, &ev, H_SETSIZE, INT_MAX);    /* unlimited */
1.1       christos  320:        history_length = 0;
                    321:        max_input_history = INT_MAX;
                    322:        el_set(e, EL_HIST, history, h);
                    323:
1.91      christos  324:        /* Setup resize function */
                    325:        el_set(e, EL_RESIZE, _resize_fun, &rl_line_buffer);
                    326:
1.59      christos  327:        /* setup getc function if valid */
                    328:        if (rl_getc_function)
                    329:                el_set(e, EL_GETCFN, _getc_function);
                    330:
1.1       christos  331:        /* for proper prompt printing in readline() */
1.78      christos  332:        if (rl_set_prompt("") == -1) {
1.92      christos  333:                history_end(h);
1.23      christos  334:                el_end(e);
                    335:                return -1;
                    336:        }
1.82      christos  337:        el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE);
1.39      christos  338:        el_set(e, EL_SIGNAL, rl_catch_signals);
1.1       christos  339:
                    340:        /* set default mode to "emacs"-style and read setting afterwards */
1.112     christos  341:        /* so this can be overridden */
1.1       christos  342:        el_set(e, EL_EDITOR, "emacs");
1.32      christos  343:        if (rl_terminal_name != NULL)
                    344:                el_set(e, EL_TERMINAL, rl_terminal_name);
                    345:        else
                    346:                el_get(e, EL_TERMINAL, &rl_terminal_name);
1.1       christos  347:
1.12      jdolecek  348:        /*
1.35      christos  349:         * Word completion - this has to go AFTER rebinding keys
1.12      jdolecek  350:         * to emacs-style.
                    351:         */
1.1       christos  352:        el_set(e, EL_ADDFN, "rl_complete",
1.35      christos  353:            "ReadLine compatible completion function",
1.11      lukem     354:            _el_rl_complete);
1.1       christos  355:        el_set(e, EL_BIND, "^I", "rl_complete", NULL);
1.46      christos  356:
                    357:        /*
                    358:         * Send TSTP when ^Z is pressed.
                    359:         */
                    360:        el_set(e, EL_ADDFN, "rl_tstp",
                    361:            "ReadLine compatible suspend function",
                    362:            _el_rl_tstp);
                    363:        el_set(e, EL_BIND, "^Z", "rl_tstp", NULL);
1.12      jdolecek  364:
1.114   ! christos  365:        /*
        !           366:         * Set some readline compatible key-bindings.
        !           367:         */
        !           368:        el_set(e, EL_BIND, "^R", "em-inc-search-prev", NULL);
        !           369:
        !           370:        /*
        !           371:         * Allow the use of Home/End keys.
        !           372:         */
        !           373:        el_set(e, EL_BIND, "\\e[1~", "ed-move-to-beg", NULL);
        !           374:        el_set(e, EL_BIND, "\\e[4~", "ed-move-to-end", NULL);
        !           375:        el_set(e, EL_BIND, "\\e[7~", "ed-move-to-beg", NULL);
        !           376:        el_set(e, EL_BIND, "\\e[8~", "ed-move-to-end", NULL);
        !           377:        el_set(e, EL_BIND, "\\e[H", "ed-move-to-beg", NULL);
        !           378:        el_set(e, EL_BIND, "\\e[F", "ed-move-to-end", NULL);
        !           379:
        !           380:        /*
        !           381:         * Allow the use of the Delete/Insert keys.
        !           382:         */
        !           383:        el_set(e, EL_BIND, "\\e[3~", "ed-delete-next-char", NULL);
        !           384:        el_set(e, EL_BIND, "\\e[2~", "ed-quoted-insert", NULL);
        !           385:
        !           386:        /*
        !           387:         * Ctrl-left-arrow and Ctrl-right-arrow for word moving.
        !           388:         */
        !           389:        el_set(e, EL_BIND, "\\e[1;5C", "em-next-word", NULL);
        !           390:        el_set(e, EL_BIND, "\\e[1;5D", "ed-prev-word", NULL);
        !           391:        el_set(e, EL_BIND, "\\e[5C", "em-next-word", NULL);
        !           392:        el_set(e, EL_BIND, "\\e[5D", "ed-prev-word", NULL);
        !           393:        el_set(e, EL_BIND, "\\e\\e[C", "em-next-word", NULL);
        !           394:        el_set(e, EL_BIND, "\\e\\e[D", "ed-prev-word", NULL);
        !           395:
1.1       christos  396:        /* read settings from configuration file */
                    397:        el_source(e, NULL);
                    398:
1.12      jdolecek  399:        /*
                    400:         * Unfortunately, some applications really do use rl_point
                    401:         * and rl_line_buffer directly.
                    402:         */
1.91      christos  403:        _resize_fun(e, &rl_line_buffer);
1.49      christos  404:        _rl_update_pos();
1.1       christos  405:
1.32      christos  406:        if (rl_startup_hook)
                    407:                (*rl_startup_hook)(NULL, 0);
                    408:
1.96      christos  409:        return 0;
1.1       christos  410: }
                    411:
1.11      lukem     412:
1.1       christos  413: /*
                    414:  * read one line from input stream and return it, chomping
                    415:  * trailing newline (if there is any)
                    416:  */
                    417: char *
1.70      christos  418: readline(const char *p)
1.1       christos  419: {
1.92      christos  420:        HistEvent ev;
1.70      christos  421:        const char * volatile prompt = p;
1.3       thorpej   422:        int count;
1.1       christos  423:        const char *ret;
1.20      christos  424:        char *buf;
1.38      christos  425:        static int used_event_hook;
1.1       christos  426:
                    427:        if (e == NULL || h == NULL)
                    428:                rl_initialize();
                    429:
1.38      christos  430:        rl_done = 0;
                    431:
1.70      christos  432:        (void)setjmp(topbuf);
                    433:
1.9       jdolecek  434:        /* update prompt accordingly to what has been passed */
1.78      christos  435:        if (rl_set_prompt(prompt) == -1)
                    436:                return NULL;
1.32      christos  437:
                    438:        if (rl_pre_input_hook)
                    439:                (*rl_pre_input_hook)(NULL, 0);
                    440:
1.38      christos  441:        if (rl_event_hook && !(e->el_flags&NO_TTY)) {
                    442:                el_set(e, EL_GETCFN, _rl_event_read_char);
                    443:                used_event_hook = 1;
                    444:        }
                    445:
                    446:        if (!rl_event_hook && used_event_hook) {
                    447:                el_set(e, EL_GETCFN, EL_BUILTIN_GETCFN);
                    448:                used_event_hook = 0;
                    449:        }
                    450:
1.32      christos  451:        rl_already_prompted = 0;
                    452:
1.1       christos  453:        /* get one line from input stream */
                    454:        ret = el_gets(e, &count);
                    455:
                    456:        if (ret && count > 0) {
1.5       christos  457:                int lastidx;
1.1       christos  458:
1.20      christos  459:                buf = strdup(ret);
1.23      christos  460:                if (buf == NULL)
                    461:                        return NULL;
1.1       christos  462:                lastidx = count - 1;
1.20      christos  463:                if (buf[lastidx] == '\n')
                    464:                        buf[lastidx] = '\0';
1.1       christos  465:        } else
1.20      christos  466:                buf = NULL;
1.1       christos  467:
1.92      christos  468:        history(h, &ev, H_GETSIZE);
1.1       christos  469:        history_length = ev.num;
                    470:
1.20      christos  471:        return buf;
1.1       christos  472: }
                    473:
                    474: /*
                    475:  * history functions
                    476:  */
                    477:
                    478: /*
                    479:  * is normally called before application starts to use
                    480:  * history expansion functions
                    481:  */
                    482: void
1.11      lukem     483: using_history(void)
1.1       christos  484: {
                    485:        if (h == NULL || e == NULL)
                    486:                rl_initialize();
                    487: }
                    488:
1.11      lukem     489:
1.1       christos  490: /*
                    491:  * substitute ``what'' with ``with'', returning resulting string; if
1.29      wiz       492:  * globally == 1, substitutes all occurrences of what, otherwise only the
1.1       christos  493:  * first one
                    494:  */
1.11      lukem     495: static char *
                    496: _rl_compat_sub(const char *str, const char *what, const char *with,
                    497:     int globally)
                    498: {
1.40      christos  499:        const   char    *s;
                    500:        char    *r, *result;
                    501:        size_t  len, with_len, what_len;
1.1       christos  502:
1.40      christos  503:        len = strlen(str);
                    504:        with_len = strlen(with);
                    505:        what_len = strlen(what);
                    506:
                    507:        /* calculate length we need for result */
                    508:        s = str;
                    509:        while (*s) {
                    510:                if (*s == *what && !strncmp(s, what, what_len)) {
                    511:                        len += with_len - what_len;
                    512:                        if (!globally)
                    513:                                break;
                    514:                        s += what_len;
                    515:                } else
                    516:                        s++;
                    517:        }
1.95      christos  518:        r = result = el_malloc((len + 1) * sizeof(*r));
1.23      christos  519:        if (result == NULL)
                    520:                return NULL;
1.40      christos  521:        s = str;
                    522:        while (*s) {
                    523:                if (*s == *what && !strncmp(s, what, what_len)) {
                    524:                        (void)strncpy(r, with, with_len);
                    525:                        r += with_len;
                    526:                        s += what_len;
                    527:                        if (!globally) {
                    528:                                (void)strcpy(r, s);
1.96      christos  529:                                return result;
1.1       christos  530:                        }
1.40      christos  531:                } else
                    532:                        *r++ = *s++;
                    533:        }
1.76      christos  534:        *r = '\0';
1.96      christos  535:        return result;
1.40      christos  536: }
                    537:
                    538: static char    *last_search_pat;       /* last !?pat[?] search pattern */
                    539: static char    *last_search_match;     /* last !?pat[?] that matched */
                    540:
                    541: const char *
                    542: get_history_event(const char *cmd, int *cindex, int qchar)
                    543: {
                    544:        int idx, sign, sub, num, begin, ret;
                    545:        size_t len;
                    546:        char    *pat;
                    547:        const char *rptr;
1.92      christos  548:        HistEvent ev;
1.40      christos  549:
                    550:        idx = *cindex;
                    551:        if (cmd[idx++] != history_expansion_char)
1.96      christos  552:                return NULL;
1.40      christos  553:
                    554:        /* find out which event to take */
1.76      christos  555:        if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') {
1.92      christos  556:                if (history(h, &ev, H_FIRST) != 0)
1.96      christos  557:                        return NULL;
1.40      christos  558:                *cindex = cmd[idx]? (idx + 1):idx;
1.92      christos  559:                return ev.str;
1.40      christos  560:        }
                    561:        sign = 0;
                    562:        if (cmd[idx] == '-') {
                    563:                sign = 1;
                    564:                idx++;
                    565:        }
                    566:
                    567:        if ('0' <= cmd[idx] && cmd[idx] <= '9') {
                    568:                HIST_ENTRY *rl_he;
                    569:
                    570:                num = 0;
                    571:                while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') {
                    572:                        num = num * 10 + cmd[idx] - '0';
                    573:                        idx++;
                    574:                }
                    575:                if (sign)
                    576:                        num = history_length - num + 1;
                    577:
                    578:                if (!(rl_he = history_get(num)))
1.96      christos  579:                        return NULL;
1.40      christos  580:
                    581:                *cindex = idx;
1.96      christos  582:                return rl_he->line;
1.40      christos  583:        }
                    584:        sub = 0;
                    585:        if (cmd[idx] == '?') {
                    586:                sub = 1;
                    587:                idx++;
                    588:        }
                    589:        begin = idx;
                    590:        while (cmd[idx]) {
                    591:                if (cmd[idx] == '\n')
                    592:                        break;
                    593:                if (sub && cmd[idx] == '?')
                    594:                        break;
                    595:                if (!sub && (cmd[idx] == ':' || cmd[idx] == ' '
                    596:                                    || cmd[idx] == '\t' || cmd[idx] == qchar))
                    597:                        break;
                    598:                idx++;
                    599:        }
1.99      christos  600:        len = (size_t)idx - (size_t)begin;
1.40      christos  601:        if (sub && cmd[idx] == '?')
                    602:                idx++;
                    603:        if (sub && len == 0 && last_search_pat && *last_search_pat)
                    604:                pat = last_search_pat;
                    605:        else if (len == 0)
1.96      christos  606:                return NULL;
1.40      christos  607:        else {
1.95      christos  608:                if ((pat = el_malloc((len + 1) * sizeof(*pat))) == NULL)
1.40      christos  609:                        return NULL;
                    610:                (void)strncpy(pat, cmd + begin, len);
                    611:                pat[len] = '\0';
                    612:        }
                    613:
1.92      christos  614:        if (history(h, &ev, H_CURR) != 0) {
1.40      christos  615:                if (pat != last_search_pat)
1.95      christos  616:                        el_free(pat);
1.96      christos  617:                return NULL;
1.40      christos  618:        }
                    619:        num = ev.num;
                    620:
                    621:        if (sub) {
                    622:                if (pat != last_search_pat) {
                    623:                        if (last_search_pat)
1.95      christos  624:                                el_free(last_search_pat);
1.40      christos  625:                        last_search_pat = pat;
1.1       christos  626:                }
1.40      christos  627:                ret = history_search(pat, -1);
                    628:        } else
                    629:                ret = history_search_prefix(pat, -1);
                    630:
                    631:        if (ret == -1) {
                    632:                /* restore to end of list on failed search */
1.92      christos  633:                history(h, &ev, H_FIRST);
1.40      christos  634:                (void)fprintf(rl_outstream, "%s: Event not found\n", pat);
                    635:                if (pat != last_search_pat)
1.95      christos  636:                        el_free(pat);
1.96      christos  637:                return NULL;
1.40      christos  638:        }
                    639:
                    640:        if (sub && len) {
                    641:                if (last_search_match && last_search_match != pat)
1.95      christos  642:                        el_free(last_search_match);
1.40      christos  643:                last_search_match = pat;
                    644:        }
                    645:
                    646:        if (pat != last_search_pat)
1.95      christos  647:                el_free(pat);
1.40      christos  648:
1.92      christos  649:        if (history(h, &ev, H_CURR) != 0)
1.96      christos  650:                return NULL;
1.40      christos  651:        *cindex = idx;
1.92      christos  652:        rptr = ev.str;
1.40      christos  653:
                    654:        /* roll back to original position */
1.92      christos  655:        (void)history(h, &ev, H_SET, num);
1.1       christos  656:
1.40      christos  657:        return rptr;
1.1       christos  658: }
                    659:
                    660: /*
                    661:  * the real function doing history expansion - takes as argument command
                    662:  * to do and data upon which the command should be executed
                    663:  * does expansion the way I've understood readline documentation
                    664:  *
                    665:  * returns 0 if data was not modified, 1 if it was and 2 if the string
                    666:  * should be only printed and not executed; in case of error,
                    667:  * returns -1 and *result points to NULL
1.113     snj       668:  * it's the caller's responsibility to free() the string returned in *result
1.1       christos  669:  */
                    670: static int
1.40      christos  671: _history_expand_command(const char *command, size_t offs, size_t cmdlen,
                    672:     char **result)
1.11      lukem     673: {
1.40      christos  674:        char *tmp, *search = NULL, *aptr;
                    675:        const char *ptr, *cmd;
1.11      lukem     676:        static char *from = NULL, *to = NULL;
1.40      christos  677:        int start, end, idx, has_mods = 0;
                    678:        int p_on = 0, g_on = 0;
1.1       christos  679:
                    680:        *result = NULL;
1.40      christos  681:        aptr = NULL;
1.41      christos  682:        ptr = NULL;
1.1       christos  683:
1.40      christos  684:        /* First get event specifier */
                    685:        idx = 0;
1.1       christos  686:
1.40      christos  687:        if (strchr(":^*$", command[offs + 1])) {
                    688:                char str[4];
                    689:                /*
                    690:                * "!:" is shorthand for "!!:".
                    691:                * "!^", "!*" and "!$" are shorthand for
                    692:                * "!!:^", "!!:*" and "!!:$" respectively.
                    693:                */
                    694:                str[0] = str[1] = '!';
                    695:                str[2] = '0';
                    696:                ptr = get_history_event(str, &idx, 0);
                    697:                idx = (command[offs + 1] == ':')? 1:0;
                    698:                has_mods = 1;
1.1       christos  699:        } else {
1.40      christos  700:                if (command[offs + 1] == '#') {
                    701:                        /* use command so far */
1.95      christos  702:                        if ((aptr = el_malloc((offs + 1) * sizeof(*aptr)))
                    703:                            == NULL)
1.40      christos  704:                                return -1;
                    705:                        (void)strncpy(aptr, command, offs);
                    706:                        aptr[offs] = '\0';
                    707:                        idx = 1;
1.1       christos  708:                } else {
1.40      christos  709:                        int     qchar;
                    710:
                    711:                        qchar = (offs > 0 && command[offs - 1] == '"')? '"':0;
                    712:                        ptr = get_history_event(command + offs, &idx, qchar);
                    713:                }
1.99      christos  714:                has_mods = command[offs + (size_t)idx] == ':';
1.40      christos  715:        }
1.1       christos  716:
1.40      christos  717:        if (ptr == NULL && aptr == NULL)
1.96      christos  718:                return -1;
1.1       christos  719:
1.40      christos  720:        if (!has_mods) {
1.85      christos  721:                *result = strdup(aptr ? aptr : ptr);
1.40      christos  722:                if (aptr)
1.95      christos  723:                        el_free(aptr);
1.85      christos  724:                if (*result == NULL)
                    725:                        return -1;
1.96      christos  726:                return 1;
1.40      christos  727:        }
                    728:
                    729:        cmd = command + offs + idx + 1;
                    730:
                    731:        /* Now parse any word designators */
                    732:
                    733:        if (*cmd == '%')        /* last word matched by ?pat? */
                    734:                tmp = strdup(last_search_match? last_search_match:"");
                    735:        else if (strchr("^*$-0123456789", *cmd)) {
                    736:                start = end = -1;
                    737:                if (*cmd == '^')
                    738:                        start = end = 1, cmd++;
                    739:                else if (*cmd == '$')
                    740:                        start = -1, cmd++;
                    741:                else if (*cmd == '*')
                    742:                        start = 1, cmd++;
                    743:               else if (*cmd == '-' || isdigit((unsigned char) *cmd)) {
                    744:                        start = 0;
                    745:                        while (*cmd && '0' <= *cmd && *cmd <= '9')
                    746:                                start = start * 10 + *cmd++ - '0';
                    747:
                    748:                        if (*cmd == '-') {
                    749:                                if (isdigit((unsigned char) cmd[1])) {
                    750:                                        cmd++;
                    751:                                        end = 0;
                    752:                                        while (*cmd && '0' <= *cmd && *cmd <= '9')
                    753:                                                end = end * 10 + *cmd++ - '0';
                    754:                                } else if (cmd[1] == '$') {
                    755:                                        cmd += 2;
                    756:                                        end = -1;
                    757:                                } else {
                    758:                                        cmd++;
                    759:                                        end = -2;
                    760:                                }
                    761:                        } else if (*cmd == '*')
                    762:                                end = -1, cmd++;
1.1       christos  763:                        else
1.40      christos  764:                                end = start;
                    765:                }
                    766:                tmp = history_arg_extract(start, end, aptr? aptr:ptr);
                    767:                if (tmp == NULL) {
                    768:                        (void)fprintf(rl_outstream, "%s: Bad word specifier",
                    769:                            command + offs + idx);
                    770:                        if (aptr)
1.95      christos  771:                                el_free(aptr);
1.96      christos  772:                        return -1;
1.40      christos  773:                }
                    774:        } else
                    775:                tmp = strdup(aptr? aptr:ptr);
1.1       christos  776:
1.40      christos  777:        if (aptr)
1.95      christos  778:                el_free(aptr);
1.1       christos  779:
1.77      lukem     780:        if (*cmd == '\0' || ((size_t)(cmd - (command + offs)) >= cmdlen)) {
1.40      christos  781:                *result = tmp;
1.96      christos  782:                return 1;
1.1       christos  783:        }
                    784:
                    785:        for (; *cmd; cmd++) {
                    786:                if (*cmd == ':')
                    787:                        continue;
1.40      christos  788:                else if (*cmd == 'h') {         /* remove trailing path */
                    789:                        if ((aptr = strrchr(tmp, '/')) != NULL)
1.76      christos  790:                                *aptr = '\0';
1.40      christos  791:                } else if (*cmd == 't') {       /* remove leading path */
                    792:                        if ((aptr = strrchr(tmp, '/')) != NULL) {
                    793:                                aptr = strdup(aptr + 1);
1.95      christos  794:                                el_free(tmp);
1.40      christos  795:                                tmp = aptr;
                    796:                        }
                    797:                } else if (*cmd == 'r') {       /* remove trailing suffix */
                    798:                        if ((aptr = strrchr(tmp, '.')) != NULL)
1.76      christos  799:                                *aptr = '\0';
1.40      christos  800:                } else if (*cmd == 'e') {       /* remove all but suffix */
                    801:                        if ((aptr = strrchr(tmp, '.')) != NULL) {
                    802:                                aptr = strdup(aptr);
1.95      christos  803:                                el_free(tmp);
1.40      christos  804:                                tmp = aptr;
                    805:                        }
                    806:                } else if (*cmd == 'p')         /* print only */
                    807:                        p_on = 1;
1.1       christos  808:                else if (*cmd == 'g')
                    809:                        g_on = 2;
                    810:                else if (*cmd == 's' || *cmd == '&') {
1.11      lukem     811:                        char *what, *with, delim;
1.30      christos  812:                        size_t len, from_len;
1.5       christos  813:                        size_t size;
1.1       christos  814:
                    815:                        if (*cmd == '&' && (from == NULL || to == NULL))
                    816:                                continue;
                    817:                        else if (*cmd == 's') {
                    818:                                delim = *(++cmd), cmd++;
                    819:                                size = 16;
1.95      christos  820:                                what = el_realloc(from, size * sizeof(*what));
1.23      christos  821:                                if (what == NULL) {
1.95      christos  822:                                        el_free(from);
                    823:                                        el_free(tmp);
1.28      christos  824:                                        return 0;
1.23      christos  825:                                }
1.1       christos  826:                                len = 0;
                    827:                                for (; *cmd && *cmd != delim; cmd++) {
1.40      christos  828:                                        if (*cmd == '\\' && cmd[1] == delim)
1.1       christos  829:                                                cmd++;
1.23      christos  830:                                        if (len >= size) {
                    831:                                                char *nwhat;
1.95      christos  832:                                                nwhat = el_realloc(what,
                    833:                                                    (size <<= 1) *
                    834:                                                    sizeof(*nwhat));
1.23      christos  835:                                                if (nwhat == NULL) {
1.95      christos  836:                                                        el_free(what);
                    837:                                                        el_free(tmp);
1.28      christos  838:                                                        return 0;
1.23      christos  839:                                                }
                    840:                                                what = nwhat;
                    841:                                        }
1.1       christos  842:                                        what[len++] = *cmd;
                    843:                                }
                    844:                                what[len] = '\0';
                    845:                                from = what;
                    846:                                if (*what == '\0') {
1.95      christos  847:                                        el_free(what);
1.23      christos  848:                                        if (search) {
1.1       christos  849:                                                from = strdup(search);
1.62      christos  850:                                                if (from == NULL) {
1.95      christos  851:                                                        el_free(tmp);
1.28      christos  852:                                                        return 0;
1.62      christos  853:                                                }
1.23      christos  854:                                        } else {
1.1       christos  855:                                                from = NULL;
1.95      christos  856:                                                el_free(tmp);
1.96      christos  857:                                                return -1;
1.1       christos  858:                                        }
                    859:                                }
                    860:                                cmd++;  /* shift after delim */
                    861:                                if (!*cmd)
                    862:                                        continue;
                    863:
                    864:                                size = 16;
1.95      christos  865:                                with = el_realloc(to, size * sizeof(*with));
1.23      christos  866:                                if (with == NULL) {
1.95      christos  867:                                        el_free(to);
                    868:                                        el_free(tmp);
1.23      christos  869:                                        return -1;
                    870:                                }
1.1       christos  871:                                len = 0;
                    872:                                from_len = strlen(from);
                    873:                                for (; *cmd && *cmd != delim; cmd++) {
                    874:                                        if (len + from_len + 1 >= size) {
1.23      christos  875:                                                char *nwith;
1.1       christos  876:                                                size += from_len + 1;
1.95      christos  877:                                                nwith = el_realloc(with,
                    878:                                                    size * sizeof(*nwith));
1.23      christos  879:                                                if (nwith == NULL) {
1.95      christos  880:                                                        el_free(with);
                    881:                                                        el_free(tmp);
1.23      christos  882:                                                        return -1;
                    883:                                                }
                    884:                                                with = nwith;
1.1       christos  885:                                        }
                    886:                                        if (*cmd == '&') {
                    887:                                                /* safe */
1.40      christos  888:                                                (void)strcpy(&with[len], from);
1.1       christos  889:                                                len += from_len;
                    890:                                                continue;
                    891:                                        }
                    892:                                        if (*cmd == '\\'
                    893:                                            && (*(cmd + 1) == delim
                    894:                                                || *(cmd + 1) == '&'))
                    895:                                                cmd++;
                    896:                                        with[len++] = *cmd;
                    897:                                }
                    898:                                with[len] = '\0';
                    899:                                to = with;
                    900:                        }
                    901:
1.40      christos  902:                        aptr = _rl_compat_sub(tmp, from, to, g_on);
                    903:                        if (aptr) {
1.95      christos  904:                                el_free(tmp);
1.40      christos  905:                                tmp = aptr;
1.23      christos  906:                        }
1.40      christos  907:                        g_on = 0;
1.1       christos  908:                }
1.5       christos  909:        }
1.40      christos  910:        *result = tmp;
1.96      christos  911:        return p_on? 2:1;
1.1       christos  912: }
                    913:
1.11      lukem     914:
1.1       christos  915: /*
                    916:  * csh-style history expansion
                    917:  */
                    918: int
1.11      lukem     919: history_expand(char *str, char **output)
                    920: {
1.40      christos  921:        int ret = 0;
                    922:        size_t idx, i, size;
                    923:        char *tmp, *result;
1.1       christos  924:
                    925:        if (h == NULL || e == NULL)
                    926:                rl_initialize();
                    927:
1.40      christos  928:        if (history_expansion_char == 0) {
                    929:                *output = strdup(str);
1.96      christos  930:                return 0;
1.40      christos  931:        }
1.1       christos  932:
1.40      christos  933:        *output = NULL;
1.1       christos  934:        if (str[0] == history_subst_char) {
                    935:                /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
1.100     christos  936:                *output = el_malloc((strlen(str) + 4 + 1) * sizeof(**output));
1.40      christos  937:                if (*output == NULL)
                    938:                        return 0;
                    939:                (*output)[0] = (*output)[1] = history_expansion_char;
                    940:                (*output)[2] = ':';
                    941:                (*output)[3] = 's';
                    942:                (void)strcpy((*output) + 4, str);
                    943:                str = *output;
                    944:        } else {
                    945:                *output = strdup(str);
                    946:                if (*output == NULL)
                    947:                        return 0;
1.1       christos  948:        }
1.40      christos  949:
1.63      christos  950: #define ADD_STRING(what, len, fr)                                      \
1.1       christos  951:        {                                                               \
1.23      christos  952:                if (idx + len + 1 > size) {                             \
1.95      christos  953:                        char *nresult = el_realloc(result,              \
                    954:                            (size += len + 1) * sizeof(*nresult));      \
1.23      christos  955:                        if (nresult == NULL) {                          \
1.95      christos  956:                                el_free(*output);                       \
1.65      christos  957:                                if (/*CONSTCOND*/fr)                    \
1.95      christos  958:                                        el_free(tmp);                   \
1.28      christos  959:                                return 0;                               \
1.23      christos  960:                        }                                               \
                    961:                        result = nresult;                               \
                    962:                }                                                       \
1.1       christos  963:                (void)strncpy(&result[idx], what, len);                 \
                    964:                idx += len;                                             \
                    965:                result[idx] = '\0';                                     \
                    966:        }
                    967:
                    968:        result = NULL;
                    969:        size = idx = 0;
1.64      christos  970:        tmp = NULL;
1.1       christos  971:        for (i = 0; str[i];) {
1.40      christos  972:                int qchar, loop_again;
                    973:                size_t len, start, j;
1.1       christos  974:
1.40      christos  975:                qchar = 0;
1.1       christos  976:                loop_again = 1;
                    977:                start = j = i;
                    978: loop:
                    979:                for (; str[j]; j++) {
                    980:                        if (str[j] == '\\' &&
                    981:                            str[j + 1] == history_expansion_char) {
1.40      christos  982:                                (void)strcpy(&str[j], &str[j + 1]);
1.1       christos  983:                                continue;
                    984:                        }
                    985:                        if (!loop_again) {
1.40      christos  986:                                if (isspace((unsigned char) str[j])
                    987:                                    || str[j] == qchar)
1.1       christos  988:                                        break;
                    989:                        }
                    990:                        if (str[j] == history_expansion_char
                    991:                            && !strchr(history_no_expand_chars, str[j + 1])
                    992:                            && (!history_inhibit_expansion_function ||
1.40      christos  993:                            (*history_inhibit_expansion_function)(str,
                    994:                            (int)j) == 0))
1.1       christos  995:                                break;
                    996:                }
                    997:
1.40      christos  998:                if (str[j] && loop_again) {
1.1       christos  999:                        i = j;
1.40      christos 1000:                        qchar = (j > 0 && str[j - 1] == '"' )? '"':0;
1.1       christos 1001:                        j++;
                   1002:                        if (str[j] == history_expansion_char)
                   1003:                                j++;
                   1004:                        loop_again = 0;
                   1005:                        goto loop;
                   1006:                }
                   1007:                len = i - start;
1.64      christos 1008:                ADD_STRING(&str[start], len, 0);
1.1       christos 1009:
1.40      christos 1010:                if (str[i] == '\0' || str[i] != history_expansion_char) {
1.1       christos 1011:                        len = j - i;
1.64      christos 1012:                        ADD_STRING(&str[i], len, 0);
1.1       christos 1013:                        if (start == 0)
1.40      christos 1014:                                ret = 0;
1.1       christos 1015:                        else
1.40      christos 1016:                                ret = 1;
1.1       christos 1017:                        break;
                   1018:                }
1.40      christos 1019:                ret = _history_expand_command (str, i, (j - i), &tmp);
                   1020:                if (ret > 0 && tmp) {
                   1021:                        len = strlen(tmp);
1.63      christos 1022:                        ADD_STRING(tmp, len, 1);
1.66      christos 1023:                }
                   1024:                if (tmp) {
1.95      christos 1025:                        el_free(tmp);
1.64      christos 1026:                        tmp = NULL;
1.1       christos 1027:                }
                   1028:                i = j;
1.40      christos 1029:        }
1.1       christos 1030:
1.40      christos 1031:        /* ret is 2 for "print only" option */
                   1032:        if (ret == 2) {
                   1033:                add_history(result);
1.1       christos 1034: #ifdef GDB_411_HACK
                   1035:                /* gdb 4.11 has been shipped with readline, where */
                   1036:                /* history_expand() returned -1 when the line     */
                   1037:                /* should not be executed; in readline 2.1+       */
                   1038:                /* it should return 2 in such a case              */
1.40      christos 1039:                ret = -1;
1.1       christos 1040: #endif
                   1041:        }
1.95      christos 1042:        el_free(*output);
1.1       christos 1043:        *output = result;
                   1044:
1.96      christos 1045:        return ret;
1.1       christos 1046: }
                   1047:
1.40      christos 1048: /*
                   1049: * Return a string consisting of arguments of "str" from "start" to "end".
                   1050: */
                   1051: char *
                   1052: history_arg_extract(int start, int end, const char *str)
                   1053: {
                   1054:        size_t  i, len, max;
1.84      christos 1055:        char    **arr, *result = NULL;
1.40      christos 1056:
                   1057:        arr = history_tokenize(str);
                   1058:        if (!arr)
1.84      christos 1059:                return NULL;
                   1060:        if (arr && *arr == NULL)
                   1061:                goto out;
1.40      christos 1062:
                   1063:        for (max = 0; arr[max]; max++)
                   1064:                continue;
                   1065:        max--;
                   1066:
                   1067:        if (start == '$')
1.80      christos 1068:                start = (int)max;
1.40      christos 1069:        if (end == '$')
1.80      christos 1070:                end = (int)max;
1.40      christos 1071:        if (end < 0)
1.80      christos 1072:                end = (int)max + end + 1;
1.40      christos 1073:        if (start < 0)
                   1074:                start = end;
                   1075:
1.84      christos 1076:        if (start < 0 || end < 0 || (size_t)start > max ||
                   1077:            (size_t)end > max || start > end)
                   1078:                goto out;
1.40      christos 1079:
1.99      christos 1080:        for (i = (size_t)start, len = 0; i <= (size_t)end; i++)
1.40      christos 1081:                len += strlen(arr[i]) + 1;
                   1082:        len++;
1.95      christos 1083:        result = el_malloc(len * sizeof(*result));
1.40      christos 1084:        if (result == NULL)
1.84      christos 1085:                goto out;
1.40      christos 1086:
1.99      christos 1087:        for (i = (size_t)start, len = 0; i <= (size_t)end; i++) {
1.40      christos 1088:                (void)strcpy(result + len, arr[i]);
                   1089:                len += strlen(arr[i]);
1.77      lukem    1090:                if (i < (size_t)end)
1.40      christos 1091:                        result[len++] = ' ';
                   1092:        }
1.76      christos 1093:        result[len] = '\0';
1.40      christos 1094:
1.84      christos 1095: out:
1.40      christos 1096:        for (i = 0; arr[i]; i++)
1.95      christos 1097:                el_free(arr[i]);
                   1098:        el_free(arr);
1.40      christos 1099:
1.84      christos 1100:        return result;
1.40      christos 1101: }
1.11      lukem    1102:
1.1       christos 1103: /*
1.40      christos 1104:  * Parse the string into individual tokens,
                   1105:  * similar to how shell would do it.
1.1       christos 1106:  */
                   1107: char **
1.11      lukem    1108: history_tokenize(const char *str)
1.1       christos 1109: {
1.40      christos 1110:        int size = 1, idx = 0, i, start;
1.5       christos 1111:        size_t len;
1.1       christos 1112:        char **result = NULL, *temp, delim = '\0';
                   1113:
1.40      christos 1114:        for (i = 0; str[i];) {
1.4       christos 1115:                while (isspace((unsigned char) str[i]))
1.1       christos 1116:                        i++;
                   1117:                start = i;
1.40      christos 1118:                for (; str[i];) {
1.9       jdolecek 1119:                        if (str[i] == '\\') {
1.14      jdolecek 1120:                                if (str[i+1] != '\0')
1.9       jdolecek 1121:                                        i++;
                   1122:                        } else if (str[i] == delim)
1.1       christos 1123:                                delim = '\0';
                   1124:                        else if (!delim &&
1.11      lukem    1125:                                    (isspace((unsigned char) str[i]) ||
                   1126:                                strchr("()<>;&|$", str[i])))
1.1       christos 1127:                                break;
                   1128:                        else if (!delim && strchr("'`\"", str[i]))
                   1129:                                delim = str[i];
1.40      christos 1130:                        if (str[i])
                   1131:                                i++;
1.1       christos 1132:                }
                   1133:
1.40      christos 1134:                if (idx + 2 >= size) {
1.23      christos 1135:                        char **nresult;
1.1       christos 1136:                        size <<= 1;
1.99      christos 1137:                        nresult = el_realloc(result, (size_t)size * sizeof(*nresult));
1.23      christos 1138:                        if (nresult == NULL) {
1.95      christos 1139:                                el_free(result);
1.23      christos 1140:                                return NULL;
                   1141:                        }
                   1142:                        result = nresult;
1.1       christos 1143:                }
1.99      christos 1144:                len = (size_t)i - (size_t)start;
                   1145:                temp = el_malloc((size_t)(len + 1) * sizeof(*temp));
1.23      christos 1146:                if (temp == NULL) {
1.40      christos 1147:                        for (i = 0; i < idx; i++)
1.95      christos 1148:                                el_free(result[i]);
                   1149:                        el_free(result);
1.23      christos 1150:                        return NULL;
                   1151:                }
1.40      christos 1152:                (void)strncpy(temp, &str[start], len);
1.1       christos 1153:                temp[len] = '\0';
1.40      christos 1154:                result[idx++] = temp;
                   1155:                result[idx] = NULL;
                   1156:                if (str[i])
                   1157:                        i++;
1.1       christos 1158:        }
1.96      christos 1159:        return result;
1.1       christos 1160: }
                   1161:
1.11      lukem    1162:
1.1       christos 1163: /*
                   1164:  * limit size of history record to ``max'' events
                   1165:  */
                   1166: void
1.11      lukem    1167: stifle_history(int max)
1.1       christos 1168: {
1.92      christos 1169:        HistEvent ev;
1.1       christos 1170:
                   1171:        if (h == NULL || e == NULL)
                   1172:                rl_initialize();
                   1173:
1.92      christos 1174:        if (history(h, &ev, H_SETSIZE, max) == 0)
1.1       christos 1175:                max_input_history = max;
                   1176: }
                   1177:
1.11      lukem    1178:
1.1       christos 1179: /*
                   1180:  * "unlimit" size of history - set the limit to maximum allowed int value
                   1181:  */
                   1182: int
1.11      lukem    1183: unstifle_history(void)
1.1       christos 1184: {
1.92      christos 1185:        HistEvent ev;
1.1       christos 1186:        int omax;
                   1187:
1.92      christos 1188:        history(h, &ev, H_SETSIZE, INT_MAX);
1.1       christos 1189:        omax = max_input_history;
                   1190:        max_input_history = INT_MAX;
1.96      christos 1191:        return omax;            /* some value _must_ be returned */
1.1       christos 1192: }
                   1193:
1.11      lukem    1194:
1.1       christos 1195: int
1.11      lukem    1196: history_is_stifled(void)
1.1       christos 1197: {
1.11      lukem    1198:
1.1       christos 1199:        /* cannot return true answer */
1.96      christos 1200:        return max_input_history != INT_MAX;
1.1       christos 1201: }
                   1202:
1.85      christos 1203: static const char _history_tmp_template[] = "/tmp/.historyXXXXXX";
                   1204:
                   1205: int
                   1206: history_truncate_file (const char *filename, int nlines)
                   1207: {
                   1208:        int ret = 0;
                   1209:        FILE *fp, *tp;
                   1210:        char template[sizeof(_history_tmp_template)];
                   1211:        char buf[4096];
                   1212:        int fd;
                   1213:        char *cp;
                   1214:        off_t off;
                   1215:        int count = 0;
                   1216:        ssize_t left = 0;
                   1217:
                   1218:        if (filename == NULL && (filename = _default_history_file()) == NULL)
                   1219:                return errno;
                   1220:        if ((fp = fopen(filename, "r+")) == NULL)
                   1221:                return errno;
                   1222:        strcpy(template, _history_tmp_template);
                   1223:        if ((fd = mkstemp(template)) == -1) {
                   1224:                ret = errno;
                   1225:                goto out1;
                   1226:        }
                   1227:
                   1228:        if ((tp = fdopen(fd, "r+")) == NULL) {
                   1229:                close(fd);
                   1230:                ret = errno;
                   1231:                goto out2;
                   1232:        }
                   1233:
                   1234:        for(;;) {
1.98      christos 1235:                if (fread(buf, sizeof(buf), (size_t)1, fp) != 1) {
1.85      christos 1236:                        if (ferror(fp)) {
                   1237:                                ret = errno;
                   1238:                                break;
                   1239:                        }
                   1240:                        if (fseeko(fp, (off_t)sizeof(buf) * count, SEEK_SET) ==
                   1241:                            (off_t)-1) {
                   1242:                                ret = errno;
                   1243:                                break;
                   1244:                        }
1.99      christos 1245:                        left = (ssize_t)fread(buf, (size_t)1, sizeof(buf), fp);
1.85      christos 1246:                        if (ferror(fp)) {
                   1247:                                ret = errno;
                   1248:                                break;
                   1249:                        }
                   1250:                        if (left == 0) {
                   1251:                                count--;
                   1252:                                left = sizeof(buf);
1.98      christos 1253:                        } else if (fwrite(buf, (size_t)left, (size_t)1, tp)
                   1254:                            != 1) {
1.85      christos 1255:                                ret = errno;
                   1256:                                break;
                   1257:                        }
                   1258:                        fflush(tp);
                   1259:                        break;
                   1260:                }
1.98      christos 1261:                if (fwrite(buf, sizeof(buf), (size_t)1, tp) != 1) {
1.85      christos 1262:                        ret = errno;
                   1263:                        break;
                   1264:                }
                   1265:                count++;
                   1266:        }
                   1267:        if (ret)
                   1268:                goto out3;
                   1269:        cp = buf + left - 1;
                   1270:        if(*cp != '\n')
                   1271:                cp++;
                   1272:        for(;;) {
                   1273:                while (--cp >= buf) {
                   1274:                        if (*cp == '\n') {
                   1275:                                if (--nlines == 0) {
                   1276:                                        if (++cp >= buf + sizeof(buf)) {
                   1277:                                                count++;
                   1278:                                                cp = buf;
                   1279:                                        }
                   1280:                                        break;
                   1281:                                }
                   1282:                        }
                   1283:                }
                   1284:                if (nlines <= 0 || count == 0)
                   1285:                        break;
                   1286:                count--;
                   1287:                if (fseeko(tp, (off_t)sizeof(buf) * count, SEEK_SET) < 0) {
                   1288:                        ret = errno;
                   1289:                        break;
                   1290:                }
1.98      christos 1291:                if (fread(buf, sizeof(buf), (size_t)1, tp) != 1) {
1.85      christos 1292:                        if (ferror(tp)) {
                   1293:                                ret = errno;
                   1294:                                break;
                   1295:                        }
                   1296:                        ret = EAGAIN;
                   1297:                        break;
                   1298:                }
                   1299:                cp = buf + sizeof(buf);
                   1300:        }
                   1301:
                   1302:        if (ret || nlines > 0)
                   1303:                goto out3;
                   1304:
1.98      christos 1305:        if (fseeko(fp, (off_t)0, SEEK_SET) == (off_t)-1) {
1.85      christos 1306:                ret = errno;
                   1307:                goto out3;
                   1308:        }
                   1309:
                   1310:        if (fseeko(tp, (off_t)sizeof(buf) * count + (cp - buf), SEEK_SET) ==
                   1311:            (off_t)-1) {
                   1312:                ret = errno;
                   1313:                goto out3;
                   1314:        }
                   1315:
                   1316:        for(;;) {
1.99      christos 1317:                if ((left = (ssize_t)fread(buf, (size_t)1, sizeof(buf), tp)) == 0) {
1.85      christos 1318:                        if (ferror(fp))
                   1319:                                ret = errno;
                   1320:                        break;
                   1321:                }
1.98      christos 1322:                if (fwrite(buf, (size_t)left, (size_t)1, fp) != 1) {
1.85      christos 1323:                        ret = errno;
                   1324:                        break;
                   1325:                }
                   1326:        }
                   1327:        fflush(fp);
                   1328:        if((off = ftello(fp)) > 0)
                   1329:                (void)ftruncate(fileno(fp), off);
                   1330: out3:
                   1331:        fclose(tp);
                   1332: out2:
                   1333:        unlink(template);
                   1334: out1:
                   1335:        fclose(fp);
                   1336:
                   1337:        return ret;
                   1338: }
                   1339:
1.11      lukem    1340:
1.1       christos 1341: /*
                   1342:  * read history from a file given
                   1343:  */
                   1344: int
1.11      lukem    1345: read_history(const char *filename)
1.1       christos 1346: {
1.92      christos 1347:        HistEvent ev;
1.1       christos 1348:
                   1349:        if (h == NULL || e == NULL)
                   1350:                rl_initialize();
1.85      christos 1351:        if (filename == NULL && (filename = _default_history_file()) == NULL)
                   1352:                return errno;
1.96      christos 1353:        return history(h, &ev, H_LOAD, filename) == -1 ?
                   1354:            (errno ? errno : EINVAL) : 0;
1.1       christos 1355: }
                   1356:
1.11      lukem    1357:
1.1       christos 1358: /*
                   1359:  * write history to a file given
                   1360:  */
                   1361: int
1.11      lukem    1362: write_history(const char *filename)
1.1       christos 1363: {
1.92      christos 1364:        HistEvent ev;
1.1       christos 1365:
                   1366:        if (h == NULL || e == NULL)
                   1367:                rl_initialize();
1.85      christos 1368:        if (filename == NULL && (filename = _default_history_file()) == NULL)
                   1369:                return errno;
1.96      christos 1370:        return history(h, &ev, H_SAVE, filename) == -1 ?
                   1371:            (errno ? errno : EINVAL) : 0;
1.1       christos 1372: }
                   1373:
1.11      lukem    1374:
1.1       christos 1375: /*
                   1376:  * returns history ``num''th event
                   1377:  *
                   1378:  * returned pointer points to static variable
                   1379:  */
1.2       christos 1380: HIST_ENTRY *
1.11      lukem    1381: history_get(int num)
1.1       christos 1382: {
                   1383:        static HIST_ENTRY she;
1.92      christos 1384:        HistEvent ev;
1.40      christos 1385:        int curr_num;
1.1       christos 1386:
                   1387:        if (h == NULL || e == NULL)
                   1388:                rl_initialize();
                   1389:
1.40      christos 1390:        /* save current position */
1.92      christos 1391:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1392:                return NULL;
1.1       christos 1393:        curr_num = ev.num;
1.40      christos 1394:
1.85      christos 1395:        /* start from the oldest */
1.92      christos 1396:        if (history(h, &ev, H_LAST) != 0)
1.96      christos 1397:                return NULL;    /* error */
1.40      christos 1398:
1.85      christos 1399:        /* look forwards for event matching specified offset */
1.92      christos 1400:        if (history(h, &ev, H_NEXT_EVDATA, num, &she.data))
1.96      christos 1401:                return NULL;
1.1       christos 1402:
1.92      christos 1403:        she.line = ev.str;
1.1       christos 1404:
1.40      christos 1405:        /* restore pointer to where it was */
1.92      christos 1406:        (void)history(h, &ev, H_SET, curr_num);
1.1       christos 1407:
1.96      christos 1408:        return &she;
1.1       christos 1409: }
                   1410:
1.11      lukem    1411:
1.1       christos 1412: /*
                   1413:  * add the line to history table
                   1414:  */
                   1415: int
1.11      lukem    1416: add_history(const char *line)
1.1       christos 1417: {
1.92      christos 1418:        HistEvent ev;
1.1       christos 1419:
1.104     christos 1420:        if (line == NULL)
                   1421:                return 0;
                   1422:
1.1       christos 1423:        if (h == NULL || e == NULL)
                   1424:                rl_initialize();
                   1425:
1.92      christos 1426:        (void)history(h, &ev, H_ENTER, line);
                   1427:        if (history(h, &ev, H_GETSIZE) == 0)
1.1       christos 1428:                history_length = ev.num;
                   1429:
1.96      christos 1430:        return !(history_length > 0); /* return 0 if all is okay */
1.1       christos 1431: }
                   1432:
1.11      lukem    1433:
1.1       christos 1434: /*
1.58      christos 1435:  * remove the specified entry from the history list and return it.
                   1436:  */
                   1437: HIST_ENTRY *
                   1438: remove_history(int num)
                   1439: {
1.85      christos 1440:        HIST_ENTRY *he;
1.92      christos 1441:        HistEvent ev;
1.58      christos 1442:
                   1443:        if (h == NULL || e == NULL)
                   1444:                rl_initialize();
                   1445:
1.95      christos 1446:        if ((he = el_malloc(sizeof(*he))) == NULL)
1.58      christos 1447:                return NULL;
                   1448:
1.92      christos 1449:        if (history(h, &ev, H_DELDATA, num, &he->data) != 0) {
1.95      christos 1450:                el_free(he);
1.71      christos 1451:                return NULL;
1.85      christos 1452:        }
1.71      christos 1453:
1.92      christos 1454:        he->line = ev.str;
                   1455:        if (history(h, &ev, H_GETSIZE) == 0)
1.85      christos 1456:                history_length = ev.num;
1.58      christos 1457:
1.85      christos 1458:        return he;
1.58      christos 1459: }
                   1460:
                   1461:
                   1462: /*
1.85      christos 1463:  * replace the line and data of the num-th entry
                   1464:  */
                   1465: HIST_ENTRY *
                   1466: replace_history_entry(int num, const char *line, histdata_t data)
                   1467: {
                   1468:        HIST_ENTRY *he;
1.92      christos 1469:        HistEvent ev;
1.85      christos 1470:        int curr_num;
                   1471:
                   1472:        if (h == NULL || e == NULL)
                   1473:                rl_initialize();
                   1474:
                   1475:        /* save current position */
1.92      christos 1476:        if (history(h, &ev, H_CURR) != 0)
1.85      christos 1477:                return NULL;
                   1478:        curr_num = ev.num;
                   1479:
                   1480:        /* start from the oldest */
1.92      christos 1481:        if (history(h, &ev, H_LAST) != 0)
1.85      christos 1482:                return NULL;    /* error */
                   1483:
1.95      christos 1484:        if ((he = el_malloc(sizeof(*he))) == NULL)
1.85      christos 1485:                return NULL;
                   1486:
                   1487:        /* look forwards for event matching specified offset */
1.92      christos 1488:        if (history(h, &ev, H_NEXT_EVDATA, num, &he->data))
1.85      christos 1489:                goto out;
                   1490:
1.92      christos 1491:        he->line = strdup(ev.str);
1.85      christos 1492:        if (he->line == NULL)
                   1493:                goto out;
                   1494:
1.92      christos 1495:        if (history(h, &ev, H_REPLACE, line, data))
1.85      christos 1496:                goto out;
                   1497:
                   1498:        /* restore pointer to where it was */
1.92      christos 1499:        if (history(h, &ev, H_SET, curr_num))
1.85      christos 1500:                goto out;
                   1501:
                   1502:        return he;
                   1503: out:
1.95      christos 1504:        el_free(he);
1.85      christos 1505:        return NULL;
                   1506: }
                   1507:
                   1508: /*
1.1       christos 1509:  * clear the history list - delete all entries
                   1510:  */
                   1511: void
1.11      lukem    1512: clear_history(void)
1.1       christos 1513: {
1.92      christos 1514:        HistEvent ev;
1.11      lukem    1515:
1.110     christos 1516:        if (h == NULL || e == NULL)
                   1517:                rl_initialize();
                   1518:
1.92      christos 1519:        (void)history(h, &ev, H_CLEAR);
1.85      christos 1520:        history_length = 0;
1.1       christos 1521: }
                   1522:
1.11      lukem    1523:
1.1       christos 1524: /*
                   1525:  * returns offset of the current history event
                   1526:  */
                   1527: int
1.11      lukem    1528: where_history(void)
1.1       christos 1529: {
1.92      christos 1530:        HistEvent ev;
1.1       christos 1531:        int curr_num, off;
                   1532:
1.92      christos 1533:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1534:                return 0;
1.1       christos 1535:        curr_num = ev.num;
                   1536:
1.92      christos 1537:        (void)history(h, &ev, H_FIRST);
1.1       christos 1538:        off = 1;
1.92      christos 1539:        while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
1.1       christos 1540:                off++;
                   1541:
1.96      christos 1542:        return off;
1.1       christos 1543: }
                   1544:
1.11      lukem    1545:
1.1       christos 1546: /*
                   1547:  * returns current history event or NULL if there is no such event
                   1548:  */
1.2       christos 1549: HIST_ENTRY *
1.11      lukem    1550: current_history(void)
1.1       christos 1551: {
1.11      lukem    1552:
1.96      christos 1553:        return _move_history(H_CURR);
1.1       christos 1554: }
                   1555:
1.11      lukem    1556:
1.1       christos 1557: /*
                   1558:  * returns total number of bytes history events' data are using
                   1559:  */
                   1560: int
1.11      lukem    1561: history_total_bytes(void)
1.1       christos 1562: {
1.92      christos 1563:        HistEvent ev;
1.80      christos 1564:        int curr_num;
                   1565:        size_t size;
1.1       christos 1566:
1.92      christos 1567:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1568:                return -1;
1.1       christos 1569:        curr_num = ev.num;
                   1570:
1.92      christos 1571:        (void)history(h, &ev, H_FIRST);
1.1       christos 1572:        size = 0;
                   1573:        do
1.92      christos 1574:                size += strlen(ev.str) * sizeof(*ev.str);
                   1575:        while (history(h, &ev, H_NEXT) == 0);
1.1       christos 1576:
                   1577:        /* get to the same position as before */
1.92      christos 1578:        history(h, &ev, H_PREV_EVENT, curr_num);
1.1       christos 1579:
1.96      christos 1580:        return (int)size;
1.1       christos 1581: }
                   1582:
1.11      lukem    1583:
1.1       christos 1584: /*
                   1585:  * sets the position in the history list to ``pos''
                   1586:  */
                   1587: int
1.11      lukem    1588: history_set_pos(int pos)
1.1       christos 1589: {
1.92      christos 1590:        HistEvent ev;
1.40      christos 1591:        int curr_num;
1.1       christos 1592:
1.85      christos 1593:        if (pos >= history_length || pos < 0)
1.96      christos 1594:                return -1;
1.1       christos 1595:
1.92      christos 1596:        (void)history(h, &ev, H_CURR);
1.1       christos 1597:        curr_num = ev.num;
                   1598:
1.85      christos 1599:        /*
                   1600:         * use H_DELDATA to set to nth history (without delete) by passing
                   1601:         * (void **)-1
                   1602:         */
1.92      christos 1603:        if (history(h, &ev, H_DELDATA, pos, (void **)-1)) {
                   1604:                (void)history(h, &ev, H_SET, curr_num);
1.96      christos 1605:                return -1;
1.1       christos 1606:        }
1.96      christos 1607:        return 0;
1.1       christos 1608: }
                   1609:
1.11      lukem    1610:
1.1       christos 1611: /*
                   1612:  * returns previous event in history and shifts pointer accordingly
                   1613:  */
1.2       christos 1614: HIST_ENTRY *
1.11      lukem    1615: previous_history(void)
1.1       christos 1616: {
1.11      lukem    1617:
1.96      christos 1618:        return _move_history(H_PREV);
1.1       christos 1619: }
                   1620:
1.11      lukem    1621:
1.1       christos 1622: /*
                   1623:  * returns next event in history and shifts pointer accordingly
                   1624:  */
1.2       christos 1625: HIST_ENTRY *
1.11      lukem    1626: next_history(void)
1.1       christos 1627: {
1.11      lukem    1628:
1.96      christos 1629:        return _move_history(H_NEXT);
1.1       christos 1630: }
                   1631:
1.11      lukem    1632:
1.1       christos 1633: /*
1.40      christos 1634:  * searches for first history event containing the str
1.1       christos 1635:  */
1.40      christos 1636: int
                   1637: history_search(const char *str, int direction)
1.11      lukem    1638: {
1.92      christos 1639:        HistEvent ev;
                   1640:        const char *strp;
1.11      lukem    1641:        int curr_num;
1.1       christos 1642:
1.92      christos 1643:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1644:                return -1;
1.1       christos 1645:        curr_num = ev.num;
                   1646:
                   1647:        for (;;) {
1.92      christos 1648:                if ((strp = strstr(ev.str, str)) != NULL)
1.96      christos 1649:                        return (int)(strp - ev.str);
1.92      christos 1650:                if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)
1.1       christos 1651:                        break;
                   1652:        }
1.92      christos 1653:        (void)history(h, &ev, H_SET, curr_num);
1.96      christos 1654:        return -1;
1.1       christos 1655: }
                   1656:
1.11      lukem    1657:
1.1       christos 1658: /*
                   1659:  * searches for first history event beginning with str
                   1660:  */
                   1661: int
1.11      lukem    1662: history_search_prefix(const char *str, int direction)
1.1       christos 1663: {
1.92      christos 1664:        HistEvent ev;
1.11      lukem    1665:
1.92      christos 1666:        return (history(h, &ev, direction < 0 ?
1.88      christos 1667:            H_PREV_STR : H_NEXT_STR, str));
1.1       christos 1668: }
                   1669:
1.11      lukem    1670:
1.1       christos 1671: /*
                   1672:  * search for event in history containing str, starting at offset
                   1673:  * abs(pos); continue backward, if pos<0, forward otherwise
                   1674:  */
1.5       christos 1675: /* ARGSUSED */
1.1       christos 1676: int
1.40      christos 1677: history_search_pos(const char *str,
1.30      christos 1678:                   int direction __attribute__((__unused__)), int pos)
1.1       christos 1679: {
1.92      christos 1680:        HistEvent ev;
1.11      lukem    1681:        int curr_num, off;
1.1       christos 1682:
                   1683:        off = (pos > 0) ? pos : -pos;
                   1684:        pos = (pos > 0) ? 1 : -1;
                   1685:
1.92      christos 1686:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1687:                return -1;
1.1       christos 1688:        curr_num = ev.num;
                   1689:
1.92      christos 1690:        if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
1.96      christos 1691:                return -1;
1.1       christos 1692:
                   1693:        for (;;) {
1.92      christos 1694:                if (strstr(ev.str, str))
1.96      christos 1695:                        return off;
1.92      christos 1696:                if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
1.1       christos 1697:                        break;
                   1698:        }
                   1699:
                   1700:        /* set "current" pointer back to previous state */
1.92      christos 1701:        (void)history(h, &ev,
1.88      christos 1702:            pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
1.1       christos 1703:
1.96      christos 1704:        return -1;
1.1       christos 1705: }
                   1706:
                   1707:
                   1708: /********************************/
1.40      christos 1709: /* completion functions */
1.1       christos 1710:
1.57      christos 1711: char *
                   1712: tilde_expand(char *name)
                   1713: {
                   1714:        return fn_tilde_expand(name);
                   1715: }
                   1716:
                   1717: char *
                   1718: filename_completion_function(const char *name, int state)
                   1719: {
                   1720:        return fn_filename_completion_function(name, state);
                   1721: }
                   1722:
1.1       christos 1723: /*
                   1724:  * a completion generator for usernames; returns _first_ username
                   1725:  * which starts with supplied text
                   1726:  * text contains a partial username preceded by random character
1.94      christos 1727:  * (usually '~'); state resets search from start (??? should we do that anyway)
1.113     snj      1728:  * it's the caller's responsibility to free the returned value
1.1       christos 1729:  */
1.11      lukem    1730: char *
                   1731: username_completion_function(const char *text, int state)
1.1       christos 1732: {
1.94      christos 1733: #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
                   1734:        struct passwd pwres;
1.50      christos 1735:        char pwbuf[1024];
1.94      christos 1736: #endif
                   1737:        struct passwd *pass = NULL;
1.1       christos 1738:
                   1739:        if (text[0] == '\0')
1.96      christos 1740:                return NULL;
1.1       christos 1741:
                   1742:        if (*text == '~')
                   1743:                text++;
                   1744:
                   1745:        if (state == 0)
                   1746:                setpwent();
                   1747:
1.94      christos 1748:        while (
                   1749: #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
                   1750:            getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pass) == 0 && pass != NULL
                   1751: #else
                   1752:            (pass = getpwent()) != NULL
                   1753: #endif
                   1754:            && text[0] == pass->pw_name[0]
                   1755:            && strcmp(text, pass->pw_name) == 0)
                   1756:                continue;
1.1       christos 1757:
1.94      christos 1758:        if (pass == NULL) {
1.1       christos 1759:                endpwent();
1.85      christos 1760:                return NULL;
1.1       christos 1761:        }
1.94      christos 1762:        return strdup(pass->pw_name);
1.1       christos 1763: }
                   1764:
1.11      lukem    1765:
1.1       christos 1766: /*
1.46      christos 1767:  * el-compatible wrapper to send TSTP on ^Z
                   1768:  */
                   1769: /* ARGSUSED */
                   1770: static unsigned char
                   1771: _el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__)))
                   1772: {
                   1773:        (void)kill(0, SIGTSTP);
                   1774:        return CC_NORM;
                   1775: }
1.11      lukem    1776:
1.1       christos 1777: /*
1.12      jdolecek 1778:  * Display list of strings in columnar format on readline's output stream.
                   1779:  * 'matches' is list of strings, 'len' is number of strings in 'matches',
                   1780:  * 'max' is maximum length of string in 'matches'.
                   1781:  */
                   1782: void
1.53      dsl      1783: rl_display_match_list(char **matches, int len, int max)
1.12      jdolecek 1784: {
                   1785:
1.81      christos 1786:        fn_display_match_list(e, matches, (size_t)len, (size_t)max);
1.1       christos 1787: }
                   1788:
1.56      christos 1789: static const char *
                   1790: /*ARGSUSED*/
                   1791: _rl_completion_append_character_function(const char *dummy
                   1792:     __attribute__((__unused__)))
                   1793: {
                   1794:        static char buf[2];
1.99      christos 1795:        buf[0] = (char)rl_completion_append_character;
1.76      christos 1796:        buf[1] = '\0';
1.56      christos 1797:        return buf;
                   1798: }
                   1799:
1.11      lukem    1800:
1.1       christos 1801: /*
                   1802:  * complete word at current point
                   1803:  */
1.53      dsl      1804: /* ARGSUSED */
1.1       christos 1805: int
1.53      dsl      1806: rl_complete(int ignore __attribute__((__unused__)), int invoking_key)
1.1       christos 1807: {
1.86      christos 1808: #ifdef WIDECHAR
                   1809:        static ct_buffer_t wbreak_conv, sprefix_conv;
                   1810: #endif
1.102     christos 1811:        char *breakchars;
1.86      christos 1812:
1.1       christos 1813:        if (h == NULL || e == NULL)
                   1814:                rl_initialize();
                   1815:
                   1816:        if (rl_inhibit_completion) {
1.47      christos 1817:                char arr[2];
                   1818:                arr[0] = (char)invoking_key;
                   1819:                arr[1] = '\0';
                   1820:                el_insertstr(e, arr);
1.96      christos 1821:                return CC_REFRESH;
1.53      dsl      1822:        }
                   1823:
1.102     christos 1824:        if (rl_completion_word_break_hook != NULL)
                   1825:                breakchars = (*rl_completion_word_break_hook)();
                   1826:        else
                   1827:                breakchars = rl_basic_word_break_characters;
                   1828:
1.53      dsl      1829:        /* Just look at how many global variables modify this operation! */
                   1830:        return fn_complete(e,
                   1831:            (CPFunction *)rl_completion_entry_function,
                   1832:            rl_attempted_completion_function,
1.86      christos 1833:            ct_decode_string(rl_basic_word_break_characters, &wbreak_conv),
1.102     christos 1834:            ct_decode_string(breakchars, &sprefix_conv),
1.81      christos 1835:            _rl_completion_append_character_function,
                   1836:            (size_t)rl_completion_query_items,
1.53      dsl      1837:            &rl_completion_type, &rl_attempted_completion_over,
                   1838:            &rl_point, &rl_end);
1.92      christos 1839:
                   1840:
1.1       christos 1841: }
                   1842:
1.11      lukem    1843:
1.53      dsl      1844: /* ARGSUSED */
                   1845: static unsigned char
                   1846: _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch)
                   1847: {
                   1848:        return (unsigned char)rl_complete(0, ch);
                   1849: }
                   1850:
1.1       christos 1851: /*
1.7       simonb   1852:  * misc other functions
1.1       christos 1853:  */
                   1854:
                   1855: /*
                   1856:  * bind key c to readline-type function func
                   1857:  */
                   1858: int
1.85      christos 1859: rl_bind_key(int c, rl_command_func_t *func)
1.1       christos 1860: {
1.11      lukem    1861:        int retval = -1;
1.1       christos 1862:
                   1863:        if (h == NULL || e == NULL)
                   1864:                rl_initialize();
                   1865:
                   1866:        if (func == rl_insert) {
                   1867:                /* XXX notice there is no range checking of ``c'' */
                   1868:                e->el_map.key[c] = ED_INSERT;
                   1869:                retval = 0;
                   1870:        }
1.96      christos 1871:        return retval;
1.1       christos 1872: }
                   1873:
1.11      lukem    1874:
1.1       christos 1875: /*
                   1876:  * read one key from input - handles chars pushed back
                   1877:  * to input stream also
                   1878:  */
                   1879: int
1.11      lukem    1880: rl_read_key(void)
1.1       christos 1881: {
1.11      lukem    1882:        char fooarr[2 * sizeof(int)];
1.1       christos 1883:
                   1884:        if (e == NULL || h == NULL)
                   1885:                rl_initialize();
                   1886:
1.96      christos 1887:        return el_getc(e, fooarr);
1.1       christos 1888: }
                   1889:
1.11      lukem    1890:
1.1       christos 1891: /*
                   1892:  * reset the terminal
                   1893:  */
1.5       christos 1894: /* ARGSUSED */
1.1       christos 1895: void
1.30      christos 1896: rl_reset_terminal(const char *p __attribute__((__unused__)))
1.1       christos 1897: {
1.11      lukem    1898:
1.1       christos 1899:        if (h == NULL || e == NULL)
                   1900:                rl_initialize();
                   1901:        el_reset(e);
                   1902: }
                   1903:
1.11      lukem    1904:
1.1       christos 1905: /*
                   1906:  * insert character ``c'' back into input stream, ``count'' times
                   1907:  */
                   1908: int
1.11      lukem    1909: rl_insert(int count, int c)
1.1       christos 1910: {
1.11      lukem    1911:        char arr[2];
1.1       christos 1912:
                   1913:        if (h == NULL || e == NULL)
                   1914:                rl_initialize();
                   1915:
                   1916:        /* XXX - int -> char conversion can lose on multichars */
1.99      christos 1917:        arr[0] = (char)c;
1.1       christos 1918:        arr[1] = '\0';
                   1919:
                   1920:        for (; count > 0; count--)
                   1921:                el_push(e, arr);
                   1922:
1.96      christos 1923:        return 0;
1.35      christos 1924: }
                   1925:
1.85      christos 1926: int
                   1927: rl_insert_text(const char *text)
                   1928: {
                   1929:        if (!text || *text == 0)
1.96      christos 1930:                return 0;
1.85      christos 1931:
                   1932:        if (h == NULL || e == NULL)
                   1933:                rl_initialize();
                   1934:
                   1935:        if (el_insertstr(e, text) < 0)
1.96      christos 1936:                return 0;
1.85      christos 1937:        return (int)strlen(text);
                   1938: }
                   1939:
1.35      christos 1940: /*ARGSUSED*/
                   1941: int
1.97      christos 1942: rl_newline(int count __attribute__((__unused__)),
                   1943:     int c __attribute__((__unused__)))
1.35      christos 1944: {
                   1945:        /*
                   1946:         * Readline-4.0 appears to ignore the args.
                   1947:         */
                   1948:        return rl_insert(1, '\n');
                   1949: }
                   1950:
                   1951: /*ARGSUSED*/
                   1952: static unsigned char
1.97      christos 1953: rl_bind_wrapper(EditLine *el __attribute__((__unused__)), unsigned char c)
1.35      christos 1954: {
                   1955:        if (map[c] == NULL)
                   1956:            return CC_ERROR;
1.49      christos 1957:
                   1958:        _rl_update_pos();
                   1959:
1.35      christos 1960:        (*map[c])(NULL, c);
1.38      christos 1961:
                   1962:        /* If rl_done was set by the above call, deal with it here */
                   1963:        if (rl_done)
                   1964:                return CC_EOF;
                   1965:
1.35      christos 1966:        return CC_NORM;
                   1967: }
                   1968:
                   1969: int
                   1970: rl_add_defun(const char *name, Function *fun, int c)
                   1971: {
                   1972:        char dest[8];
1.77      lukem    1973:        if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0)
1.35      christos 1974:                return -1;
                   1975:        map[(unsigned char)c] = fun;
                   1976:        el_set(e, EL_ADDFN, name, name, rl_bind_wrapper);
1.37      christos 1977:        vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0);
1.106     christos 1978:        el_set(e, EL_BIND, dest, name, NULL);
1.35      christos 1979:        return 0;
                   1980: }
                   1981:
                   1982: void
1.101     matt     1983: rl_callback_read_char(void)
1.35      christos 1984: {
1.36      christos 1985:        int count = 0, done = 0;
1.35      christos 1986:        const char *buf = el_gets(e, &count);
                   1987:        char *wbuf;
                   1988:
                   1989:        if (buf == NULL || count-- <= 0)
                   1990:                return;
1.61      christos 1991:        if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF])
1.36      christos 1992:                done = 1;
                   1993:        if (buf[count] == '\n' || buf[count] == '\r')
                   1994:                done = 2;
                   1995:
                   1996:        if (done && rl_linefunc != NULL) {
1.35      christos 1997:                el_set(e, EL_UNBUFFERED, 0);
1.36      christos 1998:                if (done == 2) {
                   1999:                    if ((wbuf = strdup(buf)) != NULL)
                   2000:                        wbuf[count] = '\0';
                   2001:                } else
                   2002:                        wbuf = NULL;
1.35      christos 2003:                (*(void (*)(const char *))rl_linefunc)(wbuf);
1.111     christos 2004:                el_set(e, EL_UNBUFFERED, 1);
1.35      christos 2005:        }
                   2006: }
                   2007:
                   2008: void
1.72      christos 2009: rl_callback_handler_install(const char *prompt, VCPFunction *linefunc)
1.35      christos 2010: {
                   2011:        if (e == NULL) {
                   2012:                rl_initialize();
                   2013:        }
1.78      christos 2014:        (void)rl_set_prompt(prompt);
1.35      christos 2015:        rl_linefunc = linefunc;
                   2016:        el_set(e, EL_UNBUFFERED, 1);
                   2017: }
                   2018:
                   2019: void
                   2020: rl_callback_handler_remove(void)
                   2021: {
                   2022:        el_set(e, EL_UNBUFFERED, 0);
1.72      christos 2023:        rl_linefunc = NULL;
1.35      christos 2024: }
                   2025:
                   2026: void
                   2027: rl_redisplay(void)
                   2028: {
                   2029:        char a[2];
1.99      christos 2030:        a[0] = (char)e->el_tty.t_c[TS_IO][C_REPRINT];
1.35      christos 2031:        a[1] = '\0';
                   2032:        el_push(e, a);
                   2033: }
                   2034:
                   2035: int
                   2036: rl_get_previous_history(int count, int key)
                   2037: {
                   2038:        char a[2];
1.99      christos 2039:        a[0] = (char)key;
1.35      christos 2040:        a[1] = '\0';
                   2041:        while (count--)
                   2042:                el_push(e, a);
                   2043:        return 0;
1.38      christos 2044: }
                   2045:
                   2046: void
                   2047: /*ARGSUSED*/
1.97      christos 2048: rl_prep_terminal(int meta_flag __attribute__((__unused__)))
1.38      christos 2049: {
                   2050:        el_set(e, EL_PREP_TERM, 1);
                   2051: }
                   2052:
                   2053: void
1.68      christos 2054: rl_deprep_terminal(void)
1.38      christos 2055: {
                   2056:        el_set(e, EL_PREP_TERM, 0);
                   2057: }
                   2058:
                   2059: int
                   2060: rl_read_init_file(const char *s)
                   2061: {
1.96      christos 2062:        return el_source(e, s);
1.38      christos 2063: }
                   2064:
                   2065: int
                   2066: rl_parse_and_bind(const char *line)
                   2067: {
                   2068:        const char **argv;
                   2069:        int argc;
                   2070:        Tokenizer *tok;
                   2071:
                   2072:        tok = tok_init(NULL);
1.44      lukem    2073:        tok_str(tok, line, &argc, &argv);
1.38      christos 2074:        argc = el_parse(e, argc, argv);
                   2075:        tok_end(tok);
1.96      christos 2076:        return argc ? 1 : 0;
1.38      christos 2077: }
                   2078:
1.51      christos 2079: int
                   2080: rl_variable_bind(const char *var, const char *value)
                   2081: {
                   2082:        /*
                   2083:         * The proper return value is undocument, but this is what the
                   2084:         * readline source seems to do.
                   2085:         */
1.106     christos 2086:        return el_set(e, EL_BIND, "", var, value, NULL) == -1 ? 1 : 0;
1.51      christos 2087: }
                   2088:
1.38      christos 2089: void
                   2090: rl_stuff_char(int c)
                   2091: {
                   2092:        char buf[2];
                   2093:
1.99      christos 2094:        buf[0] = (char)c;
1.38      christos 2095:        buf[1] = '\0';
                   2096:        el_insertstr(e, buf);
                   2097: }
                   2098:
                   2099: static int
                   2100: _rl_event_read_char(EditLine *el, char *cp)
                   2101: {
1.80      christos 2102:        int     n;
                   2103:        ssize_t num_read = 0;
1.38      christos 2104:
1.76      christos 2105:        *cp = '\0';
1.38      christos 2106:        while (rl_event_hook) {
                   2107:
                   2108:                (*rl_event_hook)();
                   2109:
                   2110: #if defined(FIONREAD)
                   2111:                if (ioctl(el->el_infd, FIONREAD, &n) < 0)
1.96      christos 2112:                        return -1;
1.38      christos 2113:                if (n)
1.98      christos 2114:                        num_read = read(el->el_infd, cp, (size_t)1);
1.38      christos 2115:                else
                   2116:                        num_read = 0;
                   2117: #elif defined(F_SETFL) && defined(O_NDELAY)
                   2118:                if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0)
1.96      christos 2119:                        return -1;
1.38      christos 2120:                if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0)
1.96      christos 2121:                        return -1;
1.38      christos 2122:                num_read = read(el->el_infd, cp, 1);
                   2123:                if (fcntl(el->el_infd, F_SETFL, n))
1.96      christos 2124:                        return -1;
1.38      christos 2125: #else
                   2126:                /* not non-blocking, but what you gonna do? */
                   2127:                num_read = read(el->el_infd, cp, 1);
1.96      christos 2128:                return -1;
1.38      christos 2129: #endif
                   2130:
                   2131:                if (num_read < 0 && errno == EAGAIN)
                   2132:                        continue;
                   2133:                if (num_read == 0)
                   2134:                        continue;
                   2135:                break;
                   2136:        }
                   2137:        if (!rl_event_hook)
                   2138:                el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN);
1.80      christos 2139:        return (int)num_read;
1.1       christos 2140: }
1.49      christos 2141:
                   2142: static void
                   2143: _rl_update_pos(void)
                   2144: {
                   2145:        const LineInfo *li = el_line(e);
                   2146:
1.80      christos 2147:        rl_point = (int)(li->cursor - li->buffer);
                   2148:        rl_end = (int)(li->lastchar - li->buffer);
1.49      christos 2149: }
1.70      christos 2150:
                   2151: void
                   2152: rl_get_screen_size(int *rows, int *cols)
                   2153: {
                   2154:        if (rows)
1.107     christos 2155:                el_get(e, EL_GETTC, "li", rows, (void *)0);
1.70      christos 2156:        if (cols)
1.107     christos 2157:                el_get(e, EL_GETTC, "co", cols, (void *)0);
1.70      christos 2158: }
                   2159:
                   2160: void
                   2161: rl_set_screen_size(int rows, int cols)
                   2162: {
                   2163:        char buf[64];
                   2164:        (void)snprintf(buf, sizeof(buf), "%d", rows);
1.106     christos 2165:        el_set(e, EL_SETTC, "li", buf, NULL);
1.70      christos 2166:        (void)snprintf(buf, sizeof(buf), "%d", cols);
1.106     christos 2167:        el_set(e, EL_SETTC, "co", buf, NULL);
1.70      christos 2168: }
                   2169:
1.71      christos 2170: char **
                   2171: rl_completion_matches(const char *str, rl_compentry_func_t *fun)
                   2172: {
                   2173:        size_t len, max, i, j, min;
                   2174:        char **list, *match, *a, *b;
                   2175:
                   2176:        len = 1;
                   2177:        max = 10;
1.95      christos 2178:        if ((list = el_malloc(max * sizeof(*list))) == NULL)
1.71      christos 2179:                return NULL;
                   2180:
                   2181:        while ((match = (*fun)(str, (int)(len - 1))) != NULL) {
1.83      christos 2182:                list[len++] = match;
1.71      christos 2183:                if (len == max) {
                   2184:                        char **nl;
                   2185:                        max += 10;
1.95      christos 2186:                        if ((nl = el_realloc(list, max * sizeof(*nl))) == NULL)
1.71      christos 2187:                                goto out;
                   2188:                        list = nl;
                   2189:                }
                   2190:        }
                   2191:        if (len == 1)
                   2192:                goto out;
                   2193:        list[len] = NULL;
                   2194:        if (len == 2) {
                   2195:                if ((list[0] = strdup(list[1])) == NULL)
                   2196:                        goto out;
                   2197:                return list;
                   2198:        }
                   2199:        qsort(&list[1], len - 1, sizeof(*list),
                   2200:            (int (*)(const void *, const void *)) strcmp);
                   2201:        min = SIZE_T_MAX;
                   2202:        for (i = 1, a = list[i]; i < len - 1; i++, a = b) {
                   2203:                b = list[i + 1];
                   2204:                for (j = 0; a[j] && a[j] == b[j]; j++)
                   2205:                        continue;
                   2206:                if (min > j)
                   2207:                        min = j;
                   2208:        }
                   2209:        if (min == 0 && *str) {
                   2210:                if ((list[0] = strdup(str)) == NULL)
                   2211:                        goto out;
                   2212:        } else {
1.95      christos 2213:                if ((list[0] = el_malloc((min + 1) * sizeof(*list[0]))) == NULL)
1.71      christos 2214:                        goto out;
                   2215:                (void)memcpy(list[0], list[1], min);
                   2216:                list[0][min] = '\0';
                   2217:        }
                   2218:        return list;
                   2219:
                   2220: out:
1.95      christos 2221:        el_free(list);
1.71      christos 2222:        return NULL;
                   2223: }
                   2224:
1.70      christos 2225: char *
                   2226: rl_filename_completion_function (const char *text, int state)
                   2227: {
                   2228:        return fn_filename_completion_function(text, state);
                   2229: }
                   2230:
1.73      christos 2231: void
                   2232: rl_forced_update_display(void)
                   2233: {
1.74      christos 2234:        el_set(e, EL_REFRESH);
1.73      christos 2235: }
                   2236:
1.70      christos 2237: int
                   2238: _rl_abort_internal(void)
                   2239: {
                   2240:        el_beep(e);
                   2241:        longjmp(topbuf, 1);
                   2242:        /*NOTREACHED*/
                   2243: }
                   2244:
                   2245: int
                   2246: _rl_qsort_string_compare(char **s1, char **s2)
                   2247: {
                   2248:        return strcoll(*s1, *s2);
                   2249: }
                   2250:
1.85      christos 2251: HISTORY_STATE *
                   2252: history_get_history_state(void)
                   2253: {
                   2254:        HISTORY_STATE *hs;
                   2255:
1.95      christos 2256:        if ((hs = el_malloc(sizeof(*hs))) == NULL)
1.96      christos 2257:                return NULL;
1.85      christos 2258:        hs->length = history_length;
1.96      christos 2259:        return hs;
1.85      christos 2260: }
                   2261:
1.70      christos 2262: int
                   2263: /*ARGSUSED*/
1.97      christos 2264: rl_kill_text(int from __attribute__((__unused__)),
                   2265:     int to __attribute__((__unused__)))
1.70      christos 2266: {
                   2267:        return 0;
                   2268: }
                   2269:
                   2270: Keymap
                   2271: rl_make_bare_keymap(void)
                   2272: {
                   2273:        return NULL;
                   2274: }
                   2275:
                   2276: Keymap
                   2277: rl_get_keymap(void)
                   2278: {
                   2279:        return NULL;
                   2280: }
                   2281:
                   2282: void
                   2283: /*ARGSUSED*/
1.97      christos 2284: rl_set_keymap(Keymap k __attribute__((__unused__)))
1.70      christos 2285: {
                   2286: }
                   2287:
                   2288: int
                   2289: /*ARGSUSED*/
1.97      christos 2290: rl_generic_bind(int type __attribute__((__unused__)),
                   2291:     const char * keyseq __attribute__((__unused__)),
                   2292:     const char * data __attribute__((__unused__)),
                   2293:     Keymap k __attribute__((__unused__)))
1.70      christos 2294: {
                   2295:        return 0;
                   2296: }
                   2297:
                   2298: int
                   2299: /*ARGSUSED*/
1.97      christos 2300: rl_bind_key_in_map(int key __attribute__((__unused__)),
                   2301:     rl_command_func_t *fun __attribute__((__unused__)),
                   2302:     Keymap k __attribute__((__unused__)))
1.70      christos 2303: {
                   2304:        return 0;
                   2305: }
1.85      christos 2306:
                   2307: /* unsupported, but needed by python */
                   2308: void
                   2309: rl_cleanup_after_signal(void)
                   2310: {
                   2311: }
1.90      christos 2312:
                   2313: int
                   2314: rl_on_new_line(void)
                   2315: {
                   2316:        return 0;
                   2317: }
1.105     christos 2318:
                   2319: void
                   2320: rl_free_line_state(void)
                   2321: {
                   2322: }

CVSweb <webmaster@jp.NetBSD.org>