feat: 发送terminal相关信息

This commit is contained in:
Pan Qiancheng 2025-12-10 22:40:51 +08:00
parent 5418ce4b38
commit 98c6bc299a
1 changed files with 51 additions and 0 deletions

View File

@ -7,6 +7,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include "debug.h" #include "debug.h"
@ -133,6 +135,55 @@ int seeking_solutions(const char* filename, char* const argv[],
write(sock, &path_len, sizeof(size_t)); write(sock, &path_len, sizeof(size_t));
write(sock, abs_path, path_len); write(sock, abs_path, path_len);
// 发送终端信息
// 1. 检查是否为交互式终端
int is_tty = isatty(STDIN_FILENO);
write(sock, &is_tty, sizeof(int));
// 2. 如果是终端,获取窗口大小
struct winsize ws;
memset(&ws, 0, sizeof(ws));
if (is_tty) {
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
}
write(sock, &ws.ws_row, sizeof(unsigned short)); // 行数
write(sock, &ws.ws_col, sizeof(unsigned short)); // 列数
write(sock, &ws.ws_xpixel, sizeof(unsigned short)); // X像素
write(sock, &ws.ws_ypixel, sizeof(unsigned short)); // Y像素
// 3. 获取终端类型
const char* term_type = getenv("TERM");
if (term_type == NULL) {
term_type = "";
}
size_t term_type_len = strlen(term_type);
write(sock, &term_type_len, sizeof(size_t));
write(sock, term_type, term_type_len);
// 4. 获取SHELL类型
const char* shell_type = getenv("SHELL");
if (shell_type == NULL) {
shell_type = "";
}
size_t shell_type_len = strlen(shell_type);
write(sock, &shell_type_len, sizeof(size_t));
write(sock, shell_type, shell_type_len);
// 5. 获取终端属性如果是tty
struct termios term_attr;
int has_termios = 0;
if (is_tty && tcgetattr(STDIN_FILENO, &term_attr) == 0) {
has_termios = 1;
}
write(sock, &has_termios, sizeof(int));
if (has_termios) {
// 发送关键的termios标志
write(sock, &term_attr.c_iflag, sizeof(tcflag_t));
write(sock, &term_attr.c_oflag, sizeof(tcflag_t));
write(sock, &term_attr.c_cflag, sizeof(tcflag_t));
write(sock, &term_attr.c_lflag, sizeof(tcflag_t));
}
// 接收服务器响应 // 接收服务器响应
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
char display_buffer[BUFFER_SIZE]; char display_buffer[BUFFER_SIZE];