55 lines
1.8 KiB
C++
55 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <map>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <vector>
|
|
#include <optional>
|
|
|
|
namespace sh
|
|
{
|
|
enum class Command
|
|
{
|
|
exit,
|
|
echo,
|
|
type,
|
|
pwd,
|
|
cd,
|
|
unknown
|
|
};
|
|
|
|
|
|
inline const std::map<const std::string_view, const Command>commandMap
|
|
{
|
|
{"exit", Command::exit},
|
|
{"echo", Command::echo},
|
|
{"type", Command::type},
|
|
{"pwd" , Command::pwd },
|
|
{"cd" , Command::cd }
|
|
};
|
|
|
|
inline constexpr std::string_view doubleQuoteEscapeChars = "\"$`\n\\";
|
|
|
|
inline constexpr std::string_view tokenDelimiters = " \t";
|
|
|
|
void tokenize(std::vector<std::string>& tokens, const std::string& input);
|
|
|
|
Command getCommand(std::string_view input);
|
|
|
|
std::optional<std::filesystem::path> isExec(const std::string& input);
|
|
|
|
void printType(const std::string& input, const std::optional<std::filesystem::path>& stdoutFilePath = std::nullopt);
|
|
|
|
std::filesystem::path getWorkingDir();
|
|
|
|
void changeDir(const std::vector<std::string>& tokens, const std::optional<std::filesystem::path>& stdoutFilePath = std::nullopt);
|
|
|
|
void printCommandStdout(std::string_view input, const std::optional<std::filesystem::path>& outputFilePath = std::nullopt, bool append = false);
|
|
|
|
void printCommandStderr(std::string_view input, const std::optional<std::filesystem::path>& outputFilePath = std::nullopt, bool append = false);
|
|
|
|
void executeCommand(const std::filesystem::path& exePath, const std::vector<std::string>& tokens, const std::optional<std::filesystem::path>& stdoutFilePath = std::nullopt, const std::optional<std::filesystem::path>& stderrFilePath = std::nullopt, bool appendStdout = false, bool appendStderr = false);
|
|
}
|