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

1.104   ! christos    1: /*     $NetBSD: readline.c,v 1.103 2012/05/15 19:07:25 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.104   ! christos   34: __RCSID("$NetBSD: readline.c,v 1.103 2012/05/15 19:07:25 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:
1.104   ! christos 1375:        if (line == NULL)
        !          1376:                return 0;
        !          1377:
1.1       christos 1378:        if (h == NULL || e == NULL)
                   1379:                rl_initialize();
                   1380:
1.92      christos 1381:        (void)history(h, &ev, H_ENTER, line);
                   1382:        if (history(h, &ev, H_GETSIZE) == 0)
1.1       christos 1383:                history_length = ev.num;
                   1384:
1.96      christos 1385:        return !(history_length > 0); /* return 0 if all is okay */
1.1       christos 1386: }
                   1387:
1.11      lukem    1388:
1.1       christos 1389: /*
1.58      christos 1390:  * remove the specified entry from the history list and return it.
                   1391:  */
                   1392: HIST_ENTRY *
                   1393: remove_history(int num)
                   1394: {
1.85      christos 1395:        HIST_ENTRY *he;
1.92      christos 1396:        HistEvent ev;
1.58      christos 1397:
                   1398:        if (h == NULL || e == NULL)
                   1399:                rl_initialize();
                   1400:
1.95      christos 1401:        if ((he = el_malloc(sizeof(*he))) == NULL)
1.58      christos 1402:                return NULL;
                   1403:
1.92      christos 1404:        if (history(h, &ev, H_DELDATA, num, &he->data) != 0) {
1.95      christos 1405:                el_free(he);
1.71      christos 1406:                return NULL;
1.85      christos 1407:        }
1.71      christos 1408:
1.92      christos 1409:        he->line = ev.str;
                   1410:        if (history(h, &ev, H_GETSIZE) == 0)
1.85      christos 1411:                history_length = ev.num;
1.58      christos 1412:
1.85      christos 1413:        return he;
1.58      christos 1414: }
                   1415:
                   1416:
                   1417: /*
1.85      christos 1418:  * replace the line and data of the num-th entry
                   1419:  */
                   1420: HIST_ENTRY *
                   1421: replace_history_entry(int num, const char *line, histdata_t data)
                   1422: {
                   1423:        HIST_ENTRY *he;
1.92      christos 1424:        HistEvent ev;
1.85      christos 1425:        int curr_num;
                   1426:
                   1427:        if (h == NULL || e == NULL)
                   1428:                rl_initialize();
                   1429:
                   1430:        /* save current position */
1.92      christos 1431:        if (history(h, &ev, H_CURR) != 0)
1.85      christos 1432:                return NULL;
                   1433:        curr_num = ev.num;
                   1434:
                   1435:        /* start from the oldest */
1.92      christos 1436:        if (history(h, &ev, H_LAST) != 0)
1.85      christos 1437:                return NULL;    /* error */
                   1438:
1.95      christos 1439:        if ((he = el_malloc(sizeof(*he))) == NULL)
1.85      christos 1440:                return NULL;
                   1441:
                   1442:        /* look forwards for event matching specified offset */
1.92      christos 1443:        if (history(h, &ev, H_NEXT_EVDATA, num, &he->data))
1.85      christos 1444:                goto out;
                   1445:
1.92      christos 1446:        he->line = strdup(ev.str);
1.85      christos 1447:        if (he->line == NULL)
                   1448:                goto out;
                   1449:
1.92      christos 1450:        if (history(h, &ev, H_REPLACE, line, data))
1.85      christos 1451:                goto out;
                   1452:
                   1453:        /* restore pointer to where it was */
1.92      christos 1454:        if (history(h, &ev, H_SET, curr_num))
1.85      christos 1455:                goto out;
                   1456:
                   1457:        return he;
                   1458: out:
1.95      christos 1459:        el_free(he);
1.85      christos 1460:        return NULL;
                   1461: }
                   1462:
                   1463: /*
1.1       christos 1464:  * clear the history list - delete all entries
                   1465:  */
                   1466: void
1.11      lukem    1467: clear_history(void)
1.1       christos 1468: {
1.92      christos 1469:        HistEvent ev;
1.11      lukem    1470:
1.92      christos 1471:        (void)history(h, &ev, H_CLEAR);
1.85      christos 1472:        history_length = 0;
1.1       christos 1473: }
                   1474:
1.11      lukem    1475:
1.1       christos 1476: /*
                   1477:  * returns offset of the current history event
                   1478:  */
                   1479: int
