Massive reformating and cleanup

This commit is contained in:
2024-12-06 21:07:54 +01:00
parent 79390b94b7
commit d2cbdc2910
33 changed files with 1030 additions and 1100 deletions

66
.clang-format Normal file
View File

@@ -0,0 +1,66 @@
---
Language: Cpp
BasedOnStyle: Mozilla
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: true
AlignEscapedNewlines: Right
AlignOperands: false
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 120
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentWidth: 4
PointerAlignment: Left
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 4
UseTab: Never
AllowShortEnumsOnASingleLine: false
BraceWrapping:
AfterEnum: false
AlignConsecutiveDeclarations:
Enabled: true
NamespaceIndentation: All

View File

@@ -21,16 +21,14 @@
#include "util/text.hpp" #include "util/text.hpp"
#include "ast/ast_base.hpp" #include "ast/ast_base.hpp"
#include "ast/ast_token.hpp"
#include "ast/ast_component.hpp" #include "ast/ast_component.hpp"
#include "ast/ast_executable.hpp" #include "ast/ast_executable.hpp"
#include "ast/ast_token.hpp"
namespace fsh { namespace fsh {
class AstFactory { class AstFactory {
public: public:
// Generates an abstract syntax tree // Generates an abstract syntax tree
static std::shared_ptr<ExecutableNode> generate_ast(std::list<Token>& list) { static std::shared_ptr<ExecutableNode> generate_ast(std::list<Token>& list) {
auto it = list.begin(); auto it = list.begin();
@@ -43,4 +41,4 @@ class AstFactory {
AstFactory(const AstFactory&) = default; AstFactory(const AstFactory&) = default;
}; };
} } // namespace fsh

View File

@@ -1,9 +1,9 @@
#pragma once #pragma once
#include <string>
#include <memory> #include <memory>
#include <stdexcept>
#include <sstream> #include <sstream>
#include <stdexcept>
#include <string>
#include "lexer.hpp" #include "lexer.hpp"
#include "util/text.hpp" #include "util/text.hpp"
@@ -24,6 +24,7 @@ enum NodeType {
class AstNode { class AstNode {
friend class std::shared_ptr<AstNode>; friend class std::shared_ptr<AstNode>;
public: public:
NodeType gtype() { return type; } NodeType gtype() { return type; }
const std::string& gname() { return name; }; const std::string& gname() { return name; };
@@ -32,16 +33,19 @@ class AstNode {
using TokenType = Lexer::TokenType; using TokenType = Lexer::TokenType;
AstNode(NodeType type, std::string name) : type(type), name(name) {} AstNode(NodeType type, std::string name) : type(type), name(name) {}
template <class T> template <class T>
static std::shared_ptr<T> Optional(std::list<Token>::iterator& it, std::shared_ptr<T>& node_ptr); static std::shared_ptr<T> Optional(std::list<Token>::iterator& it, std::shared_ptr<T>& node_ptr);
template <class T> template <class T>
static std::shared_ptr<T> Optional(std::list<Token>::iterator& it) { static std::shared_ptr<T> Optional(std::list<Token>::iterator& it) {
std::shared_ptr<T> node_ptr; std::shared_ptr<T> node_ptr;
return Optional<T>(it, node_ptr); return Optional<T>(it, node_ptr);
} }
template <class T> template <class T>
static std::shared_ptr<T> Mandatory(std::list<Token>::iterator& it) {return T::build(it);} static std::shared_ptr<T> Mandatory(std::list<Token>::iterator& it) {
return T::build(it);
}
private: private:
AstNode(); AstNode();
@@ -54,8 +58,10 @@ class ExecutableNode : public AstNode {
public: public:
virtual void print(int indent) {}; virtual void print(int indent) {};
virtual void execute(std::istream& in, std::ostream& out) {} virtual void execute(std::istream& in, std::ostream& out) {}
protected: protected:
ExecutableNode(NodeType type, std::string Name) : AstNode(type, Name) {} ExecutableNode(NodeType type, std::string Name) : AstNode(type, Name) {}
private: private:
}; };
@@ -64,4 +70,4 @@ public:
AstBuildError(std::string err) : std::runtime_error(err) {} AstBuildError(std::string err) : std::runtime_error(err) {}
}; };
} } // namespace fsh

View File

@@ -19,14 +19,11 @@ class CommandArgumentNode : public AstNode {
std::string& gvalue() { return value; } std::string& gvalue() { return value; }
protected: protected:
CommandArgumentNode(TokenNode<TokenType::WORD>::shared word) CommandArgumentNode(TokenNode<TokenType::WORD>::shared word) :
: AstNode(NodeType::COMMAND_ARGUMENT, "StringArgument"), AstNode(NodeType::COMMAND_ARGUMENT, "StringArgument"), type(word->gtoken_type()), value(word->gvalue()) {}
type(word->gtoken_type()),
value(word->gvalue()) {} CommandArgumentNode(TokenNode<TokenType::STRING_LITERAL>::shared word) :
CommandArgumentNode(TokenNode<TokenType::STRING_LITERAL>::shared word) AstNode(NodeType::COMMAND_ARGUMENT, "StringArgument"), type(word->gtoken_type()), value(word->gvalue()) {}
: AstNode(NodeType::COMMAND_ARGUMENT, "StringArgument"),
type(word->gtoken_type()),
value(word->gvalue()) {}
private: private:
Lexer::TokenType type; Lexer::TokenType type;
@@ -40,15 +37,13 @@ class LRedirectNode : public AstNode {
std::string gvalue() { return input; } std::string gvalue() { return input; }
protected: protected:
LRedirectNode(TokenNode<TokenType::LREDIRECTION>::shared, TokenNode<TokenType::WORD>::shared in) LRedirectNode(TokenNode<TokenType::LREDIRECTION>::shared, TokenNode<TokenType::WORD>::shared in) :
: AstNode(NodeType::LREDIRECTS, "LRedirectNode"), AstNode(NodeType::LREDIRECTS, "LRedirectNode"), input(in->gvalue()) {}
input(in->gvalue()) {}
private: private:
std::string input; std::string input;
}; };
class RRedirectNode : public AstNode { class RRedirectNode : public AstNode {
public: public:
static std::shared_ptr<RRedirectNode> build(std::list<Token>::iterator& it); static std::shared_ptr<RRedirectNode> build(std::list<Token>::iterator& it);
@@ -57,10 +52,8 @@ class RRedirectNode : public AstNode {
bool gappend() { return append; } bool gappend() { return append; }
protected: protected:
RRedirectNode(TokenNode<TokenType::RREDIRECTION>::shared rr, TokenNode<TokenType::WORD>::shared out) RRedirectNode(TokenNode<TokenType::RREDIRECTION>::shared rr, TokenNode<TokenType::WORD>::shared out) :
: AstNode(NodeType::RREDIRECTS, "LRedirectNode"), AstNode(NodeType::RREDIRECTS, "LRedirectNode"), output(out->gvalue()), append(rr->gvalue().size() > 1) {}
output(out->gvalue()),
append(rr->gvalue().size()>1) {}
private: private:
std::string output; std::string output;
@@ -76,10 +69,10 @@ class RedirectsNode : public AstNode {
bool gappend() { return append; } bool gappend() { return append; }
protected: protected:
RedirectsNode(std::shared_ptr<LRedirectNode> in, std::shared_ptr<RRedirectNode> out) RedirectsNode(std::shared_ptr<LRedirectNode> in, std::shared_ptr<RRedirectNode> out) :
: AstNode(NodeType::REDIRECTS, "RedirectNode"), AstNode(NodeType::REDIRECTS, "RedirectNode"), input(util::mk_optional(in)), output(util::mk_optional(out)) {
input(util::mk_optional(in)), if (out) append = out->gappend();
output(util::mk_optional(out)) {if(out) append = out->gappend();} }
private: private:
std::optional<std::string> input; std::optional<std::string> input;
@@ -87,4 +80,4 @@ class RedirectsNode : public AstNode {
bool append = false; bool append = false;
}; };
} } // namespace fsh

View File

@@ -1,13 +1,12 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <stdexcept>
#include <optional> #include <optional>
#include <stdexcept>
#include "ast/ast_base.hpp" #include "ast/ast_base.hpp"
#include "ast/ast_token.hpp"
#include "ast/ast_component.hpp" #include "ast/ast_component.hpp"
#include "ast/ast_token.hpp"
#include "lexer.hpp" #include "lexer.hpp"
#include "util/text.hpp" #include "util/text.hpp"
@@ -16,22 +15,19 @@ namespace fsh {
class CommandNode : public ExecutableNode { class CommandNode : public ExecutableNode {
using ArgumentVec = std::vector<std::shared_ptr<CommandArgumentNode> >; using ArgumentVec = std::vector<std::shared_ptr<CommandArgumentNode> >;
public: public:
static std::shared_ptr<CommandNode> build(std::list<Token>::iterator& it); static std::shared_ptr<CommandNode> build(std::list<Token>::iterator& it);
virtual void print(int indent) override; virtual void print(int indent) override;
virtual void execute(std::istream& in, std::ostream& out) override; virtual void execute(std::istream& in, std::ostream& out) override;
protected: protected:
CommandNode( CommandNode(TokenNode<TokenType::WORD>::shared cmd_name,
TokenNode<TokenType::WORD>::shared cmd_name,
TokenNode<TokenType::FLAG>::shared flag, TokenNode<TokenType::FLAG>::shared flag,
ArgumentVec args, ArgumentVec args,
std::shared_ptr<RedirectsNode> redirects std::shared_ptr<RedirectsNode> redirects) :
) : ExecutableNode(NodeType::COMMAND, "CommandNode"), ExecutableNode(NodeType::COMMAND, "CommandNode"), cmd_name(cmd_name->gvalue()), flag(util::mk_optional(flag)),
cmd_name(cmd_name->gvalue()), redirects(redirects), args(args) {}
flag(util::mk_optional(flag)),
redirects(redirects),
args(args) {}
private: private:
std::string cmd_name; std::string cmd_name;
@@ -47,17 +43,14 @@ class PipeLineNode : public ExecutableNode {
virtual void execute(std::istream& in, std::ostream& out) override; virtual void execute(std::istream& in, std::ostream& out) override;
protected: protected:
PipeLineNode(std::shared_ptr<CommandNode> l, std::shared_ptr<ExecutableNode> r) PipeLineNode(std::shared_ptr<CommandNode> l, std::shared_ptr<ExecutableNode> r) :
: ExecutableNode(NodeType::PIPELINE_COMMAND, "PipeLine"), ExecutableNode(NodeType::PIPELINE_COMMAND, "PipeLine"), l_command(l), r_command(r) {}
l_command(l),
r_command(r) {}
private: private:
std::shared_ptr<ExecutableNode> l_command; std::shared_ptr<ExecutableNode> l_command;
std::shared_ptr<ExecutableNode> r_command; std::shared_ptr<ExecutableNode> r_command;
}; };
class CommandLineNode : public ExecutableNode { class CommandLineNode : public ExecutableNode {
public: public:
static std::shared_ptr<CommandLineNode> build(std::list<Token>::iterator& it); static std::shared_ptr<CommandLineNode> build(std::list<Token>::iterator& it);
@@ -65,14 +58,12 @@ class CommandLineNode : public ExecutableNode {
virtual void execute(std::istream& in, std::ostream& out) override; virtual void execute(std::istream& in, std::ostream& out) override;
protected: protected:
CommandLineNode(std::shared_ptr<ExecutableNode> command, std::shared_ptr<ExecutableNode> pipe = nullptr) CommandLineNode(std::shared_ptr<ExecutableNode> command, std::shared_ptr<ExecutableNode> pipe = nullptr) :
: ExecutableNode(NodeType::COMMAND_LINE, "CommandLine"), ExecutableNode(NodeType::COMMAND_LINE, "CommandLine"), command(command), pipe(pipe) {}
command(command),
pipe(pipe) {}
private: private:
std::shared_ptr<ExecutableNode> command; std::shared_ptr<ExecutableNode> command;
std::shared_ptr<ExecutableNode> pipe; std::shared_ptr<ExecutableNode> pipe;
}; };
} } // namespace fsh

View File

@@ -4,7 +4,6 @@
#include "ast/ast_base.hpp" #include "ast/ast_base.hpp"
#include "util/text.hpp" #include "util/text.hpp"
namespace fsh { namespace fsh {
template <Lexer::TokenType T> template <Lexer::TokenType T>
@@ -16,8 +15,7 @@ class TokenNode : public AstNode {
static std::shared_ptr<TokenNode> build(std::list<Token>::iterator& it); static std::shared_ptr<TokenNode> build(std::list<Token>::iterator& it);
protected: protected:
TokenNode(std::string value) TokenNode(std::string value) : AstNode(NodeType::TOKEN, util::tokens[T]), value(value) {}
: AstNode(NodeType::TOKEN, util::tokens[T]), value(value) {}
private: private:
std::string value; std::string value;

View File

@@ -21,22 +21,19 @@ public:
using ArgNodes = std::vector<std::shared_ptr<CommandArgumentNode> >; using ArgNodes = std::vector<std::shared_ptr<CommandArgumentNode> >;
using BuildFunc = std::shared_ptr<_Argument> (*)(void); using BuildFunc = std::shared_ptr<_Argument> (*)(void);
struct ArgRule struct ArgRule {
{
BuildFunc build; BuildFunc build;
bool mandatory; bool mandatory;
bool extends; bool extends;
}; };
struct FlagRule struct FlagRule {
{
BuildFunc build; BuildFunc build;
bool specialinput; //Not implemented bool specialinput; //Not implemented
bool capturing; bool capturing;
bool extends; //Not implemented bool extends; //Not implemented
}; };
void add_input_rule() { void add_input_rule() {
has_input = true; has_input = true;
pos_arg_rules.push_back({ &_Argument::create<ArgInput>, false, false }); pos_arg_rules.push_back({ &_Argument::create<ArgInput>, false, false });
@@ -53,6 +50,7 @@ public:
} }
void parse(ArgManager& manager, ArgNodes& vec, FlagNode flag); void parse(ArgManager& manager, ArgNodes& vec, FlagNode flag);
bool ghas_input() { return has_input; } bool ghas_input() { return has_input; }
private: private:

View File

@@ -9,12 +9,13 @@ namespace fsh{
class _Argument { class _Argument {
public: public:
virtual void svalue(const std::string& can, const Lexer::TokenType& type = Lexer::TokenType::FLAG) {} virtual void svalue(const std::string& can, const Lexer::TokenType& type = Lexer::TokenType::FLAG) {}
virtual void svalue(std::shared_ptr<CommandArgumentNode> can) { virtual void svalue(std::shared_ptr<CommandArgumentNode> can) { svalue(can->gvalue(), can->gtoken_type()); }
svalue(can->gvalue(), can->gtoken_type());
}
template <typename T> template <typename T>
static std::shared_ptr<_Argument> create() {return std::make_shared<T>();} static std::shared_ptr<_Argument> create() {
return std::make_shared<T>();
}
protected: protected:
_Argument() {} _Argument() {}
}; };

View File

@@ -10,14 +10,13 @@ namespace fsh{
template <typename T> template <typename T>
class Argument : public _Argument { class Argument : public _Argument {
public: public:
static T& get(std::shared_ptr<_Argument> a) { static T& get(std::shared_ptr<_Argument> a) { return std::dynamic_pointer_cast<Argument<T> >(a)->gvalue(); }
return std::dynamic_pointer_cast<Argument<T> >(a)->gvalue();
}
Argument() {} Argument() {}
virtual void svalue(const std::string& val, const Lexer::TokenType& type) override; virtual void svalue(const std::string& val, const Lexer::TokenType& type) override;
virtual T& gvalue() { return value; } virtual T& gvalue() { return value; }
private: private:
bool is_string_literal; // Currently no getter bool is_string_literal; // Currently no getter
T value; T value;
@@ -29,9 +28,7 @@ void Argument<T>::svalue(const std::string& val, const Lexer::TokenType& type) {
value = val; value = val;
} else { } else {
std::stringstream ss_val(val); std::stringstream ss_val(val);
if(!(ss_val >> value)) { if (!(ss_val >> value)) { throw std::invalid_argument("Incorrect type"); }
throw std::invalid_argument("Incorrect type");
}
} }
is_string_literal = type == Lexer::TokenType::STRING_LITERAL; is_string_literal = type == Lexer::TokenType::STRING_LITERAL;
} }

View File

@@ -14,7 +14,6 @@ class ArgManager {
public: public:
ArgManager() {} ArgManager() {}
std::istream& get_input(const unsigned int id) { std::istream& get_input(const unsigned int id) {
if (id < pos_argument.size()) return ArgInput::get(pos_argument[id]); if (id < pos_argument.size()) return ArgInput::get(pos_argument[id]);
return util::cin; return util::cin;
@@ -25,21 +24,16 @@ public:
if ((unsigned int)id < pos_argument.size()) return Argument<T>::get(pos_argument[id]); if ((unsigned int)id < pos_argument.size()) return Argument<T>::get(pos_argument[id]);
return std::make_optional<T>(); return std::make_optional<T>();
} }
template <typename T> template <typename T>
std::optional<T> get(const std::string& id) { std::optional<T> get(const std::string& id) {
if (flags.find(id) != flags.end()) return Argument<T>::get(flags[id]); if (flags.find(id) != flags.end()) return Argument<T>::get(flags[id]);
return std::make_optional<T>(); return std::make_optional<T>();
} }
bool get(const std::string& id) { bool get(const std::string& id) { return flags.find(id) != flags.end(); }
return flags.find(id) != flags.end();
}
void push_arg(std::shared_ptr<_Argument> arg) { void push_arg(std::shared_ptr<_Argument> arg) { pos_argument.push_back(arg); }
pos_argument.push_back(arg); void push_flag(const std::string& id, std::shared_ptr<_Argument> arg = nullptr) { flags[id] = arg; }
}
void push_flag(const std::string &id,std::shared_ptr<_Argument> arg = nullptr) {
flags[id] = arg;
}
private: private:
PosArgs pos_argument; PosArgs pos_argument;

View File

@@ -3,36 +3,28 @@
#include <unordered_map> #include <unordered_map>
#include "cmd/cmd_base.hpp" #include "cmd/cmd_base.hpp"
#include "cmd/cmd_wc.hpp"
#include "cmd/cmd_time.hpp"
#include "cmd/cmd_date.hpp" #include "cmd/cmd_date.hpp"
#include "cmd/cmd_echo.hpp" #include "cmd/cmd_echo.hpp"
#include "cmd/cmd_touch.hpp"
#include "cmd/cmd_misc.hpp" #include "cmd/cmd_misc.hpp"
#include "cmd/cmd_time.hpp"
#include "cmd/cmd_touch.hpp"
#include "cmd/cmd_wc.hpp"
namespace fsh namespace fsh {
{
class CommandRegistry { class CommandRegistry {
public: public:
static CommandRegistry& instance() { static CommandRegistry& instance() {
static CommandRegistry cmd_registry; static CommandRegistry cmd_registry;
return cmd_registry; return cmd_registry;
} }
Command& get(const std::string n) { Command& get(const std::string n) {
if(cmds.find(n) == cmds.end()) { if (cmds.find(n) == cmds.end()) { throw std::runtime_error("Command not found"); }
throw std::runtime_error("Command not found");
}
return *(cmds[n]); return *(cmds[n]);
} }
Command& operator[](const std::string n) { Command& operator[](const std::string n) { return get(n); }
return get(n);
}
private: private:
CommandRegistry() { CommandRegistry() {
@@ -46,8 +38,6 @@ class CommandRegistry {
} }
std::unordered_map<std::string, std::unique_ptr<Command> > cmds; std::unordered_map<std::string, std::unique_ptr<Command> > cmds;
}; };
} // namespace fsh
}

View File

@@ -27,15 +27,12 @@ class Command {
} }
protected: protected:
virtual void register_flags() {}; virtual void register_flags() {};
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) {}; virtual void run(std::istream& in, std::ostream& out, ArgManager& args) {};
ArgFactory& get_factory() { return arg_factory; } ArgFactory& get_factory() { return arg_factory; }
private: private:
ArgFactory arg_factory; ArgFactory arg_factory;
}; };
} }

View File

@@ -5,7 +5,6 @@
#include "cmd/cmd_base.hpp" #include "cmd/cmd_base.hpp"
namespace fsh { namespace fsh {
class CmdDate : public Command { class CmdDate : public Command {
@@ -15,7 +14,6 @@ class CmdDate : public Command {
std::time_t time = std::time(nullptr); std::time_t time = std::time(nullptr);
out << std::asctime(std::localtime(&time)); out << std::asctime(std::localtime(&time));
} }
}; };
} }

View File

@@ -15,12 +15,9 @@ class CmdEcho : public Command {
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; std::string o;
while(getline(in, s)) { while (getline(in, s)) { o += s + "\n"; }
o += s + "\n";
}
out << o; out << o;
} }
}; };
} }

