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