1.11      lukem    1480: where_history(void)
1.1       christos 1481: {
1.92      christos 1482:        HistEvent ev;
1.1       christos 1483:        int curr_num, off;
                   1484:
1.92      christos 1485:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1486:                return 0;
1.1       christos 1487:        curr_num = ev.num;
                   1488:
1.92      christos 1489:        (void)history(h, &ev, H_FIRST);
1.1       christos 1490:        off = 1;
1.92      christos 1491:        while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
1.1       christos 1492:                off++;
                   1493:
1.96      christos 1494:        return off;
1.1       christos 1495: }
                   1496:
1.11      lukem    1497:
1.1       christos 1498: /*
                   1499:  * returns current history event or NULL if there is no such event
                   1500:  */
1.2       christos 1501: HIST_ENTRY *
1.11      lukem    1502: current_history(void)
1.1       christos 1503: {
1.11      lukem    1504:
1.96      christos 1505:        return _move_history(H_CURR);
1.1       christos 1506: }
                   1507:
1.11      lukem    1508:
1.1       christos 1509: /*
                   1510:  * returns total number of bytes history events' data are using
                   1511:  */
                   1512: int
1.11      lukem    1513: history_total_bytes(void)
1.1       christos 1514: {
1.92      christos 1515:        HistEvent ev;
1.80      christos 1516:        int curr_num;
                   1517:        size_t size;
1.1       christos 1518:
1.92      christos 1519:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1520:                return -1;
1.1       christos 1521:        curr_num = ev.num;
                   1522:
1.92      christos 1523:        (void)history(h, &ev, H_FIRST);
1.1       christos 1524:        size = 0;
                   1525:        do
1.92      christos 1526:                size += strlen(ev.str) * sizeof(*ev.str);
                   1527:        while (history(h, &ev, H_NEXT) == 0);
1.1       christos 1528:
                   1529:        /* get to the same position as before */
1.92      christos 1530:        history(h, &ev, H_PREV_EVENT, curr_num);
1.1       christos 1531:
1.96      christos 1532:        return (int)size;
1.1       christos 1533: }
                   1534:
1.11      lukem    1535:
1.1       christos 1536: /*
                   1537:  * sets the position in the history list to ``pos''
                   1538:  */
                   1539: int
1.11      lukem    1540: history_set_pos(int pos)
1.1       christos 1541: {
1.92      christos 1542:        HistEvent ev;
1.40      christos 1543:        int curr_num;
1.1       christos 1544:
1.85      christos 1545:        if (pos >= history_length || pos < 0)
1.96      christos 1546:                return -1;
1.1       christos 1547:
1.92      christos 1548:        (void)history(h, &ev, H_CURR);
1.1       christos 1549:        curr_num = ev.num;
                   1550:
1.85      christos 1551:        /*
                   1552:         * use H_DELDATA to set to nth history (without delete) by passing
                   1553:         * (void **)-1
                   1554:         */
1.92      christos 1555:        if (history(h, &ev, H_DELDATA, pos, (void **)-1)) {
                   1556:                (void)history(h, &ev, H_SET, curr_num);
1.96      christos 1557:                return -1;
1.1       christos 1558:        }
1.96      christos 1559:        return 0;
1.1       christos 1560: }
                   1561:
1.11      lukem    1562:
1.1       christos 1563: /*
                   1564:  * returns previous event in history and shifts pointer accordingly
                   1565:  */
1.2       christos 1566: HIST_ENTRY *
1.11      lukem    1567: previous_history(void)
1.1       christos 1568: {
1.11      lukem    1569:
1.96      christos 1570:        return _move_history(H_PREV);
1.1       christos 1571: }
                   1572:
1.11      lukem    1573:
1.1       christos 1574: /*
                   1575:  * returns next event in history and shifts pointer accordingly
                   1576:  */
1.2       christos 1577: HIST_ENTRY *
1.11      lukem    1578: next_history(void)
1.1       christos 1579: {
1.11      lukem    1580:
1.96      christos 1581:        return _move_history(H_NEXT);
1.1       christos 1582: }
                   1583:
1.11      lukem    1584:
1.1       christos 1585: /*
1.40      christos 1586:  * searches for first history event containing the str
1.1       christos 1587:  */
1.40      christos 1588: int
                   1589: history_search(const char *str, int direction)
