From 3b4e6bb9ccadab3cb521c72179dbd9754c8af9f7 Mon Sep 17 00:00:00 2001 From: Nicolas Viennot Date: Mon, 10 Jun 2013 22:42:16 -0400 Subject: [PATCH] Quick refactor of tmate-slave.c --- Makefile.am | 1 + tmate-slave.c | 75 +------------------------------------------------ tmate.h | 4 +++ tmux-bare.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 74 deletions(-) create mode 100644 tmux-bare.c diff --git a/Makefile.am b/Makefile.am index ae66b225..f3ade8f5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -181,6 +181,7 @@ dist_tmate_slave_SOURCES = \ tmate-ssh-client-pty.c \ tmate-ssh-client.c \ tmate-ssh-server.c \ + tmux-bare.c \ tty-acs.c \ tty-keys.c \ tty-term.c \ diff --git a/tmate-slave.c b/tmate-slave.c index 1a87e5d9..143d02d4 100644 --- a/tmate-slave.c +++ b/tmate-slave.c @@ -1,58 +1,8 @@ #include #include -#include #include #include "tmate.h" -struct event_base *ev_base; - -struct options global_options; /* server options */ -struct options global_s_options; /* session options */ -struct options global_w_options; /* window options */ -struct environ global_environ; - -struct event_base *ev_base; - -char *cfg_file; -char *shell_cmd; -int debug_level; -time_t start_time; -char socket_path[MAXPATHLEN]; -int login_shell; -char *environ_path; -pid_t environ_pid = -1; -int environ_session_id = -1; - -void -setblocking(int fd, int state) -{ - int mode; - - if ((mode = fcntl(fd, F_GETFL)) != -1) { - if (!state) - mode |= O_NONBLOCK; - else - mode &= ~O_NONBLOCK; - fcntl(fd, F_SETFL, mode); - } -} - -const char* -get_full_path(const char *wd, const char *path) -{ - static char newpath[MAXPATHLEN]; - char oldpath[MAXPATHLEN]; - - if (getcwd(oldpath, sizeof oldpath) == NULL) - return (NULL); - if (chdir(wd) != 0) - return (NULL); - if (realpath(path, newpath) != 0) - return (NULL); - chdir(oldpath); - return (newpath); -} - static void usage(void) { fprintf(stderr, "usage: tmate-slave [-p PORT]\n"); @@ -92,8 +42,6 @@ struct tmate_encoder *tmate_encoder; void tmate_spawn_slave_server(struct tmate_ssh_client *client) { - int quiet = 0; - int flags = IDENTIFY_UTF8 | IDENTIFY_256COLOURS; struct tmate_encoder encoder; struct tmate_decoder decoder; @@ -107,25 +55,7 @@ void tmate_spawn_slave_server(struct tmate_ssh_client *client) tmate_ssh_client_init(client, &encoder, &decoder); - environ_init(&global_environ); - - options_init(&global_options, NULL); - options_table_populate_tree(server_options_table, &global_options); - options_set_number(&global_options, "quiet", quiet); - - options_init(&global_s_options, NULL); - options_table_populate_tree(session_options_table, &global_s_options); - - options_init(&global_w_options, NULL); - options_table_populate_tree(window_options_table, &global_w_options); - - if (flags & IDENTIFY_UTF8) { - options_set_number(&global_s_options, "status-utf8", 1); - options_set_number(&global_s_options, "mouse-utf8", 1); - options_set_number(&global_w_options, "utf8", 1); - } - - server_start(0, NULL); + tmux_server_init(IDENTIFY_UTF8 | IDENTIFY_256COLOURS); /* never reached */ } @@ -148,9 +78,6 @@ void tmate_spawn_slave_client(struct tmate_ssh_client *ssh_client) if (openpty(&client->pty, &slave_pty, NULL, NULL, NULL) < 0) tmate_fatal("Cannot allocate pty"); - /* setsid(); */ - /* ioctl(slave_pty, TIOCSCTTY, NULL); */ - dup2(slave_pty, STDIN_FILENO); dup2(slave_pty, STDOUT_FILENO); stderr = stdout; diff --git a/tmate.h b/tmate.h index cec80cfd..eb4f34f9 100644 --- a/tmate.h +++ b/tmate.h @@ -110,4 +110,8 @@ extern void tmate_spawn_slave_client(struct tmate_ssh_client *client); /* tmate-debug.c */ extern void tmate_print_trace(void); +/* tmux-bare.c */ + +extern void tmux_server_init(int flags); + #endif diff --git a/tmux-bare.c b/tmux-bare.c new file mode 100644 index 00000000..3ed18dc8 --- /dev/null +++ b/tmux-bare.c @@ -0,0 +1,78 @@ +#include +#include +#include + +#include "tmux.h" +#include "tmate.h" + +struct event_base *ev_base; + +struct options global_options; /* server options */ +struct options global_s_options; /* session options */ +struct options global_w_options; /* window options */ +struct environ global_environ; + +char *cfg_file; +char *shell_cmd; +int debug_level; +time_t start_time; +char socket_path[MAXPATHLEN]; +int login_shell; +char *environ_path; +pid_t environ_pid = -1; +int environ_session_id = -1; + +void +setblocking(int fd, int state) +{ + int mode; + + if ((mode = fcntl(fd, F_GETFL)) != -1) { + if (!state) + mode |= O_NONBLOCK; + else + mode &= ~O_NONBLOCK; + fcntl(fd, F_SETFL, mode); + } +} + +const char* +get_full_path(const char *wd, const char *path) +{ + static char newpath[MAXPATHLEN]; + char oldpath[MAXPATHLEN]; + + if (getcwd(oldpath, sizeof oldpath) == NULL) + return (NULL); + if (chdir(wd) != 0) + return (NULL); + if (realpath(path, newpath) != 0) + return (NULL); + chdir(oldpath); + return (newpath); +} + +void tmux_server_init(int flags) +{ + int quiet = 0; + + environ_init(&global_environ); + + options_init(&global_options, NULL); + options_table_populate_tree(server_options_table, &global_options); + options_set_number(&global_options, "quiet", quiet); + + options_init(&global_s_options, NULL); + options_table_populate_tree(session_options_table, &global_s_options); + + options_init(&global_w_options, NULL); + options_table_populate_tree(window_options_table, &global_w_options); + + if (flags & IDENTIFY_UTF8) { + options_set_number(&global_s_options, "status-utf8", 1); + options_set_number(&global_s_options, "mouse-utf8", 1); + options_set_number(&global_w_options, "utf8", 1); + } + + server_start(0, NULL); +}