Home | History | Annotate | Line # | Download | only in gdbsupport
common-inferior.cc revision 1.1.1.1
      1  1.1  christos /* Functions to deal with the inferior being executed on GDB or
      2  1.1  christos    GDBserver.
      3  1.1  christos 
      4  1.1  christos    Copyright (C) 2019-2020 Free Software Foundation, Inc.
      5  1.1  christos 
      6  1.1  christos    This file is part of GDB.
      7  1.1  christos 
      8  1.1  christos    This program is free software; you can redistribute it and/or modify
      9  1.1  christos    it under the terms of the GNU General Public License as published by
     10  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     11  1.1  christos    (at your option) any later version.
     12  1.1  christos 
     13  1.1  christos    This program is distributed in the hope that it will be useful,
     14  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  1.1  christos    GNU General Public License for more details.
     17  1.1  christos 
     18  1.1  christos    You should have received a copy of the GNU General Public License
     19  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20  1.1  christos 
     21  1.1  christos #include "gdbsupport/common-defs.h"
     22  1.1  christos #include "gdbsupport/common-inferior.h"
     23  1.1  christos 
     24  1.1  christos /* See common-inferior.h.  */
     25  1.1  christos 
     26  1.1  christos bool startup_with_shell = true;
     27  1.1  christos 
     28  1.1  christos /* See common-inferior.h.  */
     29  1.1  christos 
     30  1.1  christos std::string
     31  1.1  christos construct_inferior_arguments (gdb::array_view<char * const> argv)
     32  1.1  christos {
     33  1.1  christos   std::string result;
     34  1.1  christos 
     35  1.1  christos   if (startup_with_shell)
     36  1.1  christos     {
     37  1.1  christos #ifdef __MINGW32__
     38  1.1  christos       /* This holds all the characters considered special to the
     39  1.1  christos 	 Windows shells.  */
     40  1.1  christos       static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n";
     41  1.1  christos       static const char quote = '"';
     42  1.1  christos #else
     43  1.1  christos       /* This holds all the characters considered special to the
     44  1.1  christos 	 typical Unix shells.  We include `^' because the SunOS
     45  1.1  christos 	 /bin/sh treats it as a synonym for `|'.  */
     46  1.1  christos       static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
     47  1.1  christos       static const char quote = '\'';
     48  1.1  christos #endif
     49  1.1  christos       for (int i = 0; i < argv.size (); ++i)
     50  1.1  christos 	{
     51  1.1  christos 	  if (i > 0)
     52  1.1  christos 	    result += ' ';
     53  1.1  christos 
     54  1.1  christos 	  /* Need to handle empty arguments specially.  */
     55  1.1  christos 	  if (argv[i][0] == '\0')
     56  1.1  christos 	    {
     57  1.1  christos 	      result += quote;
     58  1.1  christos 	      result += quote;
     59  1.1  christos 	    }
     60  1.1  christos 	  else
     61  1.1  christos 	    {
     62  1.1  christos #ifdef __MINGW32__
     63  1.1  christos 	      bool quoted = false;
     64  1.1  christos 
     65  1.1  christos 	      if (strpbrk (argv[i], special))
     66  1.1  christos 		{
     67  1.1  christos 		  quoted = true;
     68  1.1  christos 		  result += quote;
     69  1.1  christos 		}
     70  1.1  christos #endif
     71  1.1  christos 	      for (char *cp = argv[i]; *cp; ++cp)
     72  1.1  christos 		{
     73  1.1  christos 		  if (*cp == '\n')
     74  1.1  christos 		    {
     75  1.1  christos 		      /* A newline cannot be quoted with a backslash (it
     76  1.1  christos 			 just disappears), only by putting it inside
     77  1.1  christos 			 quotes.  */
     78  1.1  christos 		      result += quote;
     79  1.1  christos 		      result += '\n';
     80  1.1  christos 		      result += quote;
     81  1.1  christos 		    }
     82  1.1  christos 		  else
     83  1.1  christos 		    {
     84  1.1  christos #ifdef __MINGW32__
     85  1.1  christos 		      if (*cp == quote)
     86  1.1  christos #else
     87  1.1  christos 		      if (strchr (special, *cp) != NULL)
     88  1.1  christos #endif
     89  1.1  christos 			result += '\\';
     90  1.1  christos 		      result += *cp;
     91  1.1  christos 		    }
     92  1.1  christos 		}
     93  1.1  christos #ifdef __MINGW32__
     94  1.1  christos 	      if (quoted)
     95  1.1  christos 		result += quote;
     96  1.1  christos #endif
     97  1.1  christos 	    }
     98  1.1  christos 	}
     99  1.1  christos     }
    100  1.1  christos   else
    101  1.1  christos     {
    102  1.1  christos       /* In this case we can't handle arguments that contain spaces,
    103  1.1  christos 	 tabs, or newlines -- see breakup_args().  */
    104  1.1  christos       for (char *arg : argv)
    105  1.1  christos 	{
    106  1.1  christos 	  char *cp = strchr (arg, ' ');
    107  1.1  christos 	  if (cp == NULL)
    108  1.1  christos 	    cp = strchr (arg, '\t');
    109  1.1  christos 	  if (cp == NULL)
    110  1.1  christos 	    cp = strchr (arg, '\n');
    111  1.1  christos 	  if (cp != NULL)
    112  1.1  christos 	    error (_("can't handle command-line "
    113  1.1  christos 		     "argument containing whitespace"));
    114  1.1  christos 	}
    115  1.1  christos 
    116  1.1  christos       for (int i = 0; i < argv.size (); ++i)
    117  1.1  christos 	{
    118  1.1  christos 	  if (i > 0)
    119  1.1  christos 	    result += " ";
    120  1.1  christos 	  result += argv[i];
    121  1.1  christos 	}
    122  1.1  christos     }
    123  1.1  christos 
    124  1.1  christos   return result;
    125  1.1  christos }
    126