1.11      lukem    1590: {
1.92      christos 1591:        HistEvent ev;
                   1592:        const char *strp;
1.11      lukem    1593:        int curr_num;
1.1       christos 1594:
1.92      christos 1595:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1596:                return -1;
1.1       christos 1597:        curr_num = ev.num;
                   1598:
                   1599:        for (;;) {
1.92      christos 1600:                if ((strp = strstr(ev.str, str)) != NULL)
1.96      christos 1601:                        return (int)(strp - ev.str);
1.92      christos 1602:                if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)
1.1       christos 1603:                        break;
                   1604:        }
1.92      christos 1605:        (void)history(h, &ev, H_SET, curr_num);
1.96      christos 1606:        return -1;
1.1       christos 1607: }
                   1608:
1.11      lukem    1609:
1.1       christos 1610: /*
                   1611:  * searches for first history event beginning with str
                   1612:  */
                   1613: int
1.11      lukem    1614: history_search_prefix(const char *str, int direction)
1.1       christos 1615: {
1.92      christos 1616:        HistEvent ev;
1.11      lukem    1617:
1.92      christos 1618:        return (history(h, &ev, direction < 0 ?
1.88      christos 1619:            H_PREV_STR : H_NEXT_STR, str));
1.1       christos 1620: }
                   1621:
1.11      lukem    1622:
1.1       christos 1623: /*
                   1624:  * search for event in history containing str, starting at offset
                   1625:  * abs(pos); continue backward, if pos<0, forward otherwise
                   1626:  */
1.5       christos 1627: /* ARGSUSED */
1.1       christos 1628: int
1.40      christos 1629: history_search_pos(const char *str,
1.30      christos 1630:                   int direction __attribute__((__unused__)), int pos)
1.1       christos 1631: {
1.92      christos 1632:        HistEvent ev;
1.11      lukem    1633:        int curr_num, off;
1.1       christos 1634:
                   1635:        off = (pos > 0) ? pos : -pos;
                   1636:        pos = (pos > 0) ? 1 : -1;
                   1637:
1.92      christos 1638:        if (history(h, &ev, H_CURR) != 0)
1.96      christos 1639:                return -1;
1.1       christos 1640:        curr_num = ev.num;
                   1641:
1.92      christos 1642:        if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
1.96      christos 1643:                return -1;
1.1       christos 1644:
                   1645:        for (;;) {
1.92      christos 1646:                if (strstr(ev.str, str))
1.96      christos 1647:                        return off;
1.92      christos 1648:                if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
1.1       christos 1649:                        break;
                   1650:        }
                   1651:
                   1652:        /* set "current" pointer back to previous state */
1.92      christos 1653:        (void)history(h, &ev,
1.88      christos 1654:            pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
1.1       christos 1655:
1.96      christos 1656:        return -1;
1.1       christos 1657: }
                   1658:
                   1659:
                   1660: /********************************/
1.40      christos 1661: /* completion functions */
1.1       christos 1662:
1.57      christos 1663: char *
                   1664: tilde_expand(char *name)
                   1665: {
                   1666:        return fn_tilde_expand(name);
                   1667: }
                   1668:
                   1669: char *
                   1670: filename_completion_function(const char *name, int state)
                   1671: {
                   1672:        return fn_filename_completion_function(name, state);
                   1673: }
                   1674:
1.1       christos 1675: /*
                   1676:  * a completion generator for usernames; returns _first_ username
                   1677:  * which starts with supplied text
                   1678:  * text contains a partial username preceded by random character
1.94      christos 1679:  * (usually '~'); state resets search from start (??? should we do that anyway)
1.1       christos 1680:  * it's callers responsibility to free returned value
                   1681:  */
1.11      lukem    1682: char *
                   1683: username_completion_function(const char *text, int state)
1.1       christos 1684: {
1.94      christos 1685: #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
                   1686:        struct passwd pwres;
1.50      christos 1687:        char pwbuf[1024];
1.94      christos 1688: #endif
                   1689:        struct passwd *pass = NULL;
1.1       christos 1690:
                   1691:        if (text[0] == '\0')
1.96      christos 1692:                return NULL;
1.1       christos 1693:
                   1694:        if (*text == '~')
                   1695:                text++;
                   1696:
                   1697:        if (state == 0)
                   1698:                setpwent();
                   1699:
1.94      christos 1700:        while (
                   1701: #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
                   1702:            getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pass) == 0 && pass != NULL
                   1703: #else
                   1704:            (pass = getpwent()) != NULL
                   1705: #endif
                   1706:            && text[0] == pass->pw_name[0]
                   1707:            && strcmp(text, pass->pw_name) == 0)
                   1708:                continue;
1.1       christos 1709:
1.94      christos 1710:        if (pass == NULL) {
1.1       christos 1711:                endpwent();
1.85      christos 1712:                return NULL;
1.1       christos 1713:        }
1.94      christos 1714:        return strdup(pass->pw_name);
1.1       christos 1715: }
                   1716:
