2019-10-01 20:52:44 +08:00
|
|
|
/* Functions to deal with the inferior being executed on GDB or
|
|
|
|
GDBserver.
|
|
|
|
|
2021-01-01 16:03:39 +08:00
|
|
|
Copyright (C) 2019-2021 Free Software Foundation, Inc.
|
2019-10-01 20:52:44 +08:00
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include "gdbsupport/common-defs.h"
|
|
|
|
#include "gdbsupport/common-inferior.h"
|
|
|
|
|
|
|
|
/* See common-inferior.h. */
|
|
|
|
|
|
|
|
bool startup_with_shell = true;
|
2020-05-25 23:37:44 +08:00
|
|
|
|
|
|
|
/* See common-inferior.h. */
|
|
|
|
|
2020-05-25 23:38:11 +08:00
|
|
|
std::string
|
2020-05-25 23:38:32 +08:00
|
|
|
construct_inferior_arguments (gdb::array_view<char * const> argv)
|
2020-05-25 23:37:44 +08:00
|
|
|
{
|
2020-05-25 23:38:11 +08:00
|
|
|
std::string result;
|
2020-05-25 23:37:44 +08:00
|
|
|
|
|
|
|
if (startup_with_shell)
|
|
|
|
{
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
/* This holds all the characters considered special to the
|
|
|
|
Windows shells. */
|
|
|
|
static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n";
|
|
|
|
static const char quote = '"';
|
|
|
|
#else
|
|
|
|
/* This holds all the characters considered special to the
|
|
|
|
typical Unix shells. We include `^' because the SunOS
|
|
|
|
/bin/sh treats it as a synonym for `|'. */
|
|
|
|
static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
|
|
|
|
static const char quote = '\'';
|
|
|
|
#endif
|
2020-05-25 23:38:32 +08:00
|
|
|
for (int i = 0; i < argv.size (); ++i)
|
2020-05-25 23:37:44 +08:00
|
|
|
{
|
|
|
|
if (i > 0)
|
2020-05-25 23:38:11 +08:00
|
|
|
result += ' ';
|
2020-05-25 23:37:44 +08:00
|
|
|
|
|
|
|
/* Need to handle empty arguments specially. */
|
|
|
|
if (argv[i][0] == '\0')
|
|
|
|
{
|
2020-05-25 23:38:11 +08:00
|
|
|
result += quote;
|
|
|
|
result += quote;
|
2020-05-25 23:37:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef __MINGW32__
|
2020-05-25 23:38:11 +08:00
|
|
|
bool quoted = false;
|
2020-05-25 23:37:44 +08:00
|
|
|
|
|
|
|
if (strpbrk (argv[i], special))
|
|
|
|
{
|
2020-05-25 23:38:11 +08:00
|
|
|
quoted = true;
|
|
|
|
result += quote;
|
2020-05-25 23:37:44 +08:00
|
|
|
}
|
|
|
|
#endif
|
2020-05-25 23:38:11 +08:00
|
|
|
for (char *cp = argv[i]; *cp; ++cp)
|
2020-05-25 23:37:44 +08:00
|
|
|
{
|
|
|
|
if (*cp == '\n')
|
|
|
|
{
|
|
|
|
/* A newline cannot be quoted with a backslash (it
|
|
|
|
just disappears), only by putting it inside
|
|
|
|
quotes. */
|
2020-05-25 23:38:11 +08:00
|
|
|
result += quote;
|
|
|
|
result += '\n';
|
|
|
|
result += quote;
|
2020-05-25 23:37:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
if (*cp == quote)
|
|
|
|
#else
|
|
|
|
if (strchr (special, *cp) != NULL)
|
|
|
|
#endif
|
2020-05-25 23:38:11 +08:00
|
|
|
result += '\\';
|
|
|
|
result += *cp;
|
2020-05-25 23:37:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
if (quoted)
|
2020-05-25 23:38:11 +08:00
|
|
|
result += quote;
|
2020-05-25 23:37:44 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In this case we can't handle arguments that contain spaces,
|
|
|
|
tabs, or newlines -- see breakup_args(). */
|
2020-05-25 23:38:32 +08:00
|
|
|
for (char *arg : argv)
|
2020-05-25 23:37:44 +08:00
|
|
|
{
|
2020-05-25 23:38:32 +08:00
|
|
|
char *cp = strchr (arg, ' ');
|
2020-05-25 23:37:44 +08:00
|
|
|
if (cp == NULL)
|
2020-05-25 23:38:32 +08:00
|
|
|
cp = strchr (arg, '\t');
|
2020-05-25 23:37:44 +08:00
|
|
|
if (cp == NULL)
|
2020-05-25 23:38:32 +08:00
|
|
|
cp = strchr (arg, '\n');
|
2020-05-25 23:37:44 +08:00
|
|
|
if (cp != NULL)
|
|
|
|
error (_("can't handle command-line "
|
|
|
|
"argument containing whitespace"));
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:38:32 +08:00
|
|
|
for (int i = 0; i < argv.size (); ++i)
|
2020-05-25 23:37:44 +08:00
|
|
|
{
|
|
|
|
if (i > 0)
|
2020-05-25 23:38:11 +08:00
|
|
|
result += " ";
|
|
|
|
result += argv[i];
|
2020-05-25 23:37:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|