From f93c463647a9d1b4019c3c7cdd42f70ce94dbb52 Mon Sep 17 00:00:00 2001 From: Pimpest <82343504+pimpest@users.noreply.github.com> Date: Tue, 29 Jul 2025 20:22:17 +0200 Subject: [PATCH] Finished Batch + Minor improvements --- include/cmd/cmd.hpp | 4 ++++ include/cmd/cmd_batch.hpp | 25 +++++++++++++++++++++++++ include/cmd/cmd_echo.hpp | 8 +++++--- include/cmd/cmd_head.hpp | 29 +++++++++++++++++++++++++++++ include/cmd/cmd_misc.hpp | 9 +++++---- include/cmd/cmd_tr.hpp | 5 ----- src/cmd/arg.cpp | 17 +++++++++++------ src/fsh.cpp | 1 + 8 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 include/cmd/cmd_batch.hpp create mode 100644 include/cmd/cmd_head.hpp diff --git a/include/cmd/cmd.hpp b/include/cmd/cmd.hpp index 5c70eb7..d4d636b 100644 --- a/include/cmd/cmd.hpp +++ b/include/cmd/cmd.hpp @@ -3,8 +3,10 @@ #include #include "cmd/cmd_base.hpp" +#include "cmd/cmd_batch.hpp" #include "cmd/cmd_date.hpp" #include "cmd/cmd_echo.hpp" +#include "cmd/cmd_head.hpp" #include "cmd/cmd_misc.hpp" #include "cmd/cmd_time.hpp" #include "cmd/cmd_touch.hpp" @@ -38,9 +40,11 @@ namespace fsh { cmds["rm"] = Command::register_cmd(); cmds["wc"] = Command::register_cmd(); cmds["date"] = Command::register_cmd(); + cmds["head"] = Command::register_cmd(); cmds["time"] = Command::register_cmd(); cmds["echo"] = Command::register_cmd(); cmds["exit"] = Command::register_cmd(); + cmds["batch"] = Command::register_cmd(); cmds["touch"] = Command::register_cmd(); cmds["debug"] = Command::register_cmd(); cmds["prompt"] = Command::register_cmd(); diff --git a/include/cmd/cmd_batch.hpp b/include/cmd/cmd_batch.hpp new file mode 100644 index 0000000..3452567 --- /dev/null +++ b/include/cmd/cmd_batch.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "cmd/cmd_base.hpp" +#include "fsh.hpp" + +namespace fsh { + + class CmdBatch : public Command { + + protected: + virtual void register_flags() override { + ArgFactory& factory = get_factory(); + factory.add_input_rule(); + } + + virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override { + std::string s; + while (std::getline(in, s)) { + if(s == "") continue; + fsh::instance().run_line(s, util::cin, out); + } + } + }; + +} \ No newline at end of file diff --git a/include/cmd/cmd_echo.hpp b/include/cmd/cmd_echo.hpp index 88b77e2..b8dedd4 100644 --- a/include/cmd/cmd_echo.hpp +++ b/include/cmd/cmd_echo.hpp @@ -14,9 +14,11 @@ namespace fsh { virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override { std::string s; - std::string o; - while (std::getline(in, s)) { o += s + "\n"; } - out << o; + char c; + while (in.get(c)) { + s+=c; + } + out << s; } }; diff --git a/include/cmd/cmd_head.hpp b/include/cmd/cmd_head.hpp new file mode 100644 index 0000000..7029a9b --- /dev/null +++ b/include/cmd/cmd_head.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "cmd/cmd_base.hpp" + +namespace fsh { + + class CmdHead : public Command { + + protected: + virtual void register_flags() override { + ArgFactory& factory = get_factory(); + factory.add_rule((std::string) "-n", true); + factory.add_input_rule(); + } + + virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override { + std::string s; + std::string o; + + int count = args.get("-n").value(); + while (std::getline(in, s)) { + if(count-- < 1) break; + o += s + "\n"; + } + out << o; + } + }; + +} \ No newline at end of file diff --git a/include/cmd/cmd_misc.hpp b/include/cmd/cmd_misc.hpp index 1c5e386..4b25d7c 100644 --- a/include/cmd/cmd_misc.hpp +++ b/include/cmd/cmd_misc.hpp @@ -26,12 +26,13 @@ namespace fsh { virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override { std::string line; - std::getline(in, line); + while(std::getline(in, line)) { + auto tokens = Lexer::process(line); + auto ast = AstFactory::generate_ast(tokens); - auto tokens = Lexer::process(line); - auto ast = AstFactory::generate_ast(tokens); + ast->print(0); + } - ast->print(0); } }; diff --git a/include/cmd/cmd_tr.hpp b/include/cmd/cmd_tr.hpp index 3da59ea..3cb95fd 100644 --- a/include/cmd/cmd_tr.hpp +++ b/include/cmd/cmd_tr.hpp @@ -1,15 +1,10 @@ #pragma once #include "cmd/args/arg.hpp" -#include "cmd/args/arg_generic.hpp" -#include "cmd/args/arg_input.hpp" #include "cmd/args/arg_manager.hpp" #include "cmd/cmd_base.hpp" -#include "util/input.hpp" #include "util/stringliteral.hpp" -#include #include -#include #include #include diff --git a/src/cmd/arg.cpp b/src/cmd/arg.cpp index 0032fe9..393ea83 100644 --- a/src/cmd/arg.cpp +++ b/src/cmd/arg.cpp @@ -1,5 +1,6 @@ #include "cmd/args/arg.hpp" #include "cmd/args/arg_generic.hpp" +#include #include namespace fsh { @@ -34,18 +35,22 @@ namespace fsh { } void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) { - const std::string f = *flag; - int f_sz = f.size(); + const std::string_view f(*flag); + for (const auto& [key, rule] : flag_rules) { + const std::string_view k(key); + + int k_sz = k.length(); + if (k == f) { - manager.push_flag(f); + manager.push_flag(*flag); return; } - if (rule.capturing && f == k.substr(0, f_sz)) { - auto arg = build_arg(rule.build, (std::string)k.substr(f_sz)); - manager.push_flag(f, arg); + if (rule.capturing && k == f.substr(0, k_sz)) { + auto arg = build_arg(rule.build, (std::string)f.substr(k_sz)); + manager.push_flag(key, arg); return; } } diff --git a/src/fsh.cpp b/src/fsh.cpp index a125264..6ad23cb 100644 --- a/src/fsh.cpp +++ b/src/fsh.cpp @@ -22,6 +22,7 @@ namespace fsh { std::cout << environment["PROMPT"] << " "; std::string line; std::getline(util::cin, line); + if(line == "") continue; run_line(line, util::cin, std::cout); } catch (const std::exception& e) { std::cerr << e.what() << "\n"; } }