Skip to content

Commit

Permalink
refactor runtime option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiguchi-nagisa committed Dec 11, 2023
1 parent 3edee57 commit e89169a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 39 deletions.
16 changes: 10 additions & 6 deletions src/cmd_shctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static int showOptions(const DSState &state) {
for (auto &e : runtimeOptions) {
errno = 0;
if (printf("%-*s%s\n", static_cast<int>(maxNameSize), e.name,
hasFlag(state.runtimeOption, e.option) ? "on" : "off") < 0) {
state.has(e.option) ? "on" : "off") < 0) {
return errno;
}
}
Expand Down Expand Up @@ -133,11 +133,13 @@ static int restoreOptions(DSState &state, const ArrayObject &argvObj, StringRef
if (option == RuntimeOption::MONITOR) {
setJobControlSignalSetting(state, set);
}
auto opt = state.getOption();
if (set) {
setFlag(state.runtimeOption, option);
setFlag(opt, option);
} else {
unsetFlag(state.runtimeOption, option);
unsetFlag(opt, option);
}
state.setOption(opt);
}
return 0;
}
Expand Down Expand Up @@ -182,7 +184,7 @@ static int setOption(DSState &state, const ArrayObject &argvObj, const unsigned
for (auto &e : runtimeOptions) {
value += e.name;
value += "=";
value += hasFlag(state.runtimeOption, e.option) ? "on" : "off";
value += state.has(e.option) ? "on" : "off";
value += " ";
}
state.setGlobal(BuiltinVarOffset::REPLY, DSValue::createStr(std::move(value)));
Expand All @@ -205,11 +207,13 @@ static int setOption(DSState &state, const ArrayObject &argvObj, const unsigned
foundMonitor = true;
setJobControlSignalSetting(state, set);
}
auto opt = state.getOption();
if (set) {
setFlag(state.runtimeOption, option);
setFlag(opt, option);
} else {
unsetFlag(state.runtimeOption, option);
unsetFlag(opt, option);
}
state.setOption(opt);
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void raiseSystemError(DSState &st, int errorNum, std::string &&message) {
}

void raiseShellExit(DSState &st, int64_t status) {
if (hasFlag(st.runtimeOption, RuntimeOption::HUP_EXIT)) {
if (st.has(RuntimeOption::HUP_EXIT)) {
st.jobTable.send(SIGHUP);
}
int s = maskExitStatus(status);
Expand Down
2 changes: 1 addition & 1 deletion src/redir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static int redirectImpl(const RedirObject::Entry &entry, bool overwrite) {
bool RedirObject::redirect(DSState &state) {
this->saveFDs();
for (auto &entry : this->entries) {
int r = redirectImpl(entry, hasFlag(state.runtimeOption, RuntimeOption::CLOBBER));
int r = redirectImpl(entry, state.has(RuntimeOption::CLOBBER));
if (this->backupFDSet > 0 && r != 0) {
std::string msg = ERROR_REDIR;
if (entry.value) {
Expand Down
28 changes: 13 additions & 15 deletions src/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,13 @@ bool VM::attachAsyncJob(DSState &state, DSValue &&desc, unsigned int procSize, c
raiseSystemError(state, errNum, "wait failed");
return false;
}
if (forkKind == ForkKind::PIPE_FAIL && hasFlag(state.runtimeOption, RuntimeOption::ERR_RAISE)) {
if (forkKind == ForkKind::PIPE_FAIL && state.has(RuntimeOption::ERR_RAISE)) {
for (unsigned int index = 0; index < entry->getProcSize(); index++) {
auto &proc = entry->getProcs()[index];
int s = proc.exitStatus();
if (s != 0) {
if (index < entry->getProcSize() - 1 && proc.signaled() && proc.asSigNum() == SIGPIPE) {
if (!hasFlag(state.runtimeOption, RuntimeOption::FAIL_SIGPIPE)) {
if (!state.has(RuntimeOption::FAIL_SIGPIPE)) {
continue;
}
}
Expand Down Expand Up @@ -554,7 +554,7 @@ bool VM::forkAndEval(DSState &state, DSValue &&desc) {
raiseSystemError(state, errNum, "wait failed");
return false;
}
if (status != 0 && hasFlag(state.runtimeOption, RuntimeOption::ERR_RAISE)) {
if (status != 0 && state.has(RuntimeOption::ERR_RAISE)) {
std::string message = "child process exits with non-zero status: `";
message += std::to_string(status);
message += "'";
Expand Down Expand Up @@ -877,8 +877,7 @@ static void traceCmd(const DSState &state, const ArrayObject &argv) {
}

static bool checkCmdExecError(DSState &state, CmdCallAttr attr, int64_t status) {
if (status != 0 && hasFlag(attr, CmdCallAttr::RAISE) &&
hasFlag(state.runtimeOption, RuntimeOption::ERR_RAISE)) {
if (status != 0 && hasFlag(attr, CmdCallAttr::RAISE) && state.has(RuntimeOption::ERR_RAISE)) {
std::string message = "command exits with non-zero status: `";
message += std::to_string(status);
message += "'";
Expand All @@ -898,7 +897,7 @@ bool VM::callCommand(DSState &state, const ResolvedCmd &cmd, ObjPtr<ArrayObject>
name = name.substr(pos + 1);
array.refValues()[0] = DSValue::createStr(name); // not check iterator invalidation
}
if (hasFlag(state.runtimeOption, RuntimeOption::XTRACE)) {
if (state.has(RuntimeOption::XTRACE)) {
traceCmd(state, array);
}
switch (cmd.kind()) {
Expand Down Expand Up @@ -1455,17 +1454,17 @@ bool VM::addGlobbingPath(DSState &state, ArrayObject &argv, const DSValue *begin
if (tilde) {
setFlag(option, GlobMatchOption::TILDE);
}
if (hasFlag(state.runtimeOption, RuntimeOption::DOTGLOB)) {
if (state.has(RuntimeOption::DOTGLOB)) {
setFlag(option, GlobMatchOption::DOTGLOB);
}
if (hasFlag(state.runtimeOption, RuntimeOption::FASTGLOB)) {
if (state.has(RuntimeOption::FASTGLOB)) {
setFlag(option, GlobMatchOption::FASTGLOB);
}
auto matcher = createGlobMatcher<DSValueGlobMeta>(
nullptr, GlobIter(begin), GlobIter(end), [] { return DSState::isInterrupted(); }, option);
DefaultDirStackProvider provider(state);
TildeExpandStatus tildeExpandStatus{};
const bool failTilde = hasFlag(state.runtimeOption, RuntimeOption::FAIL_TILDE);
const bool failTilde = state.has(RuntimeOption::FAIL_TILDE);
auto expander = [&provider, &tildeExpandStatus, failTilde](std::string &path) {
tildeExpandStatus = expandTilde(path, true, &provider);
return tildeExpandStatus == TildeExpandStatus::OK || !failTilde;
Expand All @@ -1474,7 +1473,7 @@ bool VM::addGlobbingPath(DSState &state, ArrayObject &argv, const DSValue *begin
switch (ret) {
case GlobMatchResult::MATCH:
case GlobMatchResult::NOMATCH:
if (ret == GlobMatchResult::MATCH || hasFlag(state.runtimeOption, RuntimeOption::NULLGLOB)) {
if (ret == GlobMatchResult::MATCH || state.has(RuntimeOption::NULLGLOB)) {
argv.sortAsStrArray(oldSize); // not check iterator invalidation
return true;
} else {
Expand Down Expand Up @@ -1655,8 +1654,7 @@ bool VM::applyBraceExpansion(DSState &state, ArrayObject &argv, const DSValue *b
DefaultDirStackProvider dirStackProvider(state);
std::string str = path.asStrRef().toString();
if (auto s = expandTilde(str, true, &dirStackProvider);
s != TildeExpandStatus::OK &&
hasFlag(state.runtimeOption, RuntimeOption::FAIL_TILDE)) {
s != TildeExpandStatus::OK && state.has(RuntimeOption::FAIL_TILDE)) {
raiseTildeError(state, dirStackProvider, str, s);
return false;
}
Expand Down Expand Up @@ -1806,7 +1804,7 @@ bool VM::mainLoop(DSState &state) {
vmcase(HALT) { return true; }
vmcase(ASSERT_ENABLED) {
unsigned short offset = read16(state.stack.ip());
if (hasFlag(state.runtimeOption, RuntimeOption::ASSERT)) {
if (state.has(RuntimeOption::ASSERT)) {
state.stack.ip() += 2;
} else {
state.stack.ip() += offset - 1;
Expand Down Expand Up @@ -2329,7 +2327,7 @@ bool VM::mainLoop(DSState &state) {
std::string str = state.stack.pop().asStrRef().toString();
DefaultDirStackProvider dirStackProvider(state);
if (auto s = expandTilde(str, true, &dirStackProvider);
s != TildeExpandStatus::OK && hasFlag(state.runtimeOption, RuntimeOption::FAIL_TILDE)) {
s != TildeExpandStatus::OK && state.has(RuntimeOption::FAIL_TILDE)) {
raiseTildeError(state, dirStackProvider, str, s);
vmerror;
}
Expand Down Expand Up @@ -2841,7 +2839,7 @@ DSErrorKind VM::handleUncaughtException(DSState &state, DSError *dsError) {

// print error message
if (kind == DS_ERROR_KIND_RUNTIME_ERROR || kind == DS_ERROR_KIND_ASSERTION_ERROR ||
hasFlag(state.runtimeOption, RuntimeOption::TRACE_EXIT)) {
state.has(RuntimeOption::TRACE_EXIT)) {
typeAs<ErrorObject>(except).printStackTrace(state, ErrorObject::PrintOp::UNCAUGHT);
}

Expand Down
18 changes: 12 additions & 6 deletions src/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ struct DSState {

const timestamp initTime; // for builtin printf command

private:
RuntimeOption runtimeOption{RuntimeOption::HUP_EXIT | RuntimeOption::ASSERT |
RuntimeOption::CLOBBER | RuntimeOption::FAIL_TILDE};

public:
const bool support_strftime_plus; // if support strftime '%+' specifier

bool isInteractive{false};

RuntimeOption runtimeOption{RuntimeOption::HUP_EXIT | RuntimeOption::ASSERT |
RuntimeOption::CLOBBER | RuntimeOption::FAIL_TILDE};

DSExecMode execMode{DS_EXEC_MODE_NORMAL};

struct DumpTarget {
Expand Down Expand Up @@ -216,9 +218,7 @@ struct DSState {

bool isRootShell() const { return this->subshellLevel == 0; }

bool isJobControl() const {
return this->isRootShell() && hasFlag(this->runtimeOption, RuntimeOption::MONITOR);
}
bool isJobControl() const { return this->isRootShell() && this->has(RuntimeOption::MONITOR); }

/**
*
Expand Down Expand Up @@ -247,6 +247,12 @@ struct DSState {
}

L64X128MixRNG &getRng() { return this->rng; }

bool has(RuntimeOption option) const { return hasFlag(this->runtimeOption, option); }

RuntimeOption getOption() const { return this->runtimeOption; }

void setOption(RuntimeOption option) { this->runtimeOption = option; }
};

namespace ydsh {
Expand Down
24 changes: 14 additions & 10 deletions src/ydsh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ unsigned int DSState_option(const DSState *st) {
}

// get runtime option
if (hasFlag(st->runtimeOption, RuntimeOption::ASSERT)) {
if (st->has(RuntimeOption::ASSERT)) {
setFlag(option, DS_OPTION_ASSERT);
}
if (hasFlag(st->runtimeOption, RuntimeOption::TRACE_EXIT)) {
if (st->has(RuntimeOption::TRACE_EXIT)) {
setFlag(option, DS_OPTION_TRACE_EXIT);
}
if (hasFlag(st->runtimeOption, RuntimeOption::MONITOR)) {
if (st->has(RuntimeOption::MONITOR)) {
setFlag(option, DS_OPTION_JOB_CONTROL);
}
return option;
Expand All @@ -296,19 +296,21 @@ void DSState_setOption(DSState *st, unsigned int optionSet) {
}

// set runtime option
auto option = st->getOption();
if (hasFlag(optionSet, DS_OPTION_ASSERT)) {
setFlag(st->runtimeOption, RuntimeOption::ASSERT);
setFlag(option, RuntimeOption::ASSERT);
}
if (hasFlag(optionSet, DS_OPTION_TRACE_EXIT)) {
setFlag(st->runtimeOption, RuntimeOption::TRACE_EXIT);
setFlag(option, RuntimeOption::TRACE_EXIT);
}
if (hasFlag(optionSet, DS_OPTION_JOB_CONTROL)) {
setFlag(st->runtimeOption, RuntimeOption::MONITOR);
setFlag(option, RuntimeOption::MONITOR);
setJobControlSignalSetting(*st, true);
}
if (hasFlag(optionSet, DS_OPTION_XTRACE)) {
setFlag(st->runtimeOption, RuntimeOption::XTRACE);
setFlag(option, RuntimeOption::XTRACE);
}
st->setOption(option);
}

void DSState_unsetOption(DSState *st, unsigned int optionSet) {
Expand All @@ -321,16 +323,18 @@ void DSState_unsetOption(DSState *st, unsigned int optionSet) {
}

// unset runtime option
auto option = st->getOption();
if (hasFlag(optionSet, DS_OPTION_ASSERT)) {
unsetFlag(st->runtimeOption, RuntimeOption::ASSERT);
unsetFlag(option, RuntimeOption::ASSERT);
}
if (hasFlag(optionSet, DS_OPTION_TRACE_EXIT)) {
unsetFlag(st->runtimeOption, RuntimeOption::TRACE_EXIT);
unsetFlag(option, RuntimeOption::TRACE_EXIT);
}
if (hasFlag(optionSet, DS_OPTION_JOB_CONTROL)) {
unsetFlag(st->runtimeOption, RuntimeOption::MONITOR);
unsetFlag(option, RuntimeOption::MONITOR);
setJobControlSignalSetting(*st, false);
}
st->setOption(option);
}

void DSError_release(DSError *e) {
Expand Down

0 comments on commit e89169a

Please sign in to comment.