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