瀏覽代碼

Shell: Do not assume that stdin/stdout is a TTY

This closes #2989.
AnotherTest 5 年之前
父節點
當前提交
7b15c85ff5
共有 3 個文件被更改,包括 15 次插入5 次删除
  1. 2 1
      Shell/Builtin.cpp
  2. 12 4
      Shell/Shell.cpp
  3. 1 0
      Shell/Shell.h

+ 2 - 1
Shell/Builtin.cpp

@@ -267,7 +267,8 @@ int Shell::builtin_exit(int argc, const char** argv)
     }
     }
     stop_all_jobs();
     stop_all_jobs();
     save_history();
     save_history();
-    printf("Good-bye!\n");
+    if (m_is_interactive)
+        printf("Good-bye!\n");
     exit(exit_code);
     exit(exit_code);
     return 0;
     return 0;
 }
 }

+ 12 - 4
Shell/Shell.cpp

@@ -58,7 +58,7 @@ extern char** environ;
 
 
 void Shell::print_path(const String& path)
 void Shell::print_path(const String& path)
 {
 {
-    if (s_disable_hyperlinks) {
+    if (s_disable_hyperlinks || !m_is_interactive) {
         printf("%s", path.characters());
         printf("%s", path.characters());
         return;
         return;
     }
     }
@@ -1092,9 +1092,17 @@ Shell::Shell()
     int rc = gethostname(hostname, Shell::HostNameSize);
     int rc = gethostname(hostname, Shell::HostNameSize);
     if (rc < 0)
     if (rc < 0)
         perror("gethostname");
         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);
         auto* cwd = getcwd(nullptr, 0);

+ 1 - 0
Shell/Shell.h

@@ -218,6 +218,7 @@ private:
     Vector<LocalFrame> m_local_frames;
     Vector<LocalFrame> m_local_frames;
 
 
     HashMap<String, String> m_aliases;
     HashMap<String, String> m_aliases;
+    bool m_is_interactive { true };
 };
 };
 
 
 static constexpr bool is_word_character(char c)
 static constexpr bool is_word_character(char c)