Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 编写假装是TTY的程序_C_Linux_Macos_Go_Tty - Fatal编程技术网

C 编写假装是TTY的程序

C 编写假装是TTY的程序,c,linux,macos,go,tty,C,Linux,Macos,Go,Tty,我正在编写一个程序,从标准输入读取输入,处理输入,并将输出写入标准输出。然而,许多程序通过调用类似的函数来检查stdin是终端还是管道,并以不同的方式生成输出。如何让我的程序假装是TTY 该解决方案应该可以在Linux和macOS上运行。任何生成独立二进制文件的编程语言都是可以接受的,但Go是首选 请注意,我问的是编程问题,而不是工具。因此,脚本或unbuffer之类的东西不是我要找的。下面是在pty中运行命令并捕获其输出的完整工作代码。没有你想象的那么多 #include <signal

我正在编写一个程序,从标准输入读取输入,处理输入,并将输出写入标准输出。然而,许多程序通过调用类似的函数来检查stdin是终端还是管道,并以不同的方式生成输出。如何让我的程序假装是TTY

该解决方案应该可以在Linux和macOS上运行。任何生成独立二进制文件的编程语言都是可以接受的,但Go是首选


请注意,我问的是编程问题,而不是工具。因此,脚本或unbuffer之类的东西不是我要找的。

下面是在pty中运行命令并捕获其输出的完整工作代码。没有你想象的那么多

#include <signal.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include <util.h>

pid_t child = 0;

void sighandler(int signum) {
  if (child > 0) {
    killpg(child, signum);
    exit(signum);
  }
}

// Run a command in a pty.
// Usage: /path/to/this/binary command to run
int main(int argc, char *argv[]) {
  if (argc < 2) {
    return EX_USAGE;
  }

  int master;
  child = forkpty(&master, NULL, NULL, NULL);

  if (child == -1) {
    perror("failed to fork pty");
    return EX_OSERR;
  }

  if (child == 0) {
    // we're in the child process, so replace it with the command
    execvp(argv[1], argv + 1);
    perror("failed to execute command");
    return EX_OSERR;
  }

  // trap kill signals and forward them to child process
  signal(SIGHUP, sighandler);
  signal(SIGINT, sighandler);
  signal(SIGTERM, sighandler);

  const int buf_size = 1024;
  char buf[buf_size];
  fd_set fds;
  ssize_t bytes_read;

  // forward the output continuously
  while (1) {
    FD_ZERO(&fds);
    FD_SET(master, &fds);

    if (select(master + 1, &fds, NULL, NULL, NULL) > 0 && FD_ISSET(master, &fds)) {
      bytes_read = read(master, buf, buf_size);
      if (bytes_read <= 0) {
        return EXIT_SUCCESS;
      }

      if (write(STDOUT_FILENO, buf, bytes_read) != bytes_read) {
        perror("failed to write to stdout");
        return EX_OSERR;
      }
    }
  }
}

可能更相关。您真正想要搜索的术语是伪tty或pty。我知道,但如果我错了,请纠正我。Unix Stacke Exchange适用于Unix工具,而不是用于编程问题。其目的是保留从任何程序读取的颜色代码。对于U&L SE,这是正确的,但公认答案中的第一点将为您提供足够的提示,以帮助您继续操作。可能是这样:?