编写伪装为TTY的程序 [英] Write program that pretends to be a TTY
问题描述
我正在编写一个程序,该程序从stdin读取输入,操纵输入,并将输出写入stdout.但是,许多程序检查stdin是终端还是管道(通过调用
I'm writing a program that reads input from stdin, manipulates the input, and writes output to stdout. However, many programs check whether stdin is a terminal or a pipe (by calling a function like isatty
), and generate output differently. How do I have my program pretend to be a TTY?
该解决方案在Linux和macOS上均应适用.任何生成独立二进制文件的编程语言都是可以接受的,但是Go是首选.
The solution should work on both Linux and macOS. Any programming language that generates a standalone binary is acceptable, but Go is preferred.
请注意,我是在询问编程问题,而不是在询问工具.因此,script
或unbuffer
之类的东西不是我想要的.
Note that I'm asking a programming question, not asking for a tool. So, things like script
or unbuffer
is not something I'm looking for.
推荐答案
以下是完全有效的代码,用于在pty中运行命令并捕获其输出. (不如您想的那么多.)
The following is fully working code for running a command in a pty and capturing its output. (Not as many lines as you may have thought.)
#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的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!