GDB (API)
/home/stan/gdb/src/gdb/python/py-gdb-readline.c
Go to the documentation of this file.
00001 /* Readline support for Python.
00002 
00003    Copyright (C) 2012-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "python-internal.h"
00022 #include "exceptions.h"
00023 #include "top.h"
00024 #include "cli/cli-utils.h"
00025 #include "gdb_string.h"
00026 
00027 #include <stddef.h>
00028 
00029 /* Readline function suitable for PyOS_ReadlineFunctionPointer, which
00030    is used for Python's interactive parser and raw_input.  In both
00031    cases, sys_stdin and sys_stdout are always stdin and stdout
00032    respectively, as far as I can tell; they are ignored and
00033    command_line_input is used instead.  */
00034 
00035 static char *
00036 gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
00037                         char *prompt)
00038 {
00039   int n;
00040   char *p = NULL, *q;
00041   volatile struct gdb_exception except;
00042 
00043   TRY_CATCH (except, RETURN_MASK_ALL)
00044     p = command_line_input (prompt, 0, "python");
00045 
00046   /* Detect user interrupt (Ctrl-C).  */
00047   if (except.reason == RETURN_QUIT)
00048     return NULL;
00049 
00050   /* Handle errors by raising Python exceptions.  */
00051   if (except.reason < 0)
00052     {
00053       /* The thread state is nulled during gdbpy_readline_wrapper,
00054          with the original value saved in the following undocumented
00055          variable (see Python's Parser/myreadline.c and
00056          Modules/readline.c).  */
00057       PyEval_RestoreThread (_PyOS_ReadlineTState);
00058       gdbpy_convert_exception (except);
00059       PyEval_SaveThread ();
00060       return NULL;
00061     }
00062 
00063   /* Detect EOF (Ctrl-D).  */
00064   if (p == NULL)
00065     {
00066       q = PyMem_Malloc (1);
00067       if (q != NULL)
00068         q[0] = '\0';
00069       return q;
00070     }
00071 
00072   n = strlen (p);
00073 
00074   /* Copy the line to Python and return.  */
00075   q = PyMem_Malloc (n + 2);
00076   if (q != NULL)
00077     {
00078       strncpy (q, p, n);
00079       q[n] = '\n';
00080       q[n + 1] = '\0';
00081     }
00082   return q;
00083 }
00084 
00085 /* Initialize Python readline support.  */
00086 
00087 void
00088 gdbpy_initialize_gdb_readline (void)
00089 {
00090   /* Python's readline module conflicts with GDB's use of readline
00091      since readline is not reentrant.  Ideally, a reentrant wrapper to
00092      GDB's readline should be implemented to replace Python's readline
00093      and prevent conflicts.  For now, this file implements a
00094      sys.meta_path finder that simply fails to import the readline
00095      module.  */
00096   if (PyRun_SimpleString ("\
00097 import sys\n\
00098 \n\
00099 class GdbRemoveReadlineFinder:\n\
00100   def find_module(self, fullname, path=None):\n\
00101     if fullname == 'readline' and path is None:\n\
00102       return self\n\
00103     return None\n\
00104 \n\
00105   def load_module(self, fullname):\n\
00106     raise ImportError('readline module disabled under GDB')\n\
00107 \n\
00108 sys.meta_path.append(GdbRemoveReadlineFinder())\n\
00109 ") == 0)
00110     PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
00111 }
00112 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines