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

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

CVSweb <webmaster@jp.NetBSD.org>