Skip to content

Commit

Permalink
fixed bash pipes cutting off extra information
Browse files Browse the repository at this point in the history
  • Loading branch information
malloc-nbytes committed Jan 12, 2025
1 parent 2722d2a commit 28b1da3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/include/earl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "ast.hpp"
#include "token.hpp"

/// \brief Make sure that both obj0 and obj1 are compatible with
/// all binary opterations.
#define ASSERT_BINOP_COMPAT(obj0, obj1, op) \
do { \
if (!type_is_compatable(obj0, obj1)) { \
Expand All @@ -45,6 +47,8 @@
} \
} while (0)

/// \brief Make sure that obj0 and obj1 are the same type
/// for binary operations.
#define ASSERT_BINOP_EXACT(obj0, obj1, op) \
do { \
if (obj0->type() != obj1->type()) { \
Expand All @@ -55,6 +59,7 @@
} \
} while (0)

/// \brief Make sure that obj0 and obj1 are compatable for mutation.
#define ASSERT_MUTATE_COMPAT(obj0, obj1, stmt) \
do { \
if (!type_is_compatable(obj0, obj1)) { \
Expand All @@ -65,6 +70,7 @@
} \
} while (0)

/// \brief If obj is const, fail.
#define ASSERT_CONSTNESS(obj, stmt) \
do { \
if (obj->is_const()) { \
Expand Down
5 changes: 4 additions & 1 deletion src/input.rl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module Main

println("hello world".endswith("fjdsl"));
# $"cat ../src/1.in" |> let content;
$"whoami" |> let content;

println(content);
12 changes: 8 additions & 4 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2612,23 +2612,28 @@ eval_stmt_bash_lit(StmtBashLiteral *stmt, std::shared_ptr<Ctx> ctx) {
static std::shared_ptr<earl::value::Obj>
eval_stmt_pipe(StmtPipe *stmt, std::shared_ptr<Ctx> ctx) {
auto get_bash_res = [&](std::string cmd, Stmt *stmt) {
bool sanatize = (flags & __NO_SANITIZE_PIPES) == 0;
std::string output = "";
std::array<char, 256> buffer;
FILE *pipe = popen(cmd.c_str(), "r");
if (!pipe) {
Err::err_wstmt(stmt);
throw InterpreterException("failed to execute bash `"+cmd+"`");
}
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
output += buffer.data();
}
int ec = pclose(pipe);
if (ec == -1) {
const std::string msg = "command `"+cmd+"` failed to exit";
throw InterpreterException(msg);
}
if (output.size() > 1)
if (sanatize && output.size() > 1
&& (output.back() == ' '
|| output.back() == '\n'
|| output.back() == '\t'))
output.erase(output.size()-1);
if (((flags & __NO_SANITIZE_PIPES) == 0)
if (sanatize
&& output.size() == 1
&& (output == " "
|| output == "\n"
Expand Down Expand Up @@ -2684,7 +2689,6 @@ eval_stmt_pipe(StmtPipe *stmt, std::shared_ptr<Ctx> ctx) {
// TODO: show-muts
// TODO: attributes
auto location = unpack_ER(expr_er, ctx, true);
// std::string cmd = dynamic_cast<earl::value::Str *>(bash.get())->value();
location->mutate(get_bash_res(cmd, stmt).get(), nullptr);
}
}, to);
Expand Down

0 comments on commit 28b1da3

Please sign in to comment.