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

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

CVSweb <webmaster@jp.NetBSD.org>