readline.c 30.3 KB
Newer Older
1 2
/* This module makes GNU readline available to Python.  It has ideas
 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
3 4
 * Center.  The completer interface was inspired by Lele Gaifax.  More
 * recently, it was largely rewritten by Guido van Rossum.
5 6
 */

7
/* Standard definitions */
8 9 10
#include "Python.h"
#include <setjmp.h>
#include <signal.h>
11
#include <errno.h>
12
#include <sys/time.h>
13

14
#if defined(HAVE_SETLOCALE)
15 16 17 18 19 20 21 22
/* GNU readline() mistakenly sets the LC_CTYPE locale.
 * This is evil.  Only the user or the app's main() should do this!
 * We must save and restore the locale around the rl_initialize() call.
 */
#define SAVE_LOCALE
#include <locale.h>
#endif

23 24 25
#ifdef SAVE_LOCALE
#  define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
#else
26
#  define RESTORE_LOCALE(sl)
27 28
#endif

29
/* GNU readline definitions */
30
#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
31 32
#include <readline/readline.h>
#include <readline/history.h>
33

34
#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum's avatar
Guido van Rossum committed
35
#define completion_matches(x, y) \
36
    rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
37
#else
38
#if defined(_RL_FUNCTION_TYPEDEF)
39
extern char **completion_matches(char *, rl_compentry_func_t *);
40
#else
41 42

#if !defined(__APPLE__)
43 44
extern char **completion_matches(char *, CPFunction *);
#endif
45
#endif
46
#endif
47

48 49 50
#ifdef __APPLE__
/*
 * It is possible to link the readline module to the readline
51 52
 * emulation library of editline/libedit.
 *
53 54 55 56
 * On OSX this emulation library is not 100% API compatible
 * with the "real" readline and cannot be detected at compile-time,
 * hence we use a runtime check to detect if we're using libedit
 *
57
 * Currently there is one know API incompatibility:
58 59 60 61 62 63 64 65 66
 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
 *   index with libedit's emulation.
 * - Note that replace_history and remove_history use a 0-based index
 *   with both implementation.
 */
static int using_libedit_emulation = 0;
static const char libedit_version_tag[] = "EditLine wrapper";
#endif /* __APPLE__ */

67
#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
68 69
static void
on_completion_display_matches_hook(char **matches,
70
                                   int num_matches, int max_length);
71
#endif
72

73 74 75
/* Exported function to send one line to readline's init file parser */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
76
parse_and_bind(PyObject *self, PyObject *args)
77
{
78 79 80 81 82 83 84 85 86 87 88 89
    char *s, *copy;
    if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
        return NULL;
    /* Make a copy -- rl_parse_and_bind() modifies its argument */
    /* Bernard Herzog */
    copy = malloc(1 + strlen(s));
    if (copy == NULL)
        return PyErr_NoMemory();
    strcpy(copy, s);
    rl_parse_and_bind(copy);
    free(copy); /* Free the copy */
    Py_RETURN_NONE;
90 91
}

92 93 94
PyDoc_STRVAR(doc_parse_and_bind,
"parse_and_bind(string) -> None\n\
Parse and execute single line of a readline init file.");
95 96 97 98 99


/* Exported function to parse a readline init file */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
100
read_init_file(PyObject *self, PyObject *args)
101
{
102 103
    PyObject *filename_obj = Py_None, *filename_bytes;
    if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj))
104
        return NULL;
105 106 107 108 109 110 111
    if (filename_obj != Py_None) {
        if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
            return NULL;
        errno = rl_read_init_file(PyBytes_AsString(filename_bytes));
        Py_DECREF(filename_bytes);
    } else
        errno = rl_read_init_file(NULL);
112 113 114
    if (errno)
        return PyErr_SetFromErrno(PyExc_IOError);
    Py_RETURN_NONE;
115 116
}

117 118
PyDoc_STRVAR(doc_read_init_file,
"read_init_file([filename]) -> None\n\
119
Parse a readline initialization file.\n\
120
The default filename is the last filename used.");
121 122


123 124 125
/* Exported function to load a readline history file */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
126
read_history_file(PyObject *self, PyObject *args)
127
{
128 129
    PyObject *filename_obj = Py_None, *filename_bytes;
    if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj))
130
        return NULL;
131 132 133 134 135 136 137
    if (filename_obj != Py_None) {
        if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
            return NULL;
        errno = read_history(PyBytes_AsString(filename_bytes));
        Py_DECREF(filename_bytes);
    } else
        errno = read_history(NULL);
