Finished Batch + Minor improvements

This commit is contained in:
2025-07-29 20:22:17 +02:00
parent f54a3ba309
commit f93c463647
8 changed files with 80 additions and 18 deletions

View File

@ -3,8 +3,10 @@
#include <unordered_map> #include <unordered_map>
#include "cmd/cmd_base.hpp" #include "cmd/cmd_base.hpp"
#include "cmd/cmd_batch.hpp"
#include "cmd/cmd_date.hpp" #include "cmd/cmd_date.hpp"
#include "cmd/cmd_echo.hpp" #include "cmd/cmd_echo.hpp"
#include "cmd/cmd_head.hpp"
#include "cmd/cmd_misc.hpp" #include "cmd/cmd_misc.hpp"
#include "cmd/cmd_time.hpp" #include "cmd/cmd_time.hpp"
#include "cmd/cmd_touch.hpp" #include "cmd/cmd_touch.hpp"
@ -38,9 +40,11 @@ namespace fsh {
cmds["rm"] = Command::register_cmd<CmdRM>(); cmds["rm"] = Command::register_cmd<CmdRM>();
cmds["wc"] = Command::register_cmd<CmdWc>(); cmds["wc"] = Command::register_cmd<CmdWc>();
cmds["date"] = Command::register_cmd<CmdDate>(); cmds["date"] = Command::register_cmd<CmdDate>();
cmds["head"] = Command::register_cmd<CmdHead>();
cmds["time"] = Command::register_cmd<CmdTime>(); cmds["time"] = Command::register_cmd<CmdTime>();
cmds["echo"] = Command::register_cmd<CmdEcho>(); cmds["echo"] = Command::register_cmd<CmdEcho>();
cmds["exit"] = Command::register_cmd<CmdExit>(); cmds["exit"] = Command::register_cmd<CmdExit>();
cmds["batch"] = Command::register_cmd<CmdBatch>();
cmds["touch"] = Command::register_cmd<CmdTouch>(); cmds["touch"] = Command::register_cmd<CmdTouch>();
cmds["debug"] = Command::register_cmd<CmdPrintTree>(); cmds["debug"] = Command::register_cmd<CmdPrintTree>();
cmds["prompt"] = Command::register_cmd<CmdPrompt>(); cmds["prompt"] = Command::register_cmd<CmdPrompt>();

25
include/cmd/cmd_batch.hpp Normal file
View File

@ -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);
}
}
};
}

View File

@ -14,9 +14,11 @@ namespace fsh {
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override { virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
std::string s; std::string s;
std::string o; char c;
while (std::getline(in, s)) { o += s + "\n"; } while (in.get(c)) {
out << o; s+=c;
}
out << s;
} }
}; };

29
include/cmd/cmd_head.hpp Normal file
View File

@ -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<int>((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<int>("-n").value();
while (std::getline(in, s)) {
if(count-- < 1) break;
o += s + "\n";
}
out << o;
}
};
}

View File

@ -26,12 +26,13 @@ namespace fsh {
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override { virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
std::string line; 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); ast->print(0);
auto ast = AstFactory::generate_ast(tokens); }
ast->print(0);
} }
}; };

View File

@ -1,15 +1,10 @@
#pragma once #pragma once
#include "cmd/args/arg.hpp" #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/args/arg_manager.hpp"
#include "cmd/cmd_base.hpp" #include "cmd/cmd_base.hpp"
#include "util/input.hpp"
#include "util/stringliteral.hpp" #include "util/stringliteral.hpp"
#include <algorithm>
#include <iostream> #include <iostream>
#include <memory>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>

View File

@ -1,5 +1,6 @@
#include "cmd/args/arg.hpp" #include "cmd/args/arg.hpp"
#include "cmd/args/arg_generic.hpp" #include "cmd/args/arg_generic.hpp"
#include <iostream>
#include <string> #include <string>
namespace fsh { namespace fsh {
@ -34,18 +35,22 @@ namespace fsh {
} }
void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) { void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) {
const std::string f = *flag; const std::string_view f(*flag);
int f_sz = f.size();
for (const auto& [key, rule] : flag_rules) { for (const auto& [key, rule] : flag_rules) {
const std::string_view k(key); const std::string_view k(key);
int k_sz = k.length();
if (k == f) { if (k == f) {
manager.push_flag(f); manager.push_flag(*flag);
return; return;
} }
if (rule.capturing && f == k.substr(0, f_sz)) { if (rule.capturing && k == f.substr(0, k_sz)) {
auto arg = build_arg(rule.build, (std::string)k.substr(f_sz)); auto arg = build_arg(rule.build, (std::string)f.substr(k_sz));
manager.push_flag(f, arg); manager.push_flag(key, arg);
return; return;
} }
} }

View File

@ -22,6 +22,7 @@ namespace fsh {
std::cout << environment["PROMPT"] << " "; std::cout << environment["PROMPT"] << " ";
std::string line; std::string line;
std::getline(util::cin, line); std::getline(util::cin, line);
if(line == "") continue;
run_line(line, util::cin, std::cout); run_line(line, util::cin, std::cout);
} catch (const std::exception& e) { std::cerr << e.what() << "\n"; } } catch (const std::exception& e) { std::cerr << e.what() << "\n"; }
} }