Skip to content

Commit 3d91e27

Browse files
committed
Bump to 1.0.0
1 parent 9174d61 commit 3d91e27

File tree

1 file changed

+61
-35
lines changed

1 file changed

+61
-35
lines changed

src/server.c

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "server.h"
22

3+
#define TTYD_VERSION "1.0.0"
4+
35
volatile bool force_exit = false;
46
struct lws_context *context;
57
struct tty_server *server;
@@ -32,58 +34,70 @@ static const struct option options[] = {
3234
{"ssl-key", required_argument, NULL, 'K'},
3335
{"ssl-ca", required_argument, NULL, 'A'},
3436
{"debug", required_argument, NULL, 'd'},
37+
{"version", no_argument, NULL, 'v'},
3538
{"help", no_argument, NULL, 'h'},
3639
{NULL, 0, 0, 0}
3740
};
3841
static const char *opt_string = "p:i:c:u:g:s:r:aSC:K:A:d:vh";
3942

4043
void print_help() {
4144
fprintf(stderr, "ttyd is a tool for sharing terminal over the web\n\n"
42-
"USAGE: ttyd [options] <command> [<arguments...>]\n\n"
43-
"OPTIONS:\n"
44-
"\t--port, -p Port to listen (default: 7681)\n"
45-
"\t--interface, -i Network interface to bind\n"
46-
"\t--credential, -c Credential for Basic Authentication (format: username:password)\n"
47-
"\t--uid, -u User id to run with\n"
48-
"\t--gid, -g Group id to run with\n"
49-
"\t--signal, -s Signal to send to the command when exit it (default: SIGHUP)\n"
50-
"\t--reconnect, -r Time to reconnect for the client in seconds (default: 10)\n"
51-
"\t--ssl, -S Enable ssl\n"
52-
"\t--ssl-cert, -C Ssl certificate file path\n"
53-
"\t--ssl-key, -K Ssl key file path\n"
54-
"\t--ssl-ca, -A Ssl ca file path\n"
55-
"\t--debug, -d Set log level (0-9, default: 7)\n"
56-
"\t--help, -h Print this text and exit\n"
45+
"USAGE:\n"
46+
" ttyd [options] <command> [<arguments...>]\n\n"
47+
"VERSION:\n"
48+
" %s\n\n"
49+
"OPTIONS:\n"
50+
" --port, -p Port to listen (default: 7681)\n"
51+
" --interface, -i Network interface to bind\n"
52+
" --credential, -c Credential for Basic Authentication (format: username:password)\n"
53+
" --uid, -u User id to run with\n"
54+
" --gid, -g Group id to run with\n"
55+
" --signal, -s Signal to send to the command when exit it (default: SIGHUP)\n"
56+
" --reconnect, -r Time to reconnect for the client in seconds (default: 10)\n"
57+
" --ssl, -S Enable ssl\n"
58+
" --ssl-cert, -C Ssl certificate file path\n"
59+
" --ssl-key, -K Ssl key file path\n"
60+
" --ssl-ca, -A Ssl ca file path\n"
61+
" --debug, -d Set log level (0-9, default: 7)\n"
62+
" --version, -v Print the version and exit\n"
63+
" --help, -h Print this text and exit\n",
64+
TTYD_VERSION
5765
);
5866
}
5967

