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