View File

@@ -6,7 +6,6 @@
#include "fsh.hpp" #include "fsh.hpp"
#include "ast/ast.hpp" #include "ast/ast.hpp"
namespace fsh { namespace fsh {
class CmdExit : public Command { class CmdExit : public Command {
@@ -15,19 +14,16 @@ class CmdExit : public Command {
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override { virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
fsh::instance().environment["EXITING"] = "1"; fsh::instance().environment["EXITING"] = "1";
} }
}; };
class CmdPrintTree : public Command { class CmdPrintTree : public Command {
protected: protected:
virtual void register_flags() override { virtual void register_flags() override {
auto& factory = get_factory(); auto& factory = get_factory();
factory.add_input_rule(); factory.add_input_rule();
} }
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); std::getline(in, line);
@@ -37,7 +33,6 @@ class CmdPrintTree : public Command {
ast->print(0); ast->print(0);
} }
}; };
} }

View File

@@ -14,7 +14,6 @@ class CmdTime : public Command {
std::time_t time = std::time(nullptr); std::time_t time = std::time(nullptr);
out << std::put_time(std::localtime(&time), "%T%n"); out << std::put_time(std::localtime(&time), "%T%n");
} }
}; };
} }

View File

@@ -18,7 +18,6 @@ class CmdTouch : public Command {
throw std::runtime_error("File exists"); throw std::runtime_error("File exists");
} }
} }
}; };
} }

