Added Include folder
*facepalm*
This commit is contained in:
@ -1,10 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "cmd/args/arg_base.hpp"
|
||||
#include "ast/ast_component.hpp"
|
||||
|
||||
#include "lexer.hpp"
|
||||
#include "util/stringliteral.hpp"
|
||||
|
||||
namespace fsh {
|
||||
|
||||
template <typename T>
|
||||
@ -15,22 +21,33 @@ namespace fsh {
|
||||
Argument() {}
|
||||
|
||||
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override;
|
||||
|
||||
virtual T& gvalue() { return value; }
|
||||
|
||||
private:
|
||||
bool gis_string_literal() { return is_string_literal; }
|
||||
|
||||
protected:
|
||||
bool is_string_literal; // Currently no getter
|
||||
T value;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void Argument<T>::svalue(const std::string& val, const Lexer::TokenType& type) {
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
value = val;
|
||||
} else {
|
||||
|
||||
is_string_literal = type == Lexer::TokenType::STRING_LITERAL;
|
||||
|
||||
if constexpr (std::is_same_v<T, StringLiteral>) {
|
||||
if(type != Lexer::TokenType::STRING_LITERAL) {
|
||||
throw std::invalid_argument("Incorrect type");
|
||||
}
|
||||
}
|
||||
|
||||
if constexpr (!std::is_same_v<T, std::string> && !std::is_base_of_v<std::string, T>) {
|
||||
std::stringstream ss_val(val);
|
||||
if (!(ss_val >> value)) { throw std::invalid_argument("Incorrect type"); }
|
||||
} else {
|
||||
value = val;
|
||||
}
|
||||
is_string_literal = type == Lexer::TokenType::STRING_LITERAL;
|
||||
}
|
||||
|
||||
}
|
||||
@ -4,19 +4,20 @@
|
||||
#include <sstream>
|
||||
|
||||
#include "cmd/args/arg_base.hpp"
|
||||
#include "cmd/args/arg_generic.hpp"
|
||||
|
||||
namespace fsh {
|
||||
|
||||
class ArgInput : public _Argument {
|
||||
class ArgInput : public Argument<std::string> {
|
||||
public:
|
||||
static std::istream& get(std::shared_ptr<_Argument> a) {
|
||||
return std::dynamic_pointer_cast<ArgInput>(a)->gvalue();
|
||||
return std::dynamic_pointer_cast<ArgInput>(a)->gstream();
|
||||
}
|
||||
|
||||
ArgInput() {}
|
||||
|
||||
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override;
|
||||
virtual std::istream& gvalue();
|
||||
virtual std::istream& gstream();
|
||||
|
||||
private:
|
||||
std::optional<std::stringstream> str;
|
||||
|
||||
@ -30,11 +30,16 @@ namespace fsh {
|
||||
if (flags.find(id) != flags.end()) return Argument<T>::get(flags[id]);
|
||||
return std::make_optional<T>();
|
||||
}
|
||||
|
||||
bool get(const std::string& id) { return flags.find(id) != flags.end(); }
|
||||
|
||||
void push_arg(std::shared_ptr<_Argument> arg) { pos_argument.push_back(arg); }
|
||||
void push_flag(const std::string& id, std::shared_ptr<_Argument> arg = nullptr) { flags[id] = arg; }
|
||||
|
||||
unsigned int size() {
|
||||
return pos_argument.size();
|
||||
}
|
||||
|
||||
private:
|
||||
PosArgs pos_argument;
|
||||
FlagOpts flags;
|
||||
|
||||
@ -8,6 +8,10 @@
|
||||
#include "cmd/cmd_misc.hpp"
|
||||
#include "cmd/cmd_time.hpp"
|
||||
#include "cmd/cmd_touch.hpp"
|
||||
#include "cmd/cmd_truncate.hpp"
|
||||
#include "cmd/cmd_tr.hpp"
|
||||
#include "cmd/cmd_prompt.hpp"
|
||||
#include "cmd/cmd_rm.hpp"
|
||||
#include "cmd/cmd_wc.hpp"
|
||||
|
||||
namespace fsh {
|
||||
@ -21,7 +25,7 @@ namespace fsh {
|
||||
}
|
||||
|
||||
Command& get(const std::string n) {
|
||||
if (cmds.find(n) == cmds.end()) { throw std::runtime_error("Command not found"); }
|
||||
if (cmds.find(n) == cmds.end()) { throw std::runtime_error("Command not found " + n); }
|
||||
return *(cmds[n]);
|
||||
}
|
||||
|
||||
@ -30,13 +34,17 @@ namespace fsh {
|
||||
private:
|
||||
// Nove komande bi registrovali ovde:
|
||||
CommandRegistry() {
|
||||
cmds["wc"] = Command::register_cmd<CmdWc>();
|
||||
cmds["date"] = Command::register_cmd<CmdDate>();
|
||||
cmds["time"] = Command::register_cmd<CmdTime>();
|
||||
cmds["echo"] = Command::register_cmd<CmdEcho>();
|
||||
cmds["exit"] = Command::register_cmd<CmdExit>();
|
||||
cmds["touch"] = Command::register_cmd<CmdTouch>();
|
||||
cmds["debug"] = Command::register_cmd<CmdPrintTree>();
|
||||
cmds["tr"] = Command::register_cmd<CmdTr>();
|
||||
cmds["rm"] = Command::register_cmd<CmdRM>();
|
||||
cmds["wc"] = Command::register_cmd<CmdWc>();
|
||||
cmds["date"] = Command::register_cmd<CmdDate>();
|
||||
cmds["time"] = Command::register_cmd<CmdTime>();
|
||||
cmds["echo"] = Command::register_cmd<CmdEcho>();
|
||||
cmds["exit"] = Command::register_cmd<CmdExit>();
|
||||
cmds["touch"] = Command::register_cmd<CmdTouch>();
|
||||
cmds["debug"] = Command::register_cmd<CmdPrintTree>();
|
||||
cmds["prompt"] = Command::register_cmd<CmdPrompt>();
|
||||
cmds["truncate"] = Command::register_cmd<CmdTruncate>();
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, std::unique_ptr<Command> > cmds;
|
||||
|
||||
@ -12,12 +12,12 @@
|
||||
namespace fsh {
|
||||
|
||||
class Command {
|
||||
|
||||
public:
|
||||
using FlagNode = std::optional<std::string>&;
|
||||
using ArgNodes = std::vector<std::shared_ptr<CommandArgumentNode> >;
|
||||
|
||||
public:
|
||||
void execute(FlagNode flag, ArgNodes& vec, std::istream& in, std::ostream& out);
|
||||
virtual void execute(FlagNode flag, ArgNodes& vec, std::istream& in, std::ostream& out, ArgFactory &arg_factory);
|
||||
virtual void execute(FlagNode flag, ArgNodes& vec, std::istream& in, std::ostream& out);
|
||||
|
||||
template <typename T>
|
||||
static std::unique_ptr<Command> register_cmd() {
|
||||
|
||||
@ -15,7 +15,7 @@ namespace fsh {
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
std::string s;
|
||||
std::string o;
|
||||
while (getline(in, s)) { o += s + "\n"; }
|
||||
while (std::getline(in, s)) { o += s + "\n"; }
|
||||
out << o;
|
||||
}
|
||||
};
|
||||
|
||||
26
include/cmd/cmd_prompt.hpp
Normal file
26
include/cmd/cmd_prompt.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "cmd/cmd_base.hpp"
|
||||
#include "util/stringliteral.hpp"
|
||||
#include "fsh.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace fsh {
|
||||
|
||||
class CmdPrompt : public Command {
|
||||
|
||||
protected:
|
||||
virtual void register_flags() override {
|
||||
ArgFactory& factory = get_factory();
|
||||
factory.add_rule<StringLiteral>(1);
|
||||
}
|
||||
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
|
||||
std::string n_prompt = args.get<StringLiteral>(0).value();
|
||||
|
||||
fsh::instance().environment["PROMPT"] = n_prompt;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
26
include/cmd/cmd_rm.hpp
Normal file
26
include/cmd/cmd_rm.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "cmd/cmd_base.hpp"
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace fsh {
|
||||
|
||||
class CmdRM : public Command {
|
||||
|
||||
protected:
|
||||
virtual void register_flags() override {
|
||||
ArgFactory& factory = get_factory();
|
||||
factory.add_rule<std::string>(1);
|
||||
}
|
||||
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
|
||||
if(std::remove(args.get<std::string>(0).value().c_str())) {
|
||||
throw std::runtime_error("Could not delete file");
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
74
include/cmd/cmd_tr.hpp
Normal file
74
include/cmd/cmd_tr.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#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>
|
||||
|
||||
namespace fsh {
|
||||
|
||||
class CmdTr : public Command {
|
||||
public:
|
||||
virtual void execute(FlagNode flag, ArgNodes& vec, std::istream& in, std::ostream& out) override {
|
||||
ArgFactory af;
|
||||
switch (vec.size()) {
|
||||
case 2:
|
||||
af.add_rule<StringLiteral>(1);
|
||||
case 1:
|
||||
af.add_rule<StringLiteral>(1);
|
||||
break;
|
||||
|
||||
default:
|
||||
af.add_input_rule();
|
||||
af.add_rule<StringLiteral>(1);
|
||||
af.add_rule<StringLiteral>(1);
|
||||
break;
|
||||
}
|
||||
Command::execute(flag, vec, in,out, af);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
std::string replace_all(std::string str,const std::string &what,const std::string &with) {
|
||||
if(what == "") throw std::runtime_error("\"\" cannot be used as what");
|
||||
|
||||
unsigned long long pos = str.find(what);
|
||||
|
||||
while(pos != std::string::npos) {
|
||||
str.replace(pos,what.size(),with) ;
|
||||
pos = str.find(what,pos+with.size());
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
std::string s;
|
||||
std::string o;
|
||||
std::string what = "";
|
||||
std::string with = "";
|
||||
while (std::getline(in, s)) { o += s + "\n"; }
|
||||
|
||||
switch (args.size()) {
|
||||
case 2:
|
||||
with = args.get<StringLiteral>(1).value();
|
||||
case 1:
|
||||
what = args.get<StringLiteral>(0).value();
|
||||
break;
|
||||
case 3:
|
||||
what = args.get<StringLiteral>(1).value();
|
||||
with = args.get<StringLiteral>(2).value();
|
||||
}
|
||||
|
||||
out << replace_all(o,what,with);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
34
include/cmd/cmd_truncate.hpp
Normal file
34
include/cmd/cmd_truncate.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "cmd/cmd_base.hpp"
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace fsh {
|
||||
|
||||
class CmdTruncate : public Command {
|
||||
|
||||
protected:
|
||||
virtual void register_flags() override {
|
||||
ArgFactory& factory = get_factory();
|
||||
factory.add_rule<std::string>(1);
|
||||
}
|
||||
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
|
||||
std::string filename = args.get<std::string>(0).value();
|
||||
|
||||
if(!std::ifstream(filename, std::ios_base::in)) {
|
||||
throw std::runtime_error("File does not exist");
|
||||
}
|
||||
|
||||
if(!std::ofstream(filename, std::ios_base::out)) {
|
||||
throw std::runtime_error("Failed to truncate");
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user