6068
struct tty_server *
61-
tty_server_new(int argc, char **argv) {
69+
tty_server_new(int argc, char **argv, int start) {
6270
struct tty_server *ts;
6371
size_t cmd_len = 0;
6472

6573
ts = t_malloc(sizeof(struct tty_server));
74+
75+
memset(ts, 0, sizeof(struct tty_server));
6676
LIST_INIT(&ts->clients);
6777
ts->client_count = 0;
68-
ts->credential = NULL;
6978
ts->reconnect = 10;
7079
ts->sig_code = SIGHUP;
7180
ts->sig_name = strdup("SIGHUP");
72-
ts->argv = t_malloc(sizeof(char *) * (argc + 1));
73-
for (int i = 0; i < argc; i++) {
74-
ts->argv[i] = strdup(argv[i]);
81+
if (start == argc)
82+
return ts;
83+
84+
int cmd_argc = argc - start;
85+
char **cmd_argv = &argv[start];
86+
ts->argv = t_malloc(sizeof(char *) * (cmd_argc + 1));
87+
for (int i = 0; i < cmd_argc; i++) {
88+
ts->argv[i] = strdup(cmd_argv[i]);
7589
cmd_len += strlen(ts->argv[i]);
76-
if (i != argc - 1) {
90+
if (i != cmd_argc - 1) {
7791
cmd_len++; // for space
7892
}
7993
}
80-
ts->argv[argc] = NULL;
94+
ts->argv[cmd_argc] = NULL;
8195

8296
ts->command = t_malloc(cmd_len);
8397
char *ptr = ts->command;
84-
for (int i = 0; i < argc; i++) {
98+
for (int i = 0; i < cmd_argc; i++) {
8599
ptr = stpcpy(ptr, ts->argv[i]);
86-
if (i != argc - 1) {
100+
if (i != cmd_argc - 1) {
87101
sprintf(ptr++, "%c", ' ');
88102
}
89103
}
@@ -118,7 +132,7 @@ calc_command_start(int argc, char **argv) {
118132
while (getopt_long(argc_copy, argv_copy, opt_string, options, NULL) != -1)
119133
;
120134

121-
int start = -1;
135+
int start = argc;
122136
if (optind < argc) {
123137
char *command = argv_copy[optind];
124138
for (int i = 0; i < argc; i++) {
@@ -144,17 +158,13 @@ calc_command_start(int argc, char **argv) {
144158

145159
int
146160
main(int argc, char **argv) {
147-
if (argc == 1 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
161+
if (argc == 1) {
148162
print_help();
149-
exit(0);
163+
return 0;
150164
}
151-
// parse command line
165+
152166
int start = calc_command_start(argc, argv);
153-
if (start < 0) {
154-
fprintf(stderr, "ttyd: missing start command\n");
155-
exit(1);
156-
}
157-
server = tty_server_new(argc - start, &argv[start]);
167+
server = tty_server_new(argc, argv, start);
158168

159169
struct lws_context_creation_info info;
160170
memset(&info, 0, sizeof(info));
@@ -184,11 +194,18 @@ main(int argc, char **argv) {
184194
case 'h':
185195
print_help();
186196
return 0;
197+
case 'v':
198+
printf("ttyd version %s\n", TTYD_VERSION);
199+
return 0;
187200
case 'd':
188201
debug_level = atoi(optarg);
189202
break;
190203
case 'p':
191204
info.port = atoi(optarg);
205+
if (info.port < 0) {
206+
fprintf(stderr, "ttyd: invalid port: %s\n", optarg);
207+
return -1;
208+
}
192209
break;
193210
case 'i':
194211
strncpy(iface, optarg, sizeof(iface));
@@ -220,6 +237,10 @@ main(int argc, char **argv) {
220237
break;
221238
case 'r':
222239
server->reconnect = atoi(optarg);
240+
if (server->reconnect <= 0) {
241+
fprintf(stderr, "ttyd: invalid reconnect: %s\n", optarg);
242+
return -1;
243+
}
223244
break;
224245
case 'S':
225246
ssl = true;
@@ -240,10 +261,15 @@ main(int argc, char **argv) {
240261
break;
241262
default:
242263
print_help();
243-
exit(1);
264+
return -1;
244265
}
245266
}
246267

268+
if (server->command == NULL || strlen(server->command) == 0) {
269+
fprintf(stderr, "ttyd: missing start command\n");
270+
return -1;
271+
}
272+
247273
lws_set_log_level(debug_level, NULL);
248274

249275
if (strlen(iface) > 0)
@@ -276,7 +302,7 @@ main(int argc, char **argv) {
276302
context = lws_create_context(&info);
277303
if (context == NULL) {
278304
lwsl_err("libwebsockets init failed\n");
279-
return -1;
305+
return 1;
280306
}
281307

282308
lwsl_notice("TTY configuration:\n");

0 commit comments

Comments
 (0)