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

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

CVSweb <webmaster@jp.NetBSD.org>