1 /* This module makes GNU readline available to Python. It has ideas 2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory 3 * Center. The completer interface was inspired by Lele Gaifax. More 4 * recently, it was largely rewritten by Guido van Rossum. 5 */ 6 7 /* Standard definitions */ 8 #include "Python.h" 9 #include <stddef.h> 10 #include <signal.h> 11 #include <errno.h> 12 #include <sys/time.h> 13 14 #if defined(HAVE_SETLOCALE) 15 /* GNU readline() mistakenly sets the LC_CTYPE locale. 16 * This is evil. Only the user or the app's main() should do this! 17 * We must save and restore the locale around the rl_initialize() call. 18 */ 19 #define SAVE_LOCALE 20 #include <locale.h> 21 #endif 22 23 #ifdef SAVE_LOCALE 24 # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); } 25 #else 26 # define RESTORE_LOCALE(sl) 27 #endif 28 29 /* GNU readline definitions */ 30 #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ 31 #include <readline/readline.h> 32 #include <readline/history.h> 33 34 #ifdef HAVE_RL_COMPLETION_MATCHES 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 extern char **completion_matches(char *, CPFunction *); 44 #endif 45 #endif 46 #endif 47 48 #ifdef __APPLE__ 49 /* 50 * It is possible to link the readline module to the readline 51 * emulation library of editline/libedit. 52 * 53 * On OSX this emulation library is not 100% API compatible 54 * with the "real" readline and cannot be detected at compile-time, 55 * hence we use a runtime check to detect if we're using libedit 56 * 57 * Currently there is one known API incompatibility: 58 * - 'get_history' has a 1-based index with GNU readline, and a 0-based 59 * index with older versions of libedit's emulation. 60 * - Note that replace_history and remove_history use a 0-based index 61 * with both implementations. 62 */ 63 static int using_libedit_emulation = 0; 64 static const char libedit_version_tag[] = "EditLine wrapper"; 65 66 static int libedit_history_start = 0; 67 #endif /* __APPLE__ */ 68 69 #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK 70 static void 71 on_completion_display_matches_hook(char **matches, 72 int num_matches, int max_length); 73 #endif 74 75 /* Memory allocated for rl_completer_word_break_characters 76 (see issue #17289 for the motivation). */ 77 static char *completer_word_break_characters; 78 79 typedef struct { 80 /* Specify hook functions in Python */ 81 PyObject *completion_display_matches_hook; 82 PyObject *startup_hook; 83 PyObject *pre_input_hook; 84 85 PyObject *completer; /* Specify a word completer in Python */ 86 PyObject *begidx; 87 PyObject *endidx; 88 } readlinestate; 89 90 91 #define readline_state(o) ((readlinestate *)PyModule_GetState(o)) 92 93 static int 94 readline_clear(PyObject *m) 95 { 96 readlinestate *state = readline_state(m); 97 Py_CLEAR(state->completion_display_matches_hook); 98 Py_CLEAR(state->startup_hook); 99 Py_CLEAR(state->pre_input_hook); 100 Py_CLEAR(state->completer); 101 Py_CLEAR(state->begidx); 102 Py_CLEAR(state->endidx); 103 return 0; 104 } 105 106 static int 107 readline_traverse(PyObject *m, visitproc visit, void *arg) 108 { 109 readlinestate *state = readline_state(m); 110 Py_VISIT(state->completion_display_matches_hook); 111 Py_VISIT(state->startup_hook); 112 Py_VISIT(state->pre_input_hook); 113 Py_VISIT(state->completer); 114 Py_VISIT(state->begidx); 115 Py_VISIT(state->endidx); 116 return 0; 117 } 118 119 static void 120 readline_free(void *m) 121 { 122 readline_clear((PyObject *)m); 123 } 124 125 static PyModuleDef readlinemodule; 126 127 #define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule))) 128 129 130 /* Convert to/from multibyte C strings */ 131 132 static PyObject * 133 encode(PyObject *b) 134 { 135 return PyUnicode_EncodeLocale(b, "surrogateescape"); 136 } 137 138 static PyObject * 139 decode(const char *s) 140 { 141 return PyUnicode_DecodeLocale(s, "surrogateescape"); 142 } 143 144 145 /* Exported function to send one line to readline's init file parser */ 146 147 static PyObject * 148 parse_and_bind(PyObject *self, PyObject *string) 149 { 150 char *copy; 151 PyObject *encoded = encode(string); 152 if (encoded == NULL) { 153 return NULL; 154 } 155 /* Make a copy -- rl_parse_and_bind() modifies its argument */ 156 /* Bernard Herzog */ 157 copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded)); 158 if (copy == NULL) { 159 Py_DECREF(encoded); 160 return PyErr_NoMemory(); 161 } 162 strcpy(copy, PyBytes_AS_STRING(encoded)); 163 Py_DECREF(encoded); 164 rl_parse_and_bind(copy); 165 PyMem_Free(copy); /* Free the copy */ 166 Py_RETURN_NONE; 167 } 168 169 PyDoc_STRVAR(doc_parse_and_bind, 170 "parse_and_bind(string) -> None\n\ 171 Execute the init line provided in the string argument."); 172 173 174 /* Exported function to parse a readline init file */ 175 176 static PyObject * 177 read_init_file(PyObject *self, PyObject *args) 178 { 179 PyObject *filename_obj = Py_None, *filename_bytes; 180 if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj)) 181 return NULL; 182 if (filename_obj != Py_None) { 183 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) 184 return NULL; 185 errno = rl_read_init_file(PyBytes_AsString(filename_bytes)); 186 Py_DECREF(filename_bytes); 187 } else 188 errno = rl_read_init_file(NULL); 189 if (errno) 190 return PyErr_SetFromErrno(PyExc_OSError); 191 Py_RETURN_NONE; 192 } 193 194 PyDoc_STRVAR(doc_read_init_file, 195 "read_init_file([filename]) -> None\n\ 196 Execute a readline initialization file.\n\ 197 The default filename is the last filename used."); 198 199 200 /* Exported function to load a readline history file */ 201 202 static PyObject * 203 read_history_file(PyObject *self, PyObject *args) 204 { 205 PyObject *filename_obj = Py_None, *filename_bytes; 206 if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj)) 207 return NULL; 208 if (filename_obj != Py_None) { 209 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) 210 return NULL; 211 errno = read_history(PyBytes_AsString(filename_bytes)); 212 Py_DECREF(filename_bytes); 213 } else 214 errno = read_history(NULL); 215 if (errno) 216 return PyErr_SetFromErrno(PyExc_OSError); 217 Py_RETURN_NONE; 218 } 219 220 static int _history_length = -1; /* do not truncate history by default */ 221 PyDoc_STRVAR(doc_read_history_file, 222 "read_history_file([filename]) -> None\n\ 223 Load a readline history file.\n\ 224 The default filename is ~/.history."); 225 226 227 /* Exported function to save a readline history file */ 228 229 static PyObject * 230 write_history_file(PyObject *self, PyObject *args) 231 { 232 PyObject *filename_obj = Py_None, *filename_bytes; 233 char *filename; 234 int err; 235 if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj)) 236 return NULL; 237 if (filename_obj != Py_None) { 238 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) 239 return NULL; 240 filename = PyBytes_AsString(filename_bytes); 241 } else { 242 filename_bytes = NULL; 243 filename = NULL; 244 } 245 errno = err = write_history(filename); 246 if (!err && _history_length >= 0) 247 history_truncate_file(filename, _history_length); 248 Py_XDECREF(filename_bytes); 249 errno = err; 250 if (errno) 251 return PyErr_SetFromErrno(PyExc_OSError); 252 Py_RETURN_NONE; 253 } 254 255 PyDoc_STRVAR(doc_write_history_file, 256 "write_history_file([filename]) -> None\n\ 257 Save a readline history file.\n\ 258 The default filename is ~/.history."); 259 260 261 #ifdef HAVE_RL_APPEND_HISTORY 262 /* Exported function to save part of a readline history file */ 263 264 static PyObject * 265 append_history_file(PyObject *self, PyObject *args) 266 { 267 int nelements; 268 PyObject *filename_obj = Py_None, *filename_bytes; 269 char *filename; 270 int err; 271 if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj)) 272 return NULL; 273 if (filename_obj != Py_None) { 274 if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) 275 return NULL; 276 filename = PyBytes_AsString(filename_bytes); 277 } else { 278 filename_bytes = NULL; 279 filename = NULL; 280 } 281 errno = err = append_history(nelements, filename); 282 if (!err && _history_length >= 0) 283 history_truncate_file(filename, _history_length); 284 Py_XDECREF(filename_bytes); 285 errno = err; 286 if (errno) 287 return PyErr_SetFromErrno(PyExc_OSError); 288 Py_RETURN_NONE; 289 } 290 291 PyDoc_STRVAR(doc_append_history_file, 292 "append_history_file(nelements[, filename]) -> None\n\ 293 Append the last nelements items of the history list to file.\n\ 294 The default filename is ~/.history."); 295 #endif 296 297 298 /* Set history length */ 299 300 static PyObject* 301 set_history_length(PyObject *self, PyObject *args) 302 { 303 int length = _history_length; 304 if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) 305 return NULL; 306 _history_length = length; 307 Py_RETURN_NONE; 308 } 309 310 PyDoc_STRVAR(set_history_length_doc, 311 "set_history_length(length) -> None\n\ 312 set the maximal number of lines which will be written to\n\ 313 the history file. A negative length is used to inhibit\n\ 314 history truncation."); 315 316 317 /* Get history length */ 318 319 static PyObject* 320 get_history_length(PyObject *self, PyObject *noarg) 321 { 322 return PyLong_FromLong(_history_length); 323 } 324 325 PyDoc_STRVAR(get_history_length_doc, 326 "get_history_length() -> int\n\ 327 return the maximum number of lines that will be written to\n\ 328 the history file."); 329 330 331 /* Generic hook function setter */ 332 333 static PyObject * 334 set_hook(const char *funcname, PyObject **hook_var, PyObject *args) 335 { 336 PyObject *function = Py_None; 337 char buf[80]; 338 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); 339 if (!PyArg_ParseTuple(args, buf, &function)) 340 return NULL; 341 if (function == Py_None) { 342 Py_CLEAR(*hook_var); 343 } 344 else if (PyCallable_Check(function)) { 345 Py_INCREF(function); 346 Py_XSETREF(*hook_var, function); 347 } 348 else { 349 PyErr_Format(PyExc_TypeError, 350 "set_%.50s(func): argument not callable", 351 funcname); 352 return NULL; 353 } 354 Py_RETURN_NONE; 355 } 356 357 358 static PyObject * 359 set_completion_display_matches_hook(PyObject *self, PyObject *args) 360 { 361 PyObject *result = set_hook("completion_display_matches_hook", 362 &readlinestate_global->completion_display_matches_hook, args); 363 #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK 364 /* We cannot set this hook globally, since it replaces the 365 default completion display. */ 366 rl_completion_display_matches_hook = 367 readlinestate_global->completion_display_matches_hook ? 368 #if defined(_RL_FUNCTION_TYPEDEF) 369 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0; 370 #else 371 (VFunction *)on_completion_display_matches_hook : 0; 372 #endif 373 #endif 374 return result; 375 376 } 377 378 PyDoc_STRVAR(doc_set_completion_display_matches_hook, 379 "set_completion_display_matches_hook([function]) -> None\n\ 380 Set or remove the completion display function.\n\ 381 The function is called as\n\ 382 function(substitution, [matches], longest_match_length)\n\ 383 once each time matches need to be displayed."); 384 385 static PyObject * 386 set_startup_hook(PyObject *self, PyObject *args) 387 { 388 return set_hook("startup_hook", &readlinestate_global->startup_hook, args); 389 } 390 391 PyDoc_STRVAR(doc_set_startup_hook, 392 "set_startup_hook([function]) -> None\n\ 393 Set or remove the function invoked by the rl_startup_hook callback.\n\ 394 The function is called with no arguments just\n\ 395 before readline prints the first prompt."); 396 397 398 #ifdef HAVE_RL_PRE_INPUT_HOOK 399 400 /* Set pre-input hook */ 401 402 static PyObject * 403 set_pre_input_hook(PyObject *self, PyObject *args) 404 { 405 return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args); 406 } 407 408 PyDoc_STRVAR(doc_set_pre_input_hook, 409 "set_pre_input_hook([function]) -> None\n\ 410 Set or remove the function invoked by the rl_pre_input_hook callback.\n\ 411 The function is called with no arguments after the first prompt\n\ 412 has been printed and just before readline starts reading input\n\ 413 characters."); 414 415 #endif 416 417 418 /* Get the completion type for the scope of the tab-completion */ 419 static PyObject * 420 get_completion_type(PyObject *self, PyObject *noarg) 421 { 422 return PyLong_FromLong(rl_completion_type); 423 } 424 425 PyDoc_STRVAR(doc_get_completion_type, 426 "get_completion_type() -> int\n\ 427 Get the type of completion being attempted."); 428 429 430 /* Get the beginning index for the scope of the tab-completion */ 431 432 static PyObject * 433 get_begidx(PyObject *self, PyObject *noarg) 434 { 435 Py_INCREF(readlinestate_global->begidx); 436 return readlinestate_global->begidx; 437 } 438 439 PyDoc_STRVAR(doc_get_begidx, 440 "get_begidx() -> int\n\ 441 get the beginning index of the completion scope"); 442 443 444 /* Get the ending index for the scope of the tab-completion */ 445 446 static PyObject * 447 get_endidx(PyObject *self, PyObject *noarg) 448 { 449 Py_INCREF(readlinestate_global->endidx); 450 return readlinestate_global->endidx; 451 } 452 453 PyDoc_STRVAR(doc_get_endidx, 454 "get_endidx() -> int\n\ 455 get the ending index of the completion scope"); 456 457 458 /* Set the tab-completion word-delimiters that readline uses */ 459 460 static PyObject * 461 set_completer_delims(PyObject *self, PyObject *string) 462 { 463 char *break_chars; 464 PyObject *encoded = encode(string); 465 if (encoded == NULL) { 466 return NULL; 467 } 468 /* Keep a reference to the allocated memory in the module state in case 469 some other module modifies rl_completer_word_break_characters 470 (see issue #17289). */ 471 break_chars = strdup(PyBytes_AS_STRING(encoded)); 472 Py_DECREF(encoded); 473 if (break_chars) { 474 free(completer_word_break_characters); 475 completer_word_break_characters = break_chars; 476 rl_completer_word_break_characters = break_chars; 477 Py_RETURN_NONE; 478 } 479 else 480 return PyErr_NoMemory(); 481 } 482 483 PyDoc_STRVAR(doc_set_completer_delims, 484 "set_completer_delims(string) -> None\n\ 485 set the word delimiters for completion"); 486 487 /* _py_free_history_entry: Utility function to free a history entry. */ 488 489 #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500 490 491 /* Readline version >= 5.0 introduced a timestamp field into the history entry 492 structure; this needs to be freed to avoid a memory leak. This version of 493 readline also introduced the handy 'free_history_entry' function, which 494 takes care of the timestamp. */ 495 496 static void 497 _py_free_history_entry(HIST_ENTRY *entry) 498 { 499 histdata_t data = free_history_entry(entry); 500 free(data); 501 } 502 503 #else 504 505 /* No free_history_entry function; free everything manually. */ 506 507 static void 508 _py_free_history_entry(HIST_ENTRY *entry) 509 { 510 if (entry->line) 511 free((void *)entry->line); 512 if (entry->data) 513 free(entry->data); 514 free(entry); 515 } 516 517 #endif 518 519 static PyObject * 520 py_remove_history(PyObject *self, PyObject *args) 521 { 522 int entry_number; 523 HIST_ENTRY *entry; 524 525 if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number)) 526 return NULL; 527 if (entry_number < 0) { 528 PyErr_SetString(PyExc_ValueError, 529 "History index cannot be negative"); 530 return NULL; 531 } 532 entry = remove_history(entry_number); 533 if (!entry) { 534 PyErr_Format(PyExc_ValueError, 535 "No history item at position %d", 536 entry_number); 537 return NULL; 538 } 539 /* free memory allocated for the history entry */ 540 _py_free_history_entry(entry); 541 Py_RETURN_NONE; 542 } 543 544 PyDoc_STRVAR(doc_remove_history, 545 "remove_history_item(pos) -> None\n\ 546 remove history item given by its position"); 547 548 static PyObject * 549 py_replace_history(PyObject *self, PyObject *args) 550 { 551 int entry_number; 552 PyObject *line; 553 PyObject *encoded; 554 HIST_ENTRY *old_entry; 555 556 if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number, 557 &line)) { 558 return NULL; 559 } 560 if (entry_number < 0) { 561 PyErr_SetString(PyExc_ValueError, 562 "History index cannot be negative"); 563 return NULL; 564 } 565 encoded = encode(line); 566 if (encoded == NULL) { 567 return NULL; 568 } 569 old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL); 570 Py_DECREF(encoded); 571 if (!old_entry) { 572 PyErr_Format(PyExc_ValueError, 573 "No history item at position %d", 574 entry_number); 575 return NULL; 576 } 577 /* free memory allocated for the old history entry */ 578 _py_free_history_entry(old_entry); 579 Py_RETURN_NONE; 580 } 581 582 PyDoc_STRVAR(doc_replace_history, 583 "replace_history_item(pos, line) -> None\n\ 584 replaces history item given by its position with contents of line"); 585 586 /* Add a line to the history buffer */ 587 588 static PyObject * 589 py_add_history(PyObject *self, PyObject *string) 590 { 591 PyObject *encoded = encode(string); 592 if (encoded == NULL) { 593 return NULL; 594 } 595 add_history(PyBytes_AS_STRING(encoded)); 596 Py_DECREF(encoded); 597 Py_RETURN_NONE; 598 } 599 600 PyDoc_STRVAR(doc_add_history, 601 "add_history(string) -> None\n\ 602 add an item to the history buffer"); 603 604 static int should_auto_add_history = 1; 605 606 /* Enable or disable automatic history */ 607 608 static PyObject * 609 py_set_auto_history(PyObject *self, PyObject *args) 610 { 611 if (!PyArg_ParseTuple(args, "p:set_auto_history", 612 &should_auto_add_history)) { 613 return NULL; 614 } 615 Py_RETURN_NONE; 616 } 617 618 PyDoc_STRVAR(doc_set_auto_history, 619 "set_auto_history(enabled) -> None\n\ 620 Enables or disables automatic history."); 621 622 623 /* Get the tab-completion word-delimiters that readline uses */ 624 625 static PyObject * 626 get_completer_delims(PyObject *self, PyObject *noarg) 627 { 628 return decode(rl_completer_word_break_characters); 629 } 630 631 PyDoc_STRVAR(doc_get_completer_delims, 632 "get_completer_delims() -> string\n\ 633 get the word delimiters for completion"); 634 635 636 /* Set the completer function */ 637 638 static PyObject * 639 set_completer(PyObject *self, PyObject *args) 640 { 641 return set_hook("completer", &readlinestate_global->completer, args); 642 } 643 644 PyDoc_STRVAR(doc_set_completer, 645 "set_completer([function]) -> None\n\ 646 Set or remove the completer function.\n\ 647 The function is called as function(text, state),\n\ 648 for state in 0, 1, 2, ..., until it returns a non-string.\n\ 649 It should return the next possible completion starting with 'text'."); 650 651 652 static PyObject * 653 get_completer(PyObject *self, PyObject *noargs) 654 { 655 if (readlinestate_global->completer == NULL) { 656 Py_RETURN_NONE; 657 } 658 Py_INCREF(readlinestate_global->completer); 659 return readlinestate_global->completer; 660 } 661 662 PyDoc_STRVAR(doc_get_completer, 663 "get_completer() -> function\n\ 664 \n\ 665 Returns current completer function."); 666 667 /* Private function to get current length of history. XXX It may be 668 * possible to replace this with a direct use of history_length instead, 669 * but it's not clear whether BSD's libedit keeps history_length up to date. 670 * See issue #8065.*/ 671 672 static int 673 _py_get_history_length(void) 674 { 675 HISTORY_STATE *hist_st = history_get_history_state(); 676 int length = hist_st->length; 677 /* the history docs don't say so, but the address of hist_st changes each 678 time history_get_history_state is called which makes me think it's 679 freshly malloc'd memory... on the other hand, the address of the last 680 line stays the same as long as history isn't extended, so it appears to 681 be malloc'd but managed by the history package... */ 682 free(hist_st); 683 return length; 684 } 685 686 /* Exported function to get any element of history */ 687 688 static PyObject * 689 get_history_item(PyObject *self, PyObject *args) 690 { 691 int idx = 0; 692 HIST_ENTRY *hist_ent; 693 694 if (!PyArg_ParseTuple(args, "i:get_history_item", &idx)) 695 return NULL; 696 #ifdef __APPLE__ 697 if (using_libedit_emulation) { 698 /* Older versions of libedit's readline emulation 699 * use 0-based indexes, while readline and newer 700 * versions of libedit use 1-based indexes. 701 */ 702 int length = _py_get_history_length(); 703 704 idx = idx - 1 + libedit_history_start; 705 706 /* 707 * Apple's readline emulation crashes when 708 * the index is out of range, therefore 709 * test for that and fail gracefully. 710 */ 711 if (idx < (0 + libedit_history_start) 712 || idx >= (length + libedit_history_start)) { 713 Py_RETURN_NONE; 714 } 715 } 716 #endif /* __APPLE__ */ 717 if ((hist_ent = history_get(idx))) 718 return decode(hist_ent->line); 719 else { 720 Py_RETURN_NONE; 721 } 722 } 723 724 PyDoc_STRVAR(doc_get_history_item, 725 "get_history_item() -> string\n\ 726 return the current contents of history item at index."); 727 728 729 /* Exported function to get current length of history */ 730 731 static PyObject * 732 get_current_history_length(PyObject *self, PyObject *noarg) 733 { 734 return PyLong_FromLong((long)_py_get_history_length()); 735 } 736 737 PyDoc_STRVAR(doc_get_current_history_length, 738 "get_current_history_length() -> integer\n\ 739 return the current (not the maximum) length of history."); 740 741 742 /* Exported function to read the current line buffer */ 743 744 static PyObject * 745 get_line_buffer(PyObject *self, PyObject *noarg) 746 { 747 return decode(rl_line_buffer); 748 } 749 750 PyDoc_STRVAR(doc_get_line_buffer, 751 "get_line_buffer() -> string\n\ 752 return the current contents of the line buffer."); 753 754 755 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER 756 757 /* Exported function to clear the current history */ 758 759 static PyObject * 760 py_clear_history(PyObject *self, PyObject *noarg) 761 { 762 clear_history(); 763 Py_RETURN_NONE; 764 } 765 766 PyDoc_STRVAR(doc_clear_history, 767 "clear_history() -> None\n\ 768 Clear the current readline history."); 769 #endif 770 771 772 /* Exported function to insert text into the line buffer */ 773 774 static PyObject * 775 insert_text(PyObject *self, PyObject *string) 776 { 777 PyObject *encoded = encode(string); 778 if (encoded == NULL) { 779 return NULL; 780 } 781 rl_insert_text(PyBytes_AS_STRING(encoded)); 782 Py_DECREF(encoded); 783 Py_RETURN_NONE; 784 } 785 786 PyDoc_STRVAR(doc_insert_text, 787 "insert_text(string) -> None\n\ 788 Insert text into the line buffer at the cursor position."); 789 790 791 /* Redisplay the line buffer */ 792 793 static PyObject * 794 redisplay(PyObject *self, PyObject *noarg) 795 { 796 rl_redisplay(); 797 Py_RETURN_NONE; 798 } 799 800 PyDoc_STRVAR(doc_redisplay, 801 "redisplay() -> None\n\ 802 Change what's displayed on the screen to reflect the current\n\ 803 contents of the line buffer."); 804 805 806 /* Table of functions exported by the module */ 807 808 static struct PyMethodDef readline_methods[] = 809 { 810 {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind}, 811 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer}, 812 {"insert_text", insert_text, METH_O, doc_insert_text}, 813 {"redisplay", redisplay, METH_NOARGS, doc_redisplay}, 814 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, 815 {"read_history_file", read_history_file, 816 METH_VARARGS, doc_read_history_file}, 817 {"write_history_file", write_history_file, 818 METH_VARARGS, doc_write_history_file}, 819 #ifdef HAVE_RL_APPEND_HISTORY 820 {"append_history_file", append_history_file, 821 METH_VARARGS, doc_append_history_file}, 822 #endif 823 {"get_history_item", get_history_item, 824 METH_VARARGS, doc_get_history_item}, 825 {"get_current_history_length", (PyCFunction)get_current_history_length, 826 METH_NOARGS, doc_get_current_history_length}, 827 {"set_history_length", set_history_length, 828 METH_VARARGS, set_history_length_doc}, 829 {"get_history_length", get_history_length, 830 METH_NOARGS, get_history_length_doc}, 831 {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, 832 {"get_completer", get_completer, METH_NOARGS, doc_get_completer}, 833 {"get_completion_type", get_completion_type, 834 METH_NOARGS, doc_get_completion_type}, 835 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx}, 836 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx}, 837 838 {"set_completer_delims", set_completer_delims, 839 METH_O, doc_set_completer_delims}, 840 {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history}, 841 {"add_history", py_add_history, METH_O, doc_add_history}, 842 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history}, 843 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history}, 844 {"get_completer_delims", get_completer_delims, 845 METH_NOARGS, doc_get_completer_delims}, 846 847 {"set_completion_display_matches_hook", set_completion_display_matches_hook, 848 METH_VARARGS, doc_set_completion_display_matches_hook}, 849 {"set_startup_hook", set_startup_hook, 850 METH_VARARGS, doc_set_startup_hook}, 851 #ifdef HAVE_RL_PRE_INPUT_HOOK 852 {"set_pre_input_hook", set_pre_input_hook, 853 METH_VARARGS, doc_set_pre_input_hook}, 854 #endif 855 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER 856 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history}, 857 #endif 858 {0, 0} 859 }; 860 861 862 /* C function to call the Python hooks. */ 863 864 static int 865 on_hook(PyObject *func) 866 { 867 int result = 0; 868 if (func != NULL) { 869 PyObject *r; 870 r = _PyObject_CallNoArg(func); 871 if (r == NULL) 872 goto error; 873 if (r == Py_None) 874 result = 0; 875 else { 876 result = _PyLong_AsInt(r); 877 if (result == -1 && PyErr_Occurred()) 878 goto error; 879 } 880 Py_DECREF(r); 881 goto done; 882 error: 883 PyErr_Clear(); 884 Py_XDECREF(r); 885 done: 886 return result; 887 } 888 return result; 889 } 890 891 static int 892 #if defined(_RL_FUNCTION_TYPEDEF) 893 on_startup_hook(void) 894 #else 895 on_startup_hook() 896 #endif 897 { 898 int r; 899 PyGILState_STATE gilstate = PyGILState_Ensure(); 900 r = on_hook(readlinestate_global->startup_hook); 901 PyGILState_Release(gilstate); 902 return r; 903 } 904 905 #ifdef HAVE_RL_PRE_INPUT_HOOK 906 static int 907 #if defined(_RL_FUNCTION_TYPEDEF) 908 on_pre_input_hook(void) 909 #else 910 on_pre_input_hook() 911 #endif 912 { 913 int r; 914 PyGILState_STATE gilstate = PyGILState_Ensure(); 915 r = on_hook(readlinestate_global->pre_input_hook); 916 PyGILState_Release(gilstate); 917 return r; 918 } 919 #endif 920 921 922 /* C function to call the Python completion_display_matches */ 923 924 #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK 925 static void 926 on_completion_display_matches_hook(char **matches, 927 int num_matches, int max_length) 928 { 929 int i; 930 PyObject *sub, *m=NULL, *s=NULL, *r=NULL; 931 PyGILState_STATE gilstate = PyGILState_Ensure(); 932 m = PyList_New(num_matches); 933 if (m == NULL) 934 goto error; 935 for (i = 0; i < num_matches; i++) { 936 s = decode(matches[i+1]); 937 if (s == NULL) 938 goto error; 939 PyList_SET_ITEM(m, i, s); 940 } 941 sub = decode(matches[0]); 942 r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook, 943 "NNi", sub, m, max_length); 944 945 m=NULL; 946 947 if (r == NULL || 948 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) { 949 goto error; 950 } 951 Py_CLEAR(r); 952 953 if (0) { 954 error: 955 PyErr_Clear(); 956 Py_XDECREF(m); 957 Py_XDECREF(r); 958 } 959 PyGILState_Release(gilstate); 960 } 961 962 #endif 963 964 #ifdef HAVE_RL_RESIZE_TERMINAL 965 static volatile sig_atomic_t sigwinch_received; 966 static PyOS_sighandler_t sigwinch_ohandler; 967 968 static void 969 readline_sigwinch_handler(int signum) 970 { 971 sigwinch_received = 1; 972 if (sigwinch_ohandler && 973 sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL) 974 sigwinch_ohandler(signum); 975 976 #ifndef HAVE_SIGACTION 977 /* If the handler was installed with signal() rather than sigaction(), 978 we need to reinstall it. */ 979 PyOS_setsig(SIGWINCH, readline_sigwinch_handler); 980 #endif 981 } 982 #endif 983 984 /* C function to call the Python completer. */ 985 986 static char * 987 on_completion(const char *text, int state) 988 { 989 char *result = NULL; 990 if (readlinestate_global->completer != NULL) { 991 PyObject *r = NULL, *t; 992 PyGILState_STATE gilstate = PyGILState_Ensure(); 993 rl_attempted_completion_over = 1; 994 t = decode(text); 995 r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state); 996 if (r == NULL) 997 goto error; 998 if (r == Py_None) { 999 result = NULL; 1000 } 1001 else { 1002 PyObject *encoded = encode(r); 1003 if (encoded == NULL) 1004 goto error; 1005 result = strdup(PyBytes_AS_STRING(encoded)); 1006 Py_DECREF(encoded); 1007 } 1008 Py_DECREF(r); 1009 goto done; 1010 error: 1011 PyErr_Clear(); 1012 Py_XDECREF(r); 1013 done: 1014 PyGILState_Release(gilstate); 1015 return result; 1016 } 1017 return result; 1018 } 1019 1020 1021 /* A more flexible constructor that saves the "begidx" and "endidx" 1022 * before calling the normal completer */ 1023 1024 static char ** 1025 flex_complete(const char *text, int start, int end) 1026 { 1027 char **result; 1028 char saved; 1029 size_t start_size, end_size; 1030 wchar_t *s; 1031 PyGILState_STATE gilstate = PyGILState_Ensure(); 1032 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER 1033 rl_completion_append_character ='\0'; 1034 #endif 1035 #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND 1036 rl_completion_suppress_append = 0; 1037 #endif 1038 1039 saved = rl_line_buffer[start]; 1040 rl_line_buffer[start] = 0; 1041 s = Py_DecodeLocale(rl_line_buffer, &start_size); 1042 rl_line_buffer[start] = saved; 1043 if (s == NULL) { 1044 goto done; 1045 } 1046 PyMem_RawFree(s); 1047 saved = rl_line_buffer[end]; 1048 rl_line_buffer[end] = 0; 1049 s = Py_DecodeLocale(rl_line_buffer + start, &end_size); 1050 rl_line_buffer[end] = saved; 1051 if (s == NULL) { 1052 goto done; 1053 } 1054 PyMem_RawFree(s); 1055 start = (int)start_size; 1056 end = start + (int)end_size; 1057 1058 done: 1059 Py_XDECREF(readlinestate_global->begidx); 1060 Py_XDECREF(readlinestate_global->endidx); 1061 readlinestate_global->begidx = PyLong_FromLong((long) start); 1062 readlinestate_global->endidx = PyLong_FromLong((long) end); 1063 result = completion_matches((char *)text, *on_completion); 1064 PyGILState_Release(gilstate); 1065 return result; 1066 } 1067 1068 1069 /* Helper to initialize GNU readline properly. */ 1070 1071 static void 1072 setup_readline(readlinestate *mod_state) 1073 { 1074 #ifdef SAVE_LOCALE 1075 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); 1076 if (!saved_locale) 1077 Py_FatalError("not enough memory to save locale"); 1078 #endif 1079 1080 /* The name must be defined before initialization */ 1081 rl_readline_name = "python"; 1082 1083 #ifdef __APPLE__ 1084 /* the libedit readline emulation resets key bindings etc 1085 * when calling rl_initialize. So call it upfront 1086 */ 1087 if (using_libedit_emulation) 1088 rl_initialize(); 1089 1090 /* Detect if libedit's readline emulation uses 0-based 1091 * indexing or 1-based indexing. 1092 */ 1093 add_history("1"); 1094 if (history_get(1) == NULL) { 1095 libedit_history_start = 0; 1096 } else { 1097 libedit_history_start = 1; 1098 } 1099 clear_history(); 1100 #endif /* __APPLE__ */ 1101 1102 using_history(); 1103 1104 /* Force rebind of TAB to insert-tab */ 1105 rl_bind_key('\t', rl_insert); 1106 /* Bind both ESC-TAB and ESC-ESC to the completion function */ 1107 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); 1108 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); 1109 #ifdef HAVE_RL_RESIZE_TERMINAL 1110 /* Set up signal handler for window resize */ 1111 sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler); 1112 #endif 1113 /* Set our hook functions */ 1114 rl_startup_hook = on_startup_hook; 1115 #ifdef HAVE_RL_PRE_INPUT_HOOK 1116 rl_pre_input_hook = on_pre_input_hook; 1117 #endif 1118 /* Set our completion function */ 1119 rl_attempted_completion_function = flex_complete; 1120 /* Set Python word break characters */ 1121 completer_word_break_characters = 1122 rl_completer_word_break_characters = 1123 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); 1124 /* All nonalphanums except '.' */ 1125 1126 mod_state->begidx = PyLong_FromLong(0L); 1127 mod_state->endidx = PyLong_FromLong(0L); 1128 1129 #ifdef __APPLE__ 1130 if (!using_libedit_emulation) 1131 #endif 1132 { 1133 if (!isatty(STDOUT_FILENO)) { 1134 /* Issue #19884: stdout is not a terminal. Disable meta modifier 1135 keys to not write the ANSI sequence "\033[1034h" into stdout. On 1136 terminals supporting 8 bit characters like TERM=xterm-256color 1137 (which is now the default Fedora since Fedora 18), the meta key is 1138 used to enable support of 8 bit characters (ANSI sequence 1139 "\033[1034h"). 1140 1141 With libedit, this call makes readline() crash. */ 1142 rl_variable_bind ("enable-meta-key", "off"); 1143 } 1144 } 1145 1146 /* Initialize (allows .inputrc to override) 1147 * 1148 * XXX: A bug in the readline-2.2 library causes a memory leak 1149 * inside this function. Nothing we can do about it. 1150 */ 1151 #ifdef __APPLE__ 1152 if (using_libedit_emulation) 1153 rl_read_init_file(NULL); 1154 else 1155 #endif /* __APPLE__ */ 1156 rl_initialize(); 1157 1158 RESTORE_LOCALE(saved_locale) 1159 } 1160 1161 /* Wrapper around GNU readline that handles signals differently. */ 1162 1163 static char *completed_input_string; 1164 static void 1165 rlhandler(char *text) 1166 { 1167 completed_input_string = text; 1168 rl_callback_handler_remove(); 1169 } 1170 1171 static char * 1172 readline_until_enter_or_signal(const char *prompt, int *signal) 1173 { 1174 char * not_done_reading = ""; 1175 fd_set selectset; 1176 1177 *signal = 0; 1178 #ifdef HAVE_RL_CATCH_SIGNAL 1179 rl_catch_signals = 0; 1180 #endif 1181 1182 rl_callback_handler_install (prompt, rlhandler); 1183 FD_ZERO(&selectset); 1184 1185 completed_input_string = not_done_reading; 1186 1187 while (completed_input_string == not_done_reading) { 1188 int has_input = 0, err = 0; 1189 1190 while (!has_input) 1191 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */ 1192 1193 /* [Bug #1552726] Only limit the pause if an input hook has been 1194 defined. */ 1195 struct timeval *timeoutp = NULL; 1196 if (PyOS_InputHook) 1197 timeoutp = &timeout; 1198 #ifdef HAVE_RL_RESIZE_TERMINAL 1199 /* Update readline's view of the window size after SIGWINCH */ 1200 if (sigwinch_received) { 1201 sigwinch_received = 0; 1202 rl_resize_terminal(); 1203 } 1204 #endif 1205 FD_SET(fileno(rl_instream), &selectset); 1206 /* select resets selectset if no input was available */ 1207 has_input = select(fileno(rl_instream) + 1, &selectset, 1208 NULL, NULL, timeoutp); 1209 err = errno; 1210 if(PyOS_InputHook) PyOS_InputHook(); 1211 } 1212 1213 if (has_input > 0) { 1214 rl_callback_read_char(); 1215 } 1216 else if (err == EINTR) { 1217 int s; 1218 PyEval_RestoreThread(_PyOS_ReadlineTState); 1219 s = PyErr_CheckSignals(); 1220 PyEval_SaveThread(); 1221 if (s < 0) { 1222 rl_free_line_state(); 1223 #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700 1224 rl_callback_sigcleanup(); 1225 #endif 1226 rl_cleanup_after_signal(); 1227 rl_callback_handler_remove(); 1228 *signal = 1; 1229 completed_input_string = NULL; 1230 } 1231 } 1232 } 1233 1234 return completed_input_string; 1235 } 1236 1237 1238 static char * 1239 call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) 1240 { 1241 size_t n; 1242 char *p, *q; 1243 int signal; 1244 1245 #ifdef SAVE_LOCALE 1246 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); 1247 if (!saved_locale) 1248 Py_FatalError("not enough memory to save locale"); 1249 _Py_SetLocaleFromEnv(LC_CTYPE); 1250 #endif 1251 1252 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { 1253 rl_instream = sys_stdin; 1254 rl_outstream = sys_stdout; 1255 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER 1256 rl_prep_terminal (1); 1257 #endif 1258 } 1259 1260 p = readline_until_enter_or_signal(prompt, &signal); 1261 1262 /* we got an interrupt signal */ 1263 if (signal) { 1264 RESTORE_LOCALE(saved_locale) 1265 return NULL; 1266 } 1267 1268 /* We got an EOF, return an empty string. */ 1269 if (p == NULL) { 1270 p = PyMem_RawMalloc(1); 1271 if (p != NULL) 1272 *p = '\0'; 1273 RESTORE_LOCALE(saved_locale) 1274 return p; 1275 } 1276 1277 /* we have a valid line */ 1278 n = strlen(p); 1279 if (should_auto_add_history && n > 0) { 1280 const char *line; 1281 int length = _py_get_history_length(); 1282 if (length > 0) { 1283 HIST_ENTRY *hist_ent; 1284 #ifdef __APPLE__ 1285 if (using_libedit_emulation) { 1286 /* handle older 0-based or newer 1-based indexing */ 1287 hist_ent = history_get(length + libedit_history_start - 1); 1288 } else 1289 #endif /* __APPLE__ */ 1290 hist_ent = history_get(length); 1291 line = hist_ent ? hist_ent->line : ""; 1292 } else 1293 line = ""; 1294 if (strcmp(p, line)) 1295 add_history(p); 1296 } 1297 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and 1298 release the original. */ 1299 q = p; 1300 p = PyMem_RawMalloc(n+2); 1301 if (p != NULL) { 1302 strncpy(p, q, n); 1303 p[n] = '\n'; 1304 p[n+1] = '\0'; 1305 } 1306 free(q); 1307 RESTORE_LOCALE(saved_locale) 1308 return p; 1309 } 1310 1311 1312 /* Initialize the module */ 1313 1314 PyDoc_STRVAR(doc_module, 1315 "Importing this module enables command line editing using GNU readline."); 1316 1317 #ifdef __APPLE__ 1318 PyDoc_STRVAR(doc_module_le, 1319 "Importing this module enables command line editing using libedit readline."); 1320 #endif /* __APPLE__ */ 1321 1322 static struct PyModuleDef readlinemodule = { 1323 PyModuleDef_HEAD_INIT, 1324 "readline", 1325 doc_module, 1326 sizeof(readlinestate), 1327 readline_methods, 1328 NULL, 1329 readline_traverse, 1330 readline_clear, 1331 readline_free 1332 }; 1333 1334 1335 PyMODINIT_FUNC 1336 PyInit_readline(void) 1337 { 1338 PyObject *m; 1339 readlinestate *mod_state; 1340 1341 #ifdef __APPLE__ 1342 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) { 1343 using_libedit_emulation = 1; 1344 } 1345 1346 if (using_libedit_emulation) 1347 readlinemodule.m_doc = doc_module_le; 1348 1349 #endif /* __APPLE__ */ 1350 1351 m = PyModule_Create(&readlinemodule); 1352 1353 if (m == NULL) 1354 return NULL; 1355 1356 if (PyModule_AddIntConstant(m, "_READLINE_VERSION", 1357 RL_READLINE_VERSION) < 0) { 1358 goto error; 1359 } 1360 if (PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", 1361 rl_readline_version) < 0) { 1362 goto error; 1363 } 1364 if (PyModule_AddStringConstant(m, "_READLINE_LIBRARY_VERSION", 1365 rl_library_version) < 0) 1366 { 1367 goto error; 1368 } 1369 1370 mod_state = (readlinestate *) PyModule_GetState(m); 1371 PyOS_ReadlineFunctionPointer = call_readline; 1372 setup_readline(mod_state); 1373 1374 return m; 1375 1376 error: 1377 Py_DECREF(m); 1378 return NULL; 1379 } 1380