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