gdbsupport: Adapt construct_inferior_arguments
[deliverable/binutils-gdb.git] / gdbsupport / common-inferior.cc
1 /* Functions to deal with the inferior being executed on GDB or
2 GDBserver.
3
4 Copyright (C) 2019-2020 Free Software Foundation, Inc.
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
26 bool startup_with_shell = true;
27
28 /* See common-inferior.h. */
29
30 std::string
31 construct_inferior_arguments (int argc, char * const *argv)
32 {
33 gdb_assert (argc >= 0);
34
35 std::string result;
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
51 for (int i = 0; i < argc; ++i)
52 {
53 if (i > 0)
54 result += ' ';
55
56 /* Need to handle empty arguments specially. */
57 if (argv[i][0] == '\0')
58 {
59 result += quote;
60 result += quote;
61 }
62 else
63 {
64 #ifdef __MINGW32__
65 bool quoted = false;
66
67 if (strpbrk (argv[i], special))
68 {
69 quoted = true;
70 result += quote;
71 }
72 #endif
73 for (char *cp = argv[i]; *cp; ++cp)
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. */
80 result += quote;
81 result += '\n';
82 result += quote;
83 }
84 else
85 {
86 #ifdef __MINGW32__
87 if (*cp == quote)
88 #else
89 if (strchr (special, *cp) != NULL)
90 #endif
91 result += '\\';
92 result += *cp;
93 }
94 }
95 #ifdef __MINGW32__
96 if (quoted)
97 result += quote;
98 #endif
99 }
100 }
101 }
102 else
103 {
104 /* In this case we can't handle arguments that contain spaces,
105 tabs, or newlines -- see breakup_args(). */
106 for (int i = 0; i < argc; ++i)
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"));
116 }
117
118 for (int i = 0; i < argc; ++i)
119 {
120 if (i > 0)
121 result += " ";
122 result += argv[i];
123 }
124 }
125
126 return result;
127 }
This page took 0.034202 seconds and 4 git commands to generate.