| 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 |     if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
 | 
| 119 |         return NULL;
 | 
| 120 |     /* Make a copy -- rl_parse_and_bind() modifies its argument */
 | 
| 121 |     /* Bernard Herzog */
 | 
| 122 |     copy = malloc(1 + strlen(s));
 | 
| 123 |     if (copy == NULL)
 | 
| 124 |         return PyErr_NoMemory();
 | 
| 125 |     strcpy(copy, s);
 | 
| 126 |     rl_parse_and_bind(copy);
 | 
| 127 |     free(copy); /* Free the copy */
 | 
| 128 |     Py_RETURN_NONE;
 | 
| 129 | }
 | 
| 130 | 
 | 
| 131 | PyDoc_STRVAR(doc_parse_and_bind,
 | 
| 132 | "parse_and_bind(string) -> None\n\
 | 
| 133 | Execute the init line provided in the string argument.");
 | 
| 134 | 
 | 
| 135 | 
 | 
| 136 | /* Exported function to parse a readline init file */
 | 
| 137 | 
 | 
| 138 | static PyObject *
 | 
| 139 | read_init_file(PyObject *self, PyObject *args)
 | 
| 140 | {
 | 
| 141 |     char *s = NULL;
 | 
| 142 |     if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
 | 
| 143 |         return NULL;
 | 
| 144 |     errno = rl_read_init_file(s);
 | 
| 145 |     if (errno)
 | 
| 146 |         return PyErr_SetFromErrno(PyExc_IOError);
 | 
| 147 |     Py_RETURN_NONE;
 | 
| 148 | }
 | 
| 149 | 
 | 
| 150 | PyDoc_STRVAR(doc_read_init_file,
 | 
| 151 | "read_init_file([filename]) -> None\n\
 | 
| 152 | Execute a readline initialization file.\n\
 | 
| 153 | The default filename is the last filename used.");
 | 
| 154 | 
 | 
| 155 | 
 | 
| 156 | /* Exported function to load a readline history file */
 | 
| 157 | 
 | 
| 158 | static PyObject *
 | 
| 159 | read_history_file(PyObject *self, PyObject *args)
 | 
| 160 | {
 | 
| 161 |     char *s = NULL;
 | 
| 162 |     if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
 | 
| 163 |         return NULL;
 | 
| 164 |     errno = read_history(s);
 | 
| 165 |     if (errno)
 | 
| 166 |         return PyErr_SetFromErrno(PyExc_IOError);
 | 
| 167 |     Py_RETURN_NONE;
 | 
| 168 | }
 | 
| 169 | 
 | 
| 170 | static int _history_length = -1; /* do not truncate history by default */
 | 
| 171 | PyDoc_STRVAR(doc_read_history_file,
 | 
| 172 | "read_history_file([filename]) -> None\n\
 | 
| 173 | Load a readline history file.\n\
 | 
| 174 | The default filename is ~/.history.");
 | 
| 175 | 
 | 
| 176 | 
 | 
| 177 | /* Exported function to save a readline history file */
 | 
| 178 | 
 | 
| 179 | static PyObject *
 | 
| 180 | write_history_file(PyObject *self, PyObject *args)
 | 
| 181 | {
 | 
| 182 |     char *s = NULL;
 | 
| 183 |     if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
 | 
| 184 |         return NULL;
 | 
| 185 |     errno = write_history(s);
 | 
| 186 |     if (!errno && _history_length >= 0)
 | 
| 187 |         history_truncate_file(s, _history_length);
 | 
| 188 |     if (errno)
 | 
| 189 |         return PyErr_SetFromErrno(PyExc_IOError);
 | 
| 190 |     Py_RETURN_NONE;
 | 
| 191 | }
 | 
| 192 | 
 | 
| 193 | PyDoc_STRVAR(doc_write_history_file,
 | 
| 194 | "write_history_file([filename]) -> None\n\
 | 
| 195 | Save a readline history file.\n\
 | 
| 196 | The default filename is ~/.history.");
 | 
| 197 | 
 | 
| 198 | 
 | 
| 199 | /* Set history length */
 | 
| 200 | 
 | 
| 201 | static PyObject*
 | 
| 202 | set_history_length(PyObject *self, PyObject *args)
 | 
| 203 | {
 | 
| 204 |     int length = _history_length;
 | 
| 205 |     if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
 | 
| 206 |         return NULL;
 | 
| 207 |     _history_length = length;
 | 
| 208 |     Py_RETURN_NONE;
 | 
| 209 | }
 | 
| 210 | 
 | 
| 211 | PyDoc_STRVAR(set_history_length_doc,
 | 
| 212 | "set_history_length(length) -> None\n\
 | 
| 213 | set the maximal number of lines which will be written to\n\
 | 
| 214 | the history file. A negative length is used to inhibit\n\
 | 
| 215 | history truncation.");
 | 
| 216 | 
 | 
| 217 | 
 | 
| 218 | /* Get history length */
 | 
| 219 | 
 | 
| 220 | static PyObject*
 | 
| 221 | get_history_length(PyObject *self, PyObject *noarg)
 | 
| 222 | {
 | 
| 223 |     return PyInt_FromLong(_history_length);
 | 
| 224 | }
 | 
| 225 | 
 | 
| 226 | PyDoc_STRVAR(get_history_length_doc,
 | 
| 227 | "get_history_length() -> int\n\
 | 
| 228 | return the maximum number of lines that will be written to\n\
 | 
| 229 | the history file.");
 | 
| 230 | 
 | 
| 231 | 
 | 
| 232 | /* Generic hook function setter */
 | 
| 233 | 
 | 
| 234 | static PyObject *
 | 
| 235 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
 | 
| 236 | {
 | 
| 237 |     PyObject *function = Py_None;
 | 
| 238 |     char buf[80];
 | 
| 239 |     PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
 | 
| 240 |     if (!PyArg_ParseTuple(args, buf, &function))
 | 
| 241 |         return NULL;
 | 
| 242 |     if (function == Py_None) {
 | 
| 243 |         Py_CLEAR(*hook_var);
 | 
| 244 |     }
 | 
| 245 |     else if (PyCallable_Check(function)) {
 | 
| 246 |         PyObject *tmp = *hook_var;
 | 
| 247 |         Py_INCREF(function);
 | 
| 248 |         *hook_var = function;
 | 
| 249 |         Py_XDECREF(tmp);
 | 
| 250 |     }
 | 
| 251 |     else {
 | 
| 252 |         PyOS_snprintf(buf, sizeof(buf),
 | 
| 253 |                       "set_%.50s(func): argument not callable",
 | 
| 254 |                       funcname);
 | 
| 255 |         PyErr_SetString(PyExc_TypeError, buf);
 | 
| 256 |         return NULL;
 | 
| 257 |     }
 | 
| 258 |     Py_RETURN_NONE;
 | 
| 259 | }
 | 
| 260 | 
 | 
| 261 | 
 | 
| 262 | /* Exported functions to specify hook functions in Python */
 | 
| 263 | 
 | 
| 264 | static PyObject *completion_display_matches_hook = NULL;
 | 
| 265 | static PyObject *startup_hook = NULL;
 | 
| 266 | 
 | 
| 267 | #ifdef HAVE_RL_PRE_INPUT_HOOK
 | 
| 268 | static PyObject *pre_input_hook = NULL;
 | 
| 269 | #endif
 | 
| 270 | 
 | 
| 271 | static PyObject *
 | 
| 272 | set_completion_display_matches_hook(PyObject *self, PyObject *args)
 | 
| 273 | {
 | 
| 274 |     PyObject *result = set_hook("completion_display_matches_hook",
 | 
| 275 |                     &completion_display_matches_hook, args);
 | 
| 276 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
 | 
| 277 |     /* We cannot set this hook globally, since it replaces the
 | 
| 278 |        default completion display. */
 | 
| 279 |     rl_completion_display_matches_hook =
 | 
| 280 |         completion_display_matches_hook ?
 | 
| 281 | #if defined(_RL_FUNCTION_TYPEDEF)
 | 
| 282 |         (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
 | 
| 283 | #else
 | 
| 284 |         (VFunction *)on_completion_display_matches_hook : 0;
 | 
| 285 | #endif
 | 
| 286 | #endif
 | 
| 287 |     return result;
 | 
| 288 | 
 | 
| 289 | }
 | 
| 290 | 
 | 
| 291 | PyDoc_STRVAR(doc_set_completion_display_matches_hook,
 | 
| 292 | "set_completion_display_matches_hook([function]) -> None\n\
 | 
| 293 | Set or remove the completion display function.\n\
 | 
| 294 | The function is called as\n\
 | 
| 295 |   function(substitution, [matches], longest_match_length)\n\
 | 
| 296 | once each time matches need to be displayed.");
 | 
| 297 | 
 | 
| 298 | static PyObject *
 | 
| 299 | set_startup_hook(PyObject *self, PyObject *args)
 | 
| 300 | {
 | 
| 301 |     return set_hook("startup_hook", &startup_hook, args);
 | 
| 302 | }
 | 
| 303 | 
 | 
| 304 | PyDoc_STRVAR(doc_set_startup_hook,
 | 
| 305 | "set_startup_hook([function]) -> None\n\
 | 
| 306 | Set or remove the function invoked by the rl_startup_hook callback.\n\
 | 
| 307 | The function is called with no arguments just\n\
 | 
| 308 | before readline prints the first prompt.");
 | 
| 309 | 
 | 
| 310 | 
 | 
| 311 | #ifdef HAVE_RL_PRE_INPUT_HOOK
 | 
| 312 | 
 | 
| 313 | /* Set pre-input hook */
 | 
| 314 | 
 | 
| 315 | static PyObject *
 | 
| 316 | set_pre_input_hook(PyObject *self, PyObject *args)
 | 
| 317 | {
 | 
| 318 |     return set_hook("pre_input_hook", &pre_input_hook, args);
 | 
| 319 | }
 | 
| 320 | 
 | 
| 321 | PyDoc_STRVAR(doc_set_pre_input_hook,
 | 
| 322 | "set_pre_input_hook([function]) -> None\n\
 | 
| 323 | Set or remove the function invoked by the rl_pre_input_hook callback.\n\
 | 
| 324 | The function is called with no arguments after the first prompt\n\
 | 
| 325 | has been printed and just before readline starts reading input\n\
 | 
| 326 | characters.");
 | 
| 327 | 
 | 
| 328 | #endif
 | 
| 329 | 
 | 
| 330 | 
 | 
| 331 | /* Exported function to specify a word completer in Python */
 | 
| 332 | 
 | 
| 333 | static PyObject *completer = NULL;
 | 
| 334 | 
 | 
| 335 | static PyObject *begidx = NULL;
 | 
| 336 | static PyObject *endidx = NULL;
 | 
| 337 | 
 | 
| 338 | 
 | 
| 339 | /* Get the completion type for the scope of the tab-completion */
 | 
| 340 | static PyObject *
 | 
| 341 | get_completion_type(PyObject *self, PyObject *noarg)
 | 
| 342 | {
 | 
| 343 |   return PyInt_FromLong(rl_completion_type);
 | 
| 344 | }
 | 
| 345 | 
 | 
| 346 | PyDoc_STRVAR(doc_get_completion_type,
 | 
| 347 | "get_completion_type() -> int\n\
 | 
| 348 | Get the type of completion being attempted.");
 | 
| 349 | 
 | 
| 350 | 
 | 
| 351 | /* Get the beginning index for the scope of the tab-completion */
 | 
| 352 | 
 | 
| 353 | static PyObject *
 | 
| 354 | get_begidx(PyObject *self, PyObject *noarg)
 | 
| 355 | {
 | 
| 356 |     Py_INCREF(begidx);
 | 
| 357 |     return begidx;
 | 
| 358 | }
 | 
| 359 | 
 | 
| 360 | PyDoc_STRVAR(doc_get_begidx,
 | 
| 361 | "get_begidx() -> int\n\
 | 
| 362 | get the beginning index of the completion scope");
 | 
| 363 | 
 | 
| 364 | 
 | 
| 365 | /* Get the ending index for the scope of the tab-completion */
 | 
| 366 | 
 | 
| 367 | static PyObject *
 | 
| 368 | get_endidx(PyObject *self, PyObject *noarg)
 | 
| 369 | {
 | 
| 370 |     Py_INCREF(endidx);
 | 
| 371 |     return endidx;
 | 
| 372 | }
 | 
| 373 | 
 | 
| 374 | PyDoc_STRVAR(doc_get_endidx,
 | 
| 375 | "get_endidx() -> int\n\
 | 
| 376 | get the ending index of the completion scope");
 | 
| 377 | 
 | 
| 378 | 
 | 
| 379 | /* Set the tab-completion word-delimiters that readline uses */
 | 
| 380 | 
 | 
| 381 | static PyObject *
 | 
| 382 | set_completer_delims(PyObject *self, PyObject *args)
 | 
| 383 | {
 | 
| 384 |     char *break_chars;
 | 
| 385 | 
 | 
| 386 |     if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
 | 
| 387 |         return NULL;
 | 
| 388 |     }
 | 
| 389 |     /* Keep a reference to the allocated memory in the module state in case
 | 
| 390 |        some other module modifies rl_completer_word_break_characters
 | 
| 391 |        (see issue #17289). */
 | 
| 392 |     break_chars = strdup(break_chars);
 | 
| 393 |     if (break_chars) {
 | 
| 394 |         free(completer_word_break_characters);
 | 
| 395 |         completer_word_break_characters = break_chars;
 | 
| 396 |         rl_completer_word_break_characters = break_chars;
 | 
| 397 |         Py_RETURN_NONE;
 | 
| 398 |     }
 | 
| 399 |     else
 | 
| 400 |         return PyErr_NoMemory();
 | 
| 401 | }
 | 
| 402 | 
 | 
| 403 | PyDoc_STRVAR(doc_set_completer_delims,
 | 
| 404 | "set_completer_delims(string) -> None\n\
 | 
| 405 | set the word delimiters for completion");
 | 
| 406 | 
 | 
| 407 | /* _py_free_history_entry: Utility function to free a history entry. */
 | 
| 408 | 
 | 
| 409 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
 | 
| 410 | 
 | 
| 411 | /* Readline version >= 5.0 introduced a timestamp field into the history entry
 | 
| 412 |    structure; this needs to be freed to avoid a memory leak.  This version of
 | 
| 413 |    readline also introduced the handy 'free_history_entry' function, which
 | 
| 414 |    takes care of the timestamp. */
 | 
| 415 | 
 | 
| 416 | static void
 | 
| 417 | _py_free_history_entry(HIST_ENTRY *entry)
 | 
| 418 | {
 | 
| 419 |     histdata_t data = free_history_entry(entry);
 | 
| 420 |     free(data);
 | 
| 421 | }
 | 
| 422 | 
 | 
| 423 | #else
 | 
| 424 | 
 | 
| 425 | /* No free_history_entry function;  free everything manually. */
 | 
| 426 | 
 | 
| 427 | static void
 | 
| 428 | _py_free_history_entry(HIST_ENTRY *entry)
 | 
| 429 | {
 | 
| 430 |     if (entry->line)
 | 
| 431 |         free((void *)entry->line);
 | 
| 432 |     if (entry->data)
 | 
| 433 |         free(entry->data);
 | 
| 434 |     free(entry);
 | 
| 435 | }
 | 
| 436 | 
 | 
| 437 | #endif
 | 
| 438 | 
 | 
| 439 | static PyObject *
 | 
| 440 | py_remove_history(PyObject *self, PyObject *args)
 | 
| 441 | {
 | 
| 442 |     int entry_number;
 | 
| 443 |     HIST_ENTRY *entry;
 | 
| 444 | 
 | 
| 445 |     if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
 | 
| 446 |         return NULL;
 | 
| 447 |     if (entry_number < 0) {
 | 
| 448 |         PyErr_SetString(PyExc_ValueError,
 | 
| 449 |                         "History index cannot be negative");
 | 
| 450 |         return NULL;
 | 
| 451 |     }
 | 
| 452 |     entry = remove_history(entry_number);
 | 
| 453 |     if (!entry) {
 | 
| 454 |         PyErr_Format(PyExc_ValueError,
 | 
| 455 |                      "No history item at position %d",
 | 
| 456 |                       entry_number);
 | 
| 457 |         return NULL;
 | 
| 458 |     }
 | 
| 459 |     /* free memory allocated for the history entry */
 | 
| 460 |     _py_free_history_entry(entry);
 | 
| 461 |     Py_RETURN_NONE;
 | 
| 462 | }
 | 
| 463 | 
 | 
| 464 | PyDoc_STRVAR(doc_remove_history,
 | 
| 465 | "remove_history_item(pos) -> None\n\
 | 
| 466 | remove history item given by its position");
 | 
| 467 | 
 | 
| 468 | static PyObject *
 | 
| 469 | py_replace_history(PyObject *self, PyObject *args)
 | 
| 470 | {
 | 
| 471 |     int entry_number;
 | 
| 472 |     char *line;
 | 
| 473 |     HIST_ENTRY *old_entry;
 | 
| 474 | 
 | 
| 475 |     if (!PyArg_ParseTuple(args, "is:replace_history_item", &entry_number,
 | 
| 476 |                           &line)) {
 | 
| 477 |         return NULL;
 | 
| 478 |     }
 | 
| 479 |     if (entry_number < 0) {
 | 
| 480 |         PyErr_SetString(PyExc_ValueError,
 | 
| 481 |                         "History index cannot be negative");
 | 
| 482 |         return NULL;
 | 
| 483 |     }
 | 
| 484 |     old_entry = replace_history_entry(entry_number, line, (void *)NULL);
 | 
| 485 |     if (!old_entry) {
 | 
| 486 |         PyErr_Format(PyExc_ValueError,
 | 
| 487 |                      "No history item at position %d",
 | 
| 488 |                      entry_number);
 | 
| 489 |         return NULL;
 | 
| 490 |     }
 | 
| 491 |     /* free memory allocated for the old history entry */
 | 
| 492 |     _py_free_history_entry(old_entry);
 | 
| 493 |     Py_RETURN_NONE;
 | 
| 494 | }
 | 
| 495 | 
 | 
| 496 | PyDoc_STRVAR(doc_replace_history,
 | 
| 497 | "replace_history_item(pos, line) -> None\n\
 | 
| 498 | replaces history item given by its position with contents of line");
 | 
| 499 | 
 | 
| 500 | /* Add a line to the history buffer */
 | 
| 501 | 
 | 
| 502 | static PyObject *
 | 
| 503 | py_add_history(PyObject *self, PyObject *args)
 | 
| 504 | {
 | 
| 505 |     char *line;
 | 
| 506 | 
 | 
| 507 |     if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
 | 
| 508 |         return NULL;
 | 
| 509 |     }
 | 
| 510 |     add_history(line);
 | 
| 511 |     Py_RETURN_NONE;
 | 
| 512 | }
 | 
| 513 | 
 | 
| 514 | PyDoc_STRVAR(doc_add_history,
 | 
| 515 | "add_history(string) -> None\n\
 | 
| 516 | add an item to the history buffer");
 | 
| 517 | 
 | 
| 518 | 
 | 
| 519 | /* Get the tab-completion word-delimiters that readline uses */
 | 
| 520 | 
 | 
| 521 | static PyObject *
 | 
| 522 | get_completer_delims(PyObject *self, PyObject *noarg)
 | 
| 523 | {
 | 
| 524 |     return PyString_FromString(rl_completer_word_break_characters);
 | 
| 525 | }
 | 
| 526 | 
 | 
| 527 | PyDoc_STRVAR(doc_get_completer_delims,
 | 
| 528 | "get_completer_delims() -> string\n\
 | 
| 529 | get the word delimiters for completion");
 | 
| 530 | 
 | 
| 531 | 
 | 
| 532 | /* Set the completer function */
 | 
| 533 | 
 | 
| 534 | static PyObject *
 | 
| 535 | set_completer(PyObject *self, PyObject *args)
 | 
| 536 | {
 | 
| 537 |     return set_hook("completer", &completer, args);
 | 
| 538 | }
 | 
| 539 | 
 | 
| 540 | PyDoc_STRVAR(doc_set_completer,
 | 
| 541 | "set_completer([function]) -> None\n\
 | 
| 542 | Set or remove the completer function.\n\
 | 
| 543 | The function is called as function(text, state),\n\
 | 
| 544 | for state in 0, 1, 2, ..., until it returns a non-string.\n\
 | 
| 545 | It should return the next possible completion starting with 'text'.");
 | 
| 546 | 
 | 
| 547 | 
 | 
| 548 | static PyObject *
 | 
| 549 | get_completer(PyObject *self, PyObject *noargs)
 | 
| 550 | {
 | 
| 551 |     if (completer == NULL) {
 | 
| 552 |         Py_RETURN_NONE;
 | 
| 553 |     }
 | 
| 554 |     Py_INCREF(completer);
 | 
| 555 |     return completer;
 | 
| 556 | }
 | 
| 557 | 
 | 
| 558 | PyDoc_STRVAR(doc_get_completer,
 | 
| 559 | "get_completer() -> function\n\
 | 
| 560 | \n\
 | 
| 561 | Returns current completer function.");
 | 
| 562 | 
 | 
| 563 | /* Private function to get current length of history.  XXX It may be
 | 
| 564 |  * possible to replace this with a direct use of history_length instead,
 | 
| 565 |  * but it's not clear whether BSD's libedit keeps history_length up to date.
 | 
| 566 |  * See issue #8065.*/
 | 
| 567 | 
 | 
| 568 | static int
 | 
| 569 | _py_get_history_length(void)
 | 
| 570 | {
 | 
| 571 |     HISTORY_STATE *hist_st = history_get_history_state();
 | 
| 572 |     int length = hist_st->length;
 | 
| 573 |     /* the history docs don't say so, but the address of hist_st changes each
 | 
| 574 |        time history_get_history_state is called which makes me think it's
 | 
| 575 |        freshly malloc'd memory...  on the other hand, the address of the last
 | 
| 576 |        line stays the same as long as history isn't extended, so it appears to
 | 
| 577 |        be malloc'd but managed by the history package... */
 | 
| 578 |     free(hist_st);
 | 
| 579 |     return length;
 | 
| 580 | }
 | 
| 581 | 
 | 
| 582 | /* Exported function to get any element of history */
 | 
| 583 | 
 | 
| 584 | static PyObject *
 | 
| 585 | get_history_item(PyObject *self, PyObject *args)
 | 
| 586 | {
 | 
| 587 |     int idx = 0;
 | 
| 588 |     HIST_ENTRY *hist_ent;
 | 
| 589 | 
 | 
| 590 |     if (!PyArg_ParseTuple(args, "i:get_history_item", &idx))
 | 
| 591 |         return NULL;
 | 
| 592 | #ifdef  __APPLE__
 | 
| 593 |     if (using_libedit_emulation) {
 | 
| 594 |         /* Older versions of libedit's readline emulation
 | 
| 595 |          * use 0-based indexes, while readline and newer
 | 
| 596 |          * versions of libedit use 1-based indexes.
 | 
| 597 |          */
 | 
| 598 |         int length = _py_get_history_length();
 | 
| 599 | 
 | 
| 600 |         idx = idx - 1 + libedit_history_start;
 | 
| 601 | 
 | 
| 602 |         /*
 | 
| 603 |          * Apple's readline emulation crashes when
 | 
| 604 |          * the index is out of range, therefore
 | 
| 605 |          * test for that and fail gracefully.
 | 
| 606 |          */
 | 
| 607 |         if (idx < (0 + libedit_history_start)
 | 
| 608 |                 || idx >= (length + libedit_history_start)) {
 | 
| 609 |             Py_RETURN_NONE;
 | 
| 610 |         }
 | 
| 611 |     }
 | 
| 612 | #endif /* __APPLE__ */
 | 
| 613 |     if ((hist_ent = history_get(idx)))
 | 
| 614 |         return PyString_FromString(hist_ent->line);
 | 
| 615 |     else {
 | 
| 616 |         Py_RETURN_NONE;
 | 
| 617 |     }
 | 
| 618 | }
 | 
| 619 | 
 | 
| 620 | PyDoc_STRVAR(doc_get_history_item,
 | 
| 621 | "get_history_item() -> string\n\
 | 
| 622 | return the current contents of history item at index.");
 | 
| 623 | 
 | 
| 624 | 
 | 
| 625 | /* Exported function to get current length of history */
 | 
| 626 | 
 | 
| 627 | static PyObject *
 | 
| 628 | get_current_history_length(PyObject *self, PyObject *noarg)
 | 
| 629 | {
 | 
| 630 |     return PyInt_FromLong((long)_py_get_history_length());
 | 
| 631 | }
 | 
| 632 | 
 | 
| 633 | PyDoc_STRVAR(doc_get_current_history_length,
 | 
| 634 | "get_current_history_length() -> integer\n\
 | 
| 635 | return the current (not the maximum) length of history.");
 | 
| 636 | 
 | 
| 637 | 
 | 
| 638 | /* Exported function to read the current line buffer */
 | 
| 639 | 
 | 
| 640 | static PyObject *
 | 
| 641 | get_line_buffer(PyObject *self, PyObject *noarg)
 | 
| 642 | {
 | 
| 643 |     return PyString_FromString(rl_line_buffer);
 | 
| 644 | }
 | 
| 645 | 
 | 
| 646 | PyDoc_STRVAR(doc_get_line_buffer,
 | 
| 647 | "get_line_buffer() -> string\n\
 | 
| 648 | return the current contents of the line buffer.");
 | 
| 649 | 
 | 
| 650 | 
 | 
| 651 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
 | 
| 652 | 
 | 
| 653 | /* Exported function to clear the current history */
 | 
| 654 | 
 | 
| 655 | static PyObject *
 | 
| 656 | py_clear_history(PyObject *self, PyObject *noarg)
 | 
| 657 | {
 | 
| 658 |     clear_history();
 | 
| 659 |     Py_RETURN_NONE;
 | 
| 660 | }
 | 
| 661 | 
 | 
| 662 | PyDoc_STRVAR(doc_clear_history,
 | 
| 663 | "clear_history() -> None\n\
 | 
| 664 | Clear the current readline history.");
 | 
| 665 | #endif
 | 
| 666 | 
 | 
| 667 | /* Added for OSH.  We need to call this in our SIGWINCH handler so global
 | 
| 668 |  * variables in readline get updated. */
 | 
| 669 | static PyObject *
 | 
| 670 | py_resize_terminal(PyObject *self, PyObject *noarg)
 | 
| 671 | {
 | 
| 672 |     rl_resize_terminal();
 | 
| 673 |     Py_RETURN_NONE;
 | 
| 674 | }
 | 
| 675 | 
 | 
| 676 | /* Exported function to insert text into the line buffer */
 | 
| 677 | 
 | 
| 678 | static PyObject *
 | 
| 679 | insert_text(PyObject *self, PyObject *args)
 | 
| 680 | {
 | 
| 681 |     char *s;
 | 
| 682 |     if (!PyArg_ParseTuple(args, "s:insert_text", &s))
 | 
| 683 |         return NULL;
 | 
| 684 |     rl_insert_text(s);
 | 
| 685 |     Py_RETURN_NONE;
 | 
| 686 | }
 | 
| 687 | 
 | 
| 688 | PyDoc_STRVAR(doc_insert_text,
 | 
| 689 | "insert_text(string) -> None\n\
 | 
| 690 | Insert text into the line buffer at the cursor position.");
 | 
| 691 | 
 | 
| 692 | 
 | 
| 693 | /* Redisplay the line buffer */
 | 
| 694 | 
 | 
| 695 | static PyObject *
 | 
| 696 | redisplay(PyObject *self, PyObject *noarg)
 | 
| 697 | {
 | 
| 698 |     rl_redisplay();
 | 
| 699 |     Py_RETURN_NONE;
 | 
| 700 | }
 | 
| 701 | 
 | 
| 702 | PyDoc_STRVAR(doc_redisplay,
 | 
| 703 | "redisplay() -> None\n\
 | 
| 704 | Change what's displayed on the screen to reflect the current\n\
 | 
| 705 | contents of the line buffer.");
 | 
| 706 | 
 | 
| 707 | 
 | 
| 708 | /* Table of functions exported by the module */
 | 
| 709 | 
 | 
| 710 | #ifdef OVM_MAIN
 | 
| 711 | #include "pyext/line_input.c/readline_methods.def"
 | 
| 712 | #else
 | 
| 713 | static struct PyMethodDef readline_methods[] = {
 | 
| 714 |     {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
 | 
| 715 |     {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
 | 
| 716 |     {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
 | 
| 717 |     {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
 | 
| 718 |     {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
 | 
| 719 |     {"read_history_file", read_history_file,
 | 
| 720 |      METH_VARARGS, doc_read_history_file},
 | 
| 721 |     {"write_history_file", write_history_file,
 | 
| 722 |      METH_VARARGS, doc_write_history_file},
 | 
| 723 |     {"get_history_item", get_history_item,
 | 
| 724 |      METH_VARARGS, doc_get_history_item},
 | 
| 725 |     {"get_current_history_length", (PyCFunction)get_current_history_length,
 | 
| 726 |      METH_NOARGS, doc_get_current_history_length},
 | 
| 727 |     {"set_history_length", set_history_length,
 | 
| 728 |      METH_VARARGS, set_history_length_doc},
 | 
| 729 |     {"get_history_length", get_history_length,
 | 
| 730 |      METH_NOARGS, get_history_length_doc},
 | 
| 731 |     {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
 | 
| 732 |     {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
 | 
| 733 |     {"get_completion_type", get_completion_type,
 | 
| 734 |      METH_NOARGS, doc_get_completion_type},
 | 
| 735 |     {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
 | 
| 736 |     {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
 | 
| 737 | 
 | 
| 738 |     {"set_completer_delims", set_completer_delims,
 | 
| 739 |      METH_VARARGS, doc_set_completer_delims},
 | 
| 740 |     {"add_history", py_add_history, METH_VARARGS, doc_add_history},
 | 
| 741 |     {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
 | 
| 742 |     {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
 | 
| 743 |     {"get_completer_delims", get_completer_delims,
 | 
| 744 |      METH_NOARGS, doc_get_completer_delims},
 | 
| 745 | 
 | 
| 746 |     {"set_completion_display_matches_hook", set_completion_display_matches_hook,
 | 
| 747 |      METH_VARARGS, doc_set_completion_display_matches_hook},
 | 
| 748 |     {"set_startup_hook", set_startup_hook,
 | 
| 749 |      METH_VARARGS, doc_set_startup_hook},
 | 
| 750 |     {"set_pre_input_hook", set_pre_input_hook,
 | 
| 751 |      METH_VARARGS, doc_set_pre_input_hook},
 | 
| 752 |     {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
 | 
| 753 |     {"resize_terminal", py_resize_terminal, METH_NOARGS, ""},
 | 
| 754 |     {0, 0}
 | 
| 755 | };
 | 
| 756 | #endif
 | 
| 757 | 
 | 
| 758 | 
 | 
| 759 | /* C function to call the Python hooks. */
 | 
| 760 | 
 | 
| 761 | static int
 | 
| 762 | on_hook(PyObject *func)
 | 
| 763 | {
 | 
| 764 |     int result = 0;
 | 
| 765 |     if (func != NULL) {
 | 
| 766 |         PyObject *r;
 | 
| 767 | #ifdef WITH_THREAD
 | 
| 768 |         PyGILState_STATE gilstate = PyGILState_Ensure();
 | 
| 769 | #endif
 | 
| 770 |         r = PyObject_CallFunction(func, NULL);
 | 
| 771 |         if (r == NULL)
 | 
| 772 |             goto error;
 | 
| 773 |         if (r == Py_None)
 | 
| 774 |             result = 0;
 | 
| 775 |         else {
 | 
| 776 |             result = PyInt_AsLong(r);
 | 
| 777 |             if (result == -1 && PyErr_Occurred())
 | 
| 778 |                 goto error;
 | 
| 779 |         }
 | 
| 780 |         Py_DECREF(r);
 | 
| 781 |         goto done;
 | 
| 782 |       error:
 | 
| 783 |         PyErr_Clear();
 | 
| 784 |         Py_XDECREF(r);
 | 
| 785 |       done:
 | 
| 786 | #ifdef WITH_THREAD
 | 
| 787 |         PyGILState_Release(gilstate);
 | 
| 788 | #endif
 | 
| 789 |         return result;
 | 
| 790 |     }
 | 
| 791 |     return result;
 | 
| 792 | }
 | 
| 793 | 
 | 
| 794 | static int
 | 
| 795 | #if defined(_RL_FUNCTION_TYPEDEF)
 | 
| 796 | on_startup_hook(void)
 | 
| 797 | #else
 | 
| 798 | on_startup_hook()
 | 
| 799 | #endif
 | 
| 800 | {
 | 
| 801 |     return on_hook(startup_hook);
 | 
| 802 | }
 | 
| 803 | 
 | 
| 804 | #ifdef HAVE_RL_PRE_INPUT_HOOK
 | 
| 805 | static int
 | 
| 806 | #if defined(_RL_FUNCTION_TYPEDEF)
 | 
| 807 | on_pre_input_hook(void)
 | 
| 808 | #else
 | 
| 809 | on_pre_input_hook()
 | 
| 810 | #endif
 | 
| 811 | {
 | 
| 812 |     return on_hook(pre_input_hook);
 | 
| 813 | }
 | 
| 814 | #endif
 | 
| 815 | 
 | 
| 816 | 
 | 
| 817 | /* C function to call the Python completion_display_matches */
 | 
| 818 | 
 | 
| 819 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
 | 
| 820 | static void
 | 
| 821 | on_completion_display_matches_hook(char **matches,
 | 
| 822 |                                    int num_matches, int max_length)
 | 
| 823 | {
 | 
| 824 |     int i;
 | 
| 825 |     PyObject *m=NULL, *s=NULL, *r=NULL;
 | 
| 826 | #ifdef WITH_THREAD
 | 
| 827 |     PyGILState_STATE gilstate = PyGILState_Ensure();
 | 
| 828 | #endif
 | 
| 829 |     m = PyList_New(num_matches);
 | 
| 830 |     if (m == NULL)
 | 
| 831 |         goto error;
 | 
| 832 |     for (i = 0; i < num_matches; i++) {
 | 
| 833 |         s = PyString_FromString(matches[i+1]);
 | 
| 834 |         if (s == NULL)
 | 
| 835 |             goto error;
 | 
| 836 |         if (PyList_SetItem(m, i, s) == -1)
 | 
| 837 |             goto error;
 | 
| 838 |     }
 | 
| 839 | 
 | 
| 840 |     r = PyObject_CallFunction(completion_display_matches_hook,
 | 
| 841 |                               "sOi", matches[0], m, max_length);
 | 
| 842 | 
 | 
| 843 |     Py_DECREF(m); m=NULL;
 | 
| 844 | 
 | 
| 845 |     if (r == NULL ||
 | 
| 846 |         (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
 | 
| 847 |         goto error;
 | 
| 848 |     }
 | 
| 849 |     Py_XDECREF(r); r=NULL;
 | 
| 850 | 
 | 
| 851 |     if (0) {
 | 
| 852 |     error:
 | 
| 853 |         PyErr_Clear();
 | 
| 854 |         Py_XDECREF(m);
 | 
| 855 |         Py_XDECREF(r);
 | 
| 856 |     }
 | 
| 857 | #ifdef WITH_THREAD
 | 
| 858 |     PyGILState_Release(gilstate);
 | 
| 859 | #endif
 | 
| 860 | }
 | 
| 861 | 
 | 
| 862 | #endif
 | 
| 863 | 
 | 
| 864 | #ifdef HAVE_RL_RESIZE_TERMINAL
 | 
| 865 | static volatile sig_atomic_t sigwinch_received;
 | 
| 866 | static PyOS_sighandler_t sigwinch_ohandler;
 | 
| 867 | 
 | 
| 868 | static void
 | 
| 869 | readline_sigwinch_handler(int signum)
 | 
| 870 | {
 | 
| 871 |     sigwinch_received = 1;
 | 
| 872 |     if (sigwinch_ohandler &&
 | 
| 873 |             sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
 | 
| 874 |         sigwinch_ohandler(signum);
 | 
| 875 | }
 | 
| 876 | #endif
 | 
| 877 | 
 | 
| 878 | /* C function to call the Python completer. */
 | 
| 879 | 
 | 
| 880 | static char *
 | 
| 881 | on_completion(const char *text, int state)
 | 
| 882 | {
 | 
| 883 |     char *result = NULL;
 | 
| 884 |     if (completer != NULL) {
 | 
| 885 |         PyObject *r;
 | 
| 886 | #ifdef WITH_THREAD
 | 
| 887 |         PyGILState_STATE gilstate = PyGILState_Ensure();
 | 
| 888 | #endif
 | 
| 889 |         rl_attempted_completion_over = 1;
 | 
| 890 |         r = PyObject_CallFunction(completer, "si", text, state);
 | 
| 891 |         if (r == NULL)
 | 
| 892 |             goto error;
 | 
| 893 |         if (r == Py_None) {
 | 
| 894 |             result = NULL;
 | 
| 895 |         }
 | 
| 896 |         else {
 | 
| 897 |             char *s = PyString_AsString(r);
 | 
| 898 |             if (s == NULL)
 | 
| 899 |                 goto error;
 | 
| 900 |             result = strdup(s);
 | 
| 901 |         }
 | 
| 902 |         Py_DECREF(r);
 | 
| 903 |         goto done;
 | 
| 904 |       error:
 | 
| 905 |         PyErr_Clear();
 | 
| 906 |         Py_XDECREF(r);
 | 
| 907 |       done:
 | 
| 908 | #ifdef WITH_THREAD
 | 
| 909 |         PyGILState_Release(gilstate);
 | 
| 910 | #endif
 | 
| 911 |         return result;
 | 
| 912 |     }
 | 
| 913 |     return result;
 | 
| 914 | }
 | 
| 915 | 
 | 
| 916 | 
 | 
| 917 | /* A more flexible constructor that saves the "begidx" and "endidx"
 | 
| 918 |  * before calling the normal completer */
 | 
| 919 | 
 | 
| 920 | static char **
 | 
| 921 | flex_complete(const char *text, int start, int end)
 | 
| 922 | {
 | 
| 923 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
 | 
| 924 |     rl_completion_append_character ='\0';
 | 
| 925 | #endif
 | 
| 926 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
 | 
| 927 |     rl_completion_suppress_append = 0;
 | 
| 928 | #endif
 | 
| 929 |     Py_XDECREF(begidx);
 | 
| 930 |     Py_XDECREF(endidx);
 | 
| 931 |     begidx = PyInt_FromLong((long) start);
 | 
| 932 |     endidx = PyInt_FromLong((long) end);
 | 
| 933 |     return completion_matches(text, *on_completion);
 | 
| 934 | }
 | 
| 935 | 
 | 
| 936 | 
 | 
| 937 | /* Helper to initialize GNU readline properly. */
 | 
| 938 | 
 | 
| 939 | static void
 | 
| 940 | setup_readline(void)
 | 
| 941 | {
 | 
| 942 | #ifdef SAVE_LOCALE
 | 
| 943 |     char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
 | 
| 944 |     if (!saved_locale)
 | 
| 945 |         Py_FatalError("not enough memory to save locale");
 | 
| 946 | #endif
 | 
| 947 | 
 | 
| 948 | #ifdef __APPLE__
 | 
| 949 |     /* the libedit readline emulation resets key bindings etc
 | 
| 950 |      * when calling rl_initialize.  So call it upfront
 | 
| 951 |      */
 | 
| 952 |     if (using_libedit_emulation)
 | 
| 953 |         rl_initialize();
 | 
| 954 | 
 | 
| 955 |     /* Detect if libedit's readline emulation uses 0-based
 | 
| 956 |      * indexing or 1-based indexing.
 | 
| 957 |      */
 | 
| 958 |     add_history("1");
 | 
| 959 |     if (history_get(1) == NULL) {
 | 
| 960 |         libedit_history_start = 0;
 | 
| 961 |     } else {
 | 
| 962 |         libedit_history_start = 1;
 | 
| 963 |     }
 | 
| 964 |     clear_history();
 | 
| 965 | #endif /* __APPLE__ */
 | 
| 966 | 
 | 
| 967 |     using_history();
 | 
| 968 | 
 | 
| 969 |     rl_readline_name = "oils";
 | 
| 970 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
 | 
| 971 |     /* Allow $if term= in .inputrc to work */
 | 
| 972 |     rl_terminal_name = getenv("TERM");
 | 
| 973 | #endif
 | 
| 974 |     /* Force rebind of TAB to insert-tab */
 | 
| 975 |     rl_bind_key('\t', rl_insert);
 | 
| 976 |     /* Bind both ESC-TAB and ESC-ESC to the completion function */
 | 
| 977 |     rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
 | 
| 978 |     rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
 | 
| 979 | #ifdef HAVE_RL_RESIZE_TERMINAL
 | 
| 980 |     /* Set up signal handler for window resize */
 | 
| 981 |     sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
 | 
| 982 | #endif
 | 
| 983 |     /* Set our hook functions */
 | 
| 984 |     rl_startup_hook = on_startup_hook;
 | 
| 985 | #ifdef HAVE_RL_PRE_INPUT_HOOK
 | 
| 986 |     rl_pre_input_hook = on_pre_input_hook;
 | 
| 987 | #endif
 | 
| 988 |     /* Set our completion function */
 | 
| 989 |     rl_attempted_completion_function = flex_complete;
 | 
| 990 |     /* Set Python word break characters */
 | 
| 991 |     completer_word_break_characters =
 | 
| 992 |         rl_completer_word_break_characters =
 | 
| 993 |         strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
 | 
| 994 |         /* All nonalphanums except '.' */
 | 
| 995 | 
 | 
| 996 |     begidx = PyInt_FromLong(0L);
 | 
| 997 |     endidx = PyInt_FromLong(0L);
 | 
| 998 | 
 | 
| 999 | #ifdef __APPLE__
 | 
| 1000 |     if (!using_libedit_emulation)
 | 
| 1001 | #endif
 | 
| 1002 |     {
 | 
| 1003 |         if (!isatty(STDOUT_FILENO)) {
 | 
| 1004 |             /* Issue #19884: stdout is not a terminal. Disable meta modifier
 | 
| 1005 |                keys to not write the ANSI sequence "\033[1034h" into stdout. On
 | 
| 1006 |                terminals supporting 8 bit characters like TERM=xterm-256color
 | 
| 1007 |                (which is now the default Fedora since Fedora 18), the meta key is
 | 
| 1008 |                used to enable support of 8 bit characters (ANSI sequence
 | 
| 1009 |                "\033[1034h").
 | 
| 1010 | 
 | 
| 1011 |                With libedit, this call makes readline() crash. */
 | 
| 1012 |             rl_variable_bind ("enable-meta-key", "off");
 | 
| 1013 |         }
 | 
| 1014 |     }
 | 
| 1015 | 
 | 
| 1016 |     /* Initialize (allows .inputrc to override)
 | 
| 1017 |      *
 | 
| 1018 |      * XXX: A bug in the readline-2.2 library causes a memory leak
 | 
| 1019 |      * inside this function.  Nothing we can do about it.
 | 
| 1020 |      */
 | 
| 1021 | #ifdef __APPLE__
 | 
| 1022 |     if (using_libedit_emulation)
 | 
| 1023 |         rl_read_init_file(NULL);
 | 
| 1024 |     else
 | 
| 1025 | #endif /* __APPLE__ */
 | 
| 1026 |         rl_initialize();
 | 
| 1027 | 
 | 
| 1028 |     RESTORE_LOCALE(saved_locale)
 | 
| 1029 | }
 | 
| 1030 | 
 | 
| 1031 | /* Wrapper around GNU readline that handles signals differently. */
 | 
| 1032 | 
 | 
| 1033 | 
 | 
| 1034 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
 | 
| 1035 | 
 | 
| 1036 | static  char *completed_input_string;
 | 
| 1037 | static void
 | 
| 1038 | rlhandler(char *text)
 | 
| 1039 | {
 | 
| 1040 |     completed_input_string = text;
 | 
| 1041 |     rl_callback_handler_remove();
 | 
| 1042 | }
 | 
| 1043 | 
 | 
| 1044 | static char *
 | 
| 1045 | readline_until_enter_or_signal(char *prompt, int *signal)
 | 
| 1046 | {
 | 
| 1047 |     char * not_done_reading = "";
 | 
| 1048 |     fd_set selectset;
 | 
| 1049 | 
 | 
| 1050 |     *signal = 0;
 | 
| 1051 | #ifdef HAVE_RL_CATCH_SIGNAL
 | 
| 1052 |     rl_catch_signals = 0;
 | 
| 1053 | #endif
 | 
| 1054 |     /* OVM_MAIN: Oil is handling SIGWINCH, so readline shouldn't handle it.
 | 
| 1055 |      * Without this line, strace reveals that GNU readline is constantly
 | 
| 1056 |      * turning it on and off.
 | 
| 1057 |      * */
 | 
| 1058 |     rl_catch_sigwinch = 0;
 | 
| 1059 | 
 | 
| 1060 |     rl_callback_handler_install (prompt, rlhandler);
 | 
| 1061 |     FD_ZERO(&selectset);
 | 
| 1062 | 
 | 
| 1063 |     completed_input_string = not_done_reading;
 | 
| 1064 | 
 | 
| 1065 |     while (completed_input_string == not_done_reading) {
 | 
| 1066 |         int has_input = 0;
 | 
| 1067 | 
 | 
| 1068 |         while (!has_input)
 | 
| 1069 |         {               struct timeval timeout = {0, 100000}; /* 0.1 seconds */
 | 
| 1070 | 
 | 
| 1071 |             /* [Bug #1552726] Only limit the pause if an input hook has been
 | 
| 1072 |                defined.  */
 | 
| 1073 |             struct timeval *timeoutp = NULL;
 | 
| 1074 |             if (PyOS_InputHook)
 | 
| 1075 |                 timeoutp = &timeout;
 | 
| 1076 | #ifdef HAVE_RL_RESIZE_TERMINAL
 | 
| 1077 |             /* Update readline's view of the window size after SIGWINCH */
 | 
| 1078 |             if (sigwinch_received) {
 | 
| 1079 |                 sigwinch_received = 0;
 | 
| 1080 |                 rl_resize_terminal();
 | 
| 1081 |             }
 | 
| 1082 | #endif
 | 
| 1083 |             FD_SET(fileno(rl_instream), &selectset);
 | 
| 1084 |             /* select resets selectset if no input was available */
 | 
| 1085 |             has_input = select(fileno(rl_instream) + 1, &selectset,
 | 
| 1086 |                                NULL, NULL, timeoutp);
 | 
| 1087 |             if(PyOS_InputHook) PyOS_InputHook();
 | 
| 1088 |         }
 | 
| 1089 | 
 | 
| 1090 |         if(has_input > 0) {
 | 
| 1091 |             rl_callback_read_char();
 | 
| 1092 |         }
 | 
| 1093 |         else if (errno == EINTR) {
 | 
| 1094 |             int s;
 | 
| 1095 | #ifdef WITH_THREAD
 | 
| 1096 |             PyEval_RestoreThread(_PyOS_ReadlineTState);
 | 
| 1097 | #endif
 | 
| 1098 |             s = PyErr_CheckSignals();
 | 
| 1099 | #ifdef WITH_THREAD
 | 
| 1100 |             PyEval_SaveThread();
 | 
| 1101 | #endif
 | 
| 1102 |             if (s < 0) {
 | 
| 1103 |                 rl_free_line_state();
 | 
| 1104 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700
 | 
| 1105 |                 rl_callback_sigcleanup();
 | 
| 1106 | #endif
 | 
| 1107 |                 rl_cleanup_after_signal();
 | 
| 1108 |                 rl_callback_handler_remove();
 | 
| 1109 |                 *signal = 1;
 | 
| 1110 |                 completed_input_string = NULL;
 | 
| 1111 |             }
 | 
| 1112 |         }
 | 
| 1113 |     }
 | 
| 1114 | 
 | 
| 1115 |     return completed_input_string;
 | 
| 1116 | }
 | 
| 1117 | 
 | 
| 1118 | 
 | 
| 1119 | #else
 | 
| 1120 | 
 | 
| 1121 | /* Interrupt handler */
 | 
| 1122 | 
 | 
| 1123 | static jmp_buf jbuf;
 | 
| 1124 | 
 | 
| 1125 | /* ARGSUSED */
 | 
| 1126 | static void
 | 
| 1127 | onintr(int sig)
 | 
| 1128 | {
 | 
| 1129 |     longjmp(jbuf, 1);
 | 
| 1130 | }
 | 
| 1131 | 
 | 
| 1132 | 
 | 
| 1133 | static char *
 | 
| 1134 | readline_until_enter_or_signal(char *prompt, int *signal)
 | 
| 1135 | {
 | 
| 1136 |     PyOS_sighandler_t old_inthandler;
 | 
| 1137 |     char *p;
 | 
| 1138 | 
 | 
| 1139 |     *signal = 0;
 | 
| 1140 | 
 | 
| 1141 |     old_inthandler = PyOS_setsig(SIGINT, onintr);
 | 
| 1142 |     if (setjmp(jbuf)) {
 | 
| 1143 | #ifdef HAVE_SIGRELSE
 | 
| 1144 |         /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
 | 
| 1145 |         sigrelse(SIGINT);
 | 
| 1146 | #endif
 | 
| 1147 |         PyOS_setsig(SIGINT, old_inthandler);
 | 
| 1148 |         *signal = 1;
 | 
| 1149 |         return NULL;
 | 
| 1150 |     }
 | 
| 1151 |     rl_event_hook = PyOS_InputHook;
 | 
| 1152 |     p = readline(prompt);
 | 
| 1153 |     PyOS_setsig(SIGINT, old_inthandler);
 | 
| 1154 | 
 | 
| 1155 |     return p;
 | 
| 1156 | }
 | 
| 1157 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
 | 
| 1158 | 
 | 
| 1159 | 
 | 
| 1160 | static char *
 | 
| 1161 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
 | 
| 1162 | {
 | 
| 1163 |     size_t n;
 | 
| 1164 |     char *p, *q;
 | 
| 1165 |     int signal;
 | 
| 1166 | 
 | 
| 1167 | #ifdef SAVE_LOCALE
 | 
| 1168 |     char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
 | 
| 1169 |     if (!saved_locale)
 | 
| 1170 |         Py_FatalError("not enough memory to save locale");
 | 
| 1171 |     setlocale(LC_CTYPE, "");
 | 
| 1172 | #endif
 | 
| 1173 | 
 | 
| 1174 |     if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
 | 
| 1175 |         rl_instream = sys_stdin;
 | 
| 1176 |         rl_outstream = sys_stdout;
 | 
| 1177 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
 | 
| 1178 |         rl_prep_terminal (1);
 | 
| 1179 | #endif
 | 
| 1180 |     }
 | 
| 1181 | 
 | 
| 1182 |     p = readline_until_enter_or_signal(prompt, &signal);
 | 
| 1183 | 
 | 
| 1184 |     /* we got an interrupt signal */
 | 
| 1185 |     if (signal) {
 | 
| 1186 |         RESTORE_LOCALE(saved_locale)
 | 
| 1187 |         return NULL;
 | 
| 1188 |     }
 | 
| 1189 | 
 | 
| 1190 |     /* We got an EOF, return an empty string. */
 | 
| 1191 |     if (p == NULL) {
 | 
| 1192 |         p = PyMem_Malloc(1);
 | 
| 1193 |         if (p != NULL)
 | 
| 1194 |             *p = '\0';
 | 
| 1195 |         RESTORE_LOCALE(saved_locale)
 | 
| 1196 |         return p;
 | 
| 1197 |     }
 | 
| 1198 | 
 | 
| 1199 |     /* we have a valid line */
 | 
| 1200 |     n = strlen(p);
 | 
| 1201 |     /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
 | 
| 1202 |        release the original. */
 | 
| 1203 |     q = p;
 | 
| 1204 |     p = PyMem_Malloc(n+2);
 | 
| 1205 |     if (p != NULL) {
 | 
| 1206 |         strncpy(p, q, n);
 | 
| 1207 |         p[n] = '\n';
 | 
| 1208 |         p[n+1] = '\0';
 | 
| 1209 |     }
 | 
| 1210 |     free(q);
 | 
| 1211 |     RESTORE_LOCALE(saved_locale)
 | 
| 1212 |     return p;
 | 
| 1213 | }
 | 
| 1214 | 
 | 
| 1215 | 
 | 
| 1216 | /* Initialize the module */
 | 
| 1217 | 
 | 
| 1218 | PyDoc_STRVAR(doc_module,
 | 
| 1219 | "Importing this module enables command line editing using GNU readline.");
 | 
| 1220 | 
 | 
| 1221 | #ifdef __APPLE__
 | 
| 1222 | PyDoc_STRVAR(doc_module_le,
 | 
| 1223 | "Importing this module enables command line editing using libedit readline.");
 | 
| 1224 | #endif /* __APPLE__ */
 | 
| 1225 | 
 | 
| 1226 | PyMODINIT_FUNC
 | 
| 1227 | initline_input(void)
 | 
| 1228 | {
 | 
| 1229 |     PyObject *m;
 | 
| 1230 | 
 | 
| 1231 | #ifdef __APPLE__
 | 
| 1232 |     if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
 | 
| 1233 |         using_libedit_emulation = 1;
 | 
| 1234 |     }
 | 
| 1235 | 
 | 
| 1236 |     if (using_libedit_emulation)
 | 
| 1237 |         m = Py_InitModule4("line_input", readline_methods, doc_module_le,
 | 
| 1238 |                    (PyObject *)NULL, PYTHON_API_VERSION);
 | 
| 1239 |     else
 | 
| 1240 | 
 | 
| 1241 | #endif /* __APPLE__ */
 | 
| 1242 | 
 | 
| 1243 |     m = Py_InitModule4("line_input", readline_methods, doc_module,
 | 
| 1244 |                        (PyObject *)NULL, PYTHON_API_VERSION);
 | 
| 1245 |     if (m == NULL)
 | 
| 1246 |         return;
 | 
| 1247 | 
 | 
| 1248 |     PyOS_ReadlineFunctionPointer = call_readline;
 | 
| 1249 |     setup_readline();
 | 
| 1250 | 
 | 
| 1251 |     PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
 | 
| 1252 |     PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
 | 
| 1253 | }
 |