Skip to content

Commit

Permalink
Shell: Do not assume that stdin/stdout is a TTY
Browse files Browse the repository at this point in the history
This closes SerenityOS#2989.
  • Loading branch information
alimpfard authored and awesomekling committed Aug 5, 2020
1 parent 70eb7aa commit 7b15c85
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Shell/Builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ int Shell::builtin_exit(int argc, const char** argv)
}
stop_all_jobs();
save_history();
printf("Good-bye!\n");
if (m_is_interactive)
printf("Good-bye!\n");
exit(exit_code);
return 0;
}
Expand Down
16 changes: 12 additions & 4 deletions Shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extern char** environ;

void Shell::print_path(const String& path)
{
if (s_disable_hyperlinks) {
if (s_disable_hyperlinks || !m_is_interactive) {
printf("%s", path.characters());
return;
}
Expand Down Expand Up @@ -1092,9 +1092,17 @@ Shell::Shell()
int rc = gethostname(hostname, Shell::HostNameSize);
if (rc < 0)
perror("gethostname");
rc = ttyname_r(0, ttyname, Shell::TTYNameSize);
if (rc < 0)
perror("ttyname_r");

auto istty = isatty(STDIN_FILENO);
m_is_interactive = istty;

if (istty) {
rc = ttyname_r(0, ttyname, Shell::TTYNameSize);
if (rc < 0)
perror("ttyname_r");
} else {
ttyname[0] = 0;
}

{
auto* cwd = getcwd(nullptr, 0);
Expand Down
1 change: 1 addition & 0 deletions Shell/Shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class Shell : public Core::Object {
Vector<LocalFrame> m_local_frames;

HashMap<String, String> m_aliases;
bool m_is_interactive { true };
};

static constexpr bool is_word_character(char c)
Expand Down

0 comments on commit 7b15c85

Please sign in to comment.