1.11      lukem    1717:
1.1       christos 1718: /*
1.46      christos 1719:  * el-compatible wrapper to send TSTP on ^Z
                   1720:  */
                   1721: /* ARGSUSED */
                   1722: static unsigned char
                   1723: _el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__)))
                   1724: {
                   1725:        (void)kill(0, SIGTSTP);
                   1726:        return CC_NORM;
                   1727: }
1.11      lukem    1728:
1.1       christos 1729: /*
1.12      jdolecek 1730:  * Display list of strings in columnar format on readline's output stream.
                   1731:  * 'matches' is list of strings, 'len' is number of strings in 'matches',
                   1732:  * 'max' is maximum length of string in 'matches'.
                   1733:  */
                   1734: void
1.53      dsl      1735: rl_display_match_list(char **matches, int len, int max)
1.12      jdolecek 1736: {
                   1737:
1.81      christos 1738:        fn_display_match_list(e, matches, (size_t)len, (size_t)max);
1.1       christos 1739: }
                   1740:
1.56      christos 1741: static const char *
                   1742: /*ARGSUSED*/
                   1743: _rl_completion_append_character_function(const char *dummy
                   1744:     __attribute__((__unused__)))
                   1745: {
                   1746:        static char buf[2];
1.99      christos 1747:        buf[0] = (char)rl_completion_append_character;
1.76      christos 1748:        buf[1] = '\0';
1.56      christos 1749:        return buf;
                   1750: }
                   1751:
1.11      lukem    1752:
1.1       christos 1753: /*
                   1754:  * complete word at current point
                   1755:  */
1.53      dsl      1756: /* ARGSUSED */
1.1       christos 1757: int
1.53      dsl      1758: rl_complete(int ignore __attribute__((__unused__)), int invoking_key)
1.1       christos 1759: {
1.86      christos 1760: #ifdef WIDECHAR
                   1761:        static ct_buffer_t wbreak_conv, sprefix_conv;
                   1762: #endif
1.102     christos 1763:        char *breakchars;
1.86      christos 1764:
1.1       christos 1765:        if (h == NULL || e == NULL)
                   1766:                rl_initialize();
                   1767:
                   1768:        if (rl_inhibit_completion) {
1.47      christos 1769:                char arr[2];
                   1770:                arr[0] = (char)invoking_key;
                   1771:                arr[1] = '\0';
                   1772:                el_insertstr(e, arr);
1.96      christos 1773:                return CC_REFRESH;
1.53      dsl      1774:        }
                   1775:
1.102     christos 1776:        if (rl_completion_word_break_hook != NULL)
                   1777:                breakchars = (*rl_completion_word_break_hook)();
                   1778:        else
                   1779:                breakchars = rl_basic_word_break_characters;
                   1780:
1.53      dsl      1781:        /* Just look at how many global variables modify this operation! */
                   1782:        return fn_complete(e,
                   1783:            (CPFunction *)rl_completion_entry_function,
                   1784:            rl_attempted_completion_function,
1.86      christos 1785:            ct_decode_string(rl_basic_word_break_characters, &wbreak_conv),
1.102     christos 1786:            ct_decode_string(breakchars, &sprefix_conv),
1.81      christos 1787:            _rl_completion_append_character_function,
                   1788:            (size_t)rl_completion_query_items,
1.53      dsl      1789:            &rl_completion_type, &rl_attempted_completion_over,
                   1790:            &rl_point, &rl_end);
1.92      christos 1791:
                   1792:
1.1       christos 1793: }
                   1794:
1.11      lukem    1795:
1.53      dsl      1796: /* ARGSUSED */
                   1797: static unsigned char
                   1798: _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch)
                   1799: {
                   1800:        return (unsigned char)rl_complete(0, ch);
                   1801: }
                   1802:
1.1       christos 1803: /*
1.7       simonb   1804:  * misc other functions
1.1       christos 1805:  */
                   1806:
                   1807: /*
                   1808:  * bind key c to readline-type function func
                   1809:  */
                   1810: int
