18dffb485Schristos /* Functions to deal with the inferior being executed on GDB or 28dffb485Schristos GDBserver. 38dffb485Schristos 4*5ba1f45fSchristos Copyright (C) 2019-2024 Free Software Foundation, Inc. 58dffb485Schristos 68dffb485Schristos This file is part of GDB. 78dffb485Schristos 88dffb485Schristos This program is free software; you can redistribute it and/or modify 98dffb485Schristos it under the terms of the GNU General Public License as published by 108dffb485Schristos the Free Software Foundation; either version 3 of the License, or 118dffb485Schristos (at your option) any later version. 128dffb485Schristos 138dffb485Schristos This program is distributed in the hope that it will be useful, 148dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 158dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 168dffb485Schristos GNU General Public License for more details. 178dffb485Schristos 188dffb485Schristos You should have received a copy of the GNU General Public License 198dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 208dffb485Schristos 218dffb485Schristos #include "gdbsupport/common-inferior.h" 228dffb485Schristos 238dffb485Schristos /* See common-inferior.h. */ 248dffb485Schristos 258dffb485Schristos bool startup_with_shell = true; 268dffb485Schristos 278dffb485Schristos /* See common-inferior.h. */ 288dffb485Schristos 298dffb485Schristos std::string 308dffb485Schristos construct_inferior_arguments (gdb::array_view<char * const> argv) 318dffb485Schristos { 328dffb485Schristos std::string result; 338dffb485Schristos 348dffb485Schristos if (startup_with_shell) 358dffb485Schristos { 368dffb485Schristos #ifdef __MINGW32__ 378dffb485Schristos /* This holds all the characters considered special to the 388dffb485Schristos Windows shells. */ 398dffb485Schristos static const char special[] = "\"!&*|[]{}<>?`~^=;, \t\n"; 408dffb485Schristos static const char quote = '"'; 418dffb485Schristos #else 428dffb485Schristos /* This holds all the characters considered special to the 438dffb485Schristos typical Unix shells. We include `^' because the SunOS 448dffb485Schristos /bin/sh treats it as a synonym for `|'. */ 458dffb485Schristos static const char special[] = "\"!#$&*()\\|[]{}<>?'`~^; \t\n"; 468dffb485Schristos static const char quote = '\''; 478dffb485Schristos #endif 488dffb485Schristos for (int i = 0; i < argv.size (); ++i) 498dffb485Schristos { 508dffb485Schristos if (i > 0) 518dffb485Schristos result += ' '; 528dffb485Schristos 538dffb485Schristos /* Need to handle empty arguments specially. */ 548dffb485Schristos if (argv[i][0] == '\0') 558dffb485Schristos { 568dffb485Schristos result += quote; 578dffb485Schristos result += quote; 588dffb485Schristos } 598dffb485Schristos else 608dffb485Schristos { 618dffb485Schristos #ifdef __MINGW32__ 628dffb485Schristos bool quoted = false; 638dffb485Schristos 648dffb485Schristos if (strpbrk (argv[i], special)) 658dffb485Schristos { 668dffb485Schristos quoted = true; 678dffb485Schristos result += quote; 688dffb485Schristos } 698dffb485Schristos #endif 708dffb485Schristos for (char *cp = argv[i]; *cp; ++cp) 718dffb485Schristos { 728dffb485Schristos if (*cp == '\n') 738dffb485Schristos { 748dffb485Schristos /* A newline cannot be quoted with a backslash (it 758dffb485Schristos just disappears), only by putting it inside 768dffb485Schristos quotes. */ 778dffb485Schristos result += quote; 788dffb485Schristos result += '\n'; 798dffb485Schristos result += quote; 808dffb485Schristos } 818dffb485Schristos else 828dffb485Schristos { 838dffb485Schristos #ifdef __MINGW32__ 848dffb485Schristos if (*cp == quote) 858dffb485Schristos #else 868dffb485Schristos if (strchr (special, *cp) != NULL) 878dffb485Schristos #endif 888dffb485Schristos result += '\\'; 898dffb485Schristos result += *cp; 908dffb485Schristos } 918dffb485Schristos } 928dffb485Schristos #ifdef __MINGW32__ 938dffb485Schristos if (quoted) 948dffb485Schristos result += quote; 958dffb485Schristos #endif 968dffb485Schristos } 978dffb485Schristos } 988dffb485Schristos } 998dffb485Schristos else 1008dffb485Schristos { 1018dffb485Schristos /* In this case we can't handle arguments that contain spaces, 1028dffb485Schristos tabs, or newlines -- see breakup_args(). */ 1038dffb485Schristos for (char *arg : argv) 1048dffb485Schristos { 1058dffb485Schristos char *cp = strchr (arg, ' '); 1068dffb485Schristos if (cp == NULL) 1078dffb485Schristos cp = strchr (arg, '\t'); 1088dffb485Schristos if (cp == NULL) 1098dffb485Schristos cp = strchr (arg, '\n'); 1108dffb485Schristos if (cp != NULL) 1118dffb485Schristos error (_("can't handle command-line " 1128dffb485Schristos "argument containing whitespace")); 1138dffb485Schristos } 1148dffb485Schristos 1158dffb485Schristos for (int i = 0; i < argv.size (); ++i) 1168dffb485Schristos { 1178dffb485Schristos if (i > 0) 1188dffb485Schristos result += " "; 1198dffb485Schristos result += argv[i]; 1208dffb485Schristos } 1218dffb485Schristos } 1228dffb485Schristos 1238dffb485Schristos return result; 1248dffb485Schristos } 125