138 139 140
    if (errno)
        return PyErr_SetFromErrno(PyExc_IOError);
    Py_RETURN_NONE;
141 142
}

143
static int _history_length = -1; /* do not truncate history by default */
144 145
PyDoc_STRVAR(doc_read_history_file,
"read_history_file([filename]) -> None\n\
146
Load a readline history file.\n\
147
The default filename is ~/.history.");
148 149 150 151 152


/* Exported function to save a readline history file */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
153
write_history_file(PyObject *self, PyObject *args)
154
{
155 156 157
    PyObject *filename_obj = Py_None, *filename_bytes;
    char *filename;
    if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj))
158
        return NULL;
159 160 161 162 163 164 165 166 167
    if (filename_obj != Py_None) {
        if (!PyUnicode_FSConverter(filename_obj, &filename_bytes))
            return NULL;
        filename = PyBytes_AsString(filename_bytes);
    } else {
        filename_bytes = NULL;
        filename = NULL;
    }
    errno = write_history(filename);
168
    if (!errno && _history_length >= 0)
169 170
        history_truncate_file(filename, _history_length);
    Py_XDECREF(filename_bytes);
171 172 173
    if (errno)
        return PyErr_SetFromErrno(PyExc_IOError);
    Py_RETURN_NONE;
174 175
}