1.85      christos 1811: rl_bind_key(int c, rl_command_func_t *func)
1.1       christos 1812: {
1.11      lukem    1813:        int retval = -1;
1.1       christos 1814:
                   1815:        if (h == NULL || e == NULL)
                   1816:                rl_initialize();
                   1817:
                   1818:        if (func == rl_insert) {
                   1819:                /* XXX notice there is no range checking of ``c'' */
                   1820:                e->el_map.key[c] = ED_INSERT;
                   1821:                retval = 0;
                   1822:        }
1.96      christos 1823:        return retval;
1.1       christos 1824: }
                   1825:
1.11      lukem    1826:
1.1       christos 1827: /*
                   1828:  * read one key from input - handles chars pushed back
                   1829:  * to input stream also
                   1830:  */
                   1831: int
1.11      lukem    1832: rl_read_key(void)
1.1       christos 1833: {
1.11      lukem    1834:        char fooarr[2 * sizeof(int)];
1.1       christos 1835:
                   1836:        if (e == NULL || h == NULL)
                   1837:                rl_initialize();
                   1838:
1.96      christos 1839:        return el_getc(e, fooarr);
1.1       christos 1840: }
                   1841:
1.11      lukem    1842:
1.1       christos 1843: /*
                   1844:  * reset the terminal
                   1845:  */
1.5       christos 1846: /* ARGSUSED */
1.1       christos 1847: void
1.30      christos 1848: rl_reset_terminal(const char *p __attribute__((__unused__)))
1.1       christos 1849: {
1.11      lukem    1850:
1.1       christos 1851:        if (h == NULL || e == NULL)
                   1852:                rl_initialize();
                   1853:        el_reset(e);
                   1854: }
                   1855:
1.11      lukem    1856:
1.1       christos 1857: /*
                   1858:  * insert character ``c'' back into input stream, ``count'' times
                   1859:  */
                   1860: int
1.11      lukem    1861: rl_insert(int count, int c)
1.1       christos 1862: {
1.11      lukem    1863:        char arr[2];
1.1       christos 1864:
                   1865:        if (h == NULL || e == NULL)
                   1866:                rl_initialize();
                   1867:
                   1868:        /* XXX - int -> char conversion can lose on multichars */
1.99      christos 1869:        arr[0] = (char)c;
1.1       christos 1870:        arr[1] = '\0';
                   1871:
                   1872:        for (; count > 0; count--)
                   1873:                el_push(e, arr);
                   1874:
1.96      christos 1875:        return 0;
1.35      christos 1876: }
                   1877:
1.85      christos 1878: int
                   1879: rl_insert_text(const char *text)
                   1880: {
                   1881:        if (!text || *text == 0)
1.96      christos 1882:                return 0;
1.85      christos 1883:
                   1884:        if (h == NULL || e == NULL)
                   1885:                rl_initialize();
                   1886:
                   1887:        if (el_insertstr(e, text) < 0)
1.96      christos 1888:                return 0;
1.85      christos 1889:        return (int)strlen(text);
                   1890: }
                   1891:
1.35      christos 1892: /*ARGSUSED*/
                   1893: int
1.97      christos 1894: rl_newline(int count __attribute__((__unused__)),
                   1895:     int c __attribute__((__unused__)))
1.35      christos 1896: {
                   1897:        /*
                   1898:         * Readline-4.0 appears to ignore the args.
                   1899:         */
                   1900:        return rl_insert(1, '\n');
                   1901: }
                   1902:
                   1903: /*ARGSUSED*/
                   1904: static unsigned char
1.97      christos 1905: rl_bind_wrapper(EditLine *el __attribute__((__unused__)), unsigned char c)
1.35      christos 1906: {
                   1907:        if (map[c] == NULL)
                   1908:            return CC_ERROR;
1.49      christos 1909:
                   1910:        _rl_update_pos();
                   1911:
1.35      christos 1912:        (*map[c])(NULL, c);
1.38      christos 1913:
                   1914:        /* If rl_done was set by the above call, deal with it here */
                   1915:        if (rl_done)
                   1916:                return CC_EOF;
                   1917:
1.35      christos 1918:        return CC_NORM;
                   1919: }
                   1920:
                   1921: int
                   1922: rl_add_defun(const char *name, Function *fun, int c)
                   1923: {
                   1924:        char dest[8];
1.77      lukem    1925:        if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0)
1.35      christos 1926:                return -1;
                   1927:        map[(unsigned char)c] = fun;
                   1928:        el_set(e, EL_ADDFN, name, name, rl_bind_wrapper);
1.37      christos 1929:        vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0);
                   1930:        el_set(e, EL_BIND, dest, name);
