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