From: Shuanglei Tao <tsl0922@gmail.com>
Date: Sat, 1 Sep 2018 13:02:43 +0000 (+0800)
Subject: server: custom terminal type support
X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=aac89aa61781465288b3bf78358db50ae1de792c;p=ttyd.git

server: custom terminal type support
---

diff --git a/README.md b/README.md
index 8ea31f3..55ff85a 100644
--- a/README.md
+++ b/README.md
@@ -89,6 +89,7 @@ OPTIONS:
     -r, --reconnect         Time to reconnect for the client in seconds (default: 10)
     -R, --readonly          Do not allow clients to write to the TTY
     -t, --client-option     Send option to client (format: key=value), repeat to add more options
+    -T, --terminal-type     Terminal type to report, default: xterm-256color
     -O, --check-origin      Do not allow websocket connection from different origin
     -m, --max-clients       Maximum clients to support (default: 0, no limit)
     -o, --once              Accept only one client and exit on disconnection
diff --git a/man/ttyd.1 b/man/ttyd.1
index a937abc..46e4efb 100644
--- a/man/ttyd.1
+++ b/man/ttyd.1
@@ -66,6 +66,10 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows
 \-t, \-\-client\-option <key=value>
       Send option to client (format: key=value), repeat to add more options
 
+.PP
+\-T, \-\-terminal\-type
+      Terminal type to report, default: xterm\-256color
+
 .PP
 \-O, \-\-check\-origin
       Do not allow websocket connection from different origin
diff --git a/man/ttyd.man.md b/man/ttyd.man.md
index 6b45168..41d48a1 100644
--- a/man/ttyd.man.md
+++ b/man/ttyd.man.md
@@ -46,6 +46,9 @@ ttyd 1 "September 2016" ttyd "User Manual"
   -t, --client-option <key=value>
       Send option to client (format: key=value), repeat to add more options
 
+  -T, --terminal-type
+      Terminal type to report, default: xterm-256color
+
   -O, --check-origin
       Do not allow websocket connection from different origin
 
diff --git a/src/protocol.c b/src/protocol.c
index 6862b4e..c1a6668 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -171,7 +171,7 @@ thread_run_command(void *args) {
             lwsl_err("forkpty, error: %d (%s)\n", errno, strerror(errno));
             break;
         case 0: /* child */
-            if (setenv("TERM", "xterm-256color", true) < 0) {
+            if (setenv("TERM", server->terminal_type, true) < 0) {
                 perror("setenv");
                 pthread_exit((void *) 1);
             }
diff --git a/src/server.c b/src/server.c
index 4ec5919..0dfe138 100644
--- a/src/server.c
+++ b/src/server.c
@@ -64,7 +64,7 @@ static const struct option options[] = {
         {"help",         no_argument,       NULL, 'h'},
         {NULL,           0,                 0,     0}
 };
-static const char *opt_string = "p:i:c:u:g:s:r:I:aSC:K:A:Rt:Om:oBd:vh";
+static const char *opt_string = "p:i:c:u:g:s:r:I:aSC:K:A:Rt:T:Om:oBd:vh";
 
 void print_help() {
     fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n"
@@ -82,6 +82,7 @@ void print_help() {
                     "    -r, --reconnect         Time to reconnect for the client in seconds (default: 10)\n"
                     "    -R, --readonly          Do not allow clients to write to the TTY\n"
                     "    -t, --client-option     Send option to client (format: key=value), repeat to add more options\n"
+                    "    -T, --terminal-type     Terminal type to report, default: xterm-256color\n"
                     "    -O, --check-origin      Do not allow websocket connection from different origin\n"
                     "    -m, --max-clients       Maximum clients to support (default: 0, no limit)\n"
                     "    -o, --once              Accept only one client and exit on disconnection\n"
@@ -111,6 +112,7 @@ tty_server_new(int argc, char **argv, int start) {
     ts->client_count = 0;
     ts->reconnect = 10;
     ts->sig_code = SIGHUP;
+    sprintf(ts->terminal_type, "%s", "xterm-256color");
     get_sig_name(ts->sig_code, ts->sig_name, sizeof(ts->sig_name));
     if (start == argc)
         return ts;
@@ -353,6 +355,10 @@ main(int argc, char **argv) {
                 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
                 ca_path[sizeof(ca_path) - 1] = '\0';
                 break;
+            case 'T':
+                strncpy(server->terminal_type, optarg, sizeof(server->terminal_type) - 1);
+                server->terminal_type[sizeof(server->terminal_type) - 1] = '\0';
+                break;
             case '?':
                 break;
             case 't':
@@ -436,8 +442,9 @@ main(int argc, char **argv) {
     if (server->credential != NULL)
         lwsl_notice("  credential: %s\n", server->credential);
     lwsl_notice("  start command: %s\n", server->command);
-    lwsl_notice("  reconnect timeout: %ds\n", server->reconnect);
     lwsl_notice("  close signal: %s (%d)\n", server->sig_name, server->sig_code);
+    lwsl_notice("  terminal type: %s\n", server->terminal_type);
+    lwsl_notice("  reconnect timeout: %ds\n", server->reconnect);
     if (server->check_origin)
         lwsl_notice("  check origin: true\n");
     if (server->readonly)
diff --git a/src/server.h b/src/server.h
index 239f88a..3d262c4 100644
--- a/src/server.h
+++ b/src/server.h
@@ -74,6 +74,7 @@ struct tty_server {
     int max_clients;                          // maximum clients to support
     bool once;                                // whether accept only one client and exit on disconnection
     char socket_path[255];                    // UNIX domain socket path
+    char terminal_type[30];                   // terminal type to report
     pthread_mutex_t mutex;
 };