| 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 | #if defined(HAVE_SETLOCALE)
|
|---|
| 15 | /* GNU readline() mistakenly sets the LC_CTYPE locale.
|
|---|
| 16 | * This is evil. Only the user or the app's main() should do this!
|
|---|
| 17 | * We must save and restore the locale around the rl_initialize() call.
|
|---|
| 18 | */
|
|---|
| 19 | #define SAVE_LOCALE
|
|---|
| 20 | #include <locale.h>
|
|---|
| 21 | #endif
|
|---|
| 22 |
|
|---|
| 23 | #ifdef SAVE_LOCALE
|
|---|
| 24 | # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
|
|---|
| 25 | #else
|
|---|
| 26 | # define RESTORE_LOCALE(sl)
|
|---|
| 27 | #endif
|
|---|
| 28 |
|
|---|
| 29 | /* GNU readline definitions */
|
|---|
| 30 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
|
|---|
| 31 | #include <readline/readline.h>
|
|---|
| 32 | #include <readline/history.h>
|
|---|
| 33 |
|
|---|
| 34 | #ifdef HAVE_RL_COMPLETION_MATCHES
|
|---|
| 35 | #define completion_matches(x, y) \
|
|---|
| 36 | rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
|
|---|
| 37 | #else
|
|---|
| 38 | #if defined(_RL_FUNCTION_TYPEDEF)
|
|---|
| 39 | extern char **completion_matches(char *, rl_compentry_func_t *);
|
|---|
| 40 | #else
|
|---|
| 41 | #if !defined(__APPLE__)
|
|---|
| 42 | extern char **completion_matches(char *, CPFunction *);
|
|---|
| 43 | #endif
|
|---|
| 44 | #endif
|
|---|
| 45 | #endif
|
|---|
| 46 |
|
|---|
| 47 | #ifdef __APPLE__
|
|---|
| 48 | /*
|
|---|
| 49 | * It is possible to link the readline module to the readline
|
|---|
| 50 | * emulation library of editline/libedit.
|
|---|
| 51 | *
|
|---|
| 52 | * On OSX this emulation library is not 100% API compatible
|
|---|
| 53 | * with the "real" readline and cannot be detected at compile-time,
|
|---|
| 54 | * hence we use a runtime check to detect if we're using libedit
|
|---|
| 55 | *
|
|---|
| 56 | * Currently there is one know API incompatibility:
|
|---|
| 57 | * - 'get_history' has a 1-based index with GNU readline, and a 0-based
|
|---|
| 58 | * index with libedit's emulation.
|
|---|
| 59 | * - Note that replace_history and remove_history use a 0-based index
|
|---|
| 60 | * with both implementation.
|
|---|
| 61 | */
|
|---|
| 62 | static int using_libedit_emulation = 0;
|
|---|
| 63 | static const char libedit_version_tag[] = "EditLine wrapper";
|
|---|
| 64 | #endif /* __APPLE__ */
|
|---|
| 65 |
|
|---|
| 66 | static void
|
|---|
| 67 | on_completion_display_matches_hook(char **matches,
|
|---|
| 68 | int num_matches, int max_length);
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | /* Exported function to send one line to readline's init file parser */
|
|---|
| 72 |
|
|---|
| 73 | static PyObject *
|
|---|
| 74 | parse_and_bind(PyObject *self, PyObject *args)
|
|---|
| 75 | {
|
|---|
| 76 | char *s, *copy;
|
|---|
| 77 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
|
|---|
| 78 | return NULL;
|
|---|
| 79 | /* Make a copy -- rl_parse_and_bind() modifies its argument */
|
|---|
| 80 | /* Bernard Herzog */
|
|---|
| 81 | copy = malloc(1 + strlen(s));
|
|---|
| 82 | if (copy == NULL)
|
|---|
| 83 | return PyErr_NoMemory();
|
|---|
| 84 | strcpy(copy, s);
|
|---|
| 85 | rl_parse_and_bind(copy);
|
|---|
| 86 | free(copy); /* Free the copy */
|
|---|
| 87 | Py_RETURN_NONE;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | PyDoc_STRVAR(doc_parse_and_bind,
|
|---|
| 91 | "parse_and_bind(string) -> None\n\
|
|---|
| 92 | Parse and execute single line of a readline init file.");
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | /* Exported function to parse a readline init file */
|
|---|
| 96 |
|
|---|
| 97 | static PyObject *
|
|---|
| 98 | read_init_file(PyObject *self, PyObject *args)
|
|---|
| 99 | {
|
|---|
| 100 | char *s = NULL;
|
|---|
| 101 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
|
|---|
| 102 | return NULL;
|
|---|
| 103 | errno = rl_read_init_file(s);
|
|---|
| 104 | if (errno)
|
|---|
| 105 | return PyErr_SetFromErrno(PyExc_IOError);
|
|---|
| 106 | Py_RETURN_NONE;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | PyDoc_STRVAR(doc_read_init_file,
|
|---|
| 110 | "read_init_file([filename]) -> None\n\
|
|---|
| 111 | Parse a readline initialization file.\n\
|
|---|
| 112 | The default filename is the last filename used.");
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | /* Exported function to load a readline history file */
|
|---|
| 116 |
|
|---|
| 117 | static PyObject *
|
|---|
| 118 | read_history_file(PyObject *self, PyObject *args)
|
|---|
| 119 | {
|
|---|
| 120 | char *s = NULL;
|
|---|
| 121 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
|
|---|
| 122 | return NULL;
|
|---|
| 123 | errno = read_history(s);
|
|---|
| 124 | if (errno)
|
|---|
| 125 | return PyErr_SetFromErrno(PyExc_IOError);
|
|---|
| 126 | Py_RETURN_NONE;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | static int _history_length = -1; /* do not truncate history by default */
|
|---|
| 130 | PyDoc_STRVAR(doc_read_history_file,
|
|---|
| 131 | "read_history_file([filename]) -> None\n\
|
|---|
| 132 | Load a readline history file.\n\
|
|---|
| 133 | The default filename is ~/.history.");
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | /* Exported function to save a readline history file */
|
|---|
| 137 |
|
|---|
| 138 | static PyObject *
|
|---|
| 139 | write_history_file(PyObject *self, PyObject *args)
|
|---|
| 140 | {
|
|---|
| 141 | char *s = NULL;
|
|---|
| 142 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
|
|---|
| 143 | return NULL;
|
|---|
| 144 | errno = write_history(s);
|
|---|
| 145 | if (!errno && _history_length >= 0)
|
|---|
| 146 | history_truncate_file(s, _history_length);
|
|---|
| 147 | if (errno)
|
|---|
| 148 | return PyErr_SetFromErrno(PyExc_IOError);
|
|---|
| 149 | Py_RETURN_NONE;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | PyDoc_STRVAR(doc_write_history_file,
|
|---|
| 153 | "write_history_file([filename]) -> None\n\
|
|---|
| 154 | Save a readline history file.\n\
|
|---|
| 155 | The default filename is ~/.history.");
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 | /* Set history length */
|
|---|
| 159 |
|
|---|
| 160 | static PyObject*
|
|---|
| 161 | set_history_length(PyObject *self, PyObject *args)
|
|---|
| 162 | {
|
|---|
| 163 | int length = _history_length;
|
|---|
| 164 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
|
|---|
| 165 | return NULL;
|
|---|
| 166 | _history_length = length;
|
|---|
| 167 | Py_RETURN_NONE;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | PyDoc_STRVAR(set_history_length_doc,
|
|---|
| 171 | "set_history_length(length) -> None\n\
|
|---|
| 172 | set the maximal number of items which will be written to\n\
|
|---|
| 173 | the history file. A negative length is used to inhibit\n\
|
|---|
| 174 | history truncation.");
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 | /* Get history length */
|
|---|
| 178 |
|
|---|
| 179 | static PyObject*
|
|---|
| 180 | get_history_length(PyObject *self, PyObject *noarg)
|
|---|
| 181 | {
|
|---|
| 182 | return PyInt_FromLong(_history_length);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | PyDoc_STRVAR(get_history_length_doc,
|
|---|
| 186 | "get_history_length() -> int\n\
|
|---|
| 187 | return the maximum number of items that will be written to\n\
|
|---|
| 188 | the history file.");
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 | /* Generic hook function setter */
|
|---|
| 192 |
|
|---|
| 193 | static PyObject *
|
|---|
| 194 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
|
|---|
| 195 | {
|
|---|
| 196 | PyObject *function = Py_None;
|
|---|
| 197 | char buf[80];
|
|---|
| 198 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
|
|---|
| 199 | if (!PyArg_ParseTuple(args, buf, &function))
|
|---|
| 200 | return NULL;
|
|---|
| 201 | if (function == Py_None) {
|
|---|
| 202 | Py_XDECREF(*hook_var);
|
|---|
| 203 | *hook_var = NULL;
|
|---|
| 204 | }
|
|---|
| 205 | else if (PyCallable_Check(function)) {
|
|---|
| 206 | PyObject *tmp = *hook_var;
|
|---|
| 207 | Py_INCREF(function);
|
|---|
| 208 | *hook_var = function;
|
|---|
| 209 | Py_XDECREF(tmp);
|
|---|
| 210 | }
|
|---|
| 211 | else {
|
|---|
| 212 | PyOS_snprintf(buf, sizeof(buf),
|
|---|
| 213 | "set_%.50s(func): argument not callable",
|
|---|
| 214 | funcname);
|
|---|
| 215 | PyErr_SetString(PyExc_TypeError, buf);
|
|---|
| 216 | return NULL;
|
|---|
| 217 | }
|
|---|
| 218 | Py_RETURN_NONE;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 | /* Exported functions to specify hook functions in Python */
|
|---|
| 223 |
|
|---|
| 224 | static PyObject *completion_display_matches_hook = NULL;
|
|---|
| 225 | static PyObject *startup_hook = NULL;
|
|---|
| 226 |
|
|---|
| 227 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
|---|
| 228 | static PyObject *pre_input_hook = NULL;
|
|---|
| 229 | #endif
|
|---|
| 230 |
|
|---|
| 231 | static PyObject *
|
|---|
| 232 | set_completion_display_matches_hook(PyObject *self, PyObject *args)
|
|---|
| 233 | {
|
|---|
| 234 | PyObject *result = set_hook("completion_display_matches_hook",
|
|---|
| 235 | &completion_display_matches_hook, args);
|
|---|
| 236 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
|---|
| 237 | /* We cannot set this hook globally, since it replaces the
|
|---|
| 238 | default completion display. */
|
|---|
| 239 | rl_completion_display_matches_hook =
|
|---|
| 240 | completion_display_matches_hook ?
|
|---|
| 241 | #if defined(_RL_FUNCTION_TYPEDEF)
|
|---|
| 242 | (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
|
|---|
| 243 | #else
|
|---|
| 244 | (VFunction *)on_completion_display_matches_hook : 0;
|
|---|
| 245 | #endif
|
|---|
| 246 | #endif
|
|---|
| 247 | return result;
|
|---|
| 248 |
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | PyDoc_STRVAR(doc_set_completion_display_matches_hook,
|
|---|
| 252 | "set_completion_display_matches_hook([function]) -> None\n\
|
|---|
| 253 | Set or remove the completion display function.\n\
|
|---|
| 254 | The function is called as\n\
|
|---|
| 255 | function(substitution, [matches], longest_match_length)\n\
|
|---|
| 256 | once each time matches need to be displayed.");
|
|---|
| 257 |
|
|---|
| 258 | static PyObject *
|
|---|
| 259 | set_startup_hook(PyObject *self, PyObject *args)
|
|---|
| 260 | {
|
|---|
| 261 | return set_hook("startup_hook", &startup_hook, args);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | PyDoc_STRVAR(doc_set_startup_hook,
|
|---|
| 265 | "set_startup_hook([function]) -> None\n\
|
|---|
| 266 | Set or remove the startup_hook function.\n\
|
|---|
| 267 | The function is called with no arguments just\n\
|
|---|
| 268 | before readline prints the first prompt.");
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
|---|
| 272 |
|
|---|
| 273 | /* Set pre-input hook */
|
|---|
| 274 |
|
|---|
| 275 | static PyObject *
|
|---|
| 276 | set_pre_input_hook(PyObject *self, PyObject *args)
|
|---|
| 277 | {
|
|---|
| 278 | return set_hook("pre_input_hook", &pre_input_hook, args);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | PyDoc_STRVAR(doc_set_pre_input_hook,
|
|---|
| 282 | "set_pre_input_hook([function]) -> None\n\
|
|---|
| 283 | Set or remove the pre_input_hook function.\n\
|
|---|
| 284 | The function is called with no arguments after the first prompt\n\
|
|---|
| 285 | has been printed and just before readline starts reading input\n\
|
|---|
| 286 | characters.");
|
|---|
| 287 |
|
|---|
| 288 | #endif
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 | /* Exported function to specify a word completer in Python */
|
|---|
| 292 |
|
|---|
| 293 | static PyObject *completer = NULL;
|
|---|
| 294 |
|
|---|
| 295 | static PyObject *begidx = NULL;
|
|---|
| 296 | static PyObject *endidx = NULL;
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 | /* Get the completion type for the scope of the tab-completion */
|
|---|
| 300 | static PyObject *
|
|---|
| 301 | get_completion_type(PyObject *self, PyObject *noarg)
|
|---|
| 302 | {
|
|---|
| 303 | return PyInt_FromLong(rl_completion_type);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | PyDoc_STRVAR(doc_get_completion_type,
|
|---|
| 307 | "get_completion_type() -> int\n\
|
|---|
| 308 | Get the type of completion being attempted.");
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 | /* Get the beginning index for the scope of the tab-completion */
|
|---|
| 312 |
|
|---|
| 313 | static PyObject *
|
|---|
| 314 | get_begidx(PyObject *self, PyObject *noarg)
|
|---|
| 315 | {
|
|---|
| 316 | Py_INCREF(begidx);
|
|---|
| 317 | return begidx;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | PyDoc_STRVAR(doc_get_begidx,
|
|---|
| 321 | "get_begidx() -> int\n\
|
|---|
| 322 | get the beginning index of the readline tab-completion scope");
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 | /* Get the ending index for the scope of the tab-completion */
|
|---|
| 326 |
|
|---|
| 327 | static PyObject *
|
|---|
| 328 | get_endidx(PyObject *self, PyObject *noarg)
|
|---|
| 329 | {
|
|---|
| 330 | Py_INCREF(endidx);
|
|---|
| 331 | return endidx;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | PyDoc_STRVAR(doc_get_endidx,
|
|---|
| 335 | "get_endidx() -> int\n\
|
|---|
| 336 | get the ending index of the readline tab-completion scope");
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 | /* Set the tab-completion word-delimiters that readline uses */
|
|---|
| 340 |
|
|---|
| 341 | static PyObject *
|
|---|
| 342 | set_completer_delims(PyObject *self, PyObject *args)
|
|---|
| 343 | {
|
|---|
| 344 | char *break_chars;
|
|---|
| 345 |
|
|---|
| 346 | if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
|
|---|
| 347 | return NULL;
|
|---|
| 348 | }
|
|---|
| 349 | free((void*)rl_completer_word_break_characters);
|
|---|
| 350 | rl_completer_word_break_characters = strdup(break_chars);
|
|---|
| 351 | Py_RETURN_NONE;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | PyDoc_STRVAR(doc_set_completer_delims,
|
|---|
| 355 | "set_completer_delims(string) -> None\n\
|
|---|
| 356 | set the readline word delimiters for tab-completion");
|
|---|
| 357 |
|
|---|
| 358 | static PyObject *
|
|---|
| 359 | py_remove_history(PyObject *self, PyObject *args)
|
|---|
| 360 | {
|
|---|
| 361 | int entry_number;
|
|---|
| 362 | HIST_ENTRY *entry;
|
|---|
| 363 |
|
|---|
| 364 | if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
|
|---|
| 365 | return NULL;
|
|---|
| 366 | if (entry_number < 0) {
|
|---|
| 367 | PyErr_SetString(PyExc_ValueError,
|
|---|
| 368 | "History index cannot be negative");
|
|---|
| 369 | return NULL;
|
|---|
| 370 | }
|
|---|
| 371 | entry = remove_history(entry_number);
|
|---|
| 372 | if (!entry) {
|
|---|
| 373 | PyErr_Format(PyExc_ValueError,
|
|---|
| 374 | "No history item at position %d",
|
|---|
| 375 | entry_number);
|
|---|
| 376 | return NULL;
|
|---|
| 377 | }
|
|---|
| 378 | /* free memory allocated for the history entry */
|
|---|
| 379 | if (entry->line)
|
|---|
| 380 | free(entry->line);
|
|---|
| 381 | if (entry->data)
|
|---|
| 382 | free(entry->data);
|
|---|
| 383 | free(entry);
|
|---|
| 384 |
|
|---|
| 385 | Py_RETURN_NONE;
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | PyDoc_STRVAR(doc_remove_history,
|
|---|
| 389 | "remove_history_item(pos) -> None\n\
|
|---|
| 390 | remove history item given by its position");
|
|---|
| 391 |
|
|---|
| 392 | static PyObject *
|
|---|
| 393 | py_replace_history(PyObject *self, PyObject *args)
|
|---|
| 394 | {
|
|---|
| 395 | int entry_number;
|
|---|
| 396 | char *line;
|
|---|
| 397 | HIST_ENTRY *old_entry;
|
|---|
| 398 |
|
|---|
| 399 | if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
|
|---|
| 400 | &line)) {
|
|---|
| 401 | return NULL;
|
|---|
| 402 | }
|
|---|
| 403 | if (entry_number < 0) {
|
|---|
| 404 | PyErr_SetString(PyExc_ValueError,
|
|---|
| 405 | "History index cannot be negative");
|
|---|
| 406 | return NULL;
|
|---|
| 407 | }
|
|---|
| 408 | old_entry = replace_history_entry(entry_number, line, (void *)NULL);
|
|---|
| 409 | if (!old_entry) {
|
|---|
| 410 | PyErr_Format(PyExc_ValueError,
|
|---|
| 411 | "No history item at position %d",
|
|---|
| 412 | entry_number);
|
|---|
| 413 | return NULL;
|
|---|
| 414 | }
|
|---|
| 415 | /* free memory allocated for the old history entry */
|
|---|
| 416 | if (old_entry->line)
|
|---|
| 417 | free(old_entry->line);
|
|---|
| 418 | if (old_entry->data)
|
|---|
| 419 | free(old_entry->data);
|
|---|
| 420 | free(old_entry);
|
|---|
| 421 |
|
|---|
| 422 | Py_RETURN_NONE;
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | PyDoc_STRVAR(doc_replace_history,
|
|---|
| 426 | "replace_history_item(pos, line) -> None\n\
|
|---|
| 427 | replaces history item given by its position with contents of line");
|
|---|
| 428 |
|
|---|
| 429 | /* Add a line to the history buffer */
|
|---|
| 430 |
|
|---|
| 431 | static PyObject *
|
|---|
| 432 | py_add_history(PyObject *self, PyObject *args)
|
|---|
| 433 | {
|
|---|
| 434 | char *line;
|
|---|
| 435 |
|
|---|
| 436 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
|
|---|
| 437 | return NULL;
|
|---|
| 438 | }
|
|---|
| 439 | add_history(line);
|
|---|
| 440 | Py_RETURN_NONE;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | PyDoc_STRVAR(doc_add_history,
|
|---|
| 444 | "add_history(string) -> None\n\
|
|---|
| 445 | add a line to the history buffer");
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 | /* Get the tab-completion word-delimiters that readline uses */
|
|---|
| 449 |
|
|---|
| 450 | static PyObject *
|
|---|
| 451 | get_completer_delims(PyObject *self, PyObject *noarg)
|
|---|
| 452 | {
|
|---|
| 453 | return PyString_FromString(rl_completer_word_break_characters);
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | PyDoc_STRVAR(doc_get_completer_delims,
|
|---|
| 457 | "get_completer_delims() -> string\n\
|
|---|
| 458 | get the readline word delimiters for tab-completion");
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 | /* Set the completer function */
|
|---|
| 462 |
|
|---|
| 463 | static PyObject *
|
|---|
| 464 | set_completer(PyObject *self, PyObject *args)
|
|---|
| 465 | {
|
|---|
| 466 | return set_hook("completer", &completer, args);
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | PyDoc_STRVAR(doc_set_completer,
|
|---|
| 470 | "set_completer([function]) -> None\n\
|
|---|
| 471 | Set or remove the completer function.\n\
|
|---|
| 472 | The function is called as function(text, state),\n\
|
|---|
| 473 | for state in 0, 1, 2, ..., until it returns a non-string.\n\
|
|---|
| 474 | It should return the next possible completion starting with 'text'.");
|
|---|
| 475 |
|
|---|
| 476 |
|
|---|
| 477 | static PyObject *
|
|---|
| 478 | get_completer(PyObject *self, PyObject *noargs)
|
|---|
| 479 | {
|
|---|
| 480 | if (completer == NULL) {
|
|---|
| 481 | Py_RETURN_NONE;
|
|---|
| 482 | }
|
|---|
| 483 | Py_INCREF(completer);
|
|---|
| 484 | return completer;
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | PyDoc_STRVAR(doc_get_completer,
|
|---|
| 488 | "get_completer() -> function\n\
|
|---|
| 489 | \n\
|
|---|
| 490 | Returns current completer function.");
|
|---|
| 491 |
|
|---|
| 492 | /* Exported function to get any element of history */
|
|---|
| 493 |
|
|---|
| 494 | static PyObject *
|
|---|
| 495 | get_history_item(PyObject *self, PyObject *args)
|
|---|
| 496 | {
|
|---|
| 497 | int idx = 0;
|
|---|
| 498 | HIST_ENTRY *hist_ent;
|
|---|
| 499 |
|
|---|
| 500 | if (!PyArg_ParseTuple(args, "i:index", &idx))
|
|---|
| 501 | return NULL;
|
|---|
| 502 | #ifdef __APPLE__
|
|---|
| 503 | if (using_libedit_emulation) {
|
|---|
| 504 | /* Libedit emulation uses 0-based indexes,
|
|---|
| 505 | * the real one uses 1-based indexes,
|
|---|
| 506 | * adjust the index to ensure that Python
|
|---|
| 507 | * code doesn't have to worry about the
|
|---|
| 508 | * difference.
|
|---|
| 509 | */
|
|---|
| 510 | HISTORY_STATE *hist_st;
|
|---|
| 511 | hist_st = history_get_history_state();
|
|---|
| 512 |
|
|---|
| 513 | idx --;
|
|---|
| 514 |
|
|---|
| 515 | /*
|
|---|
| 516 | * Apple's readline emulation crashes when
|
|---|
| 517 | * the index is out of range, therefore
|
|---|
| 518 | * test for that and fail gracefully.
|
|---|
| 519 | */
|
|---|
| 520 | if (idx < 0 || idx >= hist_st->length) {
|
|---|
| 521 | Py_RETURN_NONE;
|
|---|
| 522 | }
|
|---|
| 523 | }
|
|---|
| 524 | #endif /* __APPLE__ */
|
|---|
| 525 | if ((hist_ent = history_get(idx)))
|
|---|
| 526 | return PyString_FromString(hist_ent->line);
|
|---|
| 527 | else {
|
|---|
| 528 | Py_RETURN_NONE;
|
|---|
| 529 | }
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | PyDoc_STRVAR(doc_get_history_item,
|
|---|
| 533 | "get_history_item() -> string\n\
|
|---|
| 534 | return the current contents of history item at index.");
|
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 | /* Exported function to get current length of history */
|
|---|
| 538 |
|
|---|
| 539 | static PyObject *
|
|---|
| 540 | get_current_history_length(PyObject *self, PyObject *noarg)
|
|---|
| 541 | {
|
|---|
| 542 | HISTORY_STATE *hist_st;
|
|---|
| 543 |
|
|---|
| 544 | hist_st = history_get_history_state();
|
|---|
| 545 | return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | PyDoc_STRVAR(doc_get_current_history_length,
|
|---|
| 549 | "get_current_history_length() -> integer\n\
|
|---|
| 550 | return the current (not the maximum) length of history.");
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 | /* Exported function to read the current line buffer */
|
|---|
| 554 |
|
|---|
| 555 | static PyObject *
|
|---|
| 556 | get_line_buffer(PyObject *self, PyObject *noarg)
|
|---|
| 557 | {
|
|---|
| 558 | return PyString_FromString(rl_line_buffer);
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | PyDoc_STRVAR(doc_get_line_buffer,
|
|---|
| 562 | "get_line_buffer() -> string\n\
|
|---|
| 563 | return the current contents of the line buffer.");
|
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
|---|
| 567 |
|
|---|
| 568 | /* Exported function to clear the current history */
|
|---|
| 569 |
|
|---|
| 570 | static PyObject *
|
|---|
| 571 | py_clear_history(PyObject *self, PyObject *noarg)
|
|---|
| 572 | {
|
|---|
| 573 | clear_history();
|
|---|
| 574 | Py_RETURN_NONE;
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | PyDoc_STRVAR(doc_clear_history,
|
|---|
| 578 | "clear_history() -> None\n\
|
|---|
| 579 | Clear the current readline history.");
|
|---|
| 580 | #endif
|
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 | /* Exported function to insert text into the line buffer */
|
|---|
| 584 |
|
|---|
| 585 | static PyObject *
|
|---|
| 586 | insert_text(PyObject *self, PyObject *args)
|
|---|
| 587 | {
|
|---|
| 588 | char *s;
|
|---|
| 589 | if (!PyArg_ParseTuple(args, "s:insert_text", &s))
|
|---|
| 590 | return NULL;
|
|---|
| 591 | rl_insert_text(s);
|
|---|
| 592 | Py_RETURN_NONE;
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | PyDoc_STRVAR(doc_insert_text,
|
|---|
| 596 | "insert_text(string) -> None\n\
|
|---|
| 597 | Insert text into the command line.");
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 | /* Redisplay the line buffer */
|
|---|
| 601 |
|
|---|
| 602 | static PyObject *
|
|---|
| 603 | redisplay(PyObject *self, PyObject *noarg)
|
|---|
| 604 | {
|
|---|
| 605 | rl_redisplay();
|
|---|
| 606 | Py_RETURN_NONE;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | PyDoc_STRVAR(doc_redisplay,
|
|---|
| 610 | "redisplay() -> None\n\
|
|---|
| 611 | Change what's displayed on the screen to reflect the current\n\
|
|---|
| 612 | contents of the line buffer.");
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 | /* Table of functions exported by the module */
|
|---|
| 616 |
|
|---|
| 617 | static struct PyMethodDef readline_methods[] =
|
|---|
| 618 | {
|
|---|
| 619 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
|
|---|
| 620 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
|
|---|
| 621 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
|
|---|
| 622 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
|
|---|
| 623 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
|
|---|
| 624 | {"read_history_file", read_history_file,
|
|---|
| 625 | METH_VARARGS, doc_read_history_file},
|
|---|
| 626 | {"write_history_file", write_history_file,
|
|---|
| 627 | METH_VARARGS, doc_write_history_file},
|
|---|
| 628 | {"get_history_item", get_history_item,
|
|---|
| 629 | METH_VARARGS, doc_get_history_item},
|
|---|
| 630 | {"get_current_history_length", (PyCFunction)get_current_history_length,
|
|---|
| 631 | METH_NOARGS, doc_get_current_history_length},
|
|---|
| 632 | {"set_history_length", set_history_length,
|
|---|
| 633 | METH_VARARGS, set_history_length_doc},
|
|---|
| 634 | {"get_history_length", get_history_length,
|
|---|
| 635 | METH_NOARGS, get_history_length_doc},
|
|---|
| 636 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
|
|---|
| 637 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
|
|---|
| 638 | {"get_completion_type", get_completion_type,
|
|---|
| 639 | METH_NOARGS, doc_get_completion_type},
|
|---|
| 640 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
|
|---|
| 641 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
|
|---|
| 642 |
|
|---|
| 643 | {"set_completer_delims", set_completer_delims,
|
|---|
| 644 | METH_VARARGS, doc_set_completer_delims},
|
|---|
| 645 | {"add_history", py_add_history, METH_VARARGS, doc_add_history},
|
|---|
| 646 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
|
|---|
| 647 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
|
|---|
| 648 | {"get_completer_delims", get_completer_delims,
|
|---|
| 649 | METH_NOARGS, doc_get_completer_delims},
|
|---|
| 650 |
|
|---|
| 651 | {"set_completion_display_matches_hook", set_completion_display_matches_hook,
|
|---|
| 652 | METH_VARARGS, doc_set_completion_display_matches_hook},
|
|---|
| 653 | {"set_startup_hook", set_startup_hook,
|
|---|
| 654 | METH_VARARGS, doc_set_startup_hook},
|
|---|
| 655 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
|---|
| 656 | {"set_pre_input_hook", set_pre_input_hook,
|
|---|
| 657 | METH_VARARGS, doc_set_pre_input_hook},
|
|---|
| 658 | #endif
|
|---|
| 659 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
|---|
| 660 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
|
|---|
| 661 | #endif
|
|---|
| 662 | {0, 0}
|
|---|
| 663 | };
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 | /* C function to call the Python hooks. */
|
|---|
| 667 |
|
|---|
| 668 | static int
|
|---|
| 669 | on_hook(PyObject *func)
|
|---|
| 670 | {
|
|---|
| 671 | int result = 0;
|
|---|
| 672 | if (func != NULL) {
|
|---|
| 673 | PyObject *r;
|
|---|
| 674 | #ifdef WITH_THREAD
|
|---|
| 675 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
|---|
| 676 | #endif
|
|---|
| 677 | r = PyObject_CallFunction(func, NULL);
|
|---|
| 678 | if (r == NULL)
|
|---|
| 679 | goto error;
|
|---|
| 680 | if (r == Py_None)
|
|---|
| 681 | result = 0;
|
|---|
| 682 | else {
|
|---|
| 683 | result = PyInt_AsLong(r);
|
|---|
| 684 | if (result == -1 && PyErr_Occurred())
|
|---|
| 685 | goto error;
|
|---|
| 686 | }
|
|---|
| 687 | Py_DECREF(r);
|
|---|
| 688 | goto done;
|
|---|
| 689 | error:
|
|---|
| 690 | PyErr_Clear();
|
|---|
| 691 | Py_XDECREF(r);
|
|---|
| 692 | done:
|
|---|
| 693 | #ifdef WITH_THREAD
|
|---|
| 694 | PyGILState_Release(gilstate);
|
|---|
| 695 | #endif
|
|---|
| 696 | return result;
|
|---|
| 697 | }
|
|---|
| 698 | return result;
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| 701 | static int
|
|---|
| 702 | on_startup_hook(void)
|
|---|
| 703 | {
|
|---|
| 704 | return on_hook(startup_hook);
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| 707 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
|---|
| 708 | static int
|
|---|
| 709 | on_pre_input_hook(void)
|
|---|
| 710 | {
|
|---|
| 711 | return on_hook(pre_input_hook);
|
|---|
| 712 | }
|
|---|
| 713 | #endif
|
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 | /* C function to call the Python completion_display_matches */
|
|---|
| 717 |
|
|---|
| 718 | static void
|
|---|
| 719 | on_completion_display_matches_hook(char **matches,
|
|---|
| 720 | int num_matches, int max_length)
|
|---|
| 721 | {
|
|---|
| 722 | int i;
|
|---|
| 723 | PyObject *m=NULL, *s=NULL, *r=NULL;
|
|---|
| 724 | #ifdef WITH_THREAD
|
|---|
| 725 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
|---|
| 726 | #endif
|
|---|
| 727 | m = PyList_New(num_matches);
|
|---|
| 728 | if (m == NULL)
|
|---|
| 729 | goto error;
|
|---|
| 730 | for (i = 0; i < num_matches; i++) {
|
|---|
| 731 | s = PyString_FromString(matches[i+1]);
|
|---|
| 732 | if (s == NULL)
|
|---|
| 733 | goto error;
|
|---|
| 734 | if (PyList_SetItem(m, i, s) == -1)
|
|---|
| 735 | goto error;
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | r = PyObject_CallFunction(completion_display_matches_hook,
|
|---|
| 739 | "sOi", matches[0], m, max_length);
|
|---|
| 740 |
|
|---|
| 741 | Py_DECREF(m), m=NULL;
|
|---|
| 742 |
|
|---|
| 743 | if (r == NULL ||
|
|---|
| 744 | (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
|
|---|
| 745 | goto error;
|
|---|
| 746 | }
|
|---|
| 747 | Py_XDECREF(r), r=NULL;
|
|---|
| 748 |
|
|---|
| 749 | if (0) {
|
|---|
| 750 | error:
|
|---|
| 751 | PyErr_Clear();
|
|---|
| 752 | Py_XDECREF(m);
|
|---|
| 753 | Py_XDECREF(r);
|
|---|
| 754 | }
|
|---|
| 755 | #ifdef WITH_THREAD
|
|---|
| 756 | PyGILState_Release(gilstate);
|
|---|
| 757 | #endif
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 | /* C function to call the Python completer. */
|
|---|
| 762 |
|
|---|
| 763 | static char *
|
|---|
| 764 | on_completion(const char *text, int state)
|
|---|
| 765 | {
|
|---|
| 766 | char *result = NULL;
|
|---|
| 767 | if (completer != NULL) {
|
|---|
| 768 | PyObject *r;
|
|---|
| 769 | #ifdef WITH_THREAD
|
|---|
| 770 | PyGILState_STATE gilstate = PyGILState_Ensure();
|
|---|
| 771 | #endif
|
|---|
| 772 | rl_attempted_completion_over = 1;
|
|---|
| 773 | r = PyObject_CallFunction(completer, "si", text, state);
|
|---|
| 774 | if (r == NULL)
|
|---|
| 775 | goto error;
|
|---|
| 776 | if (r == Py_None) {
|
|---|
| 777 | result = NULL;
|
|---|
| 778 | }
|
|---|
| 779 | else {
|
|---|
| 780 | char *s = PyString_AsString(r);
|
|---|
| 781 | if (s == NULL)
|
|---|
| 782 | goto error;
|
|---|
| 783 | result = strdup(s);
|
|---|
| 784 | }
|
|---|
| 785 | Py_DECREF(r);
|
|---|
| 786 | goto done;
|
|---|
| 787 | error:
|
|---|
| 788 | PyErr_Clear();
|
|---|
| 789 | Py_XDECREF(r);
|
|---|
| 790 | done:
|
|---|
| 791 | #ifdef WITH_THREAD
|
|---|
| 792 | PyGILState_Release(gilstate);
|
|---|
| 793 | #endif
|
|---|
| 794 | return result;
|
|---|
| 795 | }
|
|---|
| 796 | return result;
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 | /* A more flexible constructor that saves the "begidx" and "endidx"
|
|---|
| 801 | * before calling the normal completer */
|
|---|
| 802 |
|
|---|
| 803 | static char **
|
|---|
| 804 | flex_complete(char *text, int start, int end)
|
|---|
| 805 | {
|
|---|
| 806 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
|---|
| 807 | rl_completion_append_character ='\0';
|
|---|
| 808 | #endif
|
|---|
| 809 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
|
|---|
| 810 | rl_completion_suppress_append = 0;
|
|---|
| 811 | #endif
|
|---|
| 812 | Py_XDECREF(begidx);
|
|---|
| 813 | Py_XDECREF(endidx);
|
|---|
| 814 | begidx = PyInt_FromLong((long) start);
|
|---|
| 815 | endidx = PyInt_FromLong((long) end);
|
|---|
| 816 | return completion_matches(text, *on_completion);
|
|---|
| 817 | }
|
|---|
| 818 |
|
|---|
| 819 |
|
|---|
| 820 | /* Helper to initialize GNU readline properly. */
|
|---|
| 821 |
|
|---|
| 822 | static void
|
|---|
| 823 | setup_readline(void)
|
|---|
| 824 | {
|
|---|
| 825 | #ifdef SAVE_LOCALE
|
|---|
| 826 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
|---|
| 827 | if (!saved_locale)
|
|---|
| 828 | Py_FatalError("not enough memory to save locale");
|
|---|
| 829 | #endif
|
|---|
| 830 |
|
|---|
| 831 | using_history();
|
|---|
| 832 |
|
|---|
| 833 | rl_readline_name = "python";
|
|---|
| 834 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
|---|
| 835 | /* Allow $if term= in .inputrc to work */
|
|---|
| 836 | rl_terminal_name = getenv("TERM");
|
|---|
| 837 | #endif
|
|---|
| 838 | /* Force rebind of TAB to insert-tab */
|
|---|
| 839 | rl_bind_key('\t', rl_insert);
|
|---|
| 840 | /* Bind both ESC-TAB and ESC-ESC to the completion function */
|
|---|
| 841 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
|
|---|
| 842 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
|
|---|
| 843 | /* Set our hook functions */
|
|---|
| 844 | rl_startup_hook = (Function *)on_startup_hook;
|
|---|
| 845 | #ifdef HAVE_RL_PRE_INPUT_HOOK
|
|---|
| 846 | rl_pre_input_hook = (Function *)on_pre_input_hook;
|
|---|
| 847 | #endif
|
|---|
| 848 | /* Set our completion function */
|
|---|
| 849 | rl_attempted_completion_function = (CPPFunction *)flex_complete;
|
|---|
| 850 | /* Set Python word break characters */
|
|---|
| 851 | rl_completer_word_break_characters =
|
|---|
| 852 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
|
|---|
| 853 | /* All nonalphanums except '.' */
|
|---|
| 854 |
|
|---|
| 855 | begidx = PyInt_FromLong(0L);
|
|---|
| 856 | endidx = PyInt_FromLong(0L);
|
|---|
| 857 | /* Initialize (allows .inputrc to override)
|
|---|
| 858 | *
|
|---|
| 859 | * XXX: A bug in the readline-2.2 library causes a memory leak
|
|---|
| 860 | * inside this function. Nothing we can do about it.
|
|---|
| 861 | */
|
|---|
| 862 | rl_initialize();
|
|---|
| 863 |
|
|---|
| 864 | RESTORE_LOCALE(saved_locale)
|
|---|
| 865 | }
|
|---|
| 866 |
|
|---|
| 867 | /* Wrapper around GNU readline that handles signals differently. */
|
|---|
| 868 |
|
|---|
| 869 |
|
|---|
| 870 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
|
|---|
| 871 |
|
|---|
| 872 | static char *completed_input_string;
|
|---|
| 873 | static void
|
|---|
| 874 | rlhandler(char *text)
|
|---|
| 875 | {
|
|---|
| 876 | completed_input_string = text;
|
|---|
| 877 | rl_callback_handler_remove();
|
|---|
| 878 | }
|
|---|
| 879 |
|
|---|
| 880 | extern PyThreadState* _PyOS_ReadlineTState;
|
|---|
| 881 |
|
|---|
| 882 | static char *
|
|---|
| 883 | readline_until_enter_or_signal(char *prompt, int *signal)
|
|---|
| 884 | {
|
|---|
| 885 | char * not_done_reading = "";
|
|---|
| 886 | fd_set selectset;
|
|---|
| 887 |
|
|---|
| 888 | *signal = 0;
|
|---|
| 889 | #ifdef HAVE_RL_CATCH_SIGNAL
|
|---|
| 890 | rl_catch_signals = 0;
|
|---|
| 891 | #endif
|
|---|
| 892 |
|
|---|
| 893 | rl_callback_handler_install (prompt, rlhandler);
|
|---|
| 894 | FD_ZERO(&selectset);
|
|---|
| 895 |
|
|---|
| 896 | completed_input_string = not_done_reading;
|
|---|
| 897 |
|
|---|
| 898 | while (completed_input_string == not_done_reading) {
|
|---|
| 899 | int has_input = 0;
|
|---|
| 900 |
|
|---|
| 901 | while (!has_input)
|
|---|
| 902 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
|
|---|
| 903 |
|
|---|
| 904 | /* [Bug #1552726] Only limit the pause if an input hook has been
|
|---|
| 905 | defined. */
|
|---|
| 906 | struct timeval *timeoutp = NULL;
|
|---|
| 907 | if (PyOS_InputHook)
|
|---|
| 908 | timeoutp = &timeout;
|
|---|
| 909 | FD_SET(fileno(rl_instream), &selectset);
|
|---|
| 910 | /* select resets selectset if no input was available */
|
|---|
| 911 | has_input = select(fileno(rl_instream) + 1, &selectset,
|
|---|
| 912 | NULL, NULL, timeoutp);
|
|---|
| 913 | if(PyOS_InputHook) PyOS_InputHook();
|
|---|
| 914 | }
|
|---|
| 915 |
|
|---|
| 916 | if(has_input > 0) {
|
|---|
| 917 | rl_callback_read_char();
|
|---|
| 918 | }
|
|---|
| 919 | else if (errno == EINTR) {
|
|---|
| 920 | int s;
|
|---|
| 921 | #ifdef WITH_THREAD
|
|---|
| 922 | PyEval_RestoreThread(_PyOS_ReadlineTState);
|
|---|
| 923 | #endif
|
|---|
| 924 | s = PyErr_CheckSignals();
|
|---|
| 925 | #ifdef WITH_THREAD
|
|---|
| 926 | PyEval_SaveThread();
|
|---|
| 927 | #endif
|
|---|
| 928 | if (s < 0) {
|
|---|
| 929 | rl_free_line_state();
|
|---|
| 930 | rl_cleanup_after_signal();
|
|---|
| 931 | rl_callback_handler_remove();
|
|---|
| 932 | *signal = 1;
|
|---|
| 933 | completed_input_string = NULL;
|
|---|
| 934 | }
|
|---|
| 935 | }
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | return completed_input_string;
|
|---|
| 939 | }
|
|---|
| 940 |
|
|---|
| 941 |
|
|---|
| 942 | #else
|
|---|
| 943 |
|
|---|
| 944 | /* Interrupt handler */
|
|---|
| 945 |
|
|---|
| 946 | static jmp_buf jbuf;
|
|---|
| 947 |
|
|---|
| 948 | /* ARGSUSED */
|
|---|
| 949 | static void
|
|---|
| 950 | onintr(int sig)
|
|---|
| 951 | {
|
|---|
| 952 | longjmp(jbuf, 1);
|
|---|
| 953 | }
|
|---|
| 954 |
|
|---|
| 955 |
|
|---|
| 956 | static char *
|
|---|
| 957 | readline_until_enter_or_signal(char *prompt, int *signal)
|
|---|
| 958 | {
|
|---|
| 959 | PyOS_sighandler_t old_inthandler;
|
|---|
| 960 | char *p;
|
|---|
| 961 |
|
|---|
| 962 | *signal = 0;
|
|---|
| 963 |
|
|---|
| 964 | old_inthandler = PyOS_setsig(SIGINT, onintr);
|
|---|
| 965 | if (setjmp(jbuf)) {
|
|---|
| 966 | #ifdef HAVE_SIGRELSE
|
|---|
| 967 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
|
|---|
| 968 | sigrelse(SIGINT);
|
|---|
| 969 | #endif
|
|---|
| 970 | PyOS_setsig(SIGINT, old_inthandler);
|
|---|
| 971 | *signal = 1;
|
|---|
| 972 | return NULL;
|
|---|
| 973 | }
|
|---|
| 974 | rl_event_hook = PyOS_InputHook;
|
|---|
| 975 | p = readline(prompt);
|
|---|
| 976 | PyOS_setsig(SIGINT, old_inthandler);
|
|---|
| 977 |
|
|---|
| 978 | return p;
|
|---|
| 979 | }
|
|---|
| 980 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
|
|---|
| 981 |
|
|---|
| 982 |
|
|---|
| 983 | static char *
|
|---|
| 984 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
|---|
| 985 | {
|
|---|
| 986 | size_t n;
|
|---|
| 987 | char *p, *q;
|
|---|
| 988 | int signal;
|
|---|
| 989 |
|
|---|
| 990 | #ifdef SAVE_LOCALE
|
|---|
| 991 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
|---|
| 992 | if (!saved_locale)
|
|---|
| 993 | Py_FatalError("not enough memory to save locale");
|
|---|
| 994 | setlocale(LC_CTYPE, "");
|
|---|
| 995 | #endif
|
|---|
| 996 |
|
|---|
| 997 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
|
|---|
| 998 | rl_instream = sys_stdin;
|
|---|
| 999 | rl_outstream = sys_stdout;
|
|---|
| 1000 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
|---|
| 1001 | rl_prep_terminal (1);
|
|---|
| 1002 | #endif
|
|---|
| 1003 | }
|
|---|
| 1004 |
|
|---|
| 1005 | p = readline_until_enter_or_signal(prompt, &signal);
|
|---|
| 1006 |
|
|---|
| 1007 | /* we got an interrupt signal */
|
|---|
| 1008 | if (signal) {
|
|---|
| 1009 | RESTORE_LOCALE(saved_locale)
|
|---|
| 1010 | return NULL;
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | /* We got an EOF, return a empty string. */
|
|---|
| 1014 | if (p == NULL) {
|
|---|
| 1015 | p = PyMem_Malloc(1);
|
|---|
| 1016 | if (p != NULL)
|
|---|
| 1017 | *p = '\0';
|
|---|
| 1018 | RESTORE_LOCALE(saved_locale)
|
|---|
| 1019 | return p;
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | /* we have a valid line */
|
|---|
| 1023 | n = strlen(p);
|
|---|
| 1024 | if (n > 0) {
|
|---|
| 1025 | char *line;
|
|---|
| 1026 | HISTORY_STATE *state = history_get_history_state();
|
|---|
| 1027 | if (state->length > 0)
|
|---|
| 1028 | #ifdef __APPLE__
|
|---|
| 1029 | if (using_libedit_emulation) {
|
|---|
| 1030 | /*
|
|---|
| 1031 | * Libedit's emulation uses 0-based indexes,
|
|---|
| 1032 | * the real readline uses 1-based indexes.
|
|---|
| 1033 | */
|
|---|
| 1034 | line = history_get(state->length - 1)->line;
|
|---|
| 1035 | } else
|
|---|
| 1036 | #endif /* __APPLE__ */
|
|---|
| 1037 | line = history_get(state->length)->line;
|
|---|
| 1038 | else
|
|---|
| 1039 | line = "";
|
|---|
| 1040 | if (strcmp(p, line))
|
|---|
| 1041 | add_history(p);
|
|---|
| 1042 | /* the history docs don't say so, but the address of state
|
|---|
| 1043 | changes each time history_get_history_state is called
|
|---|
| 1044 | which makes me think it's freshly malloc'd memory...
|
|---|
| 1045 | on the other hand, the address of the last line stays the
|
|---|
| 1046 | same as long as history isn't extended, so it appears to
|
|---|
| 1047 | be malloc'd but managed by the history package... */
|
|---|
| 1048 | free(state);
|
|---|
| 1049 | }
|
|---|
| 1050 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
|
|---|
| 1051 | release the original. */
|
|---|
| 1052 | q = p;
|
|---|
| 1053 | p = PyMem_Malloc(n+2);
|
|---|
| 1054 | if (p != NULL) {
|
|---|
| 1055 | strncpy(p, q, n);
|
|---|
| 1056 | p[n] = '\n';
|
|---|
| 1057 | p[n+1] = '\0';
|
|---|
| 1058 | }
|
|---|
| 1059 | free(q);
|
|---|
| 1060 | RESTORE_LOCALE(saved_locale)
|
|---|
| 1061 | return p;
|
|---|
| 1062 | }
|
|---|
| 1063 |
|
|---|
| 1064 |
|
|---|
| 1065 | /* Initialize the module */
|
|---|
| 1066 |
|
|---|
| 1067 | PyDoc_STRVAR(doc_module,
|
|---|
| 1068 | "Importing this module enables command line editing using GNU readline.");
|
|---|
| 1069 |
|
|---|
| 1070 | #ifdef __APPLE__
|
|---|
| 1071 | PyDoc_STRVAR(doc_module_le,
|
|---|
| 1072 | "Importing this module enables command line editing using libedit readline.");
|
|---|
| 1073 | #endif /* __APPLE__ */
|
|---|
| 1074 |
|
|---|
| 1075 | PyMODINIT_FUNC
|
|---|
| 1076 | initreadline(void)
|
|---|
| 1077 | {
|
|---|
| 1078 | PyObject *m;
|
|---|
| 1079 |
|
|---|
| 1080 | #ifdef __APPLE__
|
|---|
| 1081 | if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
|
|---|
| 1082 | using_libedit_emulation = 1;
|
|---|
| 1083 | }
|
|---|
| 1084 |
|
|---|
| 1085 | if (using_libedit_emulation)
|
|---|
| 1086 | m = Py_InitModule4("readline", readline_methods, doc_module_le,
|
|---|
| 1087 | (PyObject *)NULL, PYTHON_API_VERSION);
|
|---|
| 1088 | else
|
|---|
| 1089 |
|
|---|
| 1090 | #endif /* __APPLE__ */
|
|---|
| 1091 |
|
|---|
| 1092 | m = Py_InitModule4("readline", readline_methods, doc_module,
|
|---|
| 1093 | (PyObject *)NULL, PYTHON_API_VERSION);
|
|---|
| 1094 | if (m == NULL)
|
|---|
| 1095 | return;
|
|---|
| 1096 |
|
|---|
| 1097 |
|
|---|
| 1098 |
|
|---|
| 1099 | PyOS_ReadlineFunctionPointer = call_readline;
|
|---|
| 1100 | setup_readline();
|
|---|
| 1101 | }
|
|---|