1.35      christos 1931:        return 0;
                   1932: }
                   1933:
                   1934: void
1.101     matt     1935: rl_callback_read_char(void)
1.35      christos 1936: {
1.36      christos 1937:        int count = 0, done = 0;
1.35      christos 1938:        const char *buf = el_gets(e, &count);
                   1939:        char *wbuf;
                   1940:
                   1941:        if (buf == NULL || count-- <= 0)
                   1942:                return;
1.61      christos 1943:        if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF])
1.36      christos 1944:                done = 1;
                   1945:        if (buf[count] == '\n' || buf[count] == '\r')
                   1946:                done = 2;
                   1947:
                   1948:        if (done && rl_linefunc != NULL) {
1.35      christos 1949:                el_set(e, EL_UNBUFFERED, 0);
1.36      christos 1950:                if (done == 2) {
                   1951:                    if ((wbuf = strdup(buf)) != NULL)
                   1952:                        wbuf[count] = '\0';
                   1953:                } else
                   1954:                        wbuf = NULL;
1.35      christos 1955:                (*(void (*)(const char *))rl_linefunc)(wbuf);
1.85      christos 1956:                //el_set(e, EL_UNBUFFERED, 1);
1.35      christos 1957:        }
                   1958: }
                   1959:
                   1960: void
1.72      christos 1961: rl_callback_handler_install(const char *prompt, VCPFunction *linefunc)
1.35      christos 1962: {
                   1963:        if (e == NULL) {
                   1964:                rl_initialize();
                   1965:        }
1.78      christos 1966:        (void)rl_set_prompt(prompt);
1.35      christos 1967:        rl_linefunc = linefunc;
                   1968:        el_set(e, EL_UNBUFFERED, 1);
                   1969: }
                   1970:
                   1971: void
                   1972: rl_callback_handler_remove(void)
                   1973: {
                   1974:        el_set(e, EL_UNBUFFERED, 0);
1.72      christos 1975:        rl_linefunc = NULL;
1.35      christos 1976: }
                   1977:
                   1978: void
                   1979: rl_redisplay(void)
                   1980: {
                   1981:        char a[2];
1.99      christos 1982:        a[0] = (char)e->el_tty.t_c[TS_IO][C_REPRINT];
1.35      christos 1983:        a[1] = '\0';
                   1984:        el_push(e, a);
                   1985: }
                   1986:
                   1987: int
                   1988: rl_get_previous_history(int count, int key)
                   1989: {
                   1990:        char a[2];
1.99      christos 1991:        a[0] = (char)key;
1.35      christos 1992:        a[1] = '\0';
                   1993:        while (count--)
                   1994:                el_push(e, a);
                   1995:        return 0;
1.38      christos 1996: }
                   1997:
                   1998: void
                   1999: /*ARGSUSED*/
1.97      christos 2000: rl_prep_terminal(int meta_flag __attribute__((__unused__)))
1.38      christos 2001: {
                   2002:        el_set(e, EL_PREP_TERM, 1);
                   2003: }
                   2004:
                   2005: void
1.68      christos 2006: rl_deprep_terminal(void)
1.38      christos 2007: {
                   2008:        el_set(e, EL_PREP_TERM, 0);
                   2009: }
                   2010:
                   2011: int
                   2012: rl_read_init_file(const char *s)
                   2013: {
1.96      christos 2014:        return el_source(e, s);
1.38      christos 2015: }
                   2016:
                   2017: int
                   2018: rl_parse_and_bind(const char *line)
                   2019: {
                   2020:        const char **argv;
                   2021:        int argc;
                   2022:        Tokenizer *tok;
                   2023:
                   2024:        tok = tok_init(NULL);
1.44      lukem    2025:        tok_str(tok, line, &argc, &argv);
1.38      christos 2026:        argc = el_parse(e, argc, argv);
                   2027:        tok_end(tok);
1.96      christos 2028:        return argc ? 1 : 0;
1.38      christos 2029: }
                   2030:
1.51      christos 2031: int
                   2032: rl_variable_bind(const char *var, const char *value)
                   2033: {
                   2034:        /*
                   2035:         * The proper return value is undocument, but this is what the
                   2036:         * readline source seems to do.
                   2037:         */
1.96      christos 2038:        return el_set(e, EL_BIND, "", var, value) == -1 ? 1 : 0;
1.51      christos 2039: }
                   2040:
