diff --git a/command.cpp b/command.cpp index ab4e77b..bbc1c8a 100644 --- a/command.cpp +++ b/command.cpp @@ -304,10 +304,9 @@ int begin_command::execute(Environment &env, const fdmask &fds, bool throwup) { if (status) return env.status(status); int rv; - { - indent_helper indent(env); - rv = vector_command::execute(env, newfds); - } + env.indent_and([&]{ + rv = vector_command::execute(env, newfds); + }); env.echo("%s", type == BEGIN ? "end" : ")"); @@ -388,13 +387,14 @@ int if_command::execute(Environment &env, const fdmask &fds, bool throwup) { env.echo("%s", s.c_str()); if (ok) continue; - { - indent_helper indent(env); - int tmp = evaluate(c->type, s, env); - if (tmp < 0) { ok = true; rv = tmp; continue; } - if (tmp == 0) continue; + + int tmp = evaluate(c->type, s, env); + if (tmp < 0) { ok = true; rv = tmp; continue; } + if (tmp == 0) continue; + + env.indent_and([&](){ rv = c->execute(env, newfds); - } + }); } env.echo("end"); diff --git a/environment.h b/environment.h index e40cae2..ad45eca 100644 --- a/environment.h +++ b/environment.h @@ -91,6 +91,13 @@ public: void echo(const char *format, ...); + template + void indent_and(FX &&fx) { + int i = _indent++; + try { fx(); _indent = i; } + catch (...) { _indent = i; throw; } + } + private: // magic variables. @@ -111,6 +118,7 @@ private: std::unordered_map _table; }; +/* class indent_helper { public: indent_helper(Environment &e) : env(e) { env._indent++; } @@ -120,6 +128,6 @@ private: Environment &env; bool active = true; }; - +*/ #endif