3
3
readline.c - GNU Readline module
6
6
created at: Wed Jan 20 13:59:32 JST 1999
8
8
Copyright (C) 1997-2008 Shugo Maeda
9
9
Copyright (C) 2008-2009 TAKAO Kouji
11
$Id: readline.c 32613 2011-07-22 04:54:09Z drbrain $
11
$Id: readline.c 34505 2012-02-09 03:25:07Z nobu $
14
14
- TAKAO Kouji <kouji at takao7 dot net> (current maintainer)
71
75
static int (*history_get_offset_func)(int);
76
static int (*history_replace_offset_func)(int);
77
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
78
static int readline_completion_append_character;
73
81
static char **readline_attempted_completion_function(const char *text,
74
82
int start, int end);
367
378
if (!isatty(fileno(rl_instream)) && errno == EBADF) rb_raise(rb_eIOError, "closed stdin");
381
int fd = fileno(rl_outstream);
382
if (fd < 0 || fstat(fd, &stbuf) != 0) {
383
rb_raise(rb_eIOError, "closed stdout");
370
388
rl_prep_terminal(1);
641
661
proc = rb_attr_get(mReadline, completion_proc);
664
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
665
rl_completion_append_character = readline_completion_append_character;
644
667
#ifdef HAVE_RL_ATTEMPTED_COMPLETION_OVER
645
668
rl_attempted_completion_over = 1;
647
670
case_fold = RTEST(rb_attr_get(mReadline, completion_case_fold));
648
671
ary = rb_funcall(proc, rb_intern("call"), 1, rb_locale_str_new_cstr(text));
649
if (TYPE(ary) != T_ARRAY)
672
if (!RB_TYPE_P(ary, T_ARRAY))
650
673
ary = rb_Array(ary);
651
674
matches = RARRAY_LEN(ary);
654
result = ALLOC_N(char *, matches + 2);
675
if (matches == 0) return NULL;
676
result = (char**)malloc((matches + 2)*sizeof(char*));
677
if (result == NULL) rb_raise(rb_eNoMemError, "failed to allocate memory");
678
enc = rb_locale_encoding();
679
encobj = rb_enc_from_encoding(enc);
655
680
for (i = 0; i < matches; i++) {
656
681
temp = rb_obj_as_string(RARRAY_PTR(ary)[i]);
657
result[i + 1] = ALLOC_N(char, RSTRING_LEN(temp) + 1);
682
StringValueCStr(temp); /* must be NUL-terminated */
683
rb_enc_check(encobj, temp);
684
result[i + 1] = (char*)malloc(RSTRING_LEN(temp) + 1);
685
if (result[i + 1] == NULL) rb_memerror();
658
686
strcpy(result[i + 1], RSTRING_PTR(temp));
660
688
result[matches + 1] = NULL;
663
691
result[0] = strdup(result[1]);
669
while (i < matches) {
670
register int c1, c2, si;
674
(c1 = TOLOWER(result[i][si])) &&
675
(c2 = TOLOWER(result[i + 1][si]));
680
(c1 = result[i][si]) &&
681
(c2 = result[i + 1][si]);
694
const char *result1 = result[1];
695
long low = strlen(result1);
697
for (i = 1; i < matches; ++i) {
701
const char *p2 = result[i + 1];
704
for (i1 = i2 = 0; i1 < low && i2 < l2; i1 += n1, i2 += n2) {
705
c1 = rb_enc_codepoint_len(result1 + i1, result1 + low, &n1, enc);
706
c2 = rb_enc_codepoint_len(p2 + i2, p2 + l2, &n2, enc);
686
if (low > si) low = si;
689
716
result[0] = ALLOC_N(char, low + 1);
690
717
strncpy(result[0], result[1], low);
1299
1326
i += history_length;
1302
entry = replace_history_entry(i, RSTRING_PTR(str), NULL);
1329
entry = replace_history_entry(history_replace_offset_func(i), RSTRING_PTR(str), NULL);
1304
1331
if (entry == NULL) {
1305
1332
rb_raise(rb_eIndexError, "invalid index");
1494
1521
/* Allow conditional parsing of the ~/.inputrc file. */
1495
1522
rl_readline_name = (char *)"Ruby";
1524
#if defined HAVE_RL_GETC_FUNCTION
1525
/* libedit check rl_getc_function only when rl_initialize() is called, */
1526
/* and using_history() call rl_initialize(). */
1527
/* This assignment should be placed before using_history() */
1528
rl_getc_function = readline_getc;
1529
id_getbyte = rb_intern_const("getbyte");
1530
#elif defined HAVE_RL_EVENT_HOOK
1531
rl_event_hook = readline_event;
1497
1534
using_history();
1499
1536
completion_proc = rb_intern(COMPLETION_PROC);
1605
1642
rb_define_const(mReadline, "USERNAME_COMPLETION_PROC", ucomp);
1606
1643
history_get_offset_func = history_get_offset_history_base;
1644
history_replace_offset_func = history_get_offset_0;
1607
1645
#if defined HAVE_RL_LIBRARY_VERSION
1608
1646
version = rb_str_new_cstr(rl_library_version);
1609
1647
#if defined HAVE_CLEAR_HISTORY || defined HAVE_REMOVE_HISTORY
1613
1651
if (history_get(history_get_offset_func(0)) == NULL) {
1614
1652
history_get_offset_func = history_get_offset_0;
1616
#if !defined HAVE_CLEAR_HISTORY
1654
#ifdef HAVE_REPLACE_HISTORY_ENTRY
1655
if (replace_history_entry(0, "a", NULL) == NULL) {
1656
history_replace_offset_func = history_get_offset_history_base;
1659
#ifdef HAVE_CLEAR_HISTORY
1617
1660
clear_history();
1633
1676
rb_define_const(mReadline, "VERSION", version);
1635
1678
rl_attempted_completion_function = readline_attempted_completion_function;
1636
#if defined HAVE_RL_GETC_FUNCTION
1637
rl_getc_function = readline_getc;
1638
id_getbyte = rb_intern_const("getbyte");
1639
#elif defined HAVE_RL_EVENT_HOOK
1640
rl_event_hook = readline_event;
1642
1679
#ifdef HAVE_RL_CATCH_SIGNALS
1643
1680
rl_catch_signals = 0;