]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/py-gdb-readline.c
b335df918ecbd260df5cea9317a5d0c0122636fc
[thirdparty/binutils-gdb.git] / gdb / python / py-gdb-readline.c
1 /* Readline support for Python.
2
3 Copyright (C) 2012-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "python-internal.h"
22 #include "exceptions.h"
23 #include "top.h"
24 #include "cli/cli-utils.h"
25 /* Readline function suitable for PyOS_ReadlineFunctionPointer, which
26 is used for Python's interactive parser and raw_input. In both
27 cases, sys_stdin and sys_stdout are always stdin and stdout
28 respectively, as far as I can tell; they are ignored and
29 command_line_input is used instead. */
30
31 static char *
32 gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
33 char *prompt)
34 {
35 int n;
36 char *p = NULL, *q;
37 volatile struct gdb_exception except;
38
39 TRY_CATCH (except, RETURN_MASK_ALL)
40 p = command_line_input (prompt, 0, "python");
41
42 /* Detect user interrupt (Ctrl-C). */
43 if (except.reason == RETURN_QUIT)
44 return NULL;
45
46 /* Handle errors by raising Python exceptions. */
47 if (except.reason < 0)
48 {
49 /* The thread state is nulled during gdbpy_readline_wrapper,
50 with the original value saved in the following undocumented
51 variable (see Python's Parser/myreadline.c and
52 Modules/readline.c). */
53 PyEval_RestoreThread (_PyOS_ReadlineTState);
54 gdbpy_convert_exception (except);
55 PyEval_SaveThread ();
56 return NULL;
57 }
58
59 /* Detect EOF (Ctrl-D). */
60 if (p == NULL)
61 {
62 q = PyMem_Malloc (1);
63 if (q != NULL)
64 q[0] = '\0';
65 return q;
66 }
67
68 n = strlen (p);
69
70 /* Copy the line to Python and return. */
71 q = PyMem_Malloc (n + 2);
72 if (q != NULL)
73 {
74 strncpy (q, p, n);
75 q[n] = '\n';
76 q[n + 1] = '\0';
77 }
78 return q;
79 }
80
81 /* Initialize Python readline support. */
82
83 void
84 gdbpy_initialize_gdb_readline (void)
85 {
86 /* Python's readline module conflicts with GDB's use of readline
87 since readline is not reentrant. Ideally, a reentrant wrapper to
88 GDB's readline should be implemented to replace Python's readline
89 and prevent conflicts. For now, this file implements a
90 sys.meta_path finder that simply fails to import the readline
91 module. */
92 if (PyRun_SimpleString ("\
93 import sys\n\
94 \n\
95 class GdbRemoveReadlineFinder:\n\
96 def find_module(self, fullname, path=None):\n\
97 if fullname == 'readline' and path is None:\n\
98 return self\n\
99 return None\n\
100 \n\
101 def load_module(self, fullname):\n\
102 raise ImportError('readline module disabled under GDB')\n\
103 \n\
104 sys.meta_path.append(GdbRemoveReadlineFinder())\n\
105 ") == 0)
106 PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
107 }
108