Finished Batch + Minor improvements
This commit is contained in:
@ -3,8 +3,10 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#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<CmdRM>();
|
||||
cmds["wc"] = Command::register_cmd<CmdWc>();
|
||||
cmds["date"] = Command::register_cmd<CmdDate>();
|
||||
cmds["head"] = Command::register_cmd<CmdHead>();
|
||||
cmds["time"] = Command::register_cmd<CmdTime>();
|
||||
cmds["echo"] = Command::register_cmd<CmdEcho>();
|
||||
cmds["exit"] = Command::register_cmd<CmdExit>();
|
||||
cmds["batch"] = Command::register_cmd<CmdBatch>();
|
||||
cmds["touch"] = Command::register_cmd<CmdTouch>();
|
||||
cmds["debug"] = Command::register_cmd<CmdPrintTree>();
|
||||
cmds["prompt"] = Command::register_cmd<CmdPrompt>();
|
||||
|
||||
25
include/cmd/cmd_batch.hpp
Normal file
25
include/cmd/cmd_batch.hpp
Normal 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
29
include/cmd/cmd_head.hpp
Normal file
29
include/cmd/cmd_head.hpp
Normal 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;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -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 <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include "cmd/args/arg.hpp"
|
||||
#include "cmd/args/arg_generic.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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"; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user