| 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 | char * |
| 31 | construct_inferior_arguments (int argc, char **argv) |
| 32 | { |
| 33 | char *result; |
| 34 | |
| 35 | /* ARGC should always be at least 1, but we double check this |
| 36 | here. This is also needed to silence -Werror-stringop |
| 37 | warnings. */ |
| 38 | gdb_assert (argc > 0); |
| 39 | |
| 40 | if (startup_with_shell) |
| 41 | { |
| 42 | #ifdef __MINGW32__ |
| 43 | /* This holds all the characters considered special to the |
| 44 | Windows shells. */ |
| 45 | static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n"; |
| 46 | static const char quote = '"'; |
| 47 | #else |
| 48 | /* This holds all the characters considered special to the |
| 49 | typical Unix shells. We include `^' because the SunOS |
| 50 | /bin/sh treats it as a synonym for `|'. */ |
| 51 | static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n"; |
| 52 | static const char quote = '\''; |
| 53 | #endif |
| 54 | int i; |
| 55 | int length = 0; |
| 56 | char *out, *cp; |
| 57 | |
| 58 | /* We over-compute the size. It shouldn't matter. */ |
| 59 | for (i = 0; i < argc; ++i) |
| 60 | length += 3 * strlen (argv[i]) + 1 + 2 * (argv[i][0] == '\0'); |
| 61 | |
| 62 | result = (char *) xmalloc (length); |
| 63 | out = result; |
| 64 | |
| 65 | for (i = 0; i < argc; ++i) |
| 66 | { |
| 67 | if (i > 0) |
| 68 | *out++ = ' '; |
| 69 | |
| 70 | /* Need to handle empty arguments specially. */ |
| 71 | if (argv[i][0] == '\0') |
| 72 | { |
| 73 | *out++ = quote; |
| 74 | *out++ = quote; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | #ifdef __MINGW32__ |
| 79 | int quoted = 0; |
| 80 | |
| 81 | if (strpbrk (argv[i], special)) |
| 82 | { |
| 83 | quoted = 1; |
| 84 | *out++ = quote; |
| 85 | } |
| 86 | #endif |
| 87 | for (cp = argv[i]; *cp; ++cp) |
| 88 | { |
| 89 | if (*cp == '\n') |
| 90 | { |
| 91 | /* A newline cannot be quoted with a backslash (it |
| 92 | just disappears), only by putting it inside |
| 93 | quotes. */ |
| 94 | *out++ = quote; |
| 95 | *out++ = '\n'; |
| 96 | *out++ = quote; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | #ifdef __MINGW32__ |
| 101 | if (*cp == quote) |
| 102 | #else |
| 103 | if (strchr (special, *cp) != NULL) |
| 104 | #endif |
| 105 | *out++ = '\\'; |
| 106 | *out++ = *cp; |
| 107 | } |
| 108 | } |
| 109 | #ifdef __MINGW32__ |
| 110 | if (quoted) |
| 111 | *out++ = quote; |
| 112 | #endif |
| 113 | } |
| 114 | } |
| 115 | *out = '\0'; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | /* In this case we can't handle arguments that contain spaces, |
| 120 | tabs, or newlines -- see breakup_args(). */ |
| 121 | int i; |
| 122 | int length = 0; |
| 123 | |
| 124 | for (i = 0; i < argc; ++i) |
| 125 | { |
| 126 | char *cp = strchr (argv[i], ' '); |
| 127 | if (cp == NULL) |
| 128 | cp = strchr (argv[i], '\t'); |
| 129 | if (cp == NULL) |
| 130 | cp = strchr (argv[i], '\n'); |
| 131 | if (cp != NULL) |
| 132 | error (_("can't handle command-line " |
| 133 | "argument containing whitespace")); |
| 134 | length += strlen (argv[i]) + 1; |
| 135 | } |
| 136 | |
| 137 | result = (char *) xmalloc (length); |
| 138 | result[0] = '\0'; |
| 139 | for (i = 0; i < argc; ++i) |
| 140 | { |
| 141 | if (i > 0) |
| 142 | strcat (result, " "); |
| 143 | strcat (result, argv[i]); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return result; |
| 148 | } |