Skip to content

Commit e07cf8b

Browse files
committed
fix: 改进命令行参数解析并支持 detached session
- 使用 getopt_long 替代手动 argv 解析,支持短选项和长选项 - 修复 argv[2] 问题,改用 optarg 获取选项参数 - 添加 -n/--new-session 支持后台创建 session - 添加未知命令检查防止程序卡住 - 在 detached 模式下正确发送 MSG_DETACH 消息 - 添加 MSG_ERR_COMMAND 错误消息支持 <noreply@anthropic.com>
1 parent bf36034 commit e07cf8b

4 files changed

Lines changed: 84 additions & 14 deletions

File tree

‎include/i18n.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef enum {
7575
MSG_ERR_OPEN_PTY,
7676
MSG_ERR_EXEC,
7777
MSG_ERR_PROTOCOL_VERSION,
78+
MSG_ERR_COMMAND,
7879

7980
/* 会话管理 */
8081
MSG_SESSION_FORMAT,

‎src/client/client.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ int client_main(struct client *c) {
579579
log_info("connected to server, fd %d", server_fd);
580580
// 保存 server 连接 fd
581581
c->server_fd = server_fd;
582-
582+
extern int new_session_detach;
583583
extern int detached_session_id;
584584
extern int list_sessions;
585585
extern int kill_session_id;
@@ -760,6 +760,10 @@ int client_main(struct client *c) {
760760
c->pane = pane_create(w, c->ws.ws_col, c->ws.ws_row, 0, 0);
761761
pane_set_master_fd(c->pane, c->master_fd);
762762
}
763+
764+
if (new_session_detach == 1) {
765+
send_server(MSG_DETACH, server_fd, NULL, 0);
766+
}
763767
// 终端窗口尺寸更新
764768
struct sigaction sa;
765769
sa.sa_handler = signal_handler;

‎src/common/i18n.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static const char *messages_en[MSG_COUNT] = {
7373
[MSG_ERR_OPEN_PTY] = "open slave pty failed\n",
7474
[MSG_ERR_EXEC] = "Execve failed\n",
7575
[MSG_ERR_PROTOCOL_VERSION] = "protocol version mismatch\n",
76+
[MSG_ERR_COMMAND] = "unknown commad",
7677

7778
/* 会话管理 - 会话列表和操作反馈 */
7879
[MSG_SESSION_FORMAT] = "%d: %s (pid %d)\n",
@@ -124,6 +125,7 @@ static const char *messages_zh[MSG_COUNT] = {
124125
[MSG_ERR_OPEN_PTY] = "打开伪终端失败\n",
125126
[MSG_ERR_EXEC] = "执行程序失败\n",
126127
[MSG_ERR_PROTOCOL_VERSION] = "协议版本错误\n",
128+
[MSG_ERR_COMMAND] = "未知指令",
127129

128130
/* 会话管理 - 会话列表和操作反馈 */
129131
[MSG_SESSION_FORMAT] = "%d: %s (进程号 %d)\n",

‎src/core/main.c‎

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@
4242
#include "version.h"
4343
#include <errno.h>
4444
#include <fcntl.h>
45+
#include <getopt.h>
4546
#include <stdio.h>
4647
#include <stdlib.h>
4748
#include <string.h>
49+
#include <sys/_types/_pid_t.h>
4850
#include <sys/select.h>
4951
#include <sys/stat.h>
5052
#include <sys/types.h>
@@ -55,6 +57,7 @@ char *socket_path;
5557
int detached_session_id = -1;
5658
int list_sessions = 0;
5759
int kill_session_id = -1;
60+
int new_session_detach = -1;
5861

5962
static void print_help(const char *prog) {
6063
printf("%s", TR(MSG_HELP_TITLE));
@@ -80,23 +83,83 @@ static void print_help(const char *prog) {
8083

8184
int main(int argc, char *argv[]) {
8285
i18n_init();
86+
int opt;
87+
int option_index = 0;
88+
static struct option long_options[] = {
89+
{"h", no_argument, 0, 'h'},
90+
{"help", no_argument, 0, 'h'},
8391

84-
if (argc == 2 &&
85-
(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
86-
print_help(argv[0]);
87-
return 0;
88-
}
89-
if (argc == 2 && (strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "-L") == 0)) {
90-
list_sessions = 1;
92+
{"l", no_argument, 0, 'l'},
93+
{"s", required_argument, 0, 's'},
94+
{"k", required_argument, 0, 'k'},
95+
{"send_keys", required_argument, 0, '_'},
96+
{"new-session", no_argument, 0, 'n'},
97+
{"n", no_argument, 0, 'n'},
98+
{"list-panes", required_argument, 0, 'p'},
99+
{0, 0, 0, 0}};
100+
101+
while ((opt = getopt_long(argc, argv, "hls:k:_:np:", long_options,
102+
&option_index)) != -1) {
103+
switch (opt) {
104+
case 'h':
105+
print_help(argv[0]);
106+
return 0;
107+
case 'l':
108+
list_sessions = 1;
109+
break;
110+
case 's':
111+
detached_session_id = strtol(optarg, NULL, 10);
112+
log_info("attaching to session id=%d\n", detached_session_id);
113+
break;
114+
case 'k':
115+
kill_session_id = strtol(optarg, NULL, 10);
116+
log_info("killing session id=%d\n", kill_session_id);
117+
break;
118+
case '_':
119+
// TODO
120+
break;
121+
case 'p':
122+
// TODO
123+
break;
124+
case 'n':
125+
new_session_detach = 1;
126+
break;
127+
case '?':
128+
printf("%s", TR(MSG_ERR_COMMAND));
129+
return -1;
130+
default:
131+
break;
132+
}
91133
}
92-
if (argc == 3 && (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "-S") == 0)) {
93-
detached_session_id = strtol(argv[2], NULL, 10);
94-
log_info("attaching to session id=%d\n", detached_session_id);
134+
135+
// 有效解析位置
136+
if (optind < argc) {
137+
printf("%s", TR(MSG_ERR_COMMAND));
138+
return -1;
95139
}
96-
if (argc == 3 && (strcmp(argv[1], "-k") == 0 || strcmp(argv[1], "-K") == 0)) {
97-
kill_session_id = strtol(argv[2], NULL, 10);
98-
log_info("killing session id=%d\n", kill_session_id);
140+
141+
if (new_session_detach == 1) {
142+
pid_t pid = fork();
143+
if (pid < 0) {
144+
perror("fork");
145+
return -1;
146+
}
147+
if (pid > 0) {
148+
return 0; // 父进程退出
149+
}
150+
// 子进程
151+
if (setsid() < 0) {
152+
perror("setsid");
153+
return -1;
154+
}
155+
close(STDIN_FILENO);
156+
close(STDOUT_FILENO);
157+
close(STDERR_FILENO);
158+
open("/dev/null", O_RDONLY);
159+
open("/dev/null", O_WRONLY);
160+
open("/dev/null", O_WRONLY);
99161
}
162+
100163
uid_t uid = getuid();
101164

102165
// 初始化程序目录

0 commit comments

Comments
 (0)