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