2003-12-17 21:55:42 +00:00
|
|
|
/*
|
2023-04-08 16:07:31 +02:00
|
|
|
* Copyright (C) 2020-2023 The DOSBox Staging Team
|
2021-01-14 18:29:34 +00:00
|
|
|
* Copyright (C) 2002-2021 The DOSBox Team
|
2003-12-17 21:55:42 +00:00
|
|
|
*
|
|
|
|
* 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 2 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
|
2004-08-04 09:12:57 +00:00
|
|
|
* GNU General Public License for more details.
|
2003-12-17 21:55:42 +00:00
|
|
|
*
|
2019-01-25 14:09:58 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-12-17 21:55:42 +00:00
|
|
|
*/
|
|
|
|
|
2005-03-25 09:46:53 +00:00
|
|
|
#ifndef DOSBOX_SHELL_H
|
|
|
|
#define DOSBOX_SHELL_H
|
2003-12-17 21:55:42 +00:00
|
|
|
|
|
|
|
#include "dosbox.h"
|
2021-01-01 00:10:26 +01:00
|
|
|
|
2021-07-18 09:22:41 -07:00
|
|
|
#include <memory>
|
2023-06-01 00:49:09 -06:00
|
|
|
#include <optional>
|
2023-06-09 22:55:39 -06:00
|
|
|
#include <stack>
|
2021-01-01 00:10:26 +01:00
|
|
|
#include <string>
|
|
|
|
|
2023-05-31 00:27:34 -06:00
|
|
|
#include "callback.h"
|
2003-12-17 21:55:42 +00:00
|
|
|
#include "programs.h"
|
|
|
|
|
2023-05-31 00:27:34 -06:00
|
|
|
class DOS_Shell;
|
|
|
|
|
|
|
|
constexpr auto CMD_MAXLINE = 4096;
|
2022-05-26 16:58:09 +12:00
|
|
|
|
2022-11-14 14:23:48 -08:00
|
|
|
extern callback_number_t call_shellstop;
|
2019-04-20 13:48:55 +00:00
|
|
|
|
2023-05-31 00:27:34 -06:00
|
|
|
/* first_shell is used to add and delete stuff from the shell env
|
2005-08-22 19:31:27 +00:00
|
|
|
* by "external" programs. (config) */
|
2023-05-31 00:27:34 -06:00
|
|
|
extern DOS_Shell* first_shell;
|
2003-12-17 21:55:42 +00:00
|
|
|
|
2023-06-01 00:49:09 -06:00
|
|
|
class ByteReader {
|
|
|
|
public:
|
2023-10-19 09:13:55 -05:00
|
|
|
virtual void Reset() = 0;
|
|
|
|
virtual std::optional<uint8_t> Read() = 0;
|
2023-06-01 00:49:09 -06:00
|
|
|
|
|
|
|
ByteReader() = default;
|
|
|
|
ByteReader(const ByteReader&) = delete;
|
|
|
|
ByteReader& operator=(const ByteReader&) = delete;
|
|
|
|
ByteReader(ByteReader&&) = delete;
|
|
|
|
ByteReader& operator=(ByteReader&&) = delete;
|
|
|
|
virtual ~ByteReader() = default;
|
|
|
|
};
|
|
|
|
|
2023-06-08 00:50:24 -06:00
|
|
|
class HostShell {
|
|
|
|
public:
|
|
|
|
virtual bool GetEnvStr(const char* entry, std::string& result) const = 0;
|
|
|
|
|
|
|
|
HostShell() = default;
|
|
|
|
HostShell(const HostShell&) = delete;
|
|
|
|
HostShell& operator=(const HostShell&) = delete;
|
|
|
|
HostShell(HostShell&&) = delete;
|
|
|
|
HostShell& operator=(HostShell&&) = delete;
|
|
|
|
virtual ~HostShell() = default;
|
|
|
|
};
|
|
|
|
|
2003-12-17 21:55:42 +00:00
|
|
|
class BatchFile {
|
|
|
|
public:
|
2023-06-17 16:38:49 -06:00
|
|
|
BatchFile(const HostShell& host, std::unique_ptr<ByteReader> input_reader,
|
2023-06-14 22:22:36 -06:00
|
|
|
std::string_view entered_name, std::string_view cmd_line,
|
2023-06-14 22:15:16 -06:00
|
|
|
bool echo_on);
|
2023-06-14 22:18:12 -06:00
|
|
|
BatchFile(const BatchFile&) = delete;
|
|
|
|
BatchFile& operator=(const BatchFile&) = delete;
|
|
|
|
BatchFile(BatchFile&&) = delete;
|
|
|
|
BatchFile& operator=(BatchFile&&) = delete;
|
2023-06-14 22:15:16 -06:00
|
|
|
virtual ~BatchFile() = default;
|
2023-06-14 22:18:12 -06:00
|
|
|
|
2023-05-31 00:27:34 -06:00
|
|
|
virtual bool ReadLine(char* line);
|
2023-03-14 22:20:45 -06:00
|
|
|
bool Goto(std::string_view label);
|
2021-01-11 14:28:40 -08:00
|
|
|
void Shift();
|
2023-06-14 22:15:16 -06:00
|
|
|
void SetEcho(bool echo_on);
|
|
|
|
[[nodiscard]] bool Echo() const;
|
2023-03-09 01:34:22 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
[[nodiscard]] std::string ExpandedBatchLine(std::string_view line) const;
|
|
|
|
[[nodiscard]] std::string GetLine();
|
2023-06-08 00:39:19 -06:00
|
|
|
|
2023-06-17 16:38:49 -06:00
|
|
|
const HostShell& shell;
|
2023-06-08 00:39:19 -06:00
|
|
|
CommandLine cmd;
|
2023-06-01 00:50:16 -06:00
|
|
|
std::unique_ptr<ByteReader> reader;
|
2023-06-14 22:15:16 -06:00
|
|
|
bool echo;
|
2003-12-17 21:55:42 +00:00
|
|
|
};
|
|
|
|
|
2007-10-28 10:58:50 +00:00
|
|
|
class AutoexecEditor;
|
2022-11-07 20:37:45 +01:00
|
|
|
class MoreOutputStrings;
|
2022-05-26 16:58:09 +12:00
|
|
|
|
|
|
|
struct SHELL_Cmd {
|
2023-05-31 00:27:34 -06:00
|
|
|
void (DOS_Shell::*handler)(char* args) = nullptr; // Handler for this
|
|
|
|
// command
|
|
|
|
const char* help = ""; // String with command help
|
|
|
|
HELP_Filter filter = HELP_Filter::Common;
|
2022-05-26 16:58:09 +12:00
|
|
|
HELP_Category category = HELP_Category::Misc;
|
|
|
|
};
|
|
|
|
|
2023-06-08 00:50:24 -06:00
|
|
|
class DOS_Shell : public Program, public HostShell {
|
2003-12-17 21:55:42 +00:00
|
|
|
private:
|
2023-05-31 00:27:34 -06:00
|
|
|
void PrintHelpForCommands(MoreOutputStrings& output, HELP_Filter req_filter);
|
2022-05-26 16:58:09 +12:00
|
|
|
void AddShellCmdsToHelpList();
|
2023-05-31 00:27:34 -06:00
|
|
|
bool WriteHelp(const std::string& command, char* args);
|
2023-05-15 23:12:55 -06:00
|
|
|
[[nodiscard]] std::string ReadCommand();
|
2023-05-05 00:17:19 -06:00
|
|
|
[[nodiscard]] std::string SubstituteEnvironmentVariables(std::string_view command);
|
2023-05-16 23:22:22 -06:00
|
|
|
[[nodiscard]] std::string Which(std::string_view name) const;
|
2021-07-11 15:45:10 -07:00
|
|
|
|
2007-10-28 10:58:50 +00:00
|
|
|
friend class AutoexecEditor;
|
2023-05-15 23:12:55 -06:00
|
|
|
std::vector<std::string> history{};
|
2023-06-09 22:55:39 -06:00
|
|
|
std::stack<BatchFile> batchfiles{};
|
2021-01-08 14:55:35 -08:00
|
|
|
|
2023-05-31 00:27:34 -06:00
|
|
|
bool exit_cmd_called = false;
|
2022-05-26 16:58:09 +12:00
|
|
|
static inline bool help_list_populated = false;
|
2003-12-17 21:55:42 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
DOS_Shell();
|
2023-06-09 22:55:39 -06:00
|
|
|
~DOS_Shell() override = default;
|
2023-05-31 00:27:34 -06:00
|
|
|
DOS_Shell(const DOS_Shell&) = delete; // prevent copy
|
2019-12-07 11:55:38 -08:00
|
|
|
DOS_Shell& operator=(const DOS_Shell&) = delete; // prevent assignment
|
2021-01-08 14:55:35 -08:00
|
|
|
void Run() override;
|
2023-06-10 23:35:43 -06:00
|
|
|
void RunBatchFile();
|
|
|
|
|
2021-01-11 14:28:40 -08:00
|
|
|
/* A load of subfunctions */
|
2023-05-31 00:27:34 -06:00
|
|
|
void ParseLine(char* line);
|
|
|
|
void GetRedirection(char* s, std::string& ifn, std::string& ofn,
|
|
|
|
std::string& pipe, bool* append);
|
|
|
|
void InputCommand(char* line);
|
2003-12-17 21:55:42 +00:00
|
|
|
void ShowPrompt();
|
2023-05-31 00:27:34 -06:00
|
|
|
void DoCommand(char* cmd);
|
2023-08-30 16:43:11 +02:00
|
|
|
|
|
|
|
// Execute external shell command / program / configuration change
|
|
|
|
// 'virtual' needed for unit tests
|
|
|
|
virtual bool ExecuteShellCommand(const char* const name, char* arguments);
|
|
|
|
bool ExecuteProgram(std::string_view name, std::string_view args);
|
|
|
|
bool ExecuteConfigChange(const char* const cmd_in, const char* const line);
|
|
|
|
|
2023-06-24 10:04:35 +02:00
|
|
|
void ReadShellHistory();
|
|
|
|
void WriteShellHistory();
|
2023-05-31 00:27:34 -06:00
|
|
|
|
2023-06-08 00:50:24 -06:00
|
|
|
bool GetEnvStr(const char* entry, std::string& result) const override;
|
2023-05-31 00:27:34 -06:00
|
|
|
bool GetEnvNum(Bitu num, std::string& result) const;
|
2023-05-31 00:13:21 -06:00
|
|
|
[[nodiscard]] Bitu GetEnvCount() const;
|
2023-05-31 00:27:34 -06:00
|
|
|
bool SetEnv(const char* entry, const char* new_string);
|
2020-06-14 19:09:56 +02:00
|
|
|
|
|
|
|
/* Commands */
|
2023-05-31 00:27:34 -06:00
|
|
|
void CMD_HELP(char* args);
|
|
|
|
void CMD_CLS(char* args);
|
|
|
|
void CMD_COPY(char* args);
|
|
|
|
void CMD_DATE(char* args);
|
|
|
|
void CMD_TIME(char* args);
|
|
|
|
void CMD_DIR(char* args);
|
|
|
|
void CMD_DELETE(char* args);
|
|
|
|
void CMD_ECHO(char* args);
|
|
|
|
void CMD_EXIT(char* args);
|
2023-08-27 19:20:21 -06:00
|
|
|
void CMD_FOR(char* args);
|
2023-05-31 00:27:34 -06:00
|
|
|
void CMD_MKDIR(char* args);
|
|
|
|
void CMD_CHDIR(char* args);
|
|
|
|
void CMD_RMDIR(char* args);
|
|
|
|
void CMD_SET(char* args);
|
|
|
|
void CMD_IF(char* args);
|
|
|
|
void CMD_GOTO(char* args);
|
|
|
|
void CMD_TYPE(char* args);
|
|
|
|
void CMD_REM(char* args);
|
|
|
|
void CMD_RENAME(char* args);
|
|
|
|
void CMD_CALL(char* args);
|
2021-01-11 14:28:40 -08:00
|
|
|
void SyntaxError();
|
2023-05-31 00:27:34 -06:00
|
|
|
void CMD_PAUSE(char* args);
|
2003-12-17 21:55:42 +00:00
|
|
|
void CMD_SUBST(char* args);
|
|
|
|
void CMD_LOADHIGH(char* args);
|
2023-05-31 00:27:34 -06:00
|
|
|
void CMD_CHOICE(char* args);
|
|
|
|
void CMD_ATTRIB(char* args);
|
|
|
|
void CMD_PATH(char* args);
|
|
|
|
void CMD_SHIFT(char* args);
|
|
|
|
void CMD_VER(char* args);
|
|
|
|
void CMD_LS(char* args);
|
2023-06-23 19:35:04 -05:00
|
|
|
void CMD_VOL(char* args);
|
2023-07-05 20:27:37 -05:00
|
|
|
void CMD_MOVE(char* args);
|
2022-05-26 16:58:33 +12:00
|
|
|
|
2003-12-17 21:55:42 +00:00
|
|
|
/* The shell's variables */
|
2023-05-31 00:27:34 -06:00
|
|
|
uint16_t input_handle = 0;
|
|
|
|
bool echo = false;
|
|
|
|
bool call = false;
|
2003-12-17 21:55:42 +00:00
|
|
|
};
|
|
|
|
|
2023-10-22 00:40:10 -07:00
|
|
|
std::tuple<std::string, std::string, std::string, bool> parse_drive_conf(
|
2023-04-08 16:07:31 +02:00
|
|
|
std::string drive_letter, const std_fs::path& conf_path);
|
2005-03-25 09:46:53 +00:00
|
|
|
|
2023-10-21 19:06:55 +02:00
|
|
|
std::string to_search_pattern(const char* arg);
|
|
|
|
|
2023-03-13 20:46:41 +01:00
|
|
|
// Localized output
|
|
|
|
|
|
|
|
char* format_date(const uint16_t year, const uint8_t month, const uint8_t day);
|
|
|
|
char* format_time(const uint8_t hour, const uint8_t min, const uint8_t sec,
|
|
|
|
const uint8_t msec, const bool full = false);
|
|
|
|
std::string format_number(const size_t num);
|
|
|
|
|
|
|
|
std::string shorten_path(const std::string& path, const size_t max_len);
|
|
|
|
|
2003-12-17 21:55:42 +00:00
|
|
|
#endif
|