1.38      christos 2041: void
                   2042: rl_stuff_char(int c)
                   2043: {
                   2044:        char buf[2];
                   2045:
1.99      christos 2046:        buf[0] = (char)c;
1.38      christos 2047:        buf[1] = '\0';
                   2048:        el_insertstr(e, buf);
                   2049: }
                   2050:
                   2051: static int
                   2052: _rl_event_read_char(EditLine *el, char *cp)
                   2053: {
1.80      christos 2054:        int     n;
                   2055:        ssize_t num_read = 0;
1.38      christos 2056:
1.76      christos 2057:        *cp = '\0';
1.38      christos 2058:        while (rl_event_hook) {
                   2059:
                   2060:                (*rl_event_hook)();
                   2061:
                   2062: #if defined(FIONREAD)
                   2063:                if (ioctl(el->el_infd, FIONREAD, &n) < 0)
1.96      christos 2064:                        return -1;
1.38      christos 2065:                if (n)
1.98      christos 2066:                        num_read = read(el->el_infd, cp, (size_t)1);
1.38      christos 2067:                else
                   2068:                        num_read = 0;
                   2069: #elif defined(F_SETFL) && defined(O_NDELAY)
                   2070:                if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0)
1.96      christos 2071:                        return -1;
1.38      christos 2072:                if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0)
1.96      christos 2073:                        return -1;
1.38      christos 2074:                num_read = read(el->el_infd, cp, 1);
                   2075:                if (fcntl(el->el_infd, F_SETFL, n))
1.96      christos 2076:                        return -1;
1.38      christos 2077: #else
                   2078:                /* not non-blocking, but what you gonna do? */
                   2079:                num_read = read(el->el_infd, cp, 1);
1.96      christos 2080:                return -1;
1.38      christos 2081: #endif
                   2082:
                   2083:                if (num_read < 0 && errno == EAGAIN)
                   2084:                        continue;
                   2085:                if (num_read == 0)
                   2086:                        continue;
                   2087:                break;
                   2088:        }
                   2089:        if (!rl_event_hook)
                   2090:                el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN);
1.80      christos 2091:        return (int)num_read;
1.1       christos 2092: }
1.49      christos 2093:
                   2094: static void
                   2095: _rl_update_pos(void)
                   2096: {
                   2097:        const LineInfo *li = el_line(e);
                   2098:
1.80      christos 2099:        rl_point = (int)(li->cursor - li->buffer);
                   2100:        rl_end = (int)(li->lastchar - li->buffer);
1.49      christos 2101: }
1.70      christos 2102:
                   2103: void
                   2104: rl_get_screen_size(int *rows, int *cols)
                   2105: {
                   2106:        if (rows)
                   2107:                el_get(e, EL_GETTC, "li", rows);
                   2108:        if (cols)
                   2109:                el_get(e, EL_GETTC, "co", cols);
                   2110: }
                   2111:
                   2112: void
                   2113: rl_set_screen_size(int rows, int cols)
                   2114: {
                   2115:        char buf[64];
                   2116:        (void)snprintf(buf, sizeof(buf), "%d", rows);
                   2117:        el_set(e, EL_SETTC, "li", buf);
                   2118:        (void)snprintf(buf, sizeof(buf), "%d", cols);
                   2119:        el_set(e, EL_SETTC, "co", buf);
                   2120: }
                   2121:
1.71      christos 2122: char **
                   2123: rl_completion_matches(const char *str, rl_compentry_func_t *fun)
                   2124: {
                   2125:        size_t len, max, i, j, min;
                   2126:        char **list, *match, *a, *b;
                   2127:
                   2128:        len = 1;
                   2129:        max = 10;
1.95      christos 2130:        if ((list = el_malloc(max * sizeof(*list))) == NULL)
1.71      christos 2131:                return NULL;
                   2132:
                   2133:        while ((match = (*fun)(str, (int)(len - 1))) != NULL) {
1.83      christos 2134:                list[len++] = match;
1.71      christos 2135:                if (len == max) {
                   2136:                        char **nl;
                   2137:                        max += 10;
1.95      christos 2138:                        if ((nl = el_realloc(list, max * sizeof(*nl))) == NULL)
1.71      christos 2139:                                goto out;
                   2140:                        list = nl;
                   2141:                }
                   2142:        }
                   2143:        if (len == 1)
                   2144:                goto out;
                   2145:        list[len] = NULL;
                   2146:        if (len == 2) {
                   2147:                if ((list[0] = strdup(list[1])) == NULL)
                   2148:                        goto out;
                   2149:                return list;
                   2150:        }
                   2151:        qsort(&list[1], len - 1, sizeof(*list),
                   2152:            (int (*)(const void *, const void *)) strcmp);
                   2153:        min = SIZE_T_MAX;
                   2154:        for (i = 1, a = list[i]; i < len - 1; i++, a = b) {
                   2155:                b = list[i + 1];
                   2156:                for (j = 0; a[j] && a[j] == b[j]; j++)
                   2157:                        continue;
                   2158:                if (min > j)
                   2159:                        min = j;
                   2160:        }
                   2161:        if (min == 0 && *str) {
                   2162:                if ((list[0] = strdup(str)) == NULL)
                   2163:                        goto out;
                   2164:        } else {
1.95      christos 2165:                if ((list[0] = el_malloc((min + 1) * sizeof(*list[0]))) == NULL)
1.71      christos 2166:                        goto out;
                   2167:                (void)memcpy(list[0], list[1], min);
                   2168:                list[0][min] = '\0';
                   2169:        }
                   2170:        return list;
                   2171:
                   2172: out:
1.95      christos 2173:        el_free(list);
1.71      christos 2174:        return NULL;
                   2175: }
                   2176:
