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

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

CVSweb <webmaster@jp.NetBSD.org>