| 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 <setjmp.h>
|
| 10 | #include <signal.h>
|
| 11 | #include <errno.h>
|
| 12 | #include <sys/time.h>
|
| 13 |
|
| 14 | /* ------------------------------------------------------------------------- */
|
| 15 |
|
| 16 | /* OVM_MAIN: This section copied from autotool-generated pyconfig.h.
|
| 17 | * We're not detecting any of it in Oil's configure script. They are for
|
| 18 | * ancient readline versions.
|
| 19 | * */
|
| 20 |
|
| 21 | /* Define if you have readline 2.1 */
|
| 22 | #define HAVE_RL_CALLBACK 1
|
| 23 |
|
| 24 | /* Define if you can turn off readline's signal handling. */
|
| 25 | #define HAVE_RL_CATCH_SIGNAL 1
|
| 26 |
|
| 27 | /* Define if you have readline 2.2 */
|
| 28 | #define HAVE_RL_COMPLETION_APPEND_CHARACTER 1
|
| 29 |
|
| 30 | /* Define if you have readline 4.0 */
|
| 31 | #define HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK 1
|
| 32 |
|
| 33 | /* Define if you have readline 4.2 */
|
| 34 | #define HAVE_RL_COMPLETION_MATCHES 1
|
| 35 |
|
| 36 | /* Define if you have rl_completion_suppress_append */
|
| 37 | #define HAVE_RL_COMPLETION_SUPPRESS_APPEND 1
|
| 38 |
|
| 39 | /* Define if you have readline 4.0 */
|
| 40 | #define HAVE_RL_PRE_INPUT_HOOK 1
|
| 41 |
|
| 42 | /* Define if you have readline 4.0 */
|
| 43 | #define HAVE_RL_RESIZE_TERMINAL 1
|
| 44 |
|
| 45 | /* ------------------------------------------------------------------------- */
|
| 46 |
|
| 47 | #if defined(HAVE_SETLOCALE)
|
| 48 | /* GNU readline() mistakenly sets the LC_CTYPE locale.
|
| 49 | * This is evil. Only the user or the app's main() should do this!
|
| 50 | * We must save and restore the locale around the rl_initialize() call.
|
| 51 | */
|
| 52 | #define SAVE_LOCALE
|
| 53 | #include <locale.h>
|
| 54 | #endif
|
| 55 |
|
| 56 | #ifdef SAVE_LOCALE
|
| 57 | # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
|
| 58 | #else
|
| 59 | # define RESTORE_LOCALE(sl)
|
| 60 | #endif
|
| 61 |
|
| 62 | /* GNU readline definitions */
|
| 63 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
|
| 64 | #include <readline/readline.h>
|
| 65 | #include <readline/history.h>
|
| 66 |
|
| 67 | #ifdef HAVE_RL_COMPLETION_MATCHES
|
| 68 | #define completion_matches(x, y) \
|
| 69 | rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
|
| 70 | #else
|
| 71 | #if defined(_RL_FUNCTION_TYPEDEF)
|
| 72 | extern char **completion_matches(char *, rl_compentry_func_t *);
|
| 73 | #else
|
| 74 |
|
| 75 | #if !defined(__APPLE__)
|
| 76 | extern char **completion_matches(char *, CPFunction *);
|
| 77 | #endif
|
| 78 | #endif
|
| 79 | #endif
|
| 80 |
|
| 81 | #ifdef __APPLE__
|
| 82 | /*
|
| 83 | * It is possible to link the readline module to the readline
|
| 84 | * emulation library of editline/libedit.
|
| 85 | *
|
| 86 | * On OSX this emulation library is not 100% API compatible
|
| 87 | * with the "real" readline and cannot be detected at compile-time,
|
| 88 | * hence we use a runtime check to detect if we're using libedit
|
| 89 | *
|
| 90 | * Currently there is one known API incompatibility:
|
| 91 | * - 'get_history' has a 1-based index with GNU readline, and a 0-based
|
| 92 | * index with older versions of libedit's emulation.
|
| 93 | * - Note that replace_history and remove_history use a 0-based index
|
| 94 | * with both implementations.
|
| 95 | */
|
| 96 | static int using_libedit_emulation = 0;
|
| 97 | static const char libedit_version_tag[] = "EditLine wrapper";
|
| 98 |
|
| 99 | static int libedit_history_start = 0;
|
| 100 | #endif /* __APPLE__ */
|
| 101 |
|
| 102 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
| 103 | static void
|
| 104 | on_completion_display_matches_hook(char **matches,
|
| 105 | int num_matches, int max_length);
|
| 106 | #endif
|
| 107 |
|
| 108 | /* Memory allocated for rl_completer_word_break_characters
|
| 109 | (see issue #17289 for the motivation). */
|
| 110 | static char *completer_word_break_characters;
|
| 111 |
|
| 112 | /* Exported function to send one line to readline's init file parser */
|
| 113 |
|
| 114 | static PyObject *
|
| 115 | parse_and_bind(PyObject *self, PyObject *args)
|
| 116 | {
|
| 117 | char *s, *copy;
|
| 118 | int binding_result;
|
| 119 |
|
| 120 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
|
| 121 | return NULL;
|
| 122 | /* Make a copy -- rl_parse_and_bind() modifies its argument */
|
| 123 | /* Bernard Herzog */
|
| 124 | copy = malloc(1 + strlen(s));
|
| 125 | if (copy == NULL)
|
| 126 | return PyErr_NoMemory();
|
| 127 | strcpy(copy, s);
|
| 128 |
|
| 129 | binding_result = rl_parse_and_bind(copy);
|
| 130 | free(copy); /* Free the copy */
|
| 131 |
|
| 132 | if (binding_result != 0) {
|
| 133 | PyErr_Format(PyExc_ValueError, "'%s': invalid binding", s);
|
| 134 | return NULL;
|
| 135 | }
|
| 136 |
|
| 137 | Py_RETURN_NONE;
|
| 138 | }
|
| 139 |
|
| 140 | PyDoc_STRVAR(doc_parse_and_bind,
|
| 141 | "parse_and_bind(string) -> None\n\
|
| 142 | Bind a key sequence to a readline function (or a variable to a value).");
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 | /* Exported function to parse a readline init file */
|
| 147 |
|
| 148 | static PyObject *
|
| 149 | read_init_file(PyObject *self, PyObject *args)
|
| 150 | {
|
| 151 | char *s = NULL;
|
| 152 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
|
| 153 | return NULL;
|
| 154 | errno = rl_read_init_file(s);
|
| 155 | if (errno)
|
| 156 | return PyErr_SetFromErrno(PyExc_IOError);
|
| 157 | Py_RETURN_NONE;
|
| 158 | }
|
| 159 |
|
| 160 | PyDoc_STRVAR(doc_read_init_file,
|
| 161 | "read_init_file([filename]) -> None\n\
|
| 162 | Execute a readline initialization file.\n\
|
| 163 | The default filename is the last filename used.");
|
| 164 |
|
| 165 |
|
| 166 | /* Exported function to load a readline history file */
|
| 167 |
|
| 168 | static PyObject *
|
| 169 | read_history_file(PyObject *self, PyObject *args)
|
| 170 | {
|
| 171 | char *s = NULL;
|
| 172 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
|
| 173 | return NULL;
|
| 174 | errno = read_history(s);
|
| 175 | if (errno)
|
| 176 | return PyErr_SetFromErrno(PyExc_IOError);
|
| 177 | Py_RETURN_NONE;
|
| 178 | }
|
| 179 |
|
| 180 | static int _history_length = -1; /* do not truncate history by default */
|
| 181 | PyDoc_STRVAR(doc_read_history_file,
|
| 182 | "read_history_file([filename]) -> None\n\
|
| 183 | Load a readline history file.\n\
|
| 184 | The default filename is ~/.history.");
|
| 185 |
|
| 186 |
|
| 187 | /* Exported function to save a readline history file */
|
| 188 |
|
| 189 | static PyObject *
|
| 190 | write_history_file(PyObject *self, PyObject *args)
|
| 191 | {
|
| 192 | char *s = NULL;
|
| 193 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
|
| 194 | return NULL;
|
| 195 | errno = write_history(s);
|
| 196 | if (!errno && _history_length >= 0)
|
| 197 | history_truncate_file(s, _history_length);
|
| 198 | if (errno)
|
| 199 | return PyErr_SetFromErrno(PyExc_IOError);
|
| 200 | Py_RETURN_NONE;
|
| 201 | }
|
| 202 |
|
| 203 | PyDoc_STRVAR(doc_write_history_file,
|
| 204 | "write_history_file([filename]) -> None\n\
|
| 205 | Save a readline history file.\n\
|
| 206 | The default filename is ~/.history.");
|
| 207 |
|
| 208 |
|
| 209 | /* Set history length */
|
| 210 |
|
| 211 | static PyObject*
|
| 212 | set_history_length(PyObject *self, PyObject *args)
|
| 213 | {
|
| 214 | int length = _history_length;
|
| 215 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
|
| 216 | return NULL;
|
| 217 | _history_length = length;
|
| 218 | Py_RETURN_NONE;
|
| 219 | }
|
| 220 |
|
| 221 | PyDoc_STRVAR(set_history_length_doc,
|
| 222 | "set_history_length(length) -> None\n\
|
| 223 | set the maximal number of lines which will be written to\n\
|
| 224 | the history file. A negative length is used to inhibit\n\
|
| 225 | history truncation.");
|
| 226 |
|
| 227 |
|
| 228 | /* Get history length */
|
| 229 |
|
| 230 | static PyObject*
|
| 231 | get_history_length(PyObject *self, PyObject *noarg)
|
| 232 | {
|
| 233 | return PyInt_FromLong(_history_length);
|
| 234 | }
|
| 235 |
|
| 236 | PyDoc_STRVAR(get_history_length_doc,
|
| 237 | "get_history_length() -> int\n\
|
| 238 | return the maximum number of lines that will be written to\n\
|
| 239 | the history file.");
|
| 240 |
|
| 241 |
|
| 242 | /* Generic hook function setter */
|
| 243 |
|
| 244 | static PyObject *
|
| 245 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
|
| 246 | {
|
| 247 | PyObject *function = Py_None;
|
| 248 | char buf[80];
|
| 249 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
|
| 250 | if (!PyArg_ParseTuple(args, buf, &function))
|
| 251 | return NULL;
|
| 252 | if (function == Py_None) {
|
| 253 | Py_CLEAR(*hook_var);
|
| 254 | }
|
| 255 | else if (PyCallable_Check(function)) {
|
| 256 | PyObject *tmp = *hook_var;
|
| 257 | Py_INCREF(function);
|
| 258 | *hook_var = function;
|
| 259 | Py_XDECREF(tmp);
|
| 260 | }
|
| 261 | else {
|
| 262 | PyOS_snprintf(buf, sizeof(buf),
|
| 263 | "set_%.50s(func): argument not callable",
|
| 264 | funcname);
|
| 265 | PyErr_SetString(PyExc_TypeError, buf);
|
| 266 | return NULL;
|
| 267 | }
|
| 268 | Py_RETURN_NONE;
|
| 269 | }
|
| 270 |
|
| 271 |
|
| 272 | /* Exported functions to specify hook functions in Python */
|
| 273 |
|
| 274 | static PyObject *completion_display_matches_hook = NULL;
|
| 275 | static PyObject *startup_hook = NULL;
|
| 276 | static PyObject *bind_shell_command_hook = NULL;
|
| 277 |
|
| 278 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
| 279 | static PyObject *pre_input_hook = NULL;
|
| 280 | #endif
|
| 281 |
|
| 282 | static PyObject *
|
| 283 | set_completion_display_matches_hook(PyObject *self, PyObject *args)
|
| 284 | {
|
| 285 | PyObject *result = set_hook("completion_display_matches_hook",
|
| 286 | &completion_display_matches_hook, args);
|
| 287 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
| 288 | /* We cannot set this hook globally, since it replaces the
|
| 289 | default completion display. */
|
| 290 | rl_completion_display_matches_hook =
|
| 291 | completion_display_matches_hook ?
|
| 292 | #if defined(_RL_FUNCTION_TYPEDEF)
|
| 293 | (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
|
| 294 | #else
|
| 295 | (VFunction *)on_completion_display_matches_hook : 0;
|
| 296 | #endif
|
| 297 | #endif
|
| 298 | return result;
|
| 299 |
|
| 300 | }
|
| 301 |
|
| 302 | PyDoc_STRVAR(doc_set_completion_display_matches_hook,
|
| 303 | "set_completion_display_matches_hook([function]) -> None\n\
|
| 304 | Set or remove the completion display function.\n\
|
| 305 | The function is called as\n\
|
| 306 | function(substitution, [matches], longest_match_length)\n\
|
| 307 | once each time matches need to be displayed.");
|
| 308 |
|
| 309 | static PyObject *
|
| 310 | set_startup_hook(PyObject *self, PyObject *args)
|
| 311 | {
|
| 312 | return set_hook("startup_hook", &startup_hook, args);
|
| 313 | }
|
| 314 |
|
| 315 | PyDoc_STRVAR(doc_set_startup_hook,
|
| 316 | "set_startup_hook([function]) -> None\n\
|
| 317 | Set or remove the function invoked by the rl_startup_hook callback.\n\
|
| 318 | The function is called with no arguments just\n\
|
| 319 | before readline prints the first prompt.");
|
| 320 |
|
| 321 |
|
| 322 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
| 323 |
|
| 324 | /* Set pre-input hook */
|
| 325 |
|
| 326 | static PyObject *
|
| 327 | set_pre_input_hook(PyObject *self, PyObject *args)
|
| 328 | {
|
| 329 | return set_hook("pre_input_hook", &pre_input_hook, args);
|
| 330 | }
|
| 331 |
|
| 332 | PyDoc_STRVAR(doc_set_pre_input_hook,
|
| 333 | "set_pre_input_hook([function]) -> None\n\
|
| 334 | Set or remove the function invoked by the rl_pre_input_hook callback.\n\
|
| 335 | The function is called with no arguments after the first prompt\n\
|
| 336 | has been printed and just before readline starts reading input\n\
|
| 337 | characters.");
|
| 338 |
|
| 339 | #endif
|
| 340 |
|
| 341 |
|
| 342 | /* Exported function to specify a word completer in Python */
|
| 343 |
|
| 344 | static PyObject *completer = NULL;
|
| 345 |
|
| 346 | static PyObject *begidx = NULL;
|
| 347 | static PyObject *endidx = NULL;
|
| 348 |
|
| 349 |
|
| 350 | /* Get the completion type for the scope of the tab-completion */
|
| 351 | static PyObject *
|
| 352 | get_completion_type(PyObject *self, PyObject *noarg)
|
| 353 | {
|
| 354 | return PyInt_FromLong(rl_completion_type);
|
| 355 | }
|
| 356 |
|
| 357 | PyDoc_STRVAR(doc_get_completion_type,
|
| 358 | "get_completion_type() -> int\n\
|
| 359 | Get the type of completion being attempted.");
|
| 360 |
|
| 361 |
|
| 362 | /* Set bind -x Python command hook */
|
| 363 |
|
| 364 | static PyObject *
|
| 365 | set_bind_shell_command_hook(PyObject *self, PyObject *args)
|
| 366 | {
|
| 367 | return set_hook("bind_shell_command_hook", &bind_shell_command_hook, args);
|
| 368 | }
|
| 369 |
|
| 370 | PyDoc_STRVAR(doc_set_bind_shell_command_hook,
|
| 371 | "set_bind_shell_command_hook([function]) -> None\n\
|
| 372 | Set or remove the function invoked by the rl_bind_keyseq_in_map callback.\n\
|
| 373 | The function is called with three arguments: the string to parse and evaluate,\n\
|
| 374 | the contents of the readline buffer to put in the READLINE_LINE env var,\n\
|
| 375 | and the int of the cursor's point in the buffer to put in READLINE_POINT.\n\
|
| 376 | It must return the READLINE_* vars in a tuple.");
|
| 377 |
|
| 378 |
|
| 379 | /* Get the beginning index for the scope of the tab-completion */
|
| 380 |
|
| 381 | static PyObject *
|
| 382 | get_begidx(PyObject *self, PyObject *noarg)
|
| 383 | {
|
| 384 | Py_INCREF(begidx);
|
| 385 | return begidx;
|
| 386 | }
|
| 387 |
|
| 388 | PyDoc_STRVAR(doc_get_begidx,
|
| 389 | "get_begidx() -> int\n\
|
| 390 | get the beginning index of the completion scope");
|
| 391 |
|
| 392 |
|
| 393 | /* Get the ending index for the scope of the tab-completion */
|
| 394 |
|
| 395 | static PyObject *
|
| 396 | get_endidx(PyObject *self, PyObject *noarg)
|
| 397 | {
|
| 398 | Py_INCREF(endidx);
|
| 399 | return endidx;
|
| 400 | }
|
| 401 |
|
| 402 | PyDoc_STRVAR(doc_get_endidx,
|
| 403 | "get_endidx() -> int\n\
|
| 404 | get the ending index of the completion scope");
|
| 405 |
|
| 406 |
|
| 407 | /* Set the tab-completion word-delimiters that readline uses */
|
| 408 |
|
| 409 | static PyObject *
|
| 410 | set_completer_delims(PyObject *self, PyObject *args)
|
| 411 | {
|
| 412 | char *break_chars;
|
| 413 |
|
| 414 | if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
|
| 415 | return NULL;
|
| 416 | }
|
| 417 | /* Keep a reference to the allocated memory in the module state in case
|
| 418 | some other module modifies rl_completer_word_break_characters
|
| 419 | (see issue #17289). */
|
| 420 | break_chars = strdup(break_chars);
|
| 421 | if (break_chars) {
|
| 422 | free(completer_word_break_characters);
|
| 423 | completer_word_break_characters = break_chars;
|
| 424 | rl_completer_word_break_characters = break_chars;
|
| 425 | Py_RETURN_NONE;
|
| 426 | }
|
| 427 | else
|
| 428 | return PyErr_NoMemory();
|
| 429 | }
|
| 430 |
|
| 431 | PyDoc_STRVAR(doc_set_completer_delims,
|
| 432 | "set_completer_delims(string) -> None\n\
|
| 433 | set the word delimiters for completion");
|
| 434 |
|
| 435 | /* _py_free_history_entry: Utility function to free a history entry. */
|
| 436 |
|
| 437 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
|
| 438 |
|
| 439 | /* Readline version >= 5.0 introduced a timestamp field into the history entry
|
| 440 | structure; this needs to be freed to avoid a memory leak. This version of
|
| 441 | readline also introduced the handy 'free_history_entry' function, which
|
| 442 | takes care of the timestamp. */
|
| 443 |
|
| 444 | static void
|
| 445 | _py_free_history_entry(HIST_ENTRY *entry)
|
| 446 | {
|
| 447 | histdata_t data = free_history_entry(entry);
|
| 448 | free(data);
|
| 449 | }
|
| 450 |
|
| 451 | #else
|
| 452 |
|
| 453 | /* No free_history_entry function; free everything manually. */
|
| 454 |
|
| 455 | static void
|
| 456 | _py_free_history_entry(HIST_ENTRY *entry)
|
| 457 | {
|
| 458 | if (entry->line)
|
| 459 | free((void *)entry->line);
|
| 460 | if (entry->data)
|
| 461 | free(entry->data);
|
| 462 | free(entry);
|
| 463 | }
|
| 464 |
|
| 465 | #endif
|
| 466 |
|
| 467 | static PyObject *
|
| 468 | py_remove_history(PyObject *self, PyObject *args)
|
| 469 | {
|
| 470 | int entry_number;
|
| 471 | HIST_ENTRY *entry;
|
| 472 |
|
| 473 | if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
|
| 474 | return NULL;
|
| 475 | if (entry_number < 0) {
|
| 476 | PyErr_SetString(PyExc_ValueError,
|
| 477 | "History index cannot be negative");
|
| 478 | return NULL;
|
| 479 | }
|
| 480 | entry = remove_history(entry_number);
|
| 481 | if (!entry) {
|
| 482 | PyErr_Format(PyExc_ValueError,
|
| 483 | "No history item at position %d",
|
| 484 | entry_number);
|
| 485 | return NULL;
|
| 486 | }
|
| 487 | /* free memory allocated for the history entry */
|
| 488 | _py_free_history_entry(entry);
|
| 489 | Py_RETURN_NONE;
|
| 490 | }
|
| 491 |
|
| 492 | PyDoc_STRVAR(doc_remove_history,
|
| 493 | "remove_history_item(pos) -> None\n\
|
| 494 | remove history item given by its position");
|
| 495 |
|
| 496 | static PyObject *
|
| 497 | py_replace_history(PyObject *self, PyObject *args)
|
| 498 | {
|
| 499 | int entry_number;
|
| 500 | char *line;
|
| 501 | HIST_ENTRY *old_entry;
|
| 502 |
|
| 503 | if (!PyArg_ParseTuple(args, "is:replace_history_item", &entry_number,
|
| 504 | &line)) {
|
| 505 | return NULL;
|
| 506 | }
|
| 507 | if (entry_number < 0) {
|
| 508 | PyErr_SetString(PyExc_ValueError,
|
| 509 | "History index cannot be negative");
|
| 510 | return NULL;
|
| 511 | }
|
| 512 | old_entry = replace_history_entry(entry_number, line, (void *)NULL);
|
| 513 | if (!old_entry) {
|
| 514 | PyErr_Format(PyExc_ValueError,
|
| 515 | "No history item at position %d",
|
| 516 | entry_number);
|
| 517 | return NULL;
|
| 518 | }
|
| 519 | /* free memory allocated for the old history entry */
|
| 520 | _py_free_history_entry(old_entry);
|
| 521 | Py_RETURN_NONE;
|
| 522 | }
|
| 523 |
|
| 524 | PyDoc_STRVAR(doc_replace_history,
|
| 525 | "replace_history_item(pos, line) -> None\n\
|
| 526 | replaces history item given by its position with contents of line");
|
| 527 |
|
| 528 | /* Add a line to the history buffer */
|
| 529 |
|
| 530 | static PyObject *
|
| 531 | py_add_history(PyObject *self, PyObject *args)
|
| 532 | {
|
| 533 | char *line;
|
| 534 |
|
| 535 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
|
| 536 | return NULL;
|
| 537 | }
|
| 538 | add_history(line);
|
| 539 | Py_RETURN_NONE;
|
| 540 | }
|
| 541 |
|
| 542 | PyDoc_STRVAR(doc_add_history,
|
| 543 | "add_history(string) -> None\n\
|
| 544 | add an item to the history buffer");
|
| 545 |
|
| 546 |
|
| 547 | /* Get the tab-completion word-delimiters that readline uses */
|
| 548 |
|
| 549 | static PyObject *
|
| 550 | get_completer_delims(PyObject *self, PyObject *noarg)
|
| 551 | {
|
| 552 | return PyString_FromString(rl_completer_word_break_characters);
|
| 553 | }
|
| 554 |
|
| 555 | PyDoc_STRVAR(doc_get_completer_delims,
|
| 556 | "get_completer_delims() -> string\n\
|
| 557 | get the word delimiters for completion");
|
| 558 |
|
| 559 |
|
| 560 | /* Set the completer function */
|
| 561 |
|
| 562 | static PyObject *
|
| 563 | set_completer(PyObject *self, PyObject *args)
|
| 564 | {
|
| 565 | return set_hook("completer", &completer, args);
|
| 566 | }
|
| 567 |
|
| 568 | PyDoc_STRVAR(doc_set_completer,
|
| 569 | "set_completer([function]) -> None\n\
|
| 570 | Set or remove the completer function.\n\
|
| 571 | The function is called as function(text, state),\n\
|
| 572 | for state in 0, 1, 2, ..., until it returns a non-string.\n\
|
| 573 | It should return the next possible completion starting with 'text'.");
|
| 574 |
|
| 575 |
|
| 576 | static PyObject *
|
| 577 | get_completer(PyObject *self, PyObject *noargs)
|
| 578 | {
|
| 579 | if (completer == NULL) {
|
| 580 | Py_RETURN_NONE;
|
| 581 | }
|
| 582 | Py_INCREF(completer);
|
| 583 | return completer;
|
| 584 | }
|
| 585 |
|
| 586 | PyDoc_STRVAR(doc_get_completer,
|
| 587 | "get_completer() -> function\n\
|
| 588 | \n\
|
| 589 | Returns current completer function.");
|
| 590 |
|
| 591 | /* Private function to get current length of history. XXX It may be
|
| 592 | * possible to replace this with a direct use of history_length instead,
|
| 593 | * but it's not clear whether BSD's libedit keeps history_length up to date.
|
| 594 | * See issue #8065.*/
|
| 595 |
|
| 596 | static int
|
| 597 | _py_get_history_length(void)
|
| 598 | {
|
| 599 | HISTORY_STATE *hist_st = history_get_history_state();
|
| 600 | int length = hist_st->length;
|
| 601 | /* the history docs don't say so, but the address of hist_st changes each
|
| 602 | time history_get_history_state is called which makes me think it's
|
| 603 | freshly malloc'd memory... on the other hand, the address of the last
|
| 604 | line stays the same as long as history isn't extended, so it appears to
|
| 605 | be malloc'd but managed by the history package... */
|
| 606 | free(hist_st);
|
| 607 | return length;
|
| 608 | }
|
| 609 |
|
| 610 | /* Exported function to get any element of history */
|
| 611 |
|
| 612 | static PyObject *
|
| 613 | get_history_item(PyObject *self, PyObject *args)
|
| 614 | {
|
| 615 | int idx = 0;
|
| 616 | HIST_ENTRY *hist_ent;
|
| 617 |
|
| 618 | if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
|
| 619 | return NULL;
|
| 620 | #ifdef __APPLE__
|
| 621 | if (using_libedit_emulation) {
|
| 622 | /* Older versions of libedit's readline emulation
|
| 623 | * use 0-based indexes, while readline and newer
|
| 624 | * versions of libedit use 1-based indexes.
|
| 625 | */
|
| 626 | int length = _py_get_history_length();
|
| 627 |
|
| 628 | idx = idx - 1 + libedit_history_start;
|
| 629 |
|
| 630 | /*
|
| 631 | * Apple's readline emulation crashes when
|
| 632 | * the index is out of range, therefore
|
| 633 | * test for that and fail gracefully.
|
| 634 | */
|
| 635 | if (idx < (0 + libedit_history_start)
|
| 636 | || idx >= (length + libedit_history_start)) {
|
| 637 | Py_RETURN_NONE;
|
| 638 | }
|
| 639 | }
|
| 640 | #endif /* __APPLE__ */
|
| 641 | if ((hist_ent = history_get(idx)))
|
| 642 | return PyString_FromString(hist_ent->line);
|
| 643 | else {
|
| 644 | Py_RETURN_NONE;
|
| 645 | }
|
| 646 | }
|
| 647 |
|
| 648 | PyDoc_STRVAR(doc_get_history_item,
|
| 649 | "get_history_item() -> string\n\
|
| 650 | return the current contents of history item at index.");
|
| 651 |
|
| 652 |
|
| 653 | /* Exported function to get current length of history */
|
| 654 |
|
| 655 | static PyObject *
|
| 656 | get_current_history_length(PyObject *self, PyObject *noarg)
|
| 657 | {
|
| 658 | return PyInt_FromLong((long)_py_get_history_length());
|
| 659 | }
|
| 660 |
|
| 661 | PyDoc_STRVAR(doc_get_current_history_length,
|
| 662 | "get_current_history_length() -> integer\n\
|
| 663 | return the current (not the maximum) length of history.");
|
| 664 |
|
| 665 |
|
| 666 | /* Exported function to read the current line buffer */
|
| 667 |
|
| 668 | static PyObject *
|
| 669 | get_line_buffer(PyObject *self, PyObject *noarg)
|
| 670 | {
|
| 671 | return PyString_FromString(rl_line_buffer);
|
| 672 | }
|
| 673 |
|
| 674 | PyDoc_STRVAR(doc_get_line_buffer,
|
| 675 | "get_line_buffer() -> string\n\
|
| 676 | return the current contents of the line buffer.");
|
| 677 |
|
| 678 |
|
| 679 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
| 680 |
|
| 681 | /* Exported function to clear the current history */
|
| 682 |
|
| 683 | static PyObject *
|
| 684 | py_clear_history(PyObject *self, PyObject *noarg)
|
| 685 | {
|
| 686 | clear_history();
|
| 687 | Py_RETURN_NONE;
|
| 688 | }
|
| 689 |
|
| 690 | PyDoc_STRVAR(doc_clear_history,
|
| 691 | "clear_history() -> None\n\
|
| 692 | Clear the current readline history.");
|
| 693 | #endif
|
| 694 |
|
| 695 | /* Added for OSH. We need to call this in our SIGWINCH handler so global
|
| 696 | * variables in readline get updated. */
|
| 697 | static PyObject *
|
| 698 | py_resize_terminal(PyObject *self, PyObject *noarg)
|
| 699 | {
|
| 700 | rl_resize_terminal();
|
| 701 | Py_RETURN_NONE;
|
| 702 | }
|
| 703 |
|
| 704 | /* Exported function to insert text into the line buffer */
|
| 705 |
|
| 706 | static PyObject *
|
| 707 | insert_text(PyObject *self, PyObject *args)
|
| 708 | {
|
| 709 | char *s;
|
| 710 | if (!PyArg_ParseTuple(args, "s:insert_text", &s))
|
| 711 | return NULL;
|
| 712 | rl_insert_text(s);
|
| 713 | Py_RETURN_NONE;
|
| 714 | }
|
| 715 |
|
| 716 | PyDoc_STRVAR(doc_insert_text,
|
| 717 | "insert_text(string) -> None\n\
|
| 718 | Insert text into the line buffer at the cursor position.");
|
| 719 |
|
| 720 |
|
| 721 | /* Redisplay the line buffer */
|
| 722 |
|
| 723 | static PyObject *
|
| 724 | redisplay(PyObject *self, PyObject *noarg)
|
| 725 | {
|
| 726 | rl_redisplay();
|
| 727 | Py_RETURN_NONE;
|
| 728 | }
|
| 729 |
|
| 730 | PyDoc_STRVAR(doc_redisplay,
|
| 731 | "redisplay() -> None\n\
|
| 732 | Change what's displayed on the screen to reflect the current\n\
|
| 733 | contents of the line buffer.");
|
| 734 |
|
| 735 |
|
| 736 | /* Functions added to implement the 'bind' builtin in OSH */
|
| 737 |
|
| 738 | /* -x/-X command keymaps */
|
| 739 | static Keymap emacs_cmd_map;
|
| 740 | static Keymap vi_insert_cmd_map;
|
| 741 | static Keymap vi_movement_cmd_map;
|
| 742 |
|
| 743 | /*
|
| 744 | These forcibly cast between a Keymap* and a rl_command_func_t*. Readline
|
| 745 | uses an additional `.type` field to keep track of the pointer's true type.
|
| 746 | */
|
| 747 | #define RL_KEYMAP_TO_FUNCTION(data) (rl_command_func_t *)(data)
|
| 748 | #define RL_FUNCTION_TO_KEYMAP(map, key) (Keymap)(map[key].function)
|
| 749 |
|
| 750 | static void
|
| 751 | _init_command_maps(void)
|
| 752 | {
|
| 753 | emacs_cmd_map = rl_make_bare_keymap();
|
| 754 | vi_insert_cmd_map = rl_make_bare_keymap();
|
| 755 | vi_movement_cmd_map = rl_make_bare_keymap();
|
| 756 |
|
| 757 | /* Ensure that Esc- and Ctrl-X are also keymaps */
|
| 758 | emacs_cmd_map[CTRL('X')].type = ISKMAP;
|
| 759 | emacs_cmd_map[CTRL('X')].function = RL_KEYMAP_TO_FUNCTION(rl_make_bare_keymap());
|
| 760 | emacs_cmd_map[ESC].type = ISKMAP;
|
| 761 | emacs_cmd_map[ESC].function = RL_KEYMAP_TO_FUNCTION(rl_make_bare_keymap());
|
| 762 | }
|
| 763 |
|
| 764 | static Keymap
|
| 765 | _get_associated_cmd_map(Keymap kmap)
|
| 766 | {
|
| 767 | if (emacs_cmd_map == NULL)
|
| 768 | _init_command_maps();
|
| 769 |
|
| 770 | if (kmap == emacs_standard_keymap)
|
| 771 | return emacs_cmd_map;
|
| 772 | else if (kmap == vi_insertion_keymap)
|
| 773 | return vi_insert_cmd_map;
|
| 774 | else if (kmap == vi_movement_keymap)
|
| 775 | return vi_movement_cmd_map;
|
| 776 | else if (kmap == emacs_meta_keymap)
|
| 777 | return (RL_FUNCTION_TO_KEYMAP(emacs_cmd_map, ESC));
|
| 778 | else if (kmap == emacs_ctlx_keymap)
|
| 779 | return (RL_FUNCTION_TO_KEYMAP(emacs_cmd_map, CTRL('X')));
|
| 780 |
|
| 781 | return (Keymap) NULL;
|
| 782 | }
|
| 783 |
|
| 784 | /* List binding functions */
|
| 785 | static PyObject*
|
| 786 | list_funmap_names(PyObject *self, PyObject *args)
|
| 787 | {
|
| 788 | rl_list_funmap_names();
|
| 789 | Py_RETURN_NONE;
|
| 790 | }
|
| 791 |
|
| 792 | PyDoc_STRVAR(doc_list_funmap_names,
|
| 793 | "list_funmap_names() -> None\n\
|
| 794 | Print all of the available readline functions.");
|
| 795 |
|
| 796 | /* Print readline functions and their bindings */
|
| 797 |
|
| 798 | static PyObject*
|
| 799 | function_dumper(PyObject *self, PyObject *args)
|
| 800 | {
|
| 801 | int print_readably;
|
| 802 |
|
| 803 | if (!PyArg_ParseTuple(args, "i:function_dumper", &print_readably))
|
| 804 | return NULL;
|
| 805 |
|
| 806 | rl_function_dumper(print_readably);
|
| 807 | Py_RETURN_NONE;
|
| 808 | }
|
| 809 |
|
| 810 | PyDoc_STRVAR(doc_list_function_dumper,
|
| 811 | "function_dumper(bool) -> None\n\
|
| 812 | Print all readline functions and their bindings.");
|
| 813 |
|
| 814 | /* Print macros, their bindings, and their string outputs */
|
| 815 |
|
| 816 | static PyObject*
|
| 817 | macro_dumper(PyObject *self, PyObject *args)
|
| 818 | {
|
| 819 | int print_readably;
|
| 820 |
|
| 821 | if (!PyArg_ParseTuple(args, "i:macro_dumper", &print_readably))
|
| 822 | return NULL;
|
| 823 |
|
| 824 | rl_macro_dumper(print_readably);
|
| 825 | Py_RETURN_NONE;
|
| 826 | }
|
| 827 |
|
| 828 | PyDoc_STRVAR(doc_list_macro_dumper,
|
| 829 | "macro_dumper(bool) -> None\n\
|
| 830 | Print all readline sequences bound to macros and the strings they output.");
|
| 831 |
|
| 832 |
|
| 833 | /* List readline variables */
|
| 834 |
|
| 835 | static PyObject*
|
| 836 | variable_dumper(PyObject *self, PyObject *args)
|
| 837 | {
|
| 838 | int print_readably;
|
| 839 |
|
| 840 | if (!PyArg_ParseTuple(args, "i:variable_dumper", &print_readably))
|
| 841 | return NULL;
|
| 842 |
|
| 843 | rl_variable_dumper(print_readably);
|
| 844 | Py_RETURN_NONE;
|
| 845 | }
|
| 846 |
|
| 847 | PyDoc_STRVAR(doc_list_variable_dumper,
|
| 848 | "variable_dumper(bool) -> None\n\
|
| 849 | List readline variables and their values.");
|
| 850 |
|
| 851 |
|
| 852 | /* Query bindings for a function name */
|
| 853 |
|
| 854 | // readline returns null-terminated string arrays
|
| 855 | void _strvec_dispose(char **strvec) {
|
| 856 | register int i;
|
| 857 |
|
| 858 | if (strvec == NULL)
|
| 859 | return;
|
| 860 |
|
| 861 | for (i = 0; strvec[i]; i++) {
|
| 862 | free(strvec[i]);
|
| 863 | }
|
| 864 |
|
| 865 | free(strvec);
|
| 866 | }
|
| 867 |
|
| 868 | // Nicely prints a strvec with commas and an and
|
| 869 | // like '"foo", "bar", and "moop"'
|
| 870 | void _pprint_strvec_list(char **strvec) {
|
| 871 | int i;
|
| 872 |
|
| 873 | for (i = 0; strvec[i]; i++) {
|
| 874 | printf("\"%s\"", strvec[i]);
|
| 875 | if (strvec[i + 1]) {
|
| 876 | printf(", ");
|
| 877 | if (!strvec[i + 2])
|
| 878 | printf("and ");
|
| 879 | }
|
| 880 | }
|
| 881 | }
|
| 882 |
|
| 883 | /*
|
| 884 | NB: readline (and bash) have a bug where they don't see certain keyseqs, even
|
| 885 | if the bindings work. E.g., if you bind a number key like "\C-7", it will be
|
| 886 | bound, but reporting code like query_bindings and function_dumper won't count it.
|
| 887 | */
|
| 888 |
|
| 889 | static PyObject*
|
| 890 | query_bindings(PyObject *self, PyObject *args)
|
| 891 | {
|
| 892 | char *fn_name;
|
| 893 | rl_command_func_t *cmd_fn;
|
| 894 | char **key_seqs;
|
| 895 |
|
| 896 | if (!PyArg_ParseTuple(args, "s:query_bindings", &fn_name))
|
| 897 | return NULL;
|
| 898 |
|
| 899 | cmd_fn = rl_named_function(fn_name);
|
| 900 |
|
| 901 | if (cmd_fn == NULL) {
|
| 902 | PyErr_Format(PyExc_ValueError, "`%s': unknown function name", fn_name);
|
| 903 | return NULL;
|
| 904 | }
|
| 905 |
|
| 906 | key_seqs = rl_invoking_keyseqs(cmd_fn);
|
| 907 |
|
| 908 | if (!key_seqs) {
|
| 909 | // print to stdout, but return an error
|
| 910 | printf("%s is not bound to any keys.\n", fn_name);
|
| 911 | PyErr_SetNone(PyExc_ValueError);
|
| 912 | return NULL;
|
| 913 | }
|
| 914 |
|
| 915 | printf("%s can be invoked via ", fn_name);
|
| 916 | _pprint_strvec_list(key_seqs);
|
| 917 | printf(".\n");
|
| 918 |
|
| 919 | _strvec_dispose(key_seqs);
|
| 920 |
|
| 921 | Py_RETURN_NONE;
|
| 922 | }
|
| 923 |
|
| 924 | PyDoc_STRVAR(doc_query_bindings,
|
| 925 | "query_bindings(str) -> None\n\
|
| 926 | Query bindings to see what's bound to a given function.");
|
| 927 |
|
| 928 |
|
| 929 | static PyObject*
|
| 930 | unbind_rl_function(PyObject *self, PyObject *args)
|
| 931 | {
|
| 932 | char *fn_name;
|
| 933 | rl_command_func_t *cmd_fn;
|
| 934 |
|
| 935 | if (!PyArg_ParseTuple(args, "s:unbind_rl_function", &fn_name))
|
| 936 | return NULL;
|
| 937 |
|
| 938 | cmd_fn = rl_named_function(fn_name);
|
| 939 | if (cmd_fn == NULL) {
|
| 940 | PyErr_Format(PyExc_ValueError, "`%s': unknown function name", fn_name);
|
| 941 | return NULL;
|
| 942 | }
|
| 943 |
|
| 944 | rl_unbind_function_in_map(cmd_fn, rl_get_keymap());
|
| 945 | Py_RETURN_NONE;
|
| 946 | }
|
| 947 |
|
| 948 | PyDoc_STRVAR(doc_unbind_rl_function,
|
| 949 | "unbind_rl_function(function_name) -> None\n\
|
| 950 | Unbind all keys bound to the named readline function in the current keymap.");
|
| 951 |
|
| 952 |
|
| 953 | static PyObject*
|
| 954 | print_shell_cmd_map(PyObject *self, PyObject *noarg)
|
| 955 | {
|
| 956 | Keymap curr_map, cmd_map;
|
| 957 |
|
| 958 | curr_map = rl_get_keymap();
|
| 959 | cmd_map = _get_associated_cmd_map(curr_map);
|
| 960 |
|
| 961 | if (cmd_map == NULL) {
|
| 962 | PyErr_SetString(PyExc_ValueError, "Could not get shell command map for current keymap");
|
| 963 | return NULL;
|
| 964 | }
|
| 965 |
|
| 966 | rl_set_keymap(cmd_map);
|
| 967 | rl_macro_dumper(1);
|
| 968 | rl_set_keymap(curr_map);
|
| 969 |
|
| 970 | Py_RETURN_NONE;
|
| 971 | }
|
| 972 |
|
| 973 | PyDoc_STRVAR(doc_print_shell_cmd_map,
|
| 974 | "print_shell_cmd_map() -> None\n\
|
| 975 | Print all bindings for shell commands in the current keymap.");
|
| 976 |
|
| 977 |
|
| 978 | /* Support fns for bind -x */
|
| 979 |
|
| 980 | /*
|
| 981 | Checks to see if a bind -x command altered $READLINE_LINE. Compares the post-command
|
| 982 | $READLINE_LINE (passed as post_line) with the current readline buffer
|
| 983 | contents. If they differ, it replaces the buffer with the new version, and adds the old
|
| 984 | version to the undo stack.
|
| 985 | */
|
| 986 | static void
|
| 987 | update_line_if_needed(char *post_line)
|
| 988 | {
|
| 989 | if (strcmp(post_line, rl_line_buffer) != 0) {
|
| 990 | rl_point = rl_end;
|
| 991 |
|
| 992 | rl_add_undo(UNDO_BEGIN, 0, 0, 0);
|
| 993 | rl_delete_text(0, rl_point);
|
| 994 | rl_point = 0;
|
| 995 | rl_end = 0;
|
| 996 | rl_mark = 0;
|
| 997 | rl_insert_text(post_line);
|
| 998 | rl_add_undo(UNDO_END, 0, 0, 0);
|
| 999 | }
|
| 1000 | }
|
| 1001 |
|
| 1002 |
|
| 1003 | /*
|
| 1004 | Checks to see if a bind -x command altered $READLINE_POINT. Compares the post-command
|
| 1005 | $READLINE_POINT (passed as post_point) with the current readline cursor point.
|
| 1006 | If they differ, it updates the cursor point to the new value.
|
| 1007 | */
|
| 1008 | static void
|
| 1009 | update_cursor_if_needed(int post_point) {
|
| 1010 | if (post_point != rl_point) {
|
| 1011 | rl_point = post_point;
|
| 1012 | if (rl_point > rl_end)
|
| 1013 | rl_point = rl_end;
|
| 1014 | else if (rl_point < 0)
|
| 1015 | rl_point = 0;
|
| 1016 | }
|
| 1017 | }
|
| 1018 |
|
| 1019 | /* Returns the shell command(s) associated with the current key sequence */
|
| 1020 | static char*
|
| 1021 | get_bound_command(Keymap cmd_map) {
|
| 1022 | int type;
|
| 1023 | char *cmd = (char *)rl_function_of_keyseq(rl_executing_keyseq, cmd_map, &type);
|
| 1024 |
|
| 1025 | if (cmd == NULL || type != ISMACR) {
|
| 1026 | PyErr_SetString(PyExc_RuntimeError,
|
| 1027 | "Cannot find shell command bound to this key sequence");
|
| 1028 | return NULL;
|
| 1029 | }
|
| 1030 |
|
| 1031 | return cmd;
|
| 1032 | }
|
| 1033 |
|
| 1034 | static void
|
| 1035 | clear_current_line(int use_ce) {
|
| 1036 | if (use_ce) {
|
| 1037 | rl_clear_visible_line();
|
| 1038 | fflush(rl_outstream);
|
| 1039 | } else {
|
| 1040 | rl_crlf();
|
| 1041 | }
|
| 1042 | }
|
| 1043 |
|
| 1044 |
|
| 1045 | /*
|
| 1046 | Main entry point for executing shell commands. Based on bash_execute_unix_command
|
| 1047 |
|
| 1048 | This function is called when a bind -x command is executed. It retrieves the
|
| 1049 | shell command(s) associated with the current key sequence, handles the
|
| 1050 | actual execution of the command, and updates the line and cursor position as
|
| 1051 | necessary.
|
| 1052 | */
|
| 1053 |
|
| 1054 | static int
|
| 1055 | on_bind_shell_command_hook(int count /* unused */, int key /* unused */) {
|
| 1056 | char *cmd;
|
| 1057 | int use_ce;
|
| 1058 | Keymap cmd_map;
|
| 1059 | PyObject *r = NULL;
|
| 1060 | #ifdef WITH_THREAD
|
| 1061 | PyGILState_STATE gilstate;
|
| 1062 | #endif
|
| 1063 | int cmd_return_code;
|
| 1064 | char *post_line_buffer;
|
| 1065 | int post_point;
|
| 1066 | int result;
|
| 1067 |
|
| 1068 | #ifdef WITH_THREAD
|
| 1069 | gilstate = PyGILState_Ensure();
|
| 1070 | #endif
|
| 1071 |
|
| 1072 | if (bind_shell_command_hook == NULL) {
|
| 1073 | PyErr_SetString(PyExc_RuntimeError, "No bind_shell_command_hook set");
|
| 1074 | return 1;
|
| 1075 | }
|
| 1076 |
|
| 1077 | cmd_map = _get_associated_cmd_map(rl_get_keymap());
|
| 1078 | cmd = get_bound_command(cmd_map);
|
| 1079 | if (!cmd) {
|
| 1080 | PyErr_SetString(PyExc_RuntimeError, "on_bind_shell_command_hook: Cannot find shell command in keymap");
|
| 1081 | rl_crlf();
|
| 1082 | rl_forced_update_display();
|
| 1083 | return 1;
|
| 1084 | }
|
| 1085 |
|
| 1086 | use_ce = rl_get_termcap("ce") != NULL;
|
| 1087 | clear_current_line(use_ce);
|
| 1088 |
|
| 1089 | r = PyObject_CallFunction(bind_shell_command_hook,
|
| 1090 | "ssi", cmd, rl_line_buffer, rl_point);
|
| 1091 | if (r == NULL) {
|
| 1092 | PyErr_Print();
|
| 1093 | result = 1;
|
| 1094 | goto cleanup;
|
| 1095 | }
|
| 1096 | if (!PyArg_ParseTuple(r, "isi", &cmd_return_code, &post_line_buffer, &post_point)) {
|
| 1097 | PyErr_SetString(PyExc_ValueError, "Expected (int, str, int) tuple from bind_shell_command_hook");
|
| 1098 | result = 1;
|
| 1099 | goto cleanup;
|
| 1100 | }
|
| 1101 |
|
| 1102 | update_line_if_needed(post_line_buffer);
|
| 1103 | update_cursor_if_needed(post_point);
|
| 1104 |
|
| 1105 |
|
| 1106 | /* Redraw the prompt */
|
| 1107 | if (use_ce && cmd_return_code != 124) {
|
| 1108 | rl_redraw_prompt_last_line();
|
| 1109 | } else {
|
| 1110 | rl_forced_update_display();
|
| 1111 | }
|
| 1112 |
|
| 1113 | result = 0;
|
| 1114 |
|
| 1115 | cleanup:
|
| 1116 | Py_XDECREF(r);
|
| 1117 | #ifdef WITH_THREAD
|
| 1118 | PyGILState_Release(gilstate);
|
| 1119 | #endif
|
| 1120 |
|
| 1121 | done:
|
| 1122 | return result;
|
| 1123 | }
|
| 1124 |
|
| 1125 |
|
| 1126 | /* Maps a key sequence to arbitrary shell code (not built-in readline fns) */
|
| 1127 | static PyObject*
|
| 1128 | bind_shell_command(PyObject *self, PyObject *args) {
|
| 1129 | const char *kseq;
|
| 1130 | const char *cmd, *cparam;
|
| 1131 | Keymap kmap, cmd_xmap;
|
| 1132 |
|
| 1133 | if (!PyArg_ParseTuple(args, "ss:bind_shell_command", &kseq, &cparam)) {
|
| 1134 | return NULL;
|
| 1135 | }
|
| 1136 |
|
| 1137 | /* readline will own the cmd string, so we need to make a copy */
|
| 1138 | cmd = strdup(cparam);
|
| 1139 | if (cmd == NULL) {
|
| 1140 | return PyErr_NoMemory();
|
| 1141 | }
|
| 1142 |
|
| 1143 | kmap = rl_get_keymap();
|
| 1144 | cmd_xmap = _get_associated_cmd_map(kmap);
|
| 1145 |
|
| 1146 |
|
| 1147 | if (rl_generic_bind(ISMACR, kseq, (char *)cmd, cmd_xmap) != 0
|
| 1148 | || rl_bind_keyseq_in_map (kseq, on_bind_shell_command_hook, kmap) != 0) {
|
| 1149 | PyErr_Format(PyExc_RuntimeError, "Failed to bind key sequence '%s' to command '%s'", kseq, cmd);
|
| 1150 | free(cmd);
|
| 1151 | return NULL;
|
| 1152 | }
|
| 1153 |
|
| 1154 | Py_RETURN_NONE;
|
| 1155 | }
|
| 1156 |
|
| 1157 | PyDoc_STRVAR(doc_bind_shell_command,
|
| 1158 | "bind_shell_command(key_sequence, command) -> None\n\
|
| 1159 | Bind a key sequence to a shell command in the current keymap.");
|
| 1160 |
|
| 1161 |
|
| 1162 | /* Remove all bindings for a given keyseq */
|
| 1163 |
|
| 1164 | int
|
| 1165 | _unbind_shell_cmd(char *keyseq)
|
| 1166 | {
|
| 1167 | /* Unbind a key sequence from the current keymap's associated shell command map. */
|
| 1168 | Keymap cmd_map;
|
| 1169 |
|
| 1170 | cmd_map = _get_associated_cmd_map(rl_get_keymap());
|
| 1171 |
|
| 1172 | if (rl_bind_keyseq_in_map(keyseq, (rl_command_func_t *)NULL, cmd_map) != 0) {
|
| 1173 | PyErr_Format(PyExc_ValueError, "'%s': can't unbind from shell command keymap", keyseq);
|
| 1174 | return 0;
|
| 1175 | }
|
| 1176 |
|
| 1177 | return 1;
|
| 1178 | }
|
| 1179 |
|
| 1180 | static PyObject*
|
| 1181 | unbind_keyseq(PyObject *self, PyObject *args)
|
| 1182 | {
|
| 1183 | char *seq, *keyseq;
|
| 1184 | int kslen, type;
|
| 1185 | rl_command_func_t *fn;
|
| 1186 |
|
| 1187 | if (!PyArg_ParseTuple(args, "s:unbind_keyseq", &seq))
|
| 1188 | return NULL;
|
| 1189 |
|
| 1190 | /* Parse and check validity of keyseq */
|
| 1191 | keyseq = (char *)malloc((2 * strlen(seq)) + 1);
|
| 1192 | if (rl_translate_keyseq(seq, keyseq, &kslen) != 0) {
|
| 1193 | free(keyseq);
|
| 1194 | PyErr_Format(PyExc_ValueError, "'%s': invalid key sequence", seq);
|
| 1195 | Py_RETURN_NONE;
|
| 1196 | }
|
| 1197 |
|
| 1198 | fn = rl_function_of_keyseq(keyseq, (Keymap)NULL, &type);
|
| 1199 | free(keyseq);
|
| 1200 |
|
| 1201 | if (!fn) {
|
| 1202 | Py_RETURN_NONE;
|
| 1203 | }
|
| 1204 |
|
| 1205 | if (type == ISKMAP) {
|
| 1206 | fn = ((Keymap)fn)[ANYOTHERKEY].function;
|
| 1207 | }
|
| 1208 |
|
| 1209 | if (rl_bind_keyseq(seq, (rl_command_func_t *)NULL) != 0) {
|
| 1210 | PyErr_Format(PyExc_ValueError, "'%s': cannot unbind", seq);
|
| 1211 | Py_RETURN_NONE;
|
| 1212 | }
|
| 1213 |
|
| 1214 | if (fn == on_bind_shell_command_hook) {
|
| 1215 | _unbind_shell_cmd (seq);
|
| 1216 | }
|
| 1217 |
|
| 1218 | Py_RETURN_NONE;
|
| 1219 | }
|
| 1220 |
|
| 1221 | PyDoc_STRVAR(doc_unbind_keyseq,
|
| 1222 | "unbind_keyseq(sequence) -> None\n\
|
| 1223 | Unbind a key sequence from the current keymap.");
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 | /* Keymap toggling code */
|
| 1228 | static Keymap orig_keymap = NULL;
|
| 1229 |
|
| 1230 | static PyObject*
|
| 1231 | use_temp_keymap(PyObject *self, PyObject *args)
|
| 1232 | {
|
| 1233 | char *keymap_name;
|
| 1234 | Keymap new_keymap;
|
| 1235 |
|
| 1236 | if (!PyArg_ParseTuple(args, "s:use_temp_keymap", &keymap_name))
|
| 1237 | return NULL;
|
| 1238 |
|
| 1239 | new_keymap = rl_get_keymap_by_name(keymap_name);
|
| 1240 | if (new_keymap == NULL) {
|
| 1241 | PyErr_Format(PyExc_ValueError, "`%s': unknown keymap name", keymap_name);
|
| 1242 | return NULL;
|
| 1243 | }
|
| 1244 |
|
| 1245 | orig_keymap = rl_get_keymap();
|
| 1246 | rl_set_keymap(new_keymap);
|
| 1247 |
|
| 1248 | Py_RETURN_NONE;
|
| 1249 | }
|
| 1250 |
|
| 1251 | PyDoc_STRVAR(doc_use_temp_keymap,
|
| 1252 | "use_temp_keymap(keymap_name) -> None\n\
|
| 1253 | Temporarily switch to named keymap, saving the current one.");
|
| 1254 |
|
| 1255 | static PyObject*
|
| 1256 | restore_orig_keymap(PyObject *self, PyObject *args)
|
| 1257 | {
|
| 1258 | if (orig_keymap != NULL) {
|
| 1259 | rl_set_keymap(orig_keymap);
|
| 1260 | orig_keymap = NULL;
|
| 1261 | }
|
| 1262 |
|
| 1263 | Py_RETURN_NONE;
|
| 1264 | }
|
| 1265 |
|
| 1266 | PyDoc_STRVAR(doc_restore_orig_keymap,
|
| 1267 | "restore_orig_keymap() -> None\n\
|
| 1268 | Restore the previously saved keymap if one exists.");
|
| 1269 |
|
| 1270 | /* Table of functions exported by the module */
|
| 1271 |
|
| 1272 | static struct PyMethodDef readline_methods[] = {
|
| 1273 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
|
| 1274 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
|
| 1275 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
|
| 1276 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
|
| 1277 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
|
| 1278 | {"read_history_file", read_history_file,
|
| 1279 | METH_VARARGS, doc_read_history_file},
|
| 1280 | {"write_history_file", write_history_file,
|
| 1281 | METH_VARARGS, doc_write_history_file},
|
| 1282 | {"get_history_item", get_history_item,
|
| 1283 | METH_VARARGS, doc_get_history_item},
|
| 1284 | {"get_current_history_length", (PyCFunction)get_current_history_length,
|
| 1285 | METH_NOARGS, doc_get_current_history_length},
|
| 1286 | {"set_history_length", set_history_length,
|
| 1287 | METH_VARARGS, set_history_length_doc},
|
| 1288 | {"get_history_length", get_history_length,
|
| 1289 | METH_NOARGS, get_history_length_doc},
|
| 1290 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
|
| 1291 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
|
| 1292 | {"get_completion_type", get_completion_type,
|
| 1293 | METH_NOARGS, doc_get_completion_type},
|
| 1294 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
|
| 1295 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
|
| 1296 |
|
| 1297 | {"set_completer_delims", set_completer_delims,
|
| 1298 | METH_VARARGS, doc_set_completer_delims},
|
| 1299 | {"add_history", py_add_history, METH_VARARGS, doc_add_history},
|
| 1300 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
|
| 1301 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
|
| 1302 | {"get_completer_delims", get_completer_delims,
|
| 1303 | METH_NOARGS, doc_get_completer_delims},
|
| 1304 |
|
| 1305 | {"set_completion_display_matches_hook", set_completion_display_matches_hook,
|
| 1306 | METH_VARARGS, doc_set_completion_display_matches_hook},
|
| 1307 | {"set_startup_hook", set_startup_hook,
|
| 1308 | METH_VARARGS, doc_set_startup_hook},
|
| 1309 | {"set_pre_input_hook", set_pre_input_hook,
|
| 1310 | METH_VARARGS, doc_set_pre_input_hook},
|
| 1311 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
|
| 1312 | {"resize_terminal", py_resize_terminal, METH_NOARGS, ""},
|
| 1313 |
|
| 1314 | /* Functions added to implement the 'bind' builtin in OSH */
|
| 1315 | {"list_funmap_names", list_funmap_names, METH_NOARGS, doc_list_funmap_names},
|
| 1316 | {"function_dumper", function_dumper, METH_VARARGS, doc_list_function_dumper},
|
| 1317 | {"macro_dumper", macro_dumper, METH_VARARGS, doc_list_macro_dumper},
|
| 1318 | {"variable_dumper", variable_dumper, METH_VARARGS, doc_list_variable_dumper},
|
| 1319 | {"query_bindings", query_bindings, METH_VARARGS, doc_query_bindings},
|
| 1320 | {"unbind_rl_function", unbind_rl_function, METH_VARARGS, doc_unbind_rl_function},
|
| 1321 | {"use_temp_keymap", use_temp_keymap, METH_VARARGS, doc_use_temp_keymap},
|
| 1322 | {"restore_orig_keymap", restore_orig_keymap, METH_NOARGS, doc_restore_orig_keymap},
|
| 1323 | {"print_shell_cmd_map", print_shell_cmd_map, METH_NOARGS, doc_print_shell_cmd_map},
|
| 1324 | {"unbind_keyseq", unbind_keyseq, METH_VARARGS, doc_unbind_keyseq},
|
| 1325 | {"bind_shell_command", bind_shell_command, METH_VARARGS, doc_bind_shell_command},
|
| 1326 | {"set_bind_shell_command_hook", set_bind_shell_command_hook,
|
| 1327 | METH_VARARGS, doc_set_bind_shell_command_hook},
|
| 1328 | {0, 0}
|
| 1329 | };
|
| 1330 |
|
| 1331 |
|
| 1332 | /* C function to call the Python hooks. */
|
| 1333 |
|
| 1334 | static int
|
| 1335 | on_hook(PyObject *func)
|
| 1336 | {
|
| 1337 | int result = 0;
|
| 1338 | if (func != NULL) {
|
| 1339 | PyObject *r;
|
| 1340 | #ifdef WITH_THREAD
|
| 1341 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
| 1342 | #endif
|
| 1343 | r = PyObject_CallFunction(func, NULL);
|
| 1344 | if (r == NULL)
|
| 1345 | goto error;
|
| 1346 | if (r == Py_None)
|
| 1347 | result = 0;
|
| 1348 | else {
|
| 1349 | result = PyInt_AsLong(r);
|
| 1350 | if (result == -1 && PyErr_Occurred())
|
| 1351 | goto error;
|
| 1352 | }
|
| 1353 | Py_DECREF(r);
|
| 1354 | goto done;
|
| 1355 | error:
|
| 1356 | PyErr_Clear();
|
| 1357 | Py_XDECREF(r);
|
| 1358 | done:
|
| 1359 | #ifdef WITH_THREAD
|
| 1360 | PyGILState_Release(gilstate);
|
| 1361 | #endif
|
| 1362 | return result;
|
| 1363 | }
|
| 1364 | return result;
|
| 1365 | }
|
| 1366 |
|
| 1367 | static int
|
| 1368 | #if defined(_RL_FUNCTION_TYPEDEF)
|
| 1369 | on_startup_hook(void)
|
| 1370 | #else
|
| 1371 | on_startup_hook()
|
| 1372 | #endif
|
| 1373 | {
|
| 1374 | return on_hook(startup_hook);
|
| 1375 | }
|
| 1376 |
|
| 1377 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
| 1378 | static int
|
| 1379 | #if defined(_RL_FUNCTION_TYPEDEF)
|
| 1380 | on_pre_input_hook(void)
|
| 1381 | #else
|
| 1382 | on_pre_input_hook()
|
| 1383 | #endif
|
| 1384 | {
|
| 1385 | return on_hook(pre_input_hook);
|
| 1386 | }
|
| 1387 | #endif
|
| 1388 |
|
| 1389 |
|
| 1390 | /* C function to call the Python completion_display_matches */
|
| 1391 |
|
| 1392 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
| 1393 | static void
|
| 1394 | on_completion_display_matches_hook(char **matches,
|
| 1395 | int num_matches, int max_length)
|
| 1396 | {
|
| 1397 | int i;
|
| 1398 | PyObject *m=NULL, *s=NULL, *r=NULL;
|
| 1399 | #ifdef WITH_THREAD
|
| 1400 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
| 1401 | #endif
|
| 1402 | m = PyList_New(num_matches);
|
| 1403 | if (m == NULL)
|
| 1404 | goto error;
|
| 1405 | for (i = 0; i < num_matches; i++) {
|
| 1406 | s = PyString_FromString(matches[i+1]);
|
| 1407 | if (s == NULL)
|
| 1408 | goto error;
|
| 1409 | if (PyList_SetItem(m, i, s) == -1)
|
| 1410 | goto error;
|
| 1411 | }
|
| 1412 |
|
| 1413 | r = PyObject_CallFunction(completion_display_matches_hook,
|
| 1414 | "sOi", matches[0], m, max_length);
|
| 1415 |
|
| 1416 | Py_DECREF(m); m=NULL;
|
| 1417 |
|
| 1418 | if (r == NULL ||
|
| 1419 | (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
|
| 1420 | goto error;
|
| 1421 | }
|
| 1422 | Py_XDECREF(r); r=NULL;
|
| 1423 |
|
| 1424 | if (0) {
|
| 1425 | error:
|
| 1426 | PyErr_Clear();
|
| 1427 | Py_XDECREF(m);
|
| 1428 | Py_XDECREF(r);
|
| 1429 | }
|
| 1430 | #ifdef WITH_THREAD
|
| 1431 | PyGILState_Release(gilstate);
|
| 1432 | #endif
|
| 1433 | }
|
| 1434 |
|
| 1435 | #endif
|
| 1436 |
|
| 1437 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
| 1438 | static volatile sig_atomic_t sigwinch_received;
|
| 1439 | static PyOS_sighandler_t sigwinch_ohandler;
|
| 1440 |
|
| 1441 | static void
|
| 1442 | readline_sigwinch_handler(int signum)
|
| 1443 | {
|
| 1444 | sigwinch_received = 1;
|
| 1445 | if (sigwinch_ohandler &&
|
| 1446 | sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
|
| 1447 | sigwinch_ohandler(signum);
|
| 1448 | }
|
| 1449 | #endif
|
| 1450 |
|
| 1451 | /* C function to call the Python completer. */
|
| 1452 |
|
| 1453 | static char *
|
| 1454 | on_completion(const char *text, int state)
|
| 1455 | {
|
| 1456 | char *result = NULL;
|
| 1457 | if (completer != NULL) {
|
| 1458 | PyObject *r;
|
| 1459 | #ifdef WITH_THREAD
|
| 1460 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
| 1461 | #endif
|
| 1462 | rl_attempted_completion_over = 1;
|
| 1463 | r = PyObject_CallFunction(completer, "si", text, state);
|
| 1464 | if (r == NULL)
|
| 1465 | goto error;
|
| 1466 | if (r == Py_None) {
|
| 1467 | result = NULL;
|
| 1468 | }
|
| 1469 | else {
|
| 1470 | char *s = PyString_AsString(r);
|
| 1471 | if (s == NULL)
|
| 1472 | goto error;
|
| 1473 | result = strdup(s);
|
| 1474 | }
|
| 1475 | Py_DECREF(r);
|
| 1476 | goto done;
|
| 1477 | error:
|
| 1478 | PyErr_Clear();
|
| 1479 | Py_XDECREF(r);
|
| 1480 | done:
|
| 1481 | #ifdef WITH_THREAD
|
| 1482 | PyGILState_Release(gilstate);
|
| 1483 | #endif
|
| 1484 | return result;
|
| 1485 | }
|
| 1486 | return result;
|
| 1487 | }
|
| 1488 |
|
| 1489 |
|
| 1490 | /* A more flexible constructor that saves the "begidx" and "endidx"
|
| 1491 | * before calling the normal completer */
|
| 1492 |
|
| 1493 | static char **
|
| 1494 | flex_complete(const char *text, int start, int end)
|
| 1495 | {
|
| 1496 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
| 1497 | rl_completion_append_character ='\0';
|
| 1498 | #endif
|
| 1499 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
|
| 1500 | rl_completion_suppress_append = 0;
|
| 1501 | #endif
|
| 1502 | Py_XDECREF(begidx);
|
| 1503 | Py_XDECREF(endidx);
|
| 1504 | begidx = PyInt_FromLong((long) start);
|
| 1505 | endidx = PyInt_FromLong((long) end);
|
| 1506 | return completion_matches(text, *on_completion);
|
| 1507 | }
|
| 1508 |
|
| 1509 |
|
| 1510 | /* Helper to initialize GNU readline properly. */
|
| 1511 |
|
| 1512 | static void
|
| 1513 | setup_readline(void)
|
| 1514 | {
|
| 1515 | #ifdef SAVE_LOCALE
|
| 1516 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
| 1517 | if (!saved_locale)
|
| 1518 | Py_FatalError("not enough memory to save locale");
|
| 1519 | #endif
|
| 1520 |
|
| 1521 | #ifdef __APPLE__
|
| 1522 | /* the libedit readline emulation resets key bindings etc
|
| 1523 | * when calling rl_initialize. So call it upfront
|
| 1524 | */
|
| 1525 | if (using_libedit_emulation)
|
| 1526 | rl_initialize();
|
| 1527 |
|
| 1528 | /* Detect if libedit's readline emulation uses 0-based
|
| 1529 | * indexing or 1-based indexing.
|
| 1530 | */
|
| 1531 | add_history("1");
|
| 1532 | if (history_get(1) == NULL) {
|
| 1533 | libedit_history_start = 0;
|
| 1534 | } else {
|
| 1535 | libedit_history_start = 1;
|
| 1536 | }
|
| 1537 | clear_history();
|
| 1538 | #endif /* __APPLE__ */
|
| 1539 |
|
| 1540 | using_history();
|
| 1541 |
|
| 1542 | rl_readline_name = "oils";
|
| 1543 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
| 1544 | /* Allow $if term= in .inputrc to work */
|
| 1545 | rl_terminal_name = getenv("TERM");
|
| 1546 | #endif
|
| 1547 | /* Force rebind of TAB to insert-tab */
|
| 1548 | rl_bind_key('\t', rl_insert);
|
| 1549 | /* Bind both ESC-TAB and ESC-ESC to the completion function */
|
| 1550 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
|
| 1551 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
|
| 1552 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
| 1553 | /* Set up signal handler for window resize */
|
| 1554 | sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
|
| 1555 | #endif
|
| 1556 | /* Set our hook functions */
|
| 1557 | rl_startup_hook = on_startup_hook;
|
| 1558 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
| 1559 | rl_pre_input_hook = on_pre_input_hook;
|
| 1560 | #endif
|
| 1561 | /* Set our completion function */
|
| 1562 | rl_attempted_completion_function = flex_complete;
|
| 1563 | /* Set Python word break characters */
|
| 1564 | completer_word_break_characters =
|
| 1565 | rl_completer_word_break_characters =
|
| 1566 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
|
| 1567 | /* All nonalphanums except '.' */
|
| 1568 |
|
| 1569 | begidx = PyInt_FromLong(0L);
|
| 1570 | endidx = PyInt_FromLong(0L);
|
| 1571 |
|
| 1572 | #ifdef __APPLE__
|
| 1573 | if (!using_libedit_emulation)
|
| 1574 | #endif
|
| 1575 | {
|
| 1576 | if (!isatty(STDOUT_FILENO)) {
|
| 1577 | /* Issue #19884: stdout is not a terminal. Disable meta modifier
|
| 1578 | keys to not write the ANSI sequence "\033[1034h" into stdout. On
|
| 1579 | terminals supporting 8 bit characters like TERM=xterm-256color
|
| 1580 | (which is now the default Fedora since Fedora 18), the meta key is
|
| 1581 | used to enable support of 8 bit characters (ANSI sequence
|
| 1582 | "\033[1034h").
|
| 1583 |
|
| 1584 | With libedit, this call makes readline() crash. */
|
| 1585 | rl_variable_bind ("enable-meta-key", "off");
|
| 1586 | }
|
| 1587 | }
|
| 1588 |
|
| 1589 | /* Initialize (allows .inputrc to override)
|
| 1590 | *
|
| 1591 | * XXX: A bug in the readline-2.2 library causes a memory leak
|
| 1592 | * inside this function. Nothing we can do about it.
|
| 1593 | */
|
| 1594 | #ifdef __APPLE__
|
| 1595 | if (using_libedit_emulation)
|
| 1596 | rl_read_init_file(NULL);
|
| 1597 | else
|
| 1598 | #endif /* __APPLE__ */
|
| 1599 | rl_initialize();
|
| 1600 |
|
| 1601 | RESTORE_LOCALE(saved_locale)
|
| 1602 | }
|
| 1603 |
|
| 1604 | /* Wrapper around GNU readline that handles signals differently. */
|
| 1605 |
|
| 1606 |
|
| 1607 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
|
| 1608 |
|
| 1609 | static char *completed_input_string;
|
| 1610 | static void
|
| 1611 | rlhandler(char *text)
|
| 1612 | {
|
| 1613 | completed_input_string = text;
|
| 1614 | rl_callback_handler_remove();
|
| 1615 | }
|
| 1616 |
|
| 1617 | static char *
|
| 1618 | readline_until_enter_or_signal(char *prompt, int *signal)
|
| 1619 | {
|
| 1620 | char * not_done_reading = "";
|
| 1621 | fd_set selectset;
|
| 1622 |
|
| 1623 | *signal = 0;
|
| 1624 | #ifdef HAVE_RL_CATCH_SIGNAL
|
| 1625 | rl_catch_signals = 0;
|
| 1626 | #endif
|
| 1627 | /* OVM_MAIN: Oil is handling SIGWINCH, so readline shouldn't handle it.
|
| 1628 | * Without this line, strace reveals that GNU readline is constantly
|
| 1629 | * turning it on and off.
|
| 1630 | * */
|
| 1631 | rl_catch_sigwinch = 0;
|
| 1632 |
|
| 1633 | rl_callback_handler_install (prompt, rlhandler);
|
| 1634 | FD_ZERO(&selectset);
|
| 1635 |
|
| 1636 | completed_input_string = not_done_reading;
|
| 1637 |
|
| 1638 | while (completed_input_string == not_done_reading) {
|
| 1639 | int has_input = 0;
|
| 1640 |
|
| 1641 | while (!has_input)
|
| 1642 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
|
| 1643 |
|
| 1644 | /* [Bug #1552726] Only limit the pause if an input hook has been
|
| 1645 | defined. */
|
| 1646 | struct timeval *timeoutp = NULL;
|
| 1647 | if (PyOS_InputHook)
|
| 1648 | timeoutp = &timeout;
|
| 1649 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
| 1650 | /* Update readline's view of the window size after SIGWINCH */
|
| 1651 | if (sigwinch_received) {
|
| 1652 | sigwinch_received = 0;
|
| 1653 | rl_resize_terminal();
|
| 1654 | }
|
| 1655 | #endif
|
| 1656 | FD_SET(fileno(rl_instream), &selectset);
|
| 1657 | /* select resets selectset if no input was available */
|
| 1658 | has_input = select(fileno(rl_instream) + 1, &selectset,
|
| 1659 | NULL, NULL, timeoutp);
|
| 1660 | if(PyOS_InputHook) PyOS_InputHook();
|
| 1661 | }
|
| 1662 |
|
| 1663 | if(has_input > 0) {
|
| 1664 | rl_callback_read_char();
|
| 1665 | }
|
| 1666 | else if (errno == EINTR) {
|
| 1667 | int s;
|
| 1668 | #ifdef WITH_THREAD
|
| 1669 | PyEval_RestoreThread(_PyOS_ReadlineTState);
|
| 1670 | #endif
|
| 1671 | s = PyErr_CheckSignals();
|
| 1672 | #ifdef WITH_THREAD
|
| 1673 | PyEval_SaveThread();
|
| 1674 | #endif
|
| 1675 | if (s < 0) {
|
| 1676 | rl_free_line_state();
|
| 1677 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
|
| 1678 | rl_callback_sigcleanup();
|
| 1679 | #endif
|
| 1680 | rl_cleanup_after_signal();
|
| 1681 | rl_callback_handler_remove();
|
| 1682 | *signal = 1;
|
| 1683 | completed_input_string = NULL;
|
| 1684 | }
|
| 1685 | }
|
| 1686 | }
|
| 1687 |
|
| 1688 | return completed_input_string;
|
| 1689 | }
|
| 1690 |
|
| 1691 |
|
| 1692 | #else
|
| 1693 |
|
| 1694 | /* Interrupt handler */
|
| 1695 |
|
| 1696 | static jmp_buf jbuf;
|
| 1697 |
|
| 1698 | /* ARGSUSED */
|
| 1699 | static void
|
| 1700 | onintr(int sig)
|
| 1701 | {
|
| 1702 | longjmp(jbuf, 1);
|
| 1703 | }
|
| 1704 |
|
| 1705 |
|
| 1706 | static char *
|
| 1707 | readline_until_enter_or_signal(char *prompt, int *signal)
|
| 1708 | {
|
| 1709 | PyOS_sighandler_t old_inthandler;
|
| 1710 | char *p;
|
| 1711 |
|
| 1712 | *signal = 0;
|
| 1713 |
|
| 1714 | old_inthandler = PyOS_setsig(SIGINT, onintr);
|
| 1715 | if (setjmp(jbuf)) {
|
| 1716 | #ifdef HAVE_SIGRELSE
|
| 1717 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
|
| 1718 | sigrelse(SIGINT);
|
| 1719 | #endif
|
| 1720 | PyOS_setsig(SIGINT, old_inthandler);
|
| 1721 | *signal = 1;
|
| 1722 | return NULL;
|
| 1723 | }
|
| 1724 | rl_event_hook = PyOS_InputHook;
|
| 1725 | p = readline(prompt);
|
| 1726 | PyOS_setsig(SIGINT, old_inthandler);
|
| 1727 |
|
| 1728 | return p;
|
| 1729 | }
|
| 1730 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
|
| 1731 |
|
| 1732 |
|
| 1733 | static char *
|
| 1734 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
| 1735 | {
|
| 1736 | size_t n;
|
| 1737 | char *p, *q;
|
| 1738 | int signal;
|
| 1739 |
|
| 1740 | #ifdef SAVE_LOCALE
|
| 1741 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
| 1742 | if (!saved_locale)
|
| 1743 | Py_FatalError("not enough memory to save locale");
|
| 1744 | setlocale(LC_CTYPE, "");
|
| 1745 | #endif
|
| 1746 |
|
| 1747 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
|
| 1748 | rl_instream = sys_stdin;
|
| 1749 | rl_outstream = sys_stdout;
|
| 1750 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
| 1751 | rl_prep_terminal (1);
|
| 1752 | #endif
|
| 1753 | }
|
| 1754 |
|
| 1755 | p = readline_until_enter_or_signal(prompt, &signal);
|
| 1756 |
|
| 1757 | /* we got an interrupt signal */
|
| 1758 | if (signal) {
|
| 1759 | RESTORE_LOCALE(saved_locale)
|
| 1760 | return NULL;
|
| 1761 | }
|
| 1762 |
|
| 1763 | /* We got an EOF, return an empty string. */
|
| 1764 | if (p == NULL) {
|
| 1765 | p = PyMem_Malloc(1);
|
| 1766 | if (p != NULL)
|
| 1767 | *p = '\0';
|
| 1768 | RESTORE_LOCALE(saved_locale)
|
| 1769 | return p;
|
| 1770 | }
|
| 1771 |
|
| 1772 | /* we have a valid line */
|
| 1773 | n = strlen(p);
|
| 1774 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
|
| 1775 | release the original. */
|
| 1776 | q = p;
|
| 1777 | p = PyMem_Malloc(n+2);
|
| 1778 | if (p != NULL) {
|
| 1779 | strncpy(p, q, n);
|
| 1780 | p[n] = '\n';
|
| 1781 | p[n+1] = '\0';
|
| 1782 | }
|
| 1783 | free(q);
|
| 1784 | RESTORE_LOCALE(saved_locale)
|
| 1785 | return p;
|
| 1786 | }
|
| 1787 |
|
| 1788 |
|
| 1789 | /* Initialize the module */
|
| 1790 |
|
| 1791 | PyDoc_STRVAR(doc_module,
|
| 1792 | "Importing this module enables command line editing using GNU readline.");
|
| 1793 |
|
| 1794 | #ifdef __APPLE__
|
| 1795 | PyDoc_STRVAR(doc_module_le,
|
| 1796 | "Importing this module enables command line editing using libedit readline.");
|
| 1797 | #endif /* __APPLE__ */
|
| 1798 |
|
| 1799 | PyMODINIT_FUNC
|
| 1800 | initline_input(void)
|
| 1801 | {
|
| 1802 | PyObject *m;
|
| 1803 |
|
| 1804 | #ifdef __APPLE__
|
| 1805 | if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
|
| 1806 | using_libedit_emulation = 1;
|
| 1807 | }
|
| 1808 |
|
| 1809 | if (using_libedit_emulation)
|
| 1810 | m = Py_InitModule4("line_input", readline_methods, doc_module_le,
|
| 1811 | (PyObject *)NULL, PYTHON_API_VERSION);
|
| 1812 | else
|
| 1813 |
|
| 1814 | #endif /* __APPLE__ */
|
| 1815 |
|
| 1816 | m = Py_InitModule4("line_input", readline_methods, doc_module,
|
| 1817 | (PyObject *)NULL, PYTHON_API_VERSION);
|
| 1818 | if (m == NULL)
|
| 1819 | return;
|
| 1820 |
|
| 1821 | PyOS_ReadlineFunctionPointer = call_readline;
|
| 1822 | setup_readline();
|
| 1823 |
|
| 1824 | PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
|
| 1825 | PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
|
| 1826 | }
|