1.70      christos 2177: char *
                   2178: rl_filename_completion_function (const char *text, int state)
                   2179: {
                   2180:        return fn_filename_completion_function(text, state);
                   2181: }
                   2182:
1.73      christos 2183: void
                   2184: rl_forced_update_display(void)
                   2185: {
1.74      christos 2186:        el_set(e, EL_REFRESH);
1.73      christos 2187: }
                   2188:
1.70      christos 2189: int
                   2190: _rl_abort_internal(void)
                   2191: {
                   2192:        el_beep(e);
                   2193:        longjmp(topbuf, 1);
                   2194:        /*NOTREACHED*/
                   2195: }
                   2196:
                   2197: int
                   2198: _rl_qsort_string_compare(char **s1, char **s2)
                   2199: {
                   2200:        return strcoll(*s1, *s2);
                   2201: }
                   2202:
1.85      christos 2203: HISTORY_STATE *
                   2204: history_get_history_state(void)
                   2205: {
                   2206:        HISTORY_STATE *hs;
                   2207:
1.95      christos 2208:        if ((hs = el_malloc(sizeof(*hs))) == NULL)
1.96      christos 2209:                return NULL;
1.85      christos 2210:        hs->length = history_length;
1.96      christos 2211:        return hs;
1.85      christos 2212: }
                   2213:
1.70      christos 2214: int
                   2215: /*ARGSUSED*/
1.97      christos 2216: rl_kill_text(int from __attribute__((__unused__)),
                   2217:     int to __attribute__((__unused__)))
1.70      christos 2218: {
                   2219:        return 0;
                   2220: }
                   2221:
                   2222: Keymap
                   2223: rl_make_bare_keymap(void)
                   2224: {
                   2225:        return NULL;
                   2226: }
                   2227:
                   2228: Keymap
                   2229: rl_get_keymap(void)
                   2230: {
                   2231:        return NULL;
                   2232: }
                   2233:
                   2234: void
                   2235: /*ARGSUSED*/
1.97      christos 2236: rl_set_keymap(Keymap k __attribute__((__unused__)))
1.70      christos 2237: {
                   2238: }
                   2239:
                   2240: int
                   2241: /*ARGSUSED*/
1.97      christos 2242: rl_generic_bind(int type __attribute__((__unused__)),
                   2243:     const char * keyseq __attribute__((__unused__)),
                   2244:     const char * data __attribute__((__unused__)),
                   2245:     Keymap k __attribute__((__unused__)))
1.70      christos 2246: {
                   2247:        return 0;
                   2248: }
                   2249:
                   2250: int
                   2251: /*ARGSUSED*/
1.97      christos 2252: rl_bind_key_in_map(int key __attribute__((__unused__)),
                   2253:     rl_command_func_t *fun __attribute__((__unused__)),
                   2254:     Keymap k __attribute__((__unused__)))
1.70      christos 2255: {
                   2256:        return 0;
                   2257: }
1.85      christos 2258:
                   2259: /* unsupported, but needed by python */
                   2260: void
                   2261: rl_cleanup_after_signal(void)
                   2262: {
                   2263: }
1.90      christos 2264:
                   2265: int
                   2266: rl_on_new_line(void)
                   2267: {
                   2268:        return 0;
                   2269: }

CVSweb <webmaster@jp.NetBSD.org>