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