View File

@@ -33,7 +33,6 @@ class CmdWc : public Command {
} }
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override { virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
bool prev_space = true;
if (args.get("-c")) { if (args.get("-c")) {
out << count_chars(in) << "\n"; out << count_chars(in) << "\n";
} else if (args.get("-w")) { } else if (args.get("-w")) {
@@ -42,7 +41,6 @@ class CmdWc : public Command {
throw std::invalid_argument("Didn't provide flag"); throw std::invalid_argument("Didn't provide flag");
} }
} }
}; };
} }

View File

@@ -6,8 +6,7 @@
#include "ast/ast.hpp" #include "ast/ast.hpp"
#include "util/input.hpp" #include "util/input.hpp"
namespace fsh namespace fsh {
{
class fsh { class fsh {
public: public:
@@ -26,8 +25,6 @@ private:
environment["PROMPT"] = "$"; environment["PROMPT"] = "$";
environment["QUIT"] = ""; environment["QUIT"] = "";
} }
}; };
} }

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <list>
#include <functional> #include <functional>
#include <list>
#include <string> #include <string>
namespace fsh { namespace fsh {
@@ -17,6 +17,7 @@ public:
LREDIRECTION, LREDIRECTION,
RREDIRECTION, RREDIRECTION,
PIPE, PIPE,
END_OF_STREAM END_OF_STREAM
}; };
@@ -33,14 +34,12 @@ private:
unsigned int idx; unsigned int idx;
Lexer(std::string str) : workspace(str), idx(0) {} Lexer(std::string str) : workspace(str), idx(0) {}
}; };
using Token = Lexer::Token; using Token = Lexer::Token;
class LexerStateMachine { class LexerStateMachine {
friend class Lexer; friend class Lexer;
private: private:
enum State { enum State {
START, START,
@@ -60,7 +59,8 @@ private:
Lexer::TokenType type; Lexer::TokenType type;
std::vector<StateHandler> state_handlers; std::vector<StateHandler> state_handlers;
LexerStateMachine(Lexer& parser) : parser(parser), token(""), type(Lexer::TokenType::WHITESPACE), state_handlers(STATE_COUNT) { LexerStateMachine(Lexer& parser) :
parser(parser), token(""), type(Lexer::TokenType::WHITESPACE), state_handlers(STATE_COUNT) {
state_handlers[State::START] = std::bind(&LexerStateMachine::handle_start, this); state_handlers[State::START] = std::bind(&LexerStateMachine::handle_start, this);
state_handlers[State::REDIRECT] = std::bind(&LexerStateMachine::handle_redirect, this); state_handlers[State::REDIRECT] = std::bind(&LexerStateMachine::handle_redirect, this);
state_handlers[State::FLAG_OPT] = std::bind(&LexerStateMachine::handle_flag_opt, this); state_handlers[State::FLAG_OPT] = std::bind(&LexerStateMachine::handle_flag_opt, this);
@@ -81,12 +81,10 @@ private:
State word_like_handler(Lexer::TokenType type, State state); State word_like_handler(Lexer::TokenType type, State state);
}; };
inline LexerStateMachine::State LexerStateMachine::set_type( inline LexerStateMachine::State LexerStateMachine::set_type(Lexer::TokenType type = Lexer::TokenType::WHITESPACE,
Lexer::TokenType type = Lexer::TokenType::WHITESPACE, State state = State::END) {
State state = State::END
){
this->type = type; this->type = type;
return state; return state;
} }
} } // namespace fsh

View File

@@ -7,9 +7,7 @@
#include <iostream> #include <iostream>
#endif #endif
namespace fsh::util {
namespace fsh::util
{
/** /**
* Applies some settings to terminals to fix some inconsistencies between * Applies some settings to terminals to fix some inconsistencies between
@@ -31,9 +29,8 @@ public:
RIGHT_ARROW RIGHT_ARROW
}; };
CursorIStreamBuffer() : valid(0), read_chars(0), cursor_pos(0), buffer() { CursorIStreamBuffer() : valid(0), read_chars(0), cursor_pos(0), buffer() { setg(buffer, buffer, buffer); }
setg(buffer,buffer,buffer);
}
protected: protected:
int underflow() override; int underflow() override;
// int uflow() override; // int uflow() override;
@@ -53,6 +50,7 @@ private:
class CursorIStream : public std::istream { class CursorIStream : public std::istream {
public: public:
CursorIStream() : std::istream(&buffer) {} CursorIStream() : std::istream(&buffer) {}
private: private:
CursorIStreamBuffer buffer; CursorIStreamBuffer buffer;
}; };
@@ -64,6 +62,4 @@ extern std::istream& cin;
#endif #endif
} }

View File

@@ -7,14 +7,11 @@
namespace fsh::util { namespace fsh::util {
extern const char* tokens[]; extern const char* tokens[];
template <class T> template <class T>
static inline std::optional<std::string> mk_optional(T t) { static inline std::optional<std::string> mk_optional(T t) {
if(t){ if (t) { return t->gvalue(); }
return t->gvalue();
}
return std::optional<std::string>(); return std::optional<std::string>();
} }
@@ -25,30 +22,21 @@ static inline bool contains(char x, std::string text) {
return 0; return 0;
}; };
static inline std::ifstream input(const std::string& x) { static inline std::ifstream input(const std::string& x) {
std::ifstream in(x, std::ios::in); std::ifstream in(x, std::ios::in);
if(!in) { if (!in) { throw std::runtime_error("Can't open file"); }
throw std::runtime_error("Can't open file");
}
return in; return in;
} }
static inline std::ofstream output(const std::string& x) { static inline std::ofstream output(const std::string& x) {
std::ofstream out(x, std::ios::out); std::ofstream out(x, std::ios::out);
if(!out.is_open()) { if (!out.is_open()) { throw std::runtime_error("Can't open file"); }
throw std::runtime_error("Can't open file");
}
return out; return out;
} }
static inline std::ofstream output_append(const std::string& x) { static inline std::ofstream output_append(const std::string& x) {
std::ofstream out(x, std::ios::app | std::ios::out); std::ofstream out(x, std::ios::app | std::ios::out);
if(!out.is_open()) { if (!out.is_open()) { throw std::runtime_error("Can't open file"); }
throw std::runtime_error("Can't open file");
}
return out; return out;
} }

View File

@@ -28,9 +28,9 @@ typename TokenNode<T>::shared TokenNode<T>::build(std::list<Token>::iterator& it
std::shared_ptr<CommandArgumentNode> CommandArgumentNode::build(std::list<Token>::iterator& it) { std::shared_ptr<CommandArgumentNode> CommandArgumentNode::build(std::list<Token>::iterator& it) {
auto word = Optional<TokenNode<TokenType::WORD>>(it); auto word = Optional<TokenNode<TokenType::WORD>>(it);
if(word) {
return std::shared_ptr<CommandArgumentNode>(new CommandArgumentNode(word)); if (word) return std::shared_ptr<CommandArgumentNode>(new CommandArgumentNode(word));
}
auto sl = Mandatory<TokenNode<TokenType::STRING_LITERAL>>(it); auto sl = Mandatory<TokenNode<TokenType::STRING_LITERAL>>(it);
return std::shared_ptr<CommandArgumentNode>(new CommandArgumentNode(sl)); return std::shared_ptr<CommandArgumentNode>(new CommandArgumentNode(sl));
} }
@@ -38,19 +38,23 @@ std::shared_ptr<CommandArgumentNode> CommandArgumentNode::build(std::list<Token>
std::shared_ptr<RRedirectNode> RRedirectNode::build(std::list<Token>::iterator& it) { std::shared_ptr<RRedirectNode> RRedirectNode::build(std::list<Token>::iterator& it) {
auto Rr = Mandatory<TokenNode<TokenType::RREDIRECTION>>(it); auto Rr = Mandatory<TokenNode<TokenType::RREDIRECTION>>(it);
auto output = Mandatory<TokenNode<TokenType::WORD>>(it); auto output = Mandatory<TokenNode<TokenType::WORD>>(it);
return std::shared_ptr<RRedirectNode>(new RRedirectNode(Rr, output)); return std::shared_ptr<RRedirectNode>(new RRedirectNode(Rr, output));
} }
std::shared_ptr<LRedirectNode> LRedirectNode::build(std::list<Token>::iterator& it) { std::shared_ptr<LRedirectNode> LRedirectNode::build(std::list<Token>::iterator& it) {
auto Lr = Mandatory<TokenNode<TokenType::LREDIRECTION>>(it); auto Lr = Mandatory<TokenNode<TokenType::LREDIRECTION>>(it);
auto output = Mandatory<TokenNode<TokenType::WORD>>(it); auto output = Mandatory<TokenNode<TokenType::WORD>>(it);
return std::shared_ptr<LRedirectNode>(new LRedirectNode(Lr, output)); return std::shared_ptr<LRedirectNode>(new LRedirectNode(Lr, output));
} }
std::shared_ptr<RedirectsNode> RedirectsNode::build(std::list<Token>::iterator& it) { std::shared_ptr<RedirectsNode> RedirectsNode::build(std::list<Token>::iterator& it) {
TokenNode<TokenType::WORD>::shared input, output; TokenNode<TokenType::WORD>::shared input, output;
auto Lr = Optional<LRedirectNode>(it); auto Lr = Optional<LRedirectNode>(it);
auto Rr = Optional<RRedirectNode>(it); auto Rr = Optional<RRedirectNode>(it);
if (!Lr) Lr = Optional<LRedirectNode>(it); if (!Lr) Lr = Optional<LRedirectNode>(it);
if (!Lr && !Rr) throw AstBuildError("No redirects where provided"); if (!Lr && !Rr) throw AstBuildError("No redirects where provided");
return std::shared_ptr<RedirectsNode>(new RedirectsNode(Lr, Rr)); return std::shared_ptr<RedirectsNode>(new RedirectsNode(Lr, Rr));
@@ -59,8 +63,9 @@ std::shared_ptr<RedirectsNode> RedirectsNode::build(std::list<Token>::iterator&
std::shared_ptr<CommandNode> CommandNode::build(std::list<Token>::iterator& it) { std::shared_ptr<CommandNode> CommandNode::build(std::list<Token>::iterator& it) {
auto cmd_name = Mandatory<TokenNode<TokenType::WORD>>(it); auto cmd_name = Mandatory<TokenNode<TokenType::WORD>>(it);
auto cmd_opts = Optional<TokenNode<TokenType::FLAG>>(it); auto cmd_opts = Optional<TokenNode<TokenType::FLAG>>(it);
ArgumentVec argv;
auto cmd_argument = Optional<CommandArgumentNode>(it); auto cmd_argument = Optional<CommandArgumentNode>(it);
ArgumentVec argv;
while (cmd_argument) { while (cmd_argument) {
argv.push_back(cmd_argument); argv.push_back(cmd_argument);
cmd_argument = Optional<CommandArgumentNode>(it); cmd_argument = Optional<CommandArgumentNode>(it);
@@ -69,7 +74,6 @@ std::shared_ptr<CommandNode> CommandNode::build(std::list<Token>::iterator& it)
return std::shared_ptr<CommandNode>(new CommandNode(cmd_name, cmd_opts, argv, cmd_redr)); return std::shared_ptr<CommandNode>(new CommandNode(cmd_name, cmd_opts, argv, cmd_redr));
} }
std::shared_ptr<PipeLineNode> PipeLineNode::build(std::list<Token>::iterator& it) { std::shared_ptr<PipeLineNode> PipeLineNode::build(std::list<Token>::iterator& it) {
Mandatory<TokenNode<TokenType::PIPE>>(it); Mandatory<TokenNode<TokenType::PIPE>>(it);
auto l_cmd = Mandatory<CommandNode>(it); auto l_cmd = Mandatory<CommandNode>(it);

View File

@@ -12,8 +12,7 @@ void CommandNode::execute(std::istream& in, std::ostream& out) {
std::ofstream f_o; std::ofstream f_o;
if (redirects) { if (redirects) {
if (redirects->goutput()) { if (redirects->goutput()) {
if(redirects->gappend()) if (redirects->gappend()) f_o = util::output_append(*(redirects->goutput()));
f_o = util::output_append(*(redirects->goutput()));
else else
f_o = util::output(*(redirects->goutput())); f_o = util::output(*(redirects->goutput()));
o = &f_o; o = &f_o;
@@ -24,14 +23,12 @@ void CommandNode::execute(std::istream& in, std::ostream& out) {
} }
} }
/// TODO: Error when input/output was overwritten and in and out weren't cin/cout /// TODO: Error when input/output was overwritten and in and out weren't cin/cout
auto& cmd_registry = CommandRegistry::instance(); auto& cmd_registry = CommandRegistry::instance();
cmd_registry[cmd_name].execute(flag, args, *i, *o); cmd_registry[cmd_name].execute(flag, args, *i, *o);
} }
void PipeLineNode::execute(std::istream& in, std::ostream& out) { void PipeLineNode::execute(std::istream& in, std::ostream& out) {
std::stringstream buffer; std::stringstream buffer;
if (r_command) { if (r_command) {

View File

@@ -5,9 +5,7 @@ namespace fsh {
void CommandNode::print(int indent) { void CommandNode::print(int indent) {
std::string t = ""; std::string t = "";
for(int i=0; i<indent;i++) { for (int i = 0; i < indent; i++) { t += "\t"; }
t+="\t";
}
std::cout << t << gname() << "\n"; std::cout << t << gname() << "\n";
std::cout << t << "\tCommand_Name = " << cmd_name << "\n"; std::cout << t << "\tCommand_Name = " << cmd_name << "\n";
if (flag) std::cout << t << "\tFlagOpt = " << *flag << "\n"; if (flag) std::cout << t << "\tFlagOpt = " << *flag << "\n";
@@ -23,9 +21,7 @@ void CommandNode::print(int indent) {
void PipeLineNode::print(int indent) { void PipeLineNode::print(int indent) {
std::string t = ""; std::string t = "";
for(int i=0; i<indent;i++) { for (int i = 0; i < indent; i++) { t += "\t"; }
t+="\t";
}
std::cout << t << gname() << "\n"; std::cout << t << gname() << "\n";
std::cout << "left_cmd:\n"; std::cout << "left_cmd:\n";
if (l_command) l_command->print(indent + 1); if (l_command) l_command->print(indent + 1);
@@ -35,9 +31,7 @@ void PipeLineNode::print(int indent) {
void CommandLineNode::print(int indent) { void CommandLineNode::print(int indent) {
std::string t = ""; std::string t = "";
for(int i=0; i<indent;i++) { for (int i = 0; i < indent; i++) { t += "\t"; }
t+="\t";
}
std::cout << t << gname() << "\n"; std::cout << t << gname() << "\n";
if (command) command->print(indent + 1); if (command) command->print(indent + 1);
if (pipe) pipe->print(indent + 1); if (pipe) pipe->print(indent + 1);

View File

@@ -8,25 +8,17 @@ void ArgInput::svalue(const std::string& val, const Lexer::TokenType& type) {
str = std::stringstream(txt); str = std::stringstream(txt);
} else { } else {
file = std::ifstream(txt, std::ios::in); file = std::ifstream(txt, std::ios::in);
if(!*file) { if (!*file) { throw std::runtime_error("Failed to open file"); }
throw std::runtime_error("Failed to open file");
} }
} }
}
std::istream& ArgInput::gvalue() { std::istream& ArgInput::gvalue() {
if(str) if (str) return *str;
return *str;
return *file; return *file;
} }
void ArgFactory::parse(ArgManager& manager, ArgNodes& vec, FlagNode flag) { void ArgFactory::parse(ArgManager& manager, ArgNodes& vec, FlagNode flag) {
if(flag) { if (flag) { parse_flag(manager, flag); }
parse_flag(manager, flag);
}
unsigned int i = 0; unsigned int i = 0;
for (const auto& arg : vec) { for (const auto& arg : vec) {
std::shared_ptr<_Argument> a; std::shared_ptr<_Argument> a;
@@ -36,7 +28,6 @@ void ArgFactory::parse(ArgManager& manager, ArgNodes& vec, FlagNode flag) {
} }
} }
void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) { void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) {
const std::string f = *flag; const std::string f = *flag;
int f_sz = f.size(); int f_sz = f.size();
@@ -56,14 +47,14 @@ void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) {
throw std::invalid_argument("Invalid flag"); throw std::invalid_argument("Invalid flag");
} }
std::shared_ptr<_Argument> ArgFactory::build_arg(BuildFunc build_func, const std::string& str) { std::shared_ptr<_Argument> ArgFactory::build_arg(BuildFunc build_func, const std::string& str) {
auto arg = build_func(); auto arg = build_func();
arg->svalue(str); arg->svalue(str);
return arg; return arg;
} }
std::shared_ptr<_Argument> ArgFactory::build_arg(BuildFunc build_func, std::shared_ptr<CommandArgumentNode> cmd_arg) { std::shared_ptr<_Argument> ArgFactory::build_arg(BuildFunc build_func,
std::shared_ptr<CommandArgumentNode> cmd_arg) {
auto arg = build_func(); auto arg = build_func();
arg->svalue(cmd_arg); arg->svalue(cmd_arg);
return arg; return arg;

View File

@@ -7,19 +7,16 @@ namespace fsh {
ArgManager args; ArgManager args;
arg_factory.parse(args, vec, flag); arg_factory.parse(args, vec, flag);
if (arg_factory.ghas_input()) { if (arg_factory.ghas_input()) {
if (&args.get_input(0) != &util::cin && &in != &util::cin) { if (&args.get_input(0) != &util::cin && &in != &util::cin) {
throw std::runtime_error("Tried to set input to command twice"); throw std::runtime_error("Tried to set input to command twice");
} }
if(&args.get_input(0) != &util::cin) if (&args.get_input(0) != &util::cin) run(args.get_input(0), out, args);
run(args.get_input(0), out, args);
else else
run(in, out, args); run(in, out, args);
} } else {
else {
run(in, out, args); run(in, out, args);
} }
in.clear(); in.clear();
} }
} } // namespace fsh

View File

@@ -23,9 +23,7 @@ namespace fsh {
std::string line; std::string line;
std::getline(util::cin, line); std::getline(util::cin, line);
run_line(line, util::cin, std::cout); run_line(line, util::cin, std::cout);
} catch(const std::exception& e) { } catch (const std::exception& e) { std::cerr << e.what() << "\n"; }
std::cerr << e.what() << "\n";
}
} }
} }
} }

View File

@@ -9,14 +9,10 @@ namespace fsh {
Token LexerStateMachine::run() { Token LexerStateMachine::run() {
State state = State::START; State state = State::START;
while (state != State::END) { while (state != State::END) { state = state_handlers[state](); }
state = state_handlers[state]();
}
return { type, token }; return { type, token };
} }
LexerStateMachine::State LexerStateMachine::handle_start() { LexerStateMachine::State LexerStateMachine::handle_start() {
const int c = parser.peek(); const int c = parser.peek();
@@ -48,13 +44,9 @@ LexerStateMachine::State LexerStateMachine::handle_flag_opt() {
return word_like_handler(Lexer::TokenType::FLAG, State::FLAG_OPT); return word_like_handler(Lexer::TokenType::FLAG, State::FLAG_OPT);
} }
LexerStateMachine::State LexerStateMachine::handle_redirect() { LexerStateMachine::State LexerStateMachine::handle_redirect() {
const int c = parser.peek(); const int c = parser.peek();
if(c == '>') { if (c == '>') { token += parser.consume(); }
token += parser.consume();
}
return set_type(Lexer::TokenType::RREDIRECTION); return set_type(Lexer::TokenType::RREDIRECTION);
} }
@@ -62,7 +54,6 @@ LexerStateMachine::State LexerStateMachine::handle_potential_word() {
return word_like_handler(Lexer::TokenType::WORD, State::POTENTIAL_WORD); return word_like_handler(Lexer::TokenType::WORD, State::POTENTIAL_WORD);
} }
LexerStateMachine::State LexerStateMachine::handle_string_literal() { LexerStateMachine::State LexerStateMachine::handle_string_literal() {
const int c = parser.peek(); const int c = parser.peek();
if (c == std::char_traits<char>::eof()) { if (c == std::char_traits<char>::eof()) {
@@ -76,7 +67,6 @@ LexerStateMachine::State LexerStateMachine::handle_string_literal() {
} }
} }
LexerStateMachine::State LexerStateMachine::word_like_handler(Lexer::TokenType type, State state) { LexerStateMachine::State LexerStateMachine::word_like_handler(Lexer::TokenType type, State state) {
const int c = parser.peek(); const int c = parser.peek();
if (util::contains(c, "\t><| ") || c == std::char_traits<char>::eof()) { if (util::contains(c, "\t><| ") || c == std::char_traits<char>::eof()) {
@@ -93,20 +83,16 @@ std::list<Token> Lexer::process(std::string line) {
Lexer parser(line); Lexer parser(line);
std::list<Token> tokens; std::list<Token> tokens;
tokens.push_back(parser.next_token()); tokens.push_back(parser.next_token());
while(tokens.back().first != TokenType::END_OF_STREAM) { while (tokens.back().first != TokenType::END_OF_STREAM) { tokens.push_back(parser.next_token()); }
tokens.push_back(parser.next_token());
}
return tokens; return tokens;
} }
int Lexer::peek() { int Lexer::peek() {
if(idx < workspace.size()) if (idx < workspace.size()) return workspace[idx];
return workspace[idx];
return std::char_traits<char>::eof(); return std::char_traits<char>::eof();
} }
int Lexer::consume() { int Lexer::consume() {
if(idx < workspace.size()) if (idx < workspace.size()) return workspace[idx++];
return workspace[idx++];
return std::char_traits<char>::eof(); return std::char_traits<char>::eof();
} }

View File

@@ -5,7 +5,6 @@
#include "fsh.hpp" #include "fsh.hpp"
#include "util/input.hpp" #include "util/input.hpp"
int main() { int main() {
fsh::util::prepTerminal(); fsh::util::prepTerminal();
fsh::fsh::instance().run(); fsh::fsh::instance().run();

View File

@@ -11,22 +11,16 @@
#endif #endif
#include <iostream> #include <iostream>
#include "util/input.hpp" #include "util/input.hpp"
namespace fsh::util {
namespace fsh::util
{
#ifndef _WIN32 #ifndef _WIN32
struct termios prev_attr; struct termios prev_attr;
void resetTerminal() { void resetTerminal() { tcsetattr(STDIN_FILENO, TCSANOW, &prev_attr); }
tcsetattr(STDIN_FILENO, TCSANOW, &prev_attr);
}
#endif #endif
void prepTerminal() { void prepTerminal() {
#ifdef _WIN32 #ifdef _WIN32
@@ -38,9 +32,7 @@ void prepTerminal() {
strelica pod UNIX sistemima */ strelica pod UNIX sistemima */
struct termios tattr; struct termios tattr;
if(!isatty(STDIN_FILENO)) { if (!isatty(STDIN_FILENO)) { return; }
return;
}
tcgetattr(STDIN_FILENO, &prev_attr); tcgetattr(STDIN_FILENO, &prev_attr);
atexit(resetTerminal); atexit(resetTerminal);
@@ -52,7 +44,6 @@ void prepTerminal() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr); tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
std::cout << "\e[?25h"; std::cout << "\e[?25h";
#endif #endif
} }
#ifndef _WIN32 #ifndef _WIN32
@@ -60,9 +51,7 @@ void prepTerminal() {
int CursorIStreamBuffer::underflow() { int CursorIStreamBuffer::underflow() {
if (gptr() < egptr()) return traits_type::to_int_type(*gptr()); if (gptr() < egptr()) return traits_type::to_int_type(*gptr());
if(read_chars >= input.size()) { if (read_chars >= input.size()) { readInputToNewLine(); }
readInputToNewLine();
}
int n = input.copy(buffer, sizeof(buffer) - 1, read_chars); int n = input.copy(buffer, sizeof(buffer) - 1, read_chars);
read_chars += n; read_chars += n;
@@ -74,12 +63,9 @@ int CursorIStreamBuffer::underflow() {
CursorIStreamBuffer::EscapeSequence CursorIStreamBuffer::testForEscapeCodes(char chr) { CursorIStreamBuffer::EscapeSequence CursorIStreamBuffer::testForEscapeCodes(char chr) {
if (valid == 2) { if (valid == 2) {
valid = 0; valid = 0;
switch (chr) switch (chr) {
{ case 'C': return RIGHT_ARROW;
case 'C': case 'D': return LEFT_ARROW;
return RIGHT_ARROW;
case 'D':
return LEFT_ARROW;
} }
} }
if (chr == '\033') { if (chr == '\033') {
@@ -94,7 +80,6 @@ CursorIStreamBuffer::EscapeSequence CursorIStreamBuffer::testForEscapeCodes(char
return FAILED; return FAILED;
} }
int CursorIStreamBuffer::readInputToNewLine() { int CursorIStreamBuffer::readInputToNewLine() {
read_chars = 0; read_chars = 0;
cursor_pos = 0; cursor_pos = 0;
@@ -109,32 +94,27 @@ int CursorIStreamBuffer::readInputToNewLine(){
std::cout << "\e[D\e[P"; std::cout << "\e[D\e[P";
input.erase(--cursor_pos, 1); input.erase(--cursor_pos, 1);
} }
} } else if (e > 0) {
else if(e>0) {
if (e == RIGHT_ARROW) { if (e == RIGHT_ARROW) {
if (cursor_pos < input.size()) { if (cursor_pos < input.size()) {
cursor_pos++; cursor_pos++;
std::cout << "\e[C"; std::cout << "\e[C";
} }
} } else if (e == LEFT_ARROW) {
else if(e==LEFT_ARROW) {
if (cursor_pos > 0) { if (cursor_pos > 0) {
cursor_pos--; cursor_pos--;
std::cout << "\e[D"; std::cout << "\e[D";
} }
} }
continue; continue;
} } else if (c == '\n') {
else if(c == '\n') {
std::cout << '\n'; std::cout << '\n';
input.push_back(c); input.push_back(c);
return 0; return 0;
} } else {
else{
input.insert(cursor_pos++, 1, c); input.insert(cursor_pos++, 1, c);
std::cout << "\033[@" << c; std::cout << "\033[@" << c;
} }
} }
input.push_back(0); input.push_back(0);
return 0; return 0;
@@ -145,5 +125,4 @@ CursorIStream cin;
std::istream& cin = std::cin; std::istream& cin = std::cin;
#endif #endif
} }

View File

@@ -2,16 +2,7 @@
namespace fsh::util { namespace fsh::util {
const char* tokens[] = { const char* tokens[] = { "WHITESPACE", "WORD", "STRING_LITERAL", "OPT", "FLAG",
"WHITESPACE", "LREDIRECTION", "RREDIRECTION", "PIPE", "END_OF_STREAM" };
"WORD",
"STRING_LITERAL",
"OPT",
"FLAG",
"LREDIRECTION",
"RREDIRECTION",
"PIPE",
"END_OF_STREAM"
};
} }