| 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 |
|
| 277 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
| 278 | static PyObject *pre_input_hook = NULL;
|
| 279 | #endif
|
| 280 |
|
| 281 | static PyObject *
|
| 282 | set_completion_display_matches_hook(PyObject *self, PyObject *args)
|
| 283 | {
|
| 284 | PyObject *result = set_hook("completion_display_matches_hook",
|
| 285 | &completion_display_matches_hook, args);
|
| 286 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
| 287 | /* We cannot set this hook globally, since it replaces the
|
| 288 | default completion display. */
|
| 289 | rl_completion_display_matches_hook =
|
| 290 | completion_display_matches_hook ?
|
| 291 | #if defined(_RL_FUNCTION_TYPEDEF)
|
| 292 | (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
|
| 293 | #else
|
| 294 | (VFunction *)on_completion_display_matches_hook : 0;
|
| 295 | #endif
|
| 296 | #endif
|
| 297 | return result;
|
| 298 |
|
| 299 | }
|
| 300 |
|
| 301 | PyDoc_STRVAR(doc_set_completion_display_matches_hook,
|
| 302 | "set_completion_display_matches_hook([function]) -> None\n\
|
| 303 | Set or remove the completion display function.\n\
|
| 304 | The function is called as\n\
|
| 305 | function(substitution, [matches], longest_match_length)\n\
|
| 306 | once each time matches need to be displayed.");
|
| 307 |
|
| 308 | static PyObject *
|
| 309 | set_startup_hook(PyObject *self, PyObject *args)
|
| 310 | {
|
| 311 | return set_hook("startup_hook", &startup_hook, args);
|
| 312 | }
|
| 313 |
|
| 314 | PyDoc_STRVAR(doc_set_startup_hook,
|
| 315 | "set_startup_hook([function]) -> None\n\
|
| 316 | Set or remove the function invoked by the rl_startup_hook callback.\n\
|
| 317 | The function is called with no arguments just\n\
|
| 318 | before readline prints the first prompt.");
|
| 319 |
|
| 320 |
|
| 321 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
| 322 |
|
| 323 | /* Set pre-input hook */
|
| 324 |
|
| 325 | static PyObject *
|
| 326 | set_pre_input_hook(PyObject *self, PyObject *args)
|
| 327 | {
|
| 328 | return set_hook("pre_input_hook", &pre_input_hook, args);
|
| 329 | }
|
| 330 |
|
| 331 | PyDoc_STRVAR(doc_set_pre_input_hook,
|
| 332 | "set_pre_input_hook([function]) -> None\n\
|
| 333 | Set or remove the function invoked by the rl_pre_input_hook callback.\n\
|
| 334 | The function is called with no arguments after the first prompt\n\
|
| 335 | has been printed and just before readline starts reading input\n\
|
| 336 | characters.");
|
| 337 |
|
| 338 | #endif
|
| 339 |
|
| 340 |
|
| 341 | /* Exported function to specify a word completer in Python */
|
| 342 |
|
| 343 | static PyObject *completer = NULL;
|
| 344 |
|
| 345 | static PyObject *begidx = NULL;
|
| 346 | static PyObject *endidx = NULL;
|
| 347 |
|
| 348 |
|
| 349 | /* Get the completion type for the scope of the tab-completion */
|
| 350 | static PyObject *
|
| 351 | get_completion_type(PyObject *self, PyObject *noarg)
|
| 352 | {
|
| 353 | return PyInt_FromLong(rl_completion_type);
|
| 354 | }
|
| 355 |
|
| 356 | PyDoc_STRVAR(doc_get_completion_type,
|
| 357 | "get_completion_type() -> int\n\
|
| 358 | Get the type of completion being attempted.");
|
| 359 |
|
| 360 |
|
| 361 | /* Get the beginning index for the scope of the tab-completion */
|
| 362 |
|
| 363 | static PyObject *
|
| 364 | get_begidx(PyObject *self, PyObject *noarg)
|
| 365 | {
|
| 366 | Py_INCREF(begidx);
|
| 367 | return begidx;
|
| 368 | }
|
| 369 |
|
| 370 | PyDoc_STRVAR(doc_get_begidx,
|
| 371 | "get_begidx() -> int\n\
|
| 372 | get the beginning index of the completion scope");
|
| 373 |
|
| 374 |
|
| 375 | /* Get the ending index for the scope of the tab-completion */
|
| 376 |
|
| 377 | static PyObject *
|
| 378 | get_endidx(PyObject *self, PyObject *noarg)
|
| 379 | {
|
| 380 | Py_INCREF(endidx);
|
| 381 | return endidx;
|
| 382 | }
|
| 383 |
|
| 384 | PyDoc_STRVAR(doc_get_endidx,
|
| 385 | "get_endidx() -> int\n\
|
| 386 | get the ending index of the completion scope");
|
| 387 |
|
| 388 |
|
| 389 | /* Set the tab-completion word-delimiters that readline uses */
|
| 390 |
|
| 391 | static PyObject *
|
| 392 | set_completer_delims(PyObject *self, PyObject *args)
|
| 393 | {
|
| 394 | char *break_chars;
|
| 395 |
|
| 396 | if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
|
| 397 | return NULL;
|
| 398 | }
|
| 399 | /* Keep a reference to the allocated memory in the module state in case
|
| 400 | some other module modifies rl_completer_word_break_characters
|
| 401 | (see issue #17289). */
|
| 402 | break_chars = strdup(break_chars);
|
| 403 | if (break_chars) {
|
| 404 | free(completer_word_break_characters);
|
| 405 | completer_word_break_characters = break_chars;
|
| 406 | rl_completer_word_break_characters = break_chars;
|
| 407 | Py_RETURN_NONE;
|
| 408 | }
|
| 409 | else
|
| 410 | return PyErr_NoMemory();
|
| 411 | }
|
| 412 |
|
| 413 | PyDoc_STRVAR(doc_set_completer_delims,
|
| 414 | "set_completer_delims(string) -> None\n\
|
| 415 | set the word delimiters for completion");
|
| 416 |
|
| 417 | /* _py_free_history_entry: Utility function to free a history entry. */
|
| 418 |
|
| 419 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
|
| 420 |
|
| 421 | /* Readline version >= 5.0 introduced a timestamp field into the history entry
|
| 422 | structure; this needs to be freed to avoid a memory leak. This version of
|
| 423 | readline also introduced the handy 'free_history_entry' function, which
|
| 424 | takes care of the timestamp. */
|
| 425 |
|
| 426 | static void
|
| 427 | _py_free_history_entry(HIST_ENTRY *entry)
|
| 428 | {
|
| 429 | histdata_t data = free_history_entry(entry);
|
| 430 | free(data);
|
| 431 | }
|
| 432 |
|
| 433 | #else
|
| 434 |
|
| 435 | /* No free_history_entry function; free everything manually. */
|
| 436 |
|
| 437 | static void
|
| 438 | _py_free_history_entry(HIST_ENTRY *entry)
|
| 439 | {
|
| 440 | if (entry->line)
|
| 441 | free((void *)entry->line);
|
| 442 | if (entry->data)
|
| 443 | free(entry->data);
|
| 444 | free(entry);
|
| 445 | }
|
| 446 |
|
| 447 | #endif
|
| 448 |
|
| 449 | static PyObject *
|
| 450 | py_remove_history(PyObject *self, PyObject *args)
|
| 451 | {
|
| 452 | int entry_number;
|
| 453 | HIST_ENTRY *entry;
|
| 454 |
|
| 455 | if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
|
| 456 | return NULL;
|
| 457 | if (entry_number < 0) {
|
| 458 | PyErr_SetString(PyExc_ValueError,
|
| 459 | "History index cannot be negative");
|
| 460 | return NULL;
|
| 461 | }
|
| 462 | entry = remove_history(entry_number);
|
| 463 | if (!entry) {
|
| 464 | PyErr_Format(PyExc_ValueError,
|
| 465 | "No history item at position %d",
|
| 466 | entry_number);
|
| 467 | return NULL;
|
| 468 | }
|
| 469 | /* free memory allocated for the history entry */
|
| 470 | _py_free_history_entry(entry);
|
| 471 | Py_RETURN_NONE;
|
| 472 | }
|
| 473 |
|
| 474 | PyDoc_STRVAR(doc_remove_history,
|
| 475 | "remove_history_item(pos) -> None\n\
|
| 476 | remove history item given by its position");
|
| 477 |
|
| 478 | static PyObject *
|
| 479 | py_replace_history(PyObject *self, PyObject *args)
|
| 480 | {
|
| 481 | int entry_number;
|
| 482 | char *line;
|
| 483 | HIST_ENTRY *old_entry;
|
| 484 |
|
| 485 | if (!PyArg_ParseTuple(args, "is:replace_history_item", &entry_number,
|
| 486 | &line)) {
|
| 487 | return NULL;
|
| 488 | }
|
| 489 | if (entry_number < 0) {
|
| 490 | PyErr_SetString(PyExc_ValueError,
|
| 491 | "History index cannot be negative");
|
| 492 | return NULL;
|
| 493 | }
|
| 494 | old_entry = replace_history_entry(entry_number, line, (void *)NULL);
|
| 495 | if (!old_entry) {
|
| 496 | PyErr_Format(PyExc_ValueError,
|
| 497 | "No history item at position %d",
|
| 498 | entry_number);
|
| 499 | return NULL;
|
| 500 | }
|
| 501 | /* free memory allocated for the old history entry */
|
| 502 | _py_free_history_entry(old_entry);
|
| 503 | Py_RETURN_NONE;
|
| 504 | }
|
| 505 |
|
| 506 | PyDoc_STRVAR(doc_replace_history,
|
| 507 | "replace_history_item(pos, line) -> None\n\
|
| 508 | replaces history item given by its position with contents of line");
|
| 509 |
|
| 510 | /* Add a line to the history buffer */
|
| 511 |
|
| 512 | static PyObject *
|
| 513 | py_add_history(PyObject *self, PyObject *args)
|
| 514 | {
|
| 515 | char *line;
|
| 516 |
|
| 517 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
|
| 518 | return NULL;
|
| 519 | }
|
| 520 | add_history(line);
|
| 521 | Py_RETURN_NONE;
|
| 522 | }
|
| 523 |
|
| 524 | PyDoc_STRVAR(doc_add_history,
|
| 525 | "add_history(string) -> None\n\
|
| 526 | add an item to the history buffer");
|
| 527 |
|
| 528 |
|
| 529 | /* Get the tab-completion word-delimiters that readline uses */
|
| 530 |
|
| 531 | static PyObject *
|
| 532 | get_completer_delims(PyObject *self, PyObject *noarg)
|
| 533 | {
|
| 534 | return PyString_FromString(rl_completer_word_break_characters);
|
| 535 | }
|
| 536 |
|
| 537 | PyDoc_STRVAR(doc_get_completer_delims,
|
| 538 | "get_completer_delims() -> string\n\
|
| 539 | get the word delimiters for completion");
|
| 540 |
|
| 541 |
|
| 542 | /* Set the completer function */
|
| 543 |
|
| 544 | static PyObject *
|
| 545 | set_completer(PyObject *self, PyObject *args)
|
| 546 | {
|
| 547 | return set_hook("completer", &completer, args);
|
| 548 | }
|
| 549 |
|
| 550 | PyDoc_STRVAR(doc_set_completer,
|
| 551 | "set_completer([function]) -> None\n\
|
| 552 | Set or remove the completer function.\n\
|
| 553 | The function is called as function(text, state),\n\
|
| 554 | for state in 0, 1, 2, ..., until it returns a non-string.\n\
|
| 555 | It should return the next possible completion starting with 'text'.");
|
| 556 |
|
| 557 |
|
| 558 | static PyObject *
|
| 559 | get_completer(PyObject *self, PyObject *noargs)
|
| 560 | {
|
| 561 | if (completer == NULL) {
|
| 562 | Py_RETURN_NONE;
|
| 563 | }
|
| 564 | Py_INCREF(completer);
|
| 565 | return completer;
|
| 566 | }
|
| 567 |
|
| 568 | PyDoc_STRVAR(doc_get_completer,
|
| 569 | "get_completer() -> function\n\
|
| 570 | \n\
|
| 571 | Returns current completer function.");
|
| 572 |
|
| 573 | /* Private function to get current length of history. XXX It may be
|
| 574 | * possible to replace this with a direct use of history_length instead,
|
| 575 | * but it's not clear whether BSD's libedit keeps history_length up to date.
|
| 576 | * See issue #8065.*/
|
| 577 |
|
| 578 | static int
|
| 579 | _py_get_history_length(void)
|
| 580 | {
|
| 581 | HISTORY_STATE *hist_st = history_get_history_state();
|
| 582 | int length = hist_st->length;
|
| 583 | /* the history docs don't say so, but the address of hist_st changes each
|
| 584 | time history_get_history_state is called which makes me think it's
|
| 585 | freshly malloc'd memory... on the other hand, the address of the last
|
| 586 | line stays the same as long as history isn't extended, so it appears to
|
| 587 | be malloc'd but managed by the history package... */
|
| 588 | free(hist_st);
|
| 589 | return length;
|
| 590 | }
|
| 591 |
|
| 592 | /* Exported function to get any element of history */
|
| 593 |
|
| 594 | static PyObject *
|
| 595 | get_history_item(PyObject *self, PyObject *args)
|
| 596 | {
|
| 597 | int idx = 0;
|
| 598 | HIST_ENTRY *hist_ent;
|
| 599 |
|
| 600 | if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
|
| 601 | return NULL;
|
| 602 | #ifdef __APPLE__
|
| 603 | if (using_libedit_emulation) {
|
| 604 | /* Older versions of libedit's readline emulation
|
| 605 | * use 0-based indexes, while readline and newer
|
| 606 | * versions of libedit use 1-based indexes.
|
| 607 | */
|
| 608 | int length = _py_get_history_length();
|
| 609 |
|
| 610 | idx = idx - 1 + libedit_history_start;
|
| 611 |
|
| 612 | /*
|
| 613 | * Apple's readline emulation crashes when
|
| 614 | * the index is out of range, therefore
|
| 615 | * test for that and fail gracefully.
|
| 616 | */
|
| 617 | if (idx < (0 + libedit_history_start)
|
| 618 | || idx >= (length + libedit_history_start)) {
|
| 619 | Py_RETURN_NONE;
|
| 620 | }
|
| 621 | }
|
| 622 | #endif /* __APPLE__ */
|
| 623 | if ((hist_ent = history_get(idx)))
|
| 624 | return PyString_FromString(hist_ent->line);
|
| 625 | else {
|
| 626 | Py_RETURN_NONE;
|
| 627 | }
|
| 628 | }
|
| 629 |
|
| 630 | PyDoc_STRVAR(doc_get_history_item,
|
| 631 | "get_history_item() -> string\n\
|
| 632 | return the current contents of history item at index.");
|
| 633 |
|
| 634 |
|
| 635 | /* Exported function to get current length of history */
|
| 636 |
|
| 637 | static PyObject *
|
| 638 | get_current_history_length(PyObject *self, PyObject *noarg)
|
| 639 | {
|
| 640 | return PyInt_FromLong((long)_py_get_history_length());
|
| 641 | }
|
| 642 |
|
| 643 | PyDoc_STRVAR(doc_get_current_history_length,
|
| 644 | "get_current_history_length() -> integer\n\
|
| 645 | return the current (not the maximum) length of history.");
|
| 646 |
|
| 647 |
|
| 648 | /* Exported function to read the current line buffer */
|
| 649 |
|
| 650 | static PyObject *
|
| 651 | get_line_buffer(PyObject *self, PyObject *noarg)
|
| 652 | {
|
| 653 | return PyString_FromString(rl_line_buffer);
|
| 654 | }
|
| 655 |
|
| 656 | PyDoc_STRVAR(doc_get_line_buffer,
|
| 657 | "get_line_buffer() -> string\n\
|
| 658 | return the current contents of the line buffer.");
|
| 659 |
|
| 660 |
|
| 661 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
| 662 |
|
| 663 | /* Exported function to clear the current history */
|
| 664 |
|
| 665 | static PyObject *
|
| 666 | py_clear_history(PyObject *self, PyObject *noarg)
|
| 667 | {
|
| 668 | clear_history();
|
| 669 | Py_RETURN_NONE;
|
| 670 | }
|
| 671 |
|
| 672 | PyDoc_STRVAR(doc_clear_history,
|
| 673 | "clear_history() -> None\n\
|
| 674 | Clear the current readline history.");
|
| 675 | #endif
|
| 676 |
|
| 677 | /* Added for OSH. We need to call this in our SIGWINCH handler so global
|
| 678 | * variables in readline get updated. */
|
| 679 | static PyObject *
|
| 680 | py_resize_terminal(PyObject *self, PyObject *noarg)
|
| 681 | {
|
| 682 | rl_resize_terminal();
|
| 683 | Py_RETURN_NONE;
|
| 684 | }
|
| 685 |
|
| 686 | /* Exported function to insert text into the line buffer */
|
| 687 |
|
| 688 | static PyObject *
|
| 689 | insert_text(PyObject *self, PyObject *args)
|
| 690 | {
|
| 691 | char *s;
|
| 692 | if (!PyArg_ParseTuple(args, "s:insert_text", &s))
|
| 693 | return NULL;
|
| 694 | rl_insert_text(s);
|
| 695 | Py_RETURN_NONE;
|
| 696 | }
|
| 697 |
|
| 698 | PyDoc_STRVAR(doc_insert_text,
|
| 699 | "insert_text(string) -> None\n\
|
| 700 | Insert text into the line buffer at the cursor position.");
|
| 701 |
|
| 702 |
|
| 703 | /* Redisplay the line buffer */
|
| 704 |
|
| 705 | static PyObject *
|
| 706 | redisplay(PyObject *self, PyObject *noarg)
|
| 707 | {
|
| 708 | rl_redisplay();
|
| 709 | Py_RETURN_NONE;
|
| 710 | }
|
| 711 |
|
| 712 | PyDoc_STRVAR(doc_redisplay,
|
| 713 | "redisplay() -> None\n\
|
| 714 | Change what's displayed on the screen to reflect the current\n\
|
| 715 | contents of the line buffer.");
|
| 716 |
|
| 717 |
|
| 718 | /* Functions added to implement the 'bind' builtin in OSH */
|
| 719 |
|
| 720 | /* -x/-X command keymaps */
|
| 721 | static Keymap emacs_cmd_map;
|
| 722 | static Keymap vi_insert_cmd_map;
|
| 723 | static Keymap vi_movement_cmd_map;
|
| 724 |
|
| 725 | /*
|
| 726 | These forcibly cast between a Keymap* and a rl_command_func_t*. Readline
|
| 727 | uses an additional `.type` field to keep track of the pointer's true type.
|
| 728 | */
|
| 729 | #define RL_KEYMAP_TO_FUNCTION(data) (rl_command_func_t *)(data)
|
| 730 | #define RL_FUNCTION_TO_KEYMAP(map, key) (Keymap)(map[key].function)
|
| 731 |
|
| 732 | static void
|
| 733 | _init_command_maps(void)
|
| 734 | {
|
| 735 | emacs_cmd_map = rl_make_bare_keymap();
|
| 736 | vi_insert_cmd_map = rl_make_bare_keymap();
|
| 737 | vi_movement_cmd_map = rl_make_bare_keymap();
|
| 738 |
|
| 739 | /* Ensure that Esc- and Ctrl-X are also keymaps */
|
| 740 | emacs_cmd_map[CTRL('X')].type = ISKMAP;
|
| 741 | emacs_cmd_map[CTRL('X')].function = RL_KEYMAP_TO_FUNCTION(rl_make_bare_keymap());
|
| 742 | emacs_cmd_map[ESC].type = ISKMAP;
|
| 743 | emacs_cmd_map[ESC].function = RL_KEYMAP_TO_FUNCTION(rl_make_bare_keymap());
|
| 744 | }
|
| 745 |
|
| 746 | static Keymap
|
| 747 | _get_associated_cmd_map(Keymap kmap)
|
| 748 | {
|
| 749 | if (emacs_cmd_map == NULL)
|
| 750 | _init_command_maps();
|
| 751 |
|
| 752 | if (kmap == emacs_standard_keymap)
|
| 753 | return emacs_cmd_map;
|
| 754 | else if (kmap == vi_insertion_keymap)
|
| 755 | return vi_insert_cmd_map;
|
| 756 | else if (kmap == vi_movement_keymap)
|
| 757 | return vi_movement_cmd_map;
|
| 758 | else if (kmap == emacs_meta_keymap)
|
| 759 | return (RL_FUNCTION_TO_KEYMAP(emacs_cmd_map, ESC));
|
| 760 | else if (kmap == emacs_ctlx_keymap)
|
| 761 | return (RL_FUNCTION_TO_KEYMAP(emacs_cmd_map, CTRL('X')));
|
| 762 |
|
| 763 | return (Keymap) NULL;
|
| 764 | }
|
| 765 |
|
| 766 | /* List binding functions */
|
| 767 | static PyObject*
|
| 768 | list_funmap_names(PyObject *self, PyObject *args)
|
| 769 | {
|
| 770 | rl_list_funmap_names();
|
| 771 | // printf ("Compiled w/ readline version: %s\n", rl_library_version ? rl_library_version : "unknown");
|
| 772 | Py_RETURN_NONE;
|
| 773 | }
|
| 774 |
|
| 775 | PyDoc_STRVAR(doc_list_funmap_names,
|
| 776 | "list_funmap_names() -> None\n\
|
| 777 | Print all of the available readline functions.");
|
| 778 |
|
| 779 | /* Print readline functions and their bindings */
|
| 780 |
|
| 781 | static PyObject*
|
| 782 | function_dumper(PyObject *self, PyObject *args)
|
| 783 | {
|
| 784 | int print_readably;
|
| 785 |
|
| 786 | if (!PyArg_ParseTuple(args, "i:function_dumper", &print_readably))
|
| 787 | return NULL;
|
| 788 |
|
| 789 | rl_function_dumper(print_readably);
|
| 790 | Py_RETURN_NONE;
|
| 791 | }
|
| 792 |
|
| 793 | PyDoc_STRVAR(doc_list_function_dumper,
|
| 794 | "function_dumper(bool) -> None\n\
|
| 795 | Print all readline functions and their bindings.");
|
| 796 |
|
| 797 | /* Print macros, their bindings, and their string outputs */
|
| 798 |
|
| 799 | static PyObject*
|
| 800 | macro_dumper(PyObject *self, PyObject *args)
|
| 801 | {
|
| 802 | int print_readably;
|
| 803 |
|
| 804 | if (!PyArg_ParseTuple(args, "i:macro_dumper", &print_readably))
|
| 805 | return NULL;
|
| 806 |
|
| 807 | rl_macro_dumper(print_readably);
|
| 808 | Py_RETURN_NONE;
|
| 809 | }
|
| 810 |
|
| 811 | PyDoc_STRVAR(doc_list_macro_dumper,
|
| 812 | "macro_dumper(bool) -> None\n\
|
| 813 | Print all readline sequences bound to macros and the strings they output.");
|
| 814 |
|
| 815 | /* List readline variables */
|
| 816 |
|
| 817 | static PyObject*
|
| 818 | variable_dumper(PyObject *self, PyObject *args)
|
| 819 | {
|
| 820 | int print_readably;
|
| 821 |
|
| 822 | if (!PyArg_ParseTuple(args, "i:variable_dumper", &print_readably))
|
| 823 | return NULL;
|
| 824 |
|
| 825 | rl_variable_dumper(print_readably);
|
| 826 | Py_RETURN_NONE;
|
| 827 | }
|
| 828 |
|
| 829 | PyDoc_STRVAR(doc_list_variable_dumper,
|
| 830 | "variable_dumper(bool) -> None\n\
|
| 831 | List readline variables and their values.");
|
| 832 |
|
| 833 |
|
| 834 | /* Query bindings for a function name */
|
| 835 |
|
| 836 | // readline returns null-terminated string arrays
|
| 837 | void _strvec_dispose(char **strvec) {
|
| 838 | register int i;
|
| 839 |
|
| 840 | if (strvec == NULL)
|
| 841 | return;
|
| 842 |
|
| 843 | for (i = 0; strvec[i]; i++) {
|
| 844 | free(strvec[i]);
|
| 845 | }
|
| 846 |
|
| 847 | free(strvec);
|
| 848 | }
|
| 849 |
|
| 850 | // Nicely prints a strvec with commas and an and
|
| 851 | // like '"foo", "bar", and "moop"'
|
| 852 | void _pprint_strvec_list(char **strvec) {
|
| 853 | int i;
|
| 854 |
|
| 855 | for (i = 0; strvec[i]; i++) {
|
| 856 | printf("\"%s\"", strvec[i]);
|
| 857 | if (strvec[i + 1]) {
|
| 858 | printf(", ");
|
| 859 | if (!strvec[i + 2])
|
| 860 | printf("and ");
|
| 861 | }
|
| 862 | }
|
| 863 | }
|
| 864 |
|
| 865 | /*
|
| 866 | NB: readline (and bash) have a bug where they don't see certain keyseqs, even
|
| 867 | if the bindings work. E.g., if you bind a number key like "\C-7", it will be
|
| 868 | bound, but reporting code like query_bindings and function_dumper won't count it.
|
| 869 | */
|
| 870 |
|
| 871 | static PyObject*
|
| 872 | query_bindings(PyObject *self, PyObject *args)
|
| 873 | {
|
| 874 | char *fn_name;
|
| 875 | rl_command_func_t *cmd_fn;
|
| 876 | char **key_seqs;
|
| 877 |
|
| 878 | if (!PyArg_ParseTuple(args, "s:query_bindings", &fn_name))
|
| 879 | return NULL;
|
| 880 |
|
| 881 | cmd_fn = rl_named_function(fn_name);
|
| 882 |
|
| 883 | if (cmd_fn == NULL) {
|
| 884 | PyErr_Format(PyExc_ValueError, "`%s': unknown function name", fn_name);
|
| 885 | return NULL;
|
| 886 | }
|
| 887 |
|
| 888 | key_seqs = rl_invoking_keyseqs(cmd_fn);
|
| 889 |
|
| 890 | if (!key_seqs) {
|
| 891 | // print to stdout, but return an error
|
| 892 | printf("%s is not bound to any keys.\n", fn_name);
|
| 893 | PyErr_SetNone(PyExc_ValueError);
|
| 894 | return NULL;
|
| 895 | }
|
| 896 |
|
| 897 | printf("%s can be invoked via ", fn_name);
|
| 898 | _pprint_strvec_list(key_seqs);
|
| 899 | printf(".\n");
|
| 900 |
|
| 901 | _strvec_dispose(key_seqs);
|
| 902 |
|
| 903 | Py_RETURN_NONE;
|
| 904 | }
|
| 905 |
|
| 906 | PyDoc_STRVAR(doc_query_bindings,
|
| 907 | "query_bindings(str) -> None\n\
|
| 908 | Query bindings to see what's bound to a given function.");
|
| 909 |
|
| 910 |
|
| 911 | static PyObject*
|
| 912 | unbind_rl_function(PyObject *self, PyObject *args)
|
| 913 | {
|
| 914 | char *fn_name;
|
| 915 | rl_command_func_t *cmd_fn;
|
| 916 |
|
| 917 | if (!PyArg_ParseTuple(args, "s:unbind_rl_function", &fn_name))
|
| 918 | return NULL;
|
| 919 |
|
| 920 | cmd_fn = rl_named_function(fn_name);
|
| 921 | if (cmd_fn == NULL) {
|
| 922 | PyErr_Format(PyExc_ValueError, "`%s': unknown function name", fn_name);
|
| 923 | return NULL;
|
| 924 | }
|
| 925 |
|
| 926 | rl_unbind_function_in_map(cmd_fn, rl_get_keymap());
|
| 927 | Py_RETURN_NONE;
|
| 928 | }
|
| 929 |
|
| 930 | PyDoc_STRVAR(doc_unbind_rl_function,
|
| 931 | "unbind_rl_function(function_name) -> None\n\
|
| 932 | Unbind all keys bound to the named readline function in the current keymap.");
|
| 933 |
|
| 934 |
|
| 935 | static PyObject*
|
| 936 | unbind_shell_cmd(PyObject *self, PyObject *args)
|
| 937 | {
|
| 938 | char *keyseq;
|
| 939 | Keymap cmd_map;
|
| 940 |
|
| 941 | if (!PyArg_ParseTuple(args, "s:unbind_shell_cmd", &keyseq))
|
| 942 | return NULL;
|
| 943 |
|
| 944 | cmd_map = _get_associated_cmd_map(rl_get_keymap());
|
| 945 | if (cmd_map == NULL) {
|
| 946 | PyErr_SetString(PyExc_ValueError, "Could not get command map for current keymap");
|
| 947 | return NULL;
|
| 948 | }
|
| 949 |
|
| 950 | if (rl_bind_keyseq_in_map(keyseq, (rl_command_func_t *)NULL, cmd_map) != 0) {
|
| 951 | PyErr_Format(PyExc_ValueError, "'%s': can't unbind from shell command keymap", keyseq);
|
| 952 | return NULL;
|
| 953 | }
|
| 954 |
|
| 955 | Py_RETURN_NONE;
|
| 956 | }
|
| 957 |
|
| 958 | PyDoc_STRVAR(doc_unbind_shell_cmd,
|
| 959 | "unbind_shell_cmd(key_sequence) -> None\n\
|
| 960 | Unbind a key sequence from the current keymap's associated shell command map.");
|
| 961 |
|
| 962 |
|
| 963 | static PyObject*
|
| 964 | print_shell_cmd_map(PyObject *self, PyObject *noarg)
|
| 965 | {
|
| 966 | Keymap curr_map, cmd_map;
|
| 967 |
|
| 968 | curr_map = rl_get_keymap();
|
| 969 | cmd_map = _get_associated_cmd_map(curr_map);
|
| 970 |
|
| 971 | if (cmd_map == NULL) {
|
| 972 | PyErr_SetString(PyExc_ValueError, "Could not get shell command map for current keymap");
|
| 973 | return NULL;
|
| 974 | }
|
| 975 |
|
| 976 | rl_set_keymap(cmd_map);
|
| 977 | rl_macro_dumper(1);
|
| 978 | rl_set_keymap(curr_map);
|
| 979 |
|
| 980 | Py_RETURN_NONE;
|
| 981 | }
|
| 982 |
|
| 983 | PyDoc_STRVAR(doc_print_shell_cmd_map,
|
| 984 | "print_shell_cmd_map() -> None\n\
|
| 985 | Print all bindings for shell commands in the current keymap.");
|
| 986 |
|
| 987 |
|
| 988 | /* Remove all bindings for a given keyseq */
|
| 989 |
|
| 990 | static PyObject*
|
| 991 | unbind_keyseq(PyObject *self, PyObject *args)
|
| 992 | {
|
| 993 | char *seq, *keyseq;
|
| 994 | int kslen, type;
|
| 995 | rl_command_func_t *fn;
|
| 996 |
|
| 997 | if (!PyArg_ParseTuple(args, "s:unbind_keyseq", &seq))
|
| 998 | return NULL;
|
| 999 |
|
| 1000 | // Commented code below based on bash 5.x unbinding code, which fails on
|
| 1001 | // readline versions before 2019.
|
| 1002 |
|
| 1003 | // keyseq = (char *)malloc((2 * strlen(seq)) + 1);
|
| 1004 | // if (rl_translate_keyseq(seq, keyseq, &kslen) != 0) {
|
| 1005 | // free(keyseq);
|
| 1006 | // PyErr_Format(PyExc_ValueError, "'%s': cannot translate key sequence", seq);
|
| 1007 | // return NULL;
|
| 1008 | // }
|
| 1009 |
|
| 1010 | // fn = rl_function_of_keyseq_len(keyseq, kslen, (Keymap)NULL, &type);
|
| 1011 | // if (!fn) {
|
| 1012 | // free(keyseq);
|
| 1013 | // Py_RETURN_NONE;
|
| 1014 | // }
|
| 1015 |
|
| 1016 | // if (type == ISKMAP) {
|
| 1017 | // fn = ((Keymap)fn)[ANYOTHERKEY].function;
|
| 1018 | // }
|
| 1019 |
|
| 1020 | // if (rl_bind_keyseq(seq, (rl_command_func_t *)NULL) != 0) {
|
| 1021 | // free(keyseq);
|
| 1022 | // PyErr_Format(PyExc_ValueError, "'%s': cannot unbind", seq);
|
| 1023 | // return NULL;
|
| 1024 | // }
|
| 1025 |
|
| 1026 | // /*
|
| 1027 | // TODO: Handle shell command unbinding if f == bash_execute_unix_command or
|
| 1028 | // rather, whatever the osh equivalent will be
|
| 1029 | // */
|
| 1030 |
|
| 1031 | // free(keyseq);
|
| 1032 | // Py_RETURN_NONE;
|
| 1033 |
|
| 1034 | // End bash 5.x unbinding code
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 | // Code below based on bash 4 unbinding code from 2011
|
| 1039 |
|
| 1040 | if (rl_bind_keyseq (seq, (rl_command_func_t *)NULL) != 0) {
|
| 1041 | PyErr_Format(PyExc_ValueError, "'%s': cannot unbind", seq);
|
| 1042 | return NULL;
|
| 1043 | }
|
| 1044 |
|
| 1045 | Py_RETURN_NONE;
|
| 1046 | }
|
| 1047 |
|
| 1048 | PyDoc_STRVAR(doc_unbind_keyseq,
|
| 1049 | "unbind_keyseq(sequence) -> None\n\
|
| 1050 | Unbind a key sequence from the current keymap.");
|
| 1051 |
|
| 1052 |
|
| 1053 | /* Keymap toggling code */
|
| 1054 | static Keymap orig_keymap = NULL;
|
| 1055 |
|
| 1056 | static PyObject*
|
| 1057 | use_temp_keymap(PyObject *self, PyObject *args)
|
| 1058 | {
|
| 1059 | char *keymap_name;
|
| 1060 | Keymap new_keymap;
|
| 1061 |
|
| 1062 | if (!PyArg_ParseTuple(args, "s:use_temp_keymap", &keymap_name))
|
| 1063 | return NULL;
|
| 1064 |
|
| 1065 | new_keymap = rl_get_keymap_by_name(keymap_name);
|
| 1066 | if (new_keymap == NULL) {
|
| 1067 | PyErr_Format(PyExc_ValueError, "`%s': unknown keymap name", keymap_name);
|
| 1068 | return NULL;
|
| 1069 | }
|
| 1070 |
|
| 1071 | orig_keymap = rl_get_keymap();
|
| 1072 | rl_set_keymap(new_keymap);
|
| 1073 |
|
| 1074 | Py_RETURN_NONE;
|
| 1075 | }
|
| 1076 |
|
| 1077 | PyDoc_STRVAR(doc_use_temp_keymap,
|
| 1078 | "use_temp_keymap(keymap_name) -> None\n\
|
| 1079 | Temporarily switch to named keymap, saving the current one.");
|
| 1080 |
|
| 1081 | static PyObject*
|
| 1082 | restore_orig_keymap(PyObject *self, PyObject *args)
|
| 1083 | {
|
| 1084 | if (orig_keymap != NULL) {
|
| 1085 | rl_set_keymap(orig_keymap);
|
| 1086 | orig_keymap = NULL;
|
| 1087 | }
|
| 1088 |
|
| 1089 | Py_RETURN_NONE;
|
| 1090 | }
|
| 1091 |
|
| 1092 | PyDoc_STRVAR(doc_restore_orig_keymap,
|
| 1093 | "restore_orig_keymap() -> None\n\
|
| 1094 | Restore the previously saved keymap if one exists.");
|
| 1095 |
|
| 1096 | /* Table of functions exported by the module */
|
| 1097 |
|
| 1098 | static struct PyMethodDef readline_methods[] = {
|
| 1099 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
|
| 1100 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
|
| 1101 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
|
| 1102 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
|
| 1103 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
|
| 1104 | {"read_history_file", read_history_file,
|
| 1105 | METH_VARARGS, doc_read_history_file},
|
| 1106 | {"write_history_file", write_history_file,
|
| 1107 | METH_VARARGS, doc_write_history_file},
|
| 1108 | {"get_history_item", get_history_item,
|
| 1109 | METH_VARARGS, doc_get_history_item},
|
| 1110 | {"get_current_history_length", (PyCFunction)get_current_history_length,
|
| 1111 | METH_NOARGS, doc_get_current_history_length},
|
| 1112 | {"set_history_length", set_history_length,
|
| 1113 | METH_VARARGS, set_history_length_doc},
|
| 1114 | {"get_history_length", get_history_length,
|
| 1115 | METH_NOARGS, get_history_length_doc},
|
| 1116 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
|
| 1117 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
|
| 1118 | {"get_completion_type", get_completion_type,
|
| 1119 | METH_NOARGS, doc_get_completion_type},
|
| 1120 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
|
| 1121 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
|
| 1122 |
|
| 1123 | {"set_completer_delims", set_completer_delims,
|
| 1124 | METH_VARARGS, doc_set_completer_delims},
|
| 1125 | {"add_history", py_add_history, METH_VARARGS, doc_add_history},
|
| 1126 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
|
| 1127 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
|
| 1128 | {"get_completer_delims", get_completer_delims,
|
| 1129 | METH_NOARGS, doc_get_completer_delims},
|
| 1130 |
|
| 1131 | {"set_completion_display_matches_hook", set_completion_display_matches_hook,
|
| 1132 | METH_VARARGS, doc_set_completion_display_matches_hook},
|
| 1133 | {"set_startup_hook", set_startup_hook,
|
| 1134 | METH_VARARGS, doc_set_startup_hook},
|
| 1135 | {"set_pre_input_hook", set_pre_input_hook,
|
| 1136 | METH_VARARGS, doc_set_pre_input_hook},
|
| 1137 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
|
| 1138 | {"resize_terminal", py_resize_terminal, METH_NOARGS, ""},
|
| 1139 |
|
| 1140 | /* Functions added to implement the 'bind' builtin in OSH */
|
| 1141 | {"list_funmap_names", list_funmap_names, METH_NOARGS, doc_list_funmap_names},
|
| 1142 | {"function_dumper", function_dumper, METH_VARARGS, doc_list_function_dumper},
|
| 1143 | {"macro_dumper", macro_dumper, METH_VARARGS, doc_list_macro_dumper},
|
| 1144 | {"variable_dumper", variable_dumper, METH_VARARGS, doc_list_variable_dumper},
|
| 1145 | {"query_bindings", query_bindings, METH_VARARGS, doc_query_bindings},
|
| 1146 | {"unbind_rl_function", unbind_rl_function, METH_VARARGS, doc_unbind_rl_function},
|
| 1147 | {"use_temp_keymap", use_temp_keymap, METH_VARARGS, doc_use_temp_keymap},
|
| 1148 | {"restore_orig_keymap", restore_orig_keymap, METH_NOARGS, doc_restore_orig_keymap},
|
| 1149 | {"unbind_shell_cmd", unbind_shell_cmd, METH_VARARGS, doc_unbind_shell_cmd},
|
| 1150 | {"print_shell_cmd_map", print_shell_cmd_map, METH_NOARGS, doc_print_shell_cmd_map},
|
| 1151 | {"unbind_keyseq", unbind_keyseq, METH_VARARGS, doc_unbind_keyseq},
|
| 1152 | {0, 0}
|
| 1153 | };
|
| 1154 |
|
| 1155 |
|
| 1156 | /* C function to call the Python hooks. */
|
| 1157 |
|
| 1158 | static int
|
| 1159 | on_hook(PyObject *func)
|
| 1160 | {
|
| 1161 | int result = 0;
|
| 1162 | if (func != NULL) {
|
| 1163 | PyObject *r;
|
| 1164 | #ifdef WITH_THREAD
|
| 1165 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
| 1166 | #endif
|
| 1167 | r = PyObject_CallFunction(func, NULL);
|
| 1168 | if (r == NULL)
|
| 1169 | goto error;
|
| 1170 | if (r == Py_None)
|
| 1171 | result = 0;
|
| 1172 | else {
|
| 1173 | result = PyInt_AsLong(r);
|
| 1174 | if (result == -1 && PyErr_Occurred())
|
| 1175 | goto error;
|
| 1176 | }
|
| 1177 | Py_DECREF(r);
|
| 1178 | goto done;
|
| 1179 | error:
|
| 1180 | PyErr_Clear();
|
| 1181 | Py_XDECREF(r);
|
| 1182 | done:
|
| 1183 | #ifdef WITH_THREAD
|
| 1184 | PyGILState_Release(gilstate);
|
| 1185 | #endif
|
| 1186 | return result;
|
| 1187 | }
|
| 1188 | return result;
|
| 1189 | }
|
| 1190 |
|
| 1191 | static int
|
| 1192 | #if defined(_RL_FUNCTION_TYPEDEF)
|
| 1193 | on_startup_hook(void)
|
| 1194 | #else
|
| 1195 | on_startup_hook()
|
| 1196 | #endif
|
| 1197 | {
|
| 1198 | return on_hook(startup_hook);
|
| 1199 | }
|
| 1200 |
|
| 1201 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
| 1202 | static int
|
| 1203 | #if defined(_RL_FUNCTION_TYPEDEF)
|
| 1204 | on_pre_input_hook(void)
|
| 1205 | #else
|
| 1206 | on_pre_input_hook()
|
| 1207 | #endif
|
| 1208 | {
|
| 1209 | return on_hook(pre_input_hook);
|
| 1210 | }
|
| 1211 | #endif
|
| 1212 |
|
| 1213 |
|
| 1214 | /* C function to call the Python completion_display_matches */
|
| 1215 |
|
| 1216 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
| 1217 | static void
|
| 1218 | on_completion_display_matches_hook(char **matches,
|
| 1219 | int num_matches, int max_length)
|
| 1220 | {
|
| 1221 | int i;
|
| 1222 | PyObject *m=NULL, *s=NULL, *r=NULL;
|
| 1223 | #ifdef WITH_THREAD
|
| 1224 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
| 1225 | #endif
|
| 1226 | m = PyList_New(num_matches);
|
| 1227 | if (m == NULL)
|
| 1228 | goto error;
|
| 1229 | for (i = 0; i < num_matches; i++) {
|
| 1230 | s = PyString_FromString(matches[i+1]);
|
| 1231 | if (s == NULL)
|
| 1232 | goto error;
|
| 1233 | if (PyList_SetItem(m, i, s) == -1)
|
| 1234 | goto error;
|
| 1235 | }
|
| 1236 |
|
| 1237 | r = PyObject_CallFunction(completion_display_matches_hook,
|
| 1238 | "sOi", matches[0], m, max_length);
|
| 1239 |
|
| 1240 | Py_DECREF(m); m=NULL;
|
| 1241 |
|
| 1242 | if (r == NULL ||
|
| 1243 | (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
|
| 1244 | goto error;
|
| 1245 | }
|
| 1246 | Py_XDECREF(r); r=NULL;
|
| 1247 |
|
| 1248 | if (0) {
|
| 1249 | error:
|
| 1250 | PyErr_Clear();
|
| 1251 | Py_XDECREF(m);
|
| 1252 | Py_XDECREF(r);
|
| 1253 | }
|
| 1254 | #ifdef WITH_THREAD
|
| 1255 | PyGILState_Release(gilstate);
|
| 1256 | #endif
|
| 1257 | }
|
| 1258 |
|
| 1259 | #endif
|
| 1260 |
|
| 1261 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
| 1262 | static volatile sig_atomic_t sigwinch_received;
|
| 1263 | static PyOS_sighandler_t sigwinch_ohandler;
|
| 1264 |
|
| 1265 | static void
|
| 1266 | readline_sigwinch_handler(int signum)
|
| 1267 | {
|
| 1268 | sigwinch_received = 1;
|
| 1269 | if (sigwinch_ohandler &&
|
| 1270 | sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
|
| 1271 | sigwinch_ohandler(signum);
|
| 1272 | }
|
| 1273 | #endif
|
| 1274 |
|
| 1275 | /* C function to call the Python completer. */
|
| 1276 |
|
| 1277 | static char *
|
| 1278 | on_completion(const char *text, int state)
|
| 1279 | {
|
| 1280 | char *result = NULL;
|
| 1281 | if (completer != NULL) {
|
| 1282 | PyObject *r;
|
| 1283 | #ifdef WITH_THREAD
|
| 1284 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
| 1285 | #endif
|
| 1286 | rl_attempted_completion_over = 1;
|
| 1287 | r = PyObject_CallFunction(completer, "si", text, state);
|
| 1288 | if (r == NULL)
|
| 1289 | goto error;
|
| 1290 | if (r == Py_None) {
|
| 1291 | result = NULL;
|
| 1292 | }
|
| 1293 | else {
|
| 1294 | char *s = PyString_AsString(r);
|
| 1295 | if (s == NULL)
|
| 1296 | goto error;
|
| 1297 | result = strdup(s);
|
| 1298 | }
|
| 1299 | Py_DECREF(r);
|
| 1300 | goto done;
|
| 1301 | error:
|
| 1302 | PyErr_Clear();
|
| 1303 | Py_XDECREF(r);
|
| 1304 | done:
|
| 1305 | #ifdef WITH_THREAD
|
| 1306 | PyGILState_Release(gilstate);
|
| 1307 | #endif
|
| 1308 | return result;
|
| 1309 | }
|
| 1310 | return result;
|
| 1311 | }
|
| 1312 |
|
| 1313 |
|
| 1314 | /* A more flexible constructor that saves the "begidx" and "endidx"
|
| 1315 | * before calling the normal completer */
|
| 1316 |
|
| 1317 | static char **
|
| 1318 | flex_complete(const char *text, int start, int end)
|
| 1319 | {
|
| 1320 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
| 1321 | rl_completion_append_character ='\0';
|
| 1322 | #endif
|
| 1323 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
|
| 1324 | rl_completion_suppress_append = 0;
|
| 1325 | #endif
|
| 1326 | Py_XDECREF(begidx);
|
| 1327 | Py_XDECREF(endidx);
|
| 1328 | begidx = PyInt_FromLong((long) start);
|
| 1329 | endidx = PyInt_FromLong((long) end);
|
| 1330 | return completion_matches(text, *on_completion);
|
| 1331 | }
|
| 1332 |
|
| 1333 |
|
| 1334 | /* Helper to initialize GNU readline properly. */
|
| 1335 |
|
| 1336 | static void
|
| 1337 | setup_readline(void)
|
| 1338 | {
|
| 1339 | #ifdef SAVE_LOCALE
|
| 1340 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
| 1341 | if (!saved_locale)
|
| 1342 | Py_FatalError("not enough memory to save locale");
|
| 1343 | #endif
|
| 1344 |
|
| 1345 | #ifdef __APPLE__
|
| 1346 | /* the libedit readline emulation resets key bindings etc
|
| 1347 | * when calling rl_initialize. So call it upfront
|
| 1348 | */
|
| 1349 | if (using_libedit_emulation)
|
| 1350 | rl_initialize();
|
| 1351 |
|
| 1352 | /* Detect if libedit's readline emulation uses 0-based
|
| 1353 | * indexing or 1-based indexing.
|
| 1354 | */
|
| 1355 | add_history("1");
|
| 1356 | if (history_get(1) == NULL) {
|
| 1357 | libedit_history_start = 0;
|
| 1358 | } else {
|
| 1359 | libedit_history_start = 1;
|
| 1360 | }
|
| 1361 | clear_history();
|
| 1362 | #endif /* __APPLE__ */
|
| 1363 |
|
| 1364 | using_history();
|
| 1365 |
|
| 1366 | rl_readline_name = "oils";
|
| 1367 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
| 1368 | /* Allow $if term= in .inputrc to work */
|
| 1369 | rl_terminal_name = getenv("TERM");
|
| 1370 | #endif
|
| 1371 | /* Force rebind of TAB to insert-tab */
|
| 1372 | rl_bind_key('\t', rl_insert);
|
| 1373 | /* Bind both ESC-TAB and ESC-ESC to the completion function */
|
| 1374 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
|
| 1375 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
|
| 1376 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
| 1377 | /* Set up signal handler for window resize */
|
| 1378 | sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
|
| 1379 | #endif
|
| 1380 | /* Set our hook functions */
|
| 1381 | rl_startup_hook = on_startup_hook;
|
| 1382 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
| 1383 | rl_pre_input_hook = on_pre_input_hook;
|
| 1384 | #endif
|
| 1385 | /* Set our completion function */
|
| 1386 | rl_attempted_completion_function = flex_complete;
|
| 1387 | /* Set Python word break characters */
|
| 1388 | completer_word_break_characters =
|
| 1389 | rl_completer_word_break_characters =
|
| 1390 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
|
| 1391 | /* All nonalphanums except '.' */
|
| 1392 |
|
| 1393 | begidx = PyInt_FromLong(0L);
|
| 1394 | endidx = PyInt_FromLong(0L);
|
| 1395 |
|
| 1396 | #ifdef __APPLE__
|
| 1397 | if (!using_libedit_emulation)
|
| 1398 | #endif
|
| 1399 | {
|
| 1400 | if (!isatty(STDOUT_FILENO)) {
|
| 1401 | /* Issue #19884: stdout is not a terminal. Disable meta modifier
|
| 1402 | keys to not write the ANSI sequence "\033[1034h" into stdout. On
|
| 1403 | terminals supporting 8 bit characters like TERM=xterm-256color
|
| 1404 | (which is now the default Fedora since Fedora 18), the meta key is
|
| 1405 | used to enable support of 8 bit characters (ANSI sequence
|
| 1406 | "\033[1034h").
|
| 1407 |
|
| 1408 | With libedit, this call makes readline() crash. */
|
| 1409 | rl_variable_bind ("enable-meta-key", "off");
|
| 1410 | }
|
| 1411 | }
|
| 1412 |
|
| 1413 | /* Initialize (allows .inputrc to override)
|
| 1414 | *
|
| 1415 | * XXX: A bug in the readline-2.2 library causes a memory leak
|
| 1416 | * inside this function. Nothing we can do about it.
|
| 1417 | */
|
| 1418 | #ifdef __APPLE__
|
| 1419 | if (using_libedit_emulation)
|
| 1420 | rl_read_init_file(NULL);
|
| 1421 | else
|
| 1422 | #endif /* __APPLE__ */
|
| 1423 | rl_initialize();
|
| 1424 |
|
| 1425 | RESTORE_LOCALE(saved_locale)
|
| 1426 | }
|
| 1427 |
|
| 1428 | /* Wrapper around GNU readline that handles signals differently. */
|
| 1429 |
|
| 1430 |
|
| 1431 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
|
| 1432 |
|
| 1433 | static char *completed_input_string;
|
| 1434 | static void
|
| 1435 | rlhandler(char *text)
|
| 1436 | {
|
| 1437 | completed_input_string = text;
|
| 1438 | rl_callback_handler_remove();
|
| 1439 | }
|
| 1440 |
|
| 1441 | static char *
|
| 1442 | readline_until_enter_or_signal(char *prompt, int *signal)
|
| 1443 | {
|
| 1444 | char * not_done_reading = "";
|
| 1445 | fd_set selectset;
|
| 1446 |
|
| 1447 | *signal = 0;
|
| 1448 | #ifdef HAVE_RL_CATCH_SIGNAL
|
| 1449 | rl_catch_signals = 0;
|
| 1450 | #endif
|
| 1451 | /* OVM_MAIN: Oil is handling SIGWINCH, so readline shouldn't handle it.
|
| 1452 | * Without this line, strace reveals that GNU readline is constantly
|
| 1453 | * turning it on and off.
|
| 1454 | * */
|
| 1455 | rl_catch_sigwinch = 0;
|
| 1456 |
|
| 1457 | rl_callback_handler_install (prompt, rlhandler);
|
| 1458 | FD_ZERO(&selectset);
|
| 1459 |
|
| 1460 | completed_input_string = not_done_reading;
|
| 1461 |
|
| 1462 | while (completed_input_string == not_done_reading) {
|
| 1463 | int has_input = 0;
|
| 1464 |
|
| 1465 | while (!has_input)
|
| 1466 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
|
| 1467 |
|
| 1468 | /* [Bug #1552726] Only limit the pause if an input hook has been
|
| 1469 | defined. */
|
| 1470 | struct timeval *timeoutp = NULL;
|
| 1471 | if (PyOS_InputHook)
|
| 1472 | timeoutp = &timeout;
|
| 1473 | #ifdef HAVE_RL_RESIZE_TERMINAL
|
| 1474 | /* Update readline's view of the window size after SIGWINCH */
|
| 1475 | if (sigwinch_received) {
|
| 1476 | sigwinch_received = 0;
|
| 1477 | rl_resize_terminal();
|
| 1478 | }
|
| 1479 | #endif
|
| 1480 | FD_SET(fileno(rl_instream), &selectset);
|
| 1481 | /* select resets selectset if no input was available */
|
| 1482 | has_input = select(fileno(rl_instream) + 1, &selectset,
|
| 1483 | NULL, NULL, timeoutp);
|
| 1484 | if(PyOS_InputHook) PyOS_InputHook();
|
| 1485 | }
|
| 1486 |
|
| 1487 | if(has_input > 0) {
|
| 1488 | rl_callback_read_char();
|
| 1489 | }
|
| 1490 | else if (errno == EINTR) {
|
| 1491 | int s;
|
| 1492 | #ifdef WITH_THREAD
|
| 1493 | PyEval_RestoreThread(_PyOS_ReadlineTState);
|
| 1494 | #endif
|
| 1495 | s = PyErr_CheckSignals();
|
| 1496 | #ifdef WITH_THREAD
|
| 1497 | PyEval_SaveThread();
|
| 1498 | #endif
|
| 1499 | if (s < 0) {
|
| 1500 | rl_free_line_state();
|
| 1501 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
|
| 1502 | rl_callback_sigcleanup();
|
| 1503 | #endif
|
| 1504 | rl_cleanup_after_signal();
|
| 1505 | rl_callback_handler_remove();
|
| 1506 | *signal = 1;
|
| 1507 | completed_input_string = NULL;
|
| 1508 | }
|
| 1509 | }
|
| 1510 | }
|
| 1511 |
|
| 1512 | return completed_input_string;
|
| 1513 | }
|
| 1514 |
|
| 1515 |
|
| 1516 | #else
|
| 1517 |
|
| 1518 | /* Interrupt handler */
|
| 1519 |
|
| 1520 | static jmp_buf jbuf;
|
| 1521 |
|
| 1522 | /* ARGSUSED */
|
| 1523 | static void
|
| 1524 | onintr(int sig)
|
| 1525 | {
|
| 1526 | longjmp(jbuf, 1);
|
| 1527 | }
|
| 1528 |
|
| 1529 |
|
| 1530 | static char *
|
| 1531 | readline_until_enter_or_signal(char *prompt, int *signal)
|
| 1532 | {
|
| 1533 | PyOS_sighandler_t old_inthandler;
|
| 1534 | char *p;
|
| 1535 |
|
| 1536 | *signal = 0;
|
| 1537 |
|
| 1538 | old_inthandler = PyOS_setsig(SIGINT, onintr);
|
| 1539 | if (setjmp(jbuf)) {
|
| 1540 | #ifdef HAVE_SIGRELSE
|
| 1541 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
|
| 1542 | sigrelse(SIGINT);
|
| 1543 | #endif
|
| 1544 | PyOS_setsig(SIGINT, old_inthandler);
|
| 1545 | *signal = 1;
|
| 1546 | return NULL;
|
| 1547 | }
|
| 1548 | rl_event_hook = PyOS_InputHook;
|
| 1549 | p = readline(prompt);
|
| 1550 | PyOS_setsig(SIGINT, old_inthandler);
|
| 1551 |
|
| 1552 | return p;
|
| 1553 | }
|
| 1554 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
|
| 1555 |
|
| 1556 |
|
| 1557 | static char *
|
| 1558 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
| 1559 | {
|
| 1560 | size_t n;
|
| 1561 | char *p, *q;
|
| 1562 | int signal;
|
| 1563 |
|
| 1564 | #ifdef SAVE_LOCALE
|
| 1565 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
| 1566 | if (!saved_locale)
|
| 1567 | Py_FatalError("not enough memory to save locale");
|
| 1568 | setlocale(LC_CTYPE, "");
|
| 1569 | #endif
|
| 1570 |
|
| 1571 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
|
| 1572 | rl_instream = sys_stdin;
|
| 1573 | rl_outstream = sys_stdout;
|
| 1574 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
| 1575 | rl_prep_terminal (1);
|
| 1576 | #endif
|
| 1577 | }
|
| 1578 |
|
| 1579 | p = readline_until_enter_or_signal(prompt, &signal);
|
| 1580 |
|
| 1581 | /* we got an interrupt signal */
|
| 1582 | if (signal) {
|
| 1583 | RESTORE_LOCALE(saved_locale)
|
| 1584 | return NULL;
|
| 1585 | }
|
| 1586 |
|
| 1587 | /* We got an EOF, return an empty string. */
|
| 1588 | if (p == NULL) {
|
| 1589 | p = PyMem_Malloc(1);
|
| 1590 | if (p != NULL)
|
| 1591 | *p = '\0';
|
| 1592 | RESTORE_LOCALE(saved_locale)
|
| 1593 | return p;
|
| 1594 | }
|
| 1595 |
|
| 1596 | /* we have a valid line */
|
| 1597 | n = strlen(p);
|
| 1598 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
|
| 1599 | release the original. */
|
| 1600 | q = p;
|
| 1601 | p = PyMem_Malloc(n+2);
|
| 1602 | if (p != NULL) {
|
| 1603 | strncpy(p, q, n);
|
| 1604 | p[n] = '\n';
|
| 1605 | p[n+1] = '\0';
|
| 1606 | }
|
| 1607 | free(q);
|
| 1608 | RESTORE_LOCALE(saved_locale)
|
| 1609 | return p;
|
| 1610 | }
|
| 1611 |
|
| 1612 |
|
| 1613 | /* Initialize the module */
|
| 1614 |
|
| 1615 | PyDoc_STRVAR(doc_module,
|
| 1616 | "Importing this module enables command line editing using GNU readline.");
|
| 1617 |
|
| 1618 | #ifdef __APPLE__
|
| 1619 | PyDoc_STRVAR(doc_module_le,
|
| 1620 | "Importing this module enables command line editing using libedit readline.");
|
| 1621 | #endif /* __APPLE__ */
|
| 1622 |
|
| 1623 | PyMODINIT_FUNC
|
| 1624 | initline_input(void)
|
| 1625 | {
|
| 1626 | PyObject *m;
|
| 1627 |
|
| 1628 | #ifdef __APPLE__
|
| 1629 | if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
|
| 1630 | using_libedit_emulation = 1;
|
| 1631 | }
|
| 1632 |
|
| 1633 | if (using_libedit_emulation)
|
| 1634 | m = Py_InitModule4("line_input", readline_methods, doc_module_le,
|
| 1635 | (PyObject *)NULL, PYTHON_API_VERSION);
|
| 1636 | else
|
| 1637 |
|
| 1638 | #endif /* __APPLE__ */
|
| 1639 |
|
| 1640 | m = Py_InitModule4("line_input", readline_methods, doc_module,
|
| 1641 | (PyObject *)NULL, PYTHON_API_VERSION);
|
| 1642 | if (m == NULL)
|
| 1643 | return;
|
| 1644 |
|
| 1645 | PyOS_ReadlineFunctionPointer = call_readline;
|
| 1646 | setup_readline();
|
| 1647 |
|
| 1648 | PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
|
| 1649 | PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
|
| 1650 | }
|