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