176 177
PyDoc_STRVAR(doc_write_history_file,
"write_history_file([filename]) -> None\n\
178
Save a readline history file.\n\
179
The default filename is ~/.history.");
180 181


Guido van Rossum's avatar
Guido van Rossum committed
182
/* Set history length */
183 184 185 186

static PyObject*
set_history_length(PyObject *self, PyObject *args)
{
187 188 189 190 191
    int length = _history_length;
    if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
        return NULL;
    _history_length = length;
    Py_RETURN_NONE;
192 193
}

Guido van Rossum's avatar
Guido van Rossum committed
194 195 196 197 198
PyDoc_STRVAR(set_history_length_doc,
"set_history_length(length) -> None\n\
set the maximal number of items which will be written to\n\
the history file. A negative length is used to inhibit\n\
history truncation.");
199 200


Guido van Rossum's avatar
Guido van Rossum committed
201
/* Get history length */
202 203

static PyObject*
204
get_history_length(PyObject *self, PyObject *noarg)
205
{
206
    return PyLong_FromLong(_history_length);
207 208
}

Guido van Rossum's avatar
Guido van Rossum committed
209 210 211 212 213 214
PyDoc_STRVAR(get_history_length_doc,
"get_history_length() -> int\n\
return the maximum number of items that will be written to\n\
the history file.");


215
/* Generic hook function setter */
216

217
static PyObject *
Michael W. Hudson's avatar
Michael W. Hudson committed
218
set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
219
{
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    PyObject *function = Py_None;
    char buf[80];
    PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
    if (!PyArg_ParseTuple(args, buf, &function))
        return NULL;
    if (function == Py_None) {
        Py_XDECREF(*hook_var);
        *hook_var = NULL;
    }
    else if (PyCallable_Check(function)) {
        PyObject *tmp = *hook_var;
        Py_INCREF(function);
        *hook_var = function;
        Py_XDECREF(tmp);
    }
    else {
        PyOS_snprintf(buf, sizeof(buf),
                      "set_%.50s(func): argument not callable",
                      funcname);
        PyErr_SetString(PyExc_TypeError, buf);
        return NULL;
    }
    Py_RETURN_NONE;
243 244
}

Guido van Rossum's avatar
Guido van Rossum committed
245

246 247
/* Exported functions to specify hook functions in Python */

248
static PyObject *completion_display_matches_hook = NULL;
249 250 251 252 253 254
static PyObject *startup_hook = NULL;

#ifdef HAVE_RL_PRE_INPUT_HOOK
static PyObject *pre_input_hook = NULL;
#endif

255 256 257
static PyObject *
set_completion_display_matches_hook(PyObject *self, PyObject *args)
{
258 259
    PyObject *result = set_hook("completion_display_matches_hook",
                    &completion_display_matches_hook, args);
260
#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
261 262 263 264
    /* We cannot set this hook globally, since it replaces the
       default completion display. */
    rl_completion_display_matches_hook =
        completion_display_matches_hook ?
265
#if defined(_RL_FUNCTION_TYPEDEF)
266
        (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
267
#else
268
        (VFunction *)on_completion_display_matches_hook : 0;
269
#endif
270
#endif
271
    return result;
272

273 274 275 276 277 278 279 280 281
}

PyDoc_STRVAR(doc_set_completion_display_matches_hook,
"set_completion_display_matches_hook([function]) -> None\n\
Set or remove the completion display function.\n\
The function is called as\n\
  function(substitution, [matches], longest_match_length)\n\
once each time matches need to be displayed.");

282 283 284
static PyObject *
set_startup_hook(PyObject *self, PyObject *args)
{
285
    return set_hook("startup_hook", &startup_hook, args);
286 287
}

288 289
PyDoc_STRVAR(doc_set_startup_hook,
"set_startup_hook([function]) -> None\n\
290 291
Set or remove the startup_hook function.\n\
The function is called with no arguments just\n\
292
before readline prints the first prompt.");
293

Guido van Rossum's avatar
Guido van Rossum committed
294

295
#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum's avatar
Guido van Rossum committed
296 297 298

/* Set pre-input hook */

299 300 301
static PyObject *
set_pre_input_hook(PyObject *self, PyObject *args)
{
302
    return set_hook("pre_input_hook", &pre_input_hook, args);
303 304
}

305 306
PyDoc_STRVAR(doc_set_pre_input_hook,
"set_pre_input_hook([function]) -> None\n\
307 308 309
Set or remove the pre_input_hook function.\n\
The function is called with no arguments after the first prompt\n\
has been printed and just before readline starts reading input\n\
310
characters.");
Guido van Rossum's avatar
Guido van Rossum committed
311

312
#endif
313

Guido van Rossum's avatar
Guido van Rossum committed
314

315 316 317 318
/* Exported function to specify a word completer in Python */

static PyObject *completer = NULL;

319 320 321
static PyObject *begidx = NULL;
static PyObject *endidx = NULL;

Guido van Rossum's avatar
Guido van Rossum committed
322

323 324 325 326
/* Get the completion type for the scope of the tab-completion */
static PyObject *
get_completion_type(PyObject *self, PyObject *noarg)
{
327
  return PyLong_FromLong(rl_completion_type);
328 329 330 331 332 333 334
}

PyDoc_STRVAR(doc_get_completion_type,
"get_completion_type() -> int\n\
Get the type of completion being attempted.");


Guido van Rossum's avatar
Guido van Rossum committed
335 336
/* Get the beginning index for the scope of the tab-completion */

337
static PyObject *
338
get_begidx(PyObject *self, PyObject *noarg)
339
{
340 341
    Py_INCREF(begidx);
    return begidx;
342 343
}

344 345 346
PyDoc_STRVAR(doc_get_begidx,
"get_begidx() -> int\n\
get the beginning index of the readline tab-completion scope");
347

Guido van Rossum's avatar
Guido van Rossum committed
348 349 350

/* Get the ending index for the scope of the tab-completion */

351
static PyObject *
352
get_endidx(PyObject *self, PyObject *noarg)
353
{
354 355
    Py_INCREF(endidx);
    return endidx;
356 357
}

358 359 360
PyDoc_STRVAR(doc_get_endidx,
"get_endidx() -> int\n\
get the ending index of the readline tab-completion scope");
361 362


Guido van Rossum's avatar
Guido van Rossum committed
363
/* Set the tab-completion word-delimiters that readline uses */
364 365

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
366
set_completer_delims(PyObject *self, PyObject *args)
367
{
368 369 370 371 372 373 374 375
    char *break_chars;

    if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
        return NULL;
    }
    free((void*)rl_completer_word_break_characters);
    rl_completer_word_break_characters = strdup(break_chars);
    Py_RETURN_NONE;
376 377
}

378 379 380
PyDoc_STRVAR(doc_set_completer_delims,
"set_completer_delims(string) -> None\n\
set the readline word delimiters for tab-completion");
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/* _py_free_history_entry: Utility function to free a history entry. */

#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500

/* Readline version >= 5.0 introduced a timestamp field into the history entry
   structure; this needs to be freed to avoid a memory leak.  This version of
   readline also introduced the handy 'free_history_entry' function, which
   takes care of the timestamp. */

static void
_py_free_history_entry(HIST_ENTRY *entry)
{
    histdata_t data = free_history_entry(entry);
    free(data);
}

#else

/* No free_history_entry function;  free everything manually. */

static void
_py_free_history_entry(HIST_ENTRY *entry)
{
    if (entry->line)
        free((void *)entry->line);
    if (entry->data)
        free(entry->data);
    free(entry);
}

#endif

414 415 416
static PyObject *
py_remove_history(PyObject *self, PyObject *args)
{
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    int entry_number;
    HIST_ENTRY *entry;

    if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
        return NULL;
    if (entry_number < 0) {
        PyErr_SetString(PyExc_ValueError,
                        "History index cannot be negative");
        return NULL;
    }
    entry = remove_history(entry_number);
    if (!entry) {
        PyErr_Format(PyExc_ValueError,
                     "No history item at position %d",
                      entry_number);
        return NULL;
    }
    /* free memory allocated for the history entry */
435
    _py_free_history_entry(entry);
436
    Py_RETURN_NONE;
437 438 439
}

PyDoc_STRVAR(doc_remove_history,
440
"remove_history_item(pos) -> None\n\
441 442 443 444 445
remove history item given by its position");

static PyObject *
py_replace_history(PyObject *self, PyObject *args)
{
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    int entry_number;
    char *line;
    HIST_ENTRY *old_entry;

    if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
                          &line)) {
        return NULL;
    }
    if (entry_number < 0) {
        PyErr_SetString(PyExc_ValueError,
                        "History index cannot be negative");
        return NULL;
    }
    old_entry = replace_history_entry(entry_number, line, (void *)NULL);
    if (!old_entry) {
        PyErr_Format(PyExc_ValueError,
                     "No history item at position %d",
                     entry_number);
        return NULL;
    }
    /* free memory allocated for the old history entry */
467
    _py_free_history_entry(old_entry);
468
    Py_RETURN_NONE;
469 470 471
}

PyDoc_STRVAR(doc_replace_history,
472
"replace_history_item(pos, line) -> None\n\
473
replaces history item given by its position with contents of line");
Guido van Rossum's avatar
Guido van Rossum committed
474 475 476

/* Add a line to the history buffer */

477 478 479
static PyObject *
py_add_history(PyObject *self, PyObject *args)
{
480
    char *line;
481

482 483 484 485 486
    if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
        return NULL;
    }
    add_history(line);
    Py_RETURN_NONE;
487 488
}

489 490 491
PyDoc_STRVAR(doc_add_history,
"add_history(string) -> None\n\
add a line to the history buffer");
492

493

Guido van Rossum's avatar
Guido van Rossum committed
494
/* Get the tab-completion word-delimiters that readline uses */
495 496

static PyObject *
497
get_completer_delims(PyObject *self, PyObject *noarg)
498
{
499
    return PyUnicode_FromString(rl_completer_word_break_characters);
500
}
Guido van Rossum's avatar
Guido van Rossum committed
501

502 503 504
PyDoc_STRVAR(doc_get_completer_delims,
"get_completer_delims() -> string\n\
get the readline word delimiters for tab-completion");
505

Guido van Rossum's avatar
Guido van Rossum committed
506 507 508

/* Set the completer function */

509
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
510
set_completer(PyObject *self, PyObject *args)
511
{
512
    return set_hook("completer", &completer, args);
513 514
}

515 516
PyDoc_STRVAR(doc_set_completer,
"set_completer([function]) -> None\n\
517 518
Set or remove the completer function.\n\
The function is called as function(text, state),\n\
519
for state in 0, 1, 2, ..., until it returns a non-string.\n\
520
It should return the next possible completion starting with 'text'.");
521

Guido van Rossum's avatar
Guido van Rossum committed
522

523
static PyObject *
524
get_completer(PyObject *self, PyObject *noargs)
525
{
526 527 528 529 530
    if (completer == NULL) {
        Py_RETURN_NONE;
    }
    Py_INCREF(completer);
    return completer;
531 532 533 534 535 536 537
}

PyDoc_STRVAR(doc_get_completer,
"get_completer() -> function\n\
\n\
Returns current completer function.");

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
/* Private function to get current length of history.  XXX It may be
 * possible to replace this with a direct use of history_length instead,
 * but it's not clear whether BSD's libedit keeps history_length up to date.
 * See issue #8065.*/

static int
_py_get_history_length(void)
{
    HISTORY_STATE *hist_st = history_get_history_state();
    int length = hist_st->length;
    /* the history docs don't say so, but the address of hist_st changes each
       time history_get_history_state is called which makes me think it's
       freshly malloc'd memory...  on the other hand, the address of the last
       line stays the same as long as history isn't extended, so it appears to
       be malloc'd but managed by the history package... */
    free(hist_st);
    return length;
}

557 558 559 560 561
/* Exported function to get any element of history */

static PyObject *
get_history_item(PyObject *self, PyObject *args)
{
562 563
    int idx = 0;
    HIST_ENTRY *hist_ent;
564

565 566
    if (!PyArg_ParseTuple(args, "i:index", &idx))
        return NULL;
567
#ifdef  __APPLE__
568 569 570 571 572 573 574
    if (using_libedit_emulation) {
        /* Libedit emulation uses 0-based indexes,
         * the real one uses 1-based indexes,
         * adjust the index to ensure that Python
         * code doesn't have to worry about the
         * difference.
         */
575
        int length = _py_get_history_length();
576 577 578 579 580 581 582
        idx --;

        /*
         * Apple's readline emulation crashes when
         * the index is out of range, therefore
         * test for that and fail gracefully.
         */
583
        if (idx < 0 || idx >= length) {
584 585 586
            Py_RETURN_NONE;
        }
    }
587
#endif /* __APPLE__ */
588 589 590 591 592
    if ((hist_ent = history_get(idx)))
        return PyUnicode_FromString(hist_ent->line);
    else {
        Py_RETURN_NONE;
    }
593 594
}

595 596 597
PyDoc_STRVAR(doc_get_history_item,
"get_history_item() -> string\n\
return the current contents of history item at index.");
598

Guido van Rossum's avatar
Guido van Rossum committed
599

600 601 602
/* Exported function to get current length of history */

static PyObject *
603
get_current_history_length(PyObject *self, PyObject *noarg)
604
{
605
    return PyLong_FromLong((long)_py_get_history_length());
606 607
}

608 609 610
PyDoc_STRVAR(doc_get_current_history_length,
"get_current_history_length() -> integer\n\
return the current (not the maximum) length of history.");
611

Guido van Rossum's avatar
Guido van Rossum committed
612

613 614 615
/* Exported function to read the current line buffer */

static PyObject *
616
get_line_buffer(PyObject *self, PyObject *noarg)
617
{
618
    return PyUnicode_FromString(rl_line_buffer);
619 620
}

621 622 623
PyDoc_STRVAR(doc_get_line_buffer,
"get_line_buffer() -> string\n\
return the current contents of the line buffer.");
624

Guido van Rossum's avatar
Guido van Rossum committed
625

626 627 628 629 630 631 632
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER

/* Exported function to clear the current history */

static PyObject *
py_clear_history(PyObject *self, PyObject *noarg)
{
633 634
    clear_history();
    Py_RETURN_NONE;
635 636 637 638 639 640 641 642
}

PyDoc_STRVAR(doc_clear_history,
"clear_history() -> None\n\
Clear the current readline history.");
#endif


643 644 645
/* Exported function to insert text into the line buffer */

static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
646
insert_text(PyObject *self, PyObject *args)
647
{
648 649 650 651 652
    char *s;
    if (!PyArg_ParseTuple(args, "s:insert_text", &s))
        return NULL;
    rl_insert_text(s);
    Py_RETURN_NONE;
653 654
}

655 656 657
PyDoc_STRVAR(doc_insert_text,
"insert_text(string) -> None\n\
Insert text into the command line.");
658

Guido van Rossum's avatar
Guido van Rossum committed
659 660 661

/* Redisplay the line buffer */

662
static PyObject *
663
redisplay(PyObject *self, PyObject *noarg)
664
{
665 666
    rl_redisplay();
    Py_RETURN_NONE;
667 668
}

669 670
PyDoc_STRVAR(doc_redisplay,
"redisplay() -> None\n\
671
Change what's displayed on the screen to reflect the current\n\
672
contents of the line buffer.");
673

Guido van Rossum's avatar
Guido van Rossum committed
674

675
/* Table of functions exported by the module */
676 677

static struct PyMethodDef readline_methods[] =
678
{
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
    {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
    {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
    {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
    {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
    {"read_history_file", read_history_file,
     METH_VARARGS, doc_read_history_file},
    {"write_history_file", write_history_file,
     METH_VARARGS, doc_write_history_file},
    {"get_history_item", get_history_item,
     METH_VARARGS, doc_get_history_item},
    {"get_current_history_length", (PyCFunction)get_current_history_length,
     METH_NOARGS, doc_get_current_history_length},
    {"set_history_length", set_history_length,
     METH_VARARGS, set_history_length_doc},
    {"get_history_length", get_history_length,
     METH_NOARGS, get_history_length_doc},
    {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
    {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
    {"get_completion_type", get_completion_type,
     METH_NOARGS, doc_get_completion_type},
    {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
    {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},

    {"set_completer_delims", set_completer_delims,
     METH_VARARGS, doc_set_completer_delims},
    {"add_history", py_add_history, METH_VARARGS, doc_add_history},
    {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
    {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
    {"get_completer_delims", get_completer_delims,
     METH_NOARGS, doc_get_completer_delims},

    {"set_completion_display_matches_hook", set_completion_display_matches_hook,
     METH_VARARGS, doc_set_completion_display_matches_hook},
    {"set_startup_hook", set_startup_hook,
     METH_VARARGS, doc_set_startup_hook},
715
#ifdef HAVE_RL_PRE_INPUT_HOOK
716 717
    {"set_pre_input_hook", set_pre_input_hook,
     METH_VARARGS, doc_set_pre_input_hook},
718 719
#endif
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
720
    {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
721
#endif
722
    {0, 0}
723 724
};

725

726 727 728
/* C function to call the Python hooks. */

static int
Michael W. Hudson's avatar
Michael W. Hudson committed
729
on_hook(PyObject *func)
730
{
731 732 733
    int result = 0;
    if (func != NULL) {
        PyObject *r;
734
#ifdef WITH_THREAD
735
        PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudson's avatar
Michael W. Hudson committed
736
#endif
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
        r = PyObject_CallFunction(func, NULL);
        if (r == NULL)
            goto error;
        if (r == Py_None)
            result = 0;
        else {
            result = PyLong_AsLong(r);
            if (result == -1 && PyErr_Occurred())
                goto error;
        }
        Py_DECREF(r);
        goto done;
      error:
        PyErr_Clear();
        Py_XDECREF(r);
      done:
753
#ifdef WITH_THREAD
754
        PyGILState_Release(gilstate);
Michael W. Hudson's avatar
Michael W. Hudson committed
755
#endif
756 757 758
        return result;
    }
    return result;
759 760 761 762 763
}

static int
on_startup_hook(void)
{
764
    return on_hook(startup_hook);
765 766 767 768 769 770
}

#ifdef HAVE_RL_PRE_INPUT_HOOK
static int
on_pre_input_hook(void)
{
771
    return on_hook(pre_input_hook);
772 773 774
}
#endif

775

776 777
/* C function to call the Python completion_display_matches */

778
#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
779 780
static void
on_completion_display_matches_hook(char **matches,
781
                                   int num_matches, int max_length)
782
{
783 784
    int i;
    PyObject *m=NULL, *s=NULL, *r=NULL;
785
#ifdef WITH_THREAD
786
    PyGILState_STATE gilstate = PyGILState_Ensure();
787
#endif
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
    m = PyList_New(num_matches);
    if (m == NULL)
        goto error;
    for (i = 0; i < num_matches; i++) {
        s = PyUnicode_FromString(matches[i+1]);
        if (s == NULL)
            goto error;
        if (PyList_SetItem(m, i, s) == -1)
            goto error;
    }
    r = PyObject_CallFunction(completion_display_matches_hook,
                              "sOi", matches[0], m, max_length);

    Py_DECREF(m); m=NULL;

    if (r == NULL ||
        (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
        goto error;
    }
    Py_XDECREF(r); r=NULL;

    if (0) {
    error:
        PyErr_Clear();
        Py_XDECREF(m);
        Py_XDECREF(r);
    }
815
#ifdef WITH_THREAD
816
    PyGILState_Release(gilstate);
817 818 819 820
#endif
}


821 822 823
/* C function to call the Python completer. */

static char *
824
on_completion(const char *text, int state)
825
{
826 827 828
    char *result = NULL;
    if (completer != NULL) {
        PyObject *r;
829
#ifdef WITH_THREAD
830
        PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudson's avatar
Michael W. Hudson committed
831
#endif
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
        rl_attempted_completion_over = 1;
        r = PyObject_CallFunction(completer, "si", text, state);
        if (r == NULL)
            goto error;
        if (r == Py_None) {
            result = NULL;
        }
        else {
            char *s = _PyUnicode_AsString(r);
            if (s == NULL)
                goto error;
            result = strdup(s);
        }
        Py_DECREF(r);
        goto done;
      error:
        PyErr_Clear();
        Py_XDECREF(r);
      done:
851
#ifdef WITH_THREAD
852
        PyGILState_Release(gilstate);
Michael W. Hudson's avatar
Michael W. Hudson committed
853
#endif
854 855 856
        return result;
    }
    return result;
857
}
858
#endif
859 860


861
/* A more flexible constructor that saves the "begidx" and "endidx"
862 863
 * before calling the normal completer */

864
static char **
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
865
flex_complete(char *text, int start, int end)
866
{
867
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
868
    rl_completion_append_character ='\0';
869 870
#endif
#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
871
    rl_completion_suppress_append = 0;
872
#endif
873 874 875 876 877
    Py_XDECREF(begidx);
    Py_XDECREF(endidx);
    begidx = PyLong_FromLong((long) start);
    endidx = PyLong_FromLong((long) end);
    return completion_matches(text, *on_completion);
878 879
}

880

881
/* Helper to initialize GNU readline properly. */
882

883
static void
884
setup_readline(void)
885
{
886
#ifdef SAVE_LOCALE
887 888 889
    char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
    if (!saved_locale)
        Py_FatalError("not enough memory to save locale");
890 891
#endif

892
    using_history();
893

894
    rl_readline_name = "python";
895
#if defined(PYOS_OS2) && defined(PYCC_GCC)
896 897
    /* Allow $if term= in .inputrc to work */
    rl_terminal_name = getenv("TERM");
898
#endif
899 900 901 902 903 904 905
    /* Force rebind of TAB to insert-tab */
    rl_bind_key('\t', rl_insert);
    /* Bind both ESC-TAB and ESC-ESC to the completion function */
    rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
    rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
    /* Set our hook functions */
    rl_startup_hook = (Function *)on_startup_hook;
906
#ifdef HAVE_RL_PRE_INPUT_HOOK
907
    rl_pre_input_hook = (Function *)on_pre_input_hook;
908
#endif
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
    /* Set our completion function */
    rl_attempted_completion_function = (CPPFunction *)flex_complete;
    /* Set Python word break characters */
    rl_completer_word_break_characters =
        strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
        /* All nonalphanums except '.' */

    begidx = PyLong_FromLong(0L);
    endidx = PyLong_FromLong(0L);
    /* Initialize (allows .inputrc to override)
     *
     * XXX: A bug in the readline-2.2 library causes a memory leak
     * inside this function.  Nothing we can do about it.
     */
    rl_initialize();

    RESTORE_LOCALE(saved_locale)
926 927
}

Michael W. Hudson's avatar
Michael W. Hudson committed
928 929 930 931 932
/* Wrapper around GNU readline that handles signals differently. */


#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)

933
static  char *completed_input_string;
Michael W. Hudson's avatar
Michael W. Hudson committed
934 935 936
static void
rlhandler(char *text)
{
937 938
    completed_input_string = text;
    rl_callback_handler_remove();
Michael W. Hudson's avatar
Michael W. Hudson committed
939 940 941 942 943 944 945
}

extern PyThreadState* _PyOS_ReadlineTState;

static char *
readline_until_enter_or_signal(char *prompt, int *signal)
{
946 947
    char * not_done_reading = "";
    fd_set selectset;
Michael W. Hudson's avatar
Michael W. Hudson committed
948

949
    *signal = 0;
Michael W. Hudson's avatar
Michael W. Hudson committed
950
#ifdef HAVE_RL_CATCH_SIGNAL
951
    rl_catch_signals = 0;
Michael W. Hudson's avatar
Michael W. Hudson committed
952 953
#endif

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
    rl_callback_handler_install (prompt, rlhandler);
    FD_ZERO(&selectset);

    completed_input_string = not_done_reading;

    while (completed_input_string == not_done_reading) {
        int has_input = 0;

        while (!has_input)
        {               struct timeval timeout = {0, 100000}; /* 0.1 seconds */

            /* [Bug #1552726] Only limit the pause if an input hook has been
               defined.  */
            struct timeval *timeoutp = NULL;
            if (PyOS_InputHook)
                timeoutp = &timeout;
            FD_SET(fileno(rl_instream), &selectset);
            /* select resets selectset if no input was available */
            has_input = select(fileno(rl_instream) + 1, &selectset,
                               NULL, NULL, timeoutp);
            if(PyOS_InputHook) PyOS_InputHook();
        }

        if(has_input > 0) {
            rl_callback_read_char();
        }
        else if (errno == EINTR) {
            int s;
982
#ifdef WITH_THREAD
983
            PyEval_RestoreThread(_PyOS_ReadlineTState);
984
#endif
985
            s = PyErr_CheckSignals();
986
#ifdef WITH_THREAD
987
            PyEval_SaveThread();
988
#endif
989 990 991 992 993 994 995 996 997 998 999
            if (s < 0) {
                rl_free_line_state();
                rl_cleanup_after_signal();
                rl_callback_handler_remove();
                *signal = 1;
                completed_input_string = NULL;
            }
        }
    }

    return completed_input_string;
Michael W. Hudson's avatar
Michael W. Hudson committed
1000 1001 1002 1003
}


#else
1004 1005 1006 1007 1008

/* Interrupt handler */

static jmp_buf jbuf;

1009
/* ARGSUSED */
1010
static void
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
1011
onintr(int sig)
1012
{
1013
    longjmp(jbuf, 1);
1014 1015
}

1016

1017
static char *
Michael W. Hudson's avatar
Michael W. Hudson committed
1018
readline_until_enter_or_signal(char *prompt, int *signal)
1019
{
1020 1021 1022 1023
    PyOS_sighandler_t old_inthandler;
    char *p;

    *signal = 0;
Guido van Rossum's avatar
Guido van Rossum committed
1024

1025 1026
    old_inthandler = PyOS_setsig(SIGINT, onintr);
    if (setjmp(jbuf)) {
1027
#ifdef HAVE_SIGRELSE
1028 1029
        /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
        sigrelse(SIGINT);
1030
#endif
1031 1032 1033 1034 1035 1036 1037
        PyOS_setsig(SIGINT, old_inthandler);
        *signal = 1;
        return NULL;
    }
    rl_event_hook = PyOS_InputHook;
    p = readline(prompt);
    PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson's avatar
Michael W. Hudson committed
1038 1039 1040 1041 1042 1043 1044 1045 1046

    return p;
}
#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */


static char *
call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
{
1047 1048 1049
    size_t n;
    char *p, *q;
    int signal;
1050

1051
#ifdef SAVE_LOCALE
1052 1053 1054 1055
    char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
    if (!saved_locale)
        Py_FatalError("not enough memory to save locale");
    setlocale(LC_CTYPE, "");
1056
#endif
Michael W. Hudson's avatar
Michael W. Hudson committed
1057

1058 1059 1060
    if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
        rl_instream = sys_stdin;
        rl_outstream = sys_stdout;
1061
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
1062
        rl_prep_terminal (1);
1063
#endif
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    }

    p = readline_until_enter_or_signal(prompt, &signal);

    /* we got an interrupt signal */
    if (signal) {
        RESTORE_LOCALE(saved_locale)
        return NULL;
    }

    /* We got an EOF, return a empty string. */
    if (p == NULL) {
        p = PyMem_Malloc(1);
        if (p != NULL)
            *p = '\0';
        RESTORE_LOCALE(saved_locale)
        return p;
    }

    /* we have a valid line */
    n = strlen(p);
    if (n > 0) {
1086
        const char *line;
1087 1088
        int length = _py_get_history_length();
        if (length > 0)
1089
#ifdef __APPLE__
1090 1091 1092 1093 1094
            if (using_libedit_emulation) {
                /*
                 * Libedit's emulation uses 0-based indexes,
                 * the real readline uses 1-based indexes.
                 */
1095
                line = (const char *)history_get(length - 1)->line;
1096
            } else
1097
#endif /* __APPLE__ */
1098
            line = (const char *)history_get(length)->line;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
        else
            line = "";
        if (strcmp(p, line))
            add_history(p);
    }
    /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
       release the original. */
    q = p;
    p = PyMem_Malloc(n+2);
    if (p != NULL) {
        strncpy(p, q, n);
        p[n] = '\n';
        p[n+1] = '\0';
    }
    free(q);
    RESTORE_LOCALE(saved_locale)
    return p;
1116 1117
}

1118 1119 1120

/* Initialize the module */

1121 1122
PyDoc_STRVAR(doc_module,
"Importing this module enables command line editing using GNU readline.");
1123

1124 1125 1126 1127
#ifdef __APPLE__
PyDoc_STRVAR(doc_module_le,
"Importing this module enables command line editing using libedit readline.");
#endif /* __APPLE__ */
1128 1129

static struct PyModuleDef readlinemodule = {
1130 1131 1132 1133 1134 1135 1136 1137 1138
    PyModuleDef_HEAD_INIT,
    "readline",
    doc_module,
    -1,
    readline_methods,
    NULL,
    NULL,
    NULL,
    NULL
1139 1140
};

1141

1142
PyMODINIT_FUNC
1143
PyInit_readline(void)
1144
{
1145
    PyObject *m;
1146

1147
#ifdef __APPLE__
1148 1149 1150
    if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
        using_libedit_emulation = 1;
    }
1151

1152 1153
    if (using_libedit_emulation)
        readlinemodule.m_doc = doc_module_le;
1154 1155 1156

#endif /* __APPLE__ */

1157
    m = PyModule_Create(&readlinemodule);
1158

1159 1160
    if (m == NULL)
        return NULL;
1161

1162 1163


1164 1165 1166
    PyOS_ReadlineFunctionPointer = call_readline;
    setup_readline();
    return m;
1167
}