-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathatch.c
More file actions
943 lines (867 loc) · 23 KB
/
atch.c
File metadata and controls
943 lines (867 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
#include "atch.h"
/* Env-var name string, computed from progname at startup. */
const char *session_envvar;
/* Build "NAME_SESSION" from the basename of progname.
** Non-alphanumeric characters are replaced with '_'.
** Uses static storage — called once before any fork. */
static void init_envvar_names(void)
{
static char envname[128];
const char *base = strrchr(progname, '/');
const char *p;
char *d;
size_t max;
base = base ? base + 1 : progname;
d = envname;
max = sizeof(envname) - sizeof("_SESSION");
for (p = base; *p && (size_t)(d - envname) < max; p++)
*d++ = (*p >= 'a' && *p <= 'z') ? (char)(*p - 'a' + 'A') :
((*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9')) ?
*p : '_';
strcpy(d, "_SESSION");
session_envvar = envname;
}
/* Returns the basename of the current session socket path. */
const char *session_shortname(void)
{
const char *p = strrchr(sockname, '/');
return p ? p + 1 : sockname;
}
/* Returns the directory where session sockets are stored. */
void get_session_dir(char *buf, size_t size)
{
const char *home = getenv("HOME");
const char *base = strrchr(progname, '/');
struct passwd *pw;
base = base ? base + 1 : progname;
/* If $HOME is unset or empty, try the passwd database. */
if (!home || !*home) {
pw = getpwuid(getuid());
if (pw && pw->pw_dir && *pw->pw_dir)
home = pw->pw_dir;
}
/* Use $HOME only if it is set and not the root directory. */
if (home && *home && strcmp(home, "/") != 0)
snprintf(buf, size, "%s/.cache/%s", home, base);
else
snprintf(buf, size, "/tmp/.%s-%d", base, (int)getuid());
}
/*
** Long-path helper for Unix socket operations: saves cwd, chdirs into the
** directory part of path, calls fn(basename), then restores cwd.
** Used by connect_socket and create_socket to handle paths > sun_path limit.
*/
int socket_with_chdir(char *path, int (*fn)(char *))
{
char *slash = strrchr(path, '/');
int dirfd, s;
if (!slash) {
errno = ENAMETOOLONG;
return -1;
}
dirfd = open(".", O_RDONLY);
if (dirfd < 0)
return -1;
*slash = '\0';
s = chdir(path) >= 0 ? fn(slash + 1) : -1;
*slash = '/';
if (s >= 0 && fchdir(dirfd) < 0) {
close(s);
s = -1;
}
close(dirfd);
return s;
}
/* argv[0] from the program */
char *progname;
/* The name of the passed in socket. */
char *sockname;
/* The character used for detaching. Defaults to '^\' */
int detach_char = '\\' - 64;
/* 1 if we should not interpret the suspend character. */
int no_suspend;
/* The default redraw method. REDRAW_UNSPEC = auto: winch if tty, none otherwise. */
int redraw_method = REDRAW_UNSPEC;
/* Clear method. CLEAR_UNSPEC = auto: move if tty, none otherwise. */
int clear_method = CLEAR_UNSPEC;
int quiet = 0;
/* 1 if we should not send ansi sequences to the terminal */
int no_ansiterm = 0;
/*
** The original terminal settings. Shared between the master and attach
** processes. The master uses it to initialize the pty, and the attacher uses
** it to restore the original settings.
*/
struct termios orig_term;
int dont_have_tty;
/* Parse a size string: bare number, or number with k/K (×1024) or m/M (×1048576).
** Writes result to *out. Returns 0 on success, 1 on error. */
static int parse_size(const char *s, size_t *out)
{
char *end;
unsigned long v;
if (!s || !*s)
return 1;
v = strtoul(s, &end, 10);
if (end == s)
return 1;
if (*end == 'k' || *end == 'K') {
v *= 1024;
end++;
} else if (*end == 'm' || *end == 'M') {
v *= 1024 * 1024;
end++;
}
if (*end != '\0')
return 1;
*out = (size_t)v;
return 0;
}
/*
** Parse option flags from argv/argc. Stops at '--' or a non-option argument.
** Returns 0 on success, 1 on error (message already printed).
*/
static int parse_options(int *argc, char ***argv)
{
while (*argc >= 1 && ***argv == '-') {
char *p;
if (strcmp((*argv)[0], "--") == 0) {
++(*argv);
--(*argc);
break;
}
for (p = (*argv)[0] + 1; *p; ++p) {
if (*p == 'E')
detach_char = -1;
else if (*p == 'z')
no_suspend = 1;
else if (*p == 'q')
quiet = 1;
else if (*p == 't')
no_ansiterm = 1;
else if (*p == 'e') {
++(*argv);
--(*argc);
if (*argc < 1) {
printf("%s: No escape character "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
if ((*argv)[0][0] == '^' && (*argv)[0][1]) {
if ((*argv)[0][1] == '?')
detach_char = '\177';
else
detach_char =
(*argv)[0][1] & 037;
} else
detach_char = (*argv)[0][0];
break;
} else if (*p == 'r') {
++(*argv);
--(*argc);
if (*argc < 1) {
printf("%s: No redraw method "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
if (strcmp((*argv)[0], "none") == 0)
redraw_method = REDRAW_NONE;
else if (strcmp((*argv)[0], "ctrl_l") == 0)
redraw_method = REDRAW_CTRL_L;
else if (strcmp((*argv)[0], "winch") == 0)
redraw_method = REDRAW_WINCH;
else {
printf("%s: Invalid redraw method "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
break;
} else if (*p == 'R') {
++(*argv);
--(*argc);
if (*argc < 1) {
printf("%s: No clear method "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
if (strcmp((*argv)[0], "none") == 0)
clear_method = CLEAR_NONE;
else if (strcmp((*argv)[0], "move") == 0)
clear_method = CLEAR_MOVE;
else {
printf("%s: Invalid clear method "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
break;
} else if (*p == 'C') {
++(*argv);
--(*argc);
if (*argc < 1) {
printf("%s: No log size "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
if (parse_size((*argv)[0], &log_max_size)) {
printf("%s: Invalid log size "
"'%s'.\n", progname, (*argv)[0]);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
break;
} else {
printf("%s: Invalid option '-%c'\n",
progname, *p);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
}
++(*argv);
--(*argc);
}
return 0;
}
/* Expand a bare session name to its full socket path in-place. */
static void expand_sockname(void)
{
char dir[512];
size_t fulllen;
char *full;
char *slash;
if (strchr(sockname, '/') != NULL)
return;
get_session_dir(dir, sizeof(dir));
slash = strrchr(dir, '/');
if (slash) {
*slash = '\0';
mkdir(dir, 0700);
*slash = '/';
}
mkdir(dir, 0700);
fulllen = strlen(dir) + 1 + strlen(sockname);
full = malloc(fulllen + 1);
snprintf(full, fulllen + 1, "%s/%s", dir, sockname);
sockname = full;
}
/* Return argv unchanged if argc > 0; otherwise return a {shell, NULL} argv. */
static char **use_shell_if_no_cmd(int argc, char **argv)
{
static char *shell_argv[2];
const char *shell;
struct passwd *pw;
if (argc > 0)
return argv;
shell = getenv("SHELL");
if (!shell || !*shell) {
pw = getpwuid(getuid());
if (pw && pw->pw_shell && *pw->pw_shell)
shell = pw->pw_shell;
}
if (!shell || !*shell)
shell = "/bin/sh";
shell_argv[0] = (char *)shell;
shell_argv[1] = NULL;
return shell_argv;
}
/* Snapshot terminal settings; sets dont_have_tty if not a tty. */
static void save_term(void)
{
if (tcgetattr(0, &orig_term) < 0) {
memset(&orig_term, 0, sizeof(struct termios));
dont_have_tty = 1;
}
}
/* Print error and return 1 if no tty is available. */
static int require_tty(void)
{
if (dont_have_tty) {
printf("%s: attaching to a session requires a terminal.\n",
progname);
return 1;
}
return 0;
}
/* Consume first arg as session name, expand it, advance argc/argv. */
static int consume_session(int *argc, char ***argv)
{
if (*argc < 1) {
printf("%s: No session was specified.\n", progname);
printf("Try '%s --help' for more information.\n", progname);
return 1;
}
sockname = **argv;
++(*argv);
--(*argc);
expand_sockname();
return 0;
}
/* True if arg matches any of the given names (NULL slots are ignored). */
static int is_cmd(const char *arg, const char *a, const char *b, const char *c)
{
return strcmp(arg, a) == 0 ||
(b && strcmp(arg, b) == 0) || (c && strcmp(arg, c) == 0);
}
/* atch list [-a] */
static int cmd_list(int argc, char **argv)
{
int show_all = 0;
while (argc >= 1 && strcmp(argv[0], "-a") == 0) {
show_all = 1;
argc--;
argv++;
}
return list_main(show_all);
}
/* atch current
** SESSION_ENVVAR holds the colon-separated ancestry chain, outermost first.
** A single (non-nested) session has no colon. */
static int cmd_current(void)
{
const char *chain = getenv(SESSION_ENVVAR);
char *copy, *seg, *colon;
const char *name;
int first;
if (!chain || !*chain)
return 1;
copy = strdup(chain);
if (!copy)
return 1;
first = 1;
seg = copy;
for (;;) {
colon = strchr(seg, ':');
if (colon)
*colon = '\0';
name = strrchr(seg, '/');
if (!first)
printf(" > ");
printf("%s", name ? name + 1 : seg);
first = 0;
if (!colon)
break;
seg = colon + 1;
}
printf("\n");
free(copy);
return 0;
}
/* atch attach <session> — strict attach, fail if missing */
static int cmd_attach(int argc, char **argv)
{
if (parse_options(&argc, &argv))
return 1;
if (consume_session(&argc, &argv))
return 1;
if (parse_options(&argc, &argv))
return 1;
if (argc > 0) {
printf("%s: Invalid number of arguments.\n", progname);
printf("Try '%s --help' for more information.\n", progname);
return 1;
}
save_term();
if (require_tty())
return 1;
return attach_main(0);
}
/* atch new <session> [cmd...] — create session and attach */
static int cmd_new(int argc, char **argv)
{
if (parse_options(&argc, &argv))
return 1;
if (consume_session(&argc, &argv))
return 1;
if (parse_options(&argc, &argv))
return 1;
argv = use_shell_if_no_cmd(argc, argv);
save_term();
if (require_tty())
return 1;
if (master_main(argv, 1, 0) != 0)
return 1;
if (!quiet)
printf("%s: session '%s' created\n", progname,
session_shortname());
return attach_main(0);
}
/* atch start <session> [cmd...] — create detached */
static int cmd_start(int argc, char **argv)
{
if (parse_options(&argc, &argv))
return 1;
if (consume_session(&argc, &argv))
return 1;
if (parse_options(&argc, &argv))
return 1;
argv = use_shell_if_no_cmd(argc, argv);
save_term();
if (master_main(argv, 0, 0) != 0)
return 1;
if (!quiet)
printf("%s: session '%s' started\n", progname,
session_shortname());
return 0;
}
/* atch run <session> [cmd...] — create, master stays in foreground */
static int cmd_run(int argc, char **argv)
{
if (parse_options(&argc, &argv))
return 1;
if (consume_session(&argc, &argv))
return 1;
if (parse_options(&argc, &argv))
return 1;
argv = use_shell_if_no_cmd(argc, argv);
save_term();
return master_main(argv, 0, 1);
}
/* atch push <session> — pipe stdin into session */
static int cmd_push(int argc, char **argv)
{
if (consume_session(&argc, &argv))
return 1;
if (argc > 0) {
printf("%s: Invalid number of arguments.\n", progname);
printf("Try '%s --help' for more information.\n", progname);
return 1;
}
return push_main();
}
/* atch kill <session> — stop session */
static int cmd_kill(int argc, char **argv)
{
int force = 0;
/* Accept -f / --force before or after the session name. */
while (argc >= 1 && (strcmp(argv[0], "-f") == 0 ||
strcmp(argv[0], "--force") == 0)) {
force = 1;
argc--;
argv++;
}
if (consume_session(&argc, &argv))
return 1;
while (argc >= 1 && (strcmp(argv[0], "-f") == 0 ||
strcmp(argv[0], "--force") == 0)) {
force = 1;
argc--;
argv++;
}
if (argc > 0) {
printf("%s: Invalid number of arguments.\n", progname);
printf("Try '%s --help' for more information.\n", progname);
return 1;
}
return kill_main(force);
}
/* atch clear <session> — truncate the on-disk session log */
static int cmd_clear(int argc, char **argv)
{
char log_path[600];
int fd;
if (argc > 0) {
if (consume_session(&argc, &argv))
return 1;
} else {
const char *chain = getenv(SESSION_ENVVAR);
const char *last;
if (!chain || !*chain) {
printf("%s: No session was specified.\n", progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
last = strrchr(chain, ':');
sockname = (char *)(last ? last + 1 : chain);
}
if (argc > 0) {
printf("%s: Invalid number of arguments.\n", progname);
printf("Try '%s --help' for more information.\n", progname);
return 1;
}
snprintf(log_path, sizeof(log_path), "%s.log", sockname);
fd = open(log_path, O_WRONLY | O_TRUNC);
if (fd >= 0) {
close(fd);
if (!quiet)
printf("%s: session '%s' log cleared\n",
progname, session_shortname());
} else if (errno != ENOENT) {
printf("%s: %s: %s\n", progname, log_path, strerror(errno));
return 1;
}
return 0;
}
/* Scan log fd backward and return the byte offset to seek to before
** printing the last nlines lines of output. */
static off_t find_tail_start(int fd, off_t size, int nlines)
{
char buf[BUFSIZE];
int count = 0;
off_t pos = size;
while (pos > 0 && count <= nlines) {
off_t chunk = pos > (off_t)sizeof(buf) ? (off_t)sizeof(buf) : pos;
ssize_t n;
ssize_t i;
pos -= chunk;
lseek(fd, pos, SEEK_SET);
n = read(fd, buf, (size_t)chunk);
if (n <= 0)
break;
for (i = n - 1; i >= 0; i--) {
if (buf[i] == '\n') {
if (++count > nlines)
return pos + i + 1;
}
}
}
return 0;
}
/* atch tail [-f] [-n N] <session> — print last N lines of session log */
static int cmd_tail(int argc, char **argv)
{
int follow = 0, nlines = 10;
char log_path[600];
unsigned char rbuf[BUFSIZE];
off_t size, start;
ssize_t n;
int fd;
/* Parse -f and -n N (also -nN) before the session name */
while (argc >= 1 && argv[0][0] == '-' && argv[0][1] != '\0') {
if (strcmp(argv[0], "-f") == 0) {
follow = 1;
argc--;
argv++;
} else if (strcmp(argv[0], "-n") == 0) {
if (argc < 2) {
printf("%s: -n requires an argument\n", progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
nlines = atoi(argv[1]);
argc -= 2;
argv += 2;
} else if (strncmp(argv[0], "-n", 2) == 0 &&
argv[0][2] != '\0') {
nlines = atoi(argv[0] + 2);
argc--;
argv++;
} else {
printf("%s: Invalid option '%s'\n", progname, argv[0]);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
}
if (nlines < 1)
nlines = 1;
if (consume_session(&argc, &argv))
return 1;
if (argc > 0) {
printf("%s: Invalid number of arguments.\n", progname);
printf("Try '%s --help' for more information.\n", progname);
return 1;
}
snprintf(log_path, sizeof(log_path), "%s.log", sockname);
fd = open(log_path, O_RDONLY);
if (fd < 0) {
if (errno == ENOENT)
printf("%s: no log for session '%s'\n", progname,
session_shortname());
else
printf("%s: %s: %s\n", progname, log_path,
strerror(errno));
return 1;
}
size = lseek(fd, 0, SEEK_END);
if (size > 0) {
start = find_tail_start(fd, size, nlines);
lseek(fd, start, SEEK_SET);
while ((n = read(fd, rbuf, sizeof(rbuf))) > 0)
write(1, rbuf, (size_t)n);
}
if (follow) {
signal(SIGPIPE, SIG_IGN);
for (;;) {
usleep(250000);
while ((n = read(fd, rbuf, sizeof(rbuf))) > 0)
write(1, rbuf, (size_t)n);
}
}
close(fd);
return 0;
}
static int cmd_rm(int argc, char **argv)
{
int all = 0;
if (argc >= 1 && strcmp(argv[0], "-a") == 0) {
all = 1;
argc--;
argv++;
}
if (all) {
if (argc > 0) {
printf("%s: Invalid number of arguments.\n", progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
return rm_main(1);
}
if (consume_session(&argc, &argv))
return 1;
if (argc > 0) {
printf("%s: Invalid number of arguments.\n", progname);
printf("Try '%s --help' for more information.\n", progname);
return 1;
}
return rm_main(0);
}
/* Default: atch <session> [cmd...] — attach-or-create */
static int cmd_open(char *session, int argc, char **argv)
{
sockname = session;
expand_sockname();
if (parse_options(&argc, &argv))
return 1;
argv = use_shell_if_no_cmd(argc, argv);
save_term();
if (require_tty())
return 1;
if (attach_main(1) != 0) {
if (errno == ECONNREFUSED || errno == ENOENT) {
int saved_errno = errno;
replay_session_log(saved_errno);
if (saved_errno == ECONNREFUSED)
unlink(sockname);
if (master_main(argv, 1, 0) != 0)
return 1;
if (!quiet)
printf("%s: session '%s' created\n", progname,
session_shortname());
}
return attach_main(0);
}
return 0;
}
static void usage(void)
{
printf(PACKAGE_NAME " - version %s, compiled on %s at %s.\n"
"Usage:\n"
" " PACKAGE_NAME " [<session> [command...]]"
"\t\tAttach to session or create it\n"
" " PACKAGE_NAME " <command> [options] ...\n"
"\n"
"Commands:\n"
" attach <session>"
"\t\t\tStrict attach (fail if session missing)\n"
" new <session> [command...]"
"\tCreate session and attach\n"
" start <session> [command...]"
"\tCreate session, detached\n"
" run <session> [command...]"
"\tCreate session, master in foreground\n"
" push <session>"
"\t\t\tPipe stdin into session\n"
" kill [-f] <session>"
"\t\tStop session (SIGTERM then SIGKILL)\n"
" -f, --force\t\t\tSkip grace period, send SIGKILL immediately\n"
" clear [<session>]"
"\t\t\tTruncate the session log\n"
" tail [-f] [-n N] <session>"
"\tPrint last N lines of session log\n"
" -f\t\t\t\tFollow log output\n"
" -n <lines>\t\t\tNumber of lines (default 10)\n"
" list [-a]\t\t\t\tList sessions (-a includes exited)\n"
" rm [-a] [<session>]"
"\t\tRemove stale/exited session(s)\n"
" -a\t\t\t\tRemove all stale and exited sessions\n"
" current\t\t\t\tPrint current session name\n"
"\n"
"Options:\n"
" -e <char>\tSet detach character (default: ^\\)\n"
" -E\t\tDisable detach character\n"
" -r <method>\tRedraw method: none | ctrl_l | winch\n"
" -R <method>\tClear method: none | move\n"
" -z\t\tDisable suspend key\n"
" -q\t\tSuppress messages\n"
" -t\t\tDisable VT100 assumptions\n"
" -C <size>\tLog cap: 0=disable, e.g. 128k, 4m (default 1m)\n"
"\nURL: " PACKAGE_URL "\n\n",
PACKAGE_VERSION, __DATE__, __TIME__);
exit(0);
}
int main(int argc, char **argv)
{
const char *cmd;
progname = argv[0];
init_envvar_names();
++argv;
--argc;
if (argc < 1)
usage();
/* --help / --version / -h */
if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0 ||
strcmp(*argv, "?") == 0)
usage();
if (strcmp(*argv, "--version") == 0) {
printf(PACKAGE_NAME " - version %s, compiled on %s at %s.\n",
PACKAGE_VERSION, __DATE__, __TIME__);
return 0;
}
/*
** Pre-pass: consume any global options (-q, -e, -E, -r, -R, -z, -t)
** that appear before the subcommand or legacy mode letter. We stop
** (without error) as soon as we see a flag that is not a known global
** option, so legacy mode letters like -a/-n still reach the dispatcher
** below unchanged.
*/
while (argc >= 1 && argv[0][0] == '-' && argv[0][1] != '\0' &&
argv[0][1] != '-') {
char c = argv[0][1];
if (c != 'e' && c != 'E' && c != 'r' && c != 'R' &&
c != 'z' && c != 'q' && c != 't' && c != 'C')
break;
if (parse_options(&argc, &argv))
return 1;
}
if (argc < 1)
usage();
/*
** Legacy backward-compat: flag-based syntax (-a, -c, -n, -N, etc.).
** Detected when the first argument is a single-dash flag.
*/
if (argc >= 1 && argv[0][0] == '-' && argv[0][1] != '\0' &&
argv[0][1] != '-') {
int mode = (*argv)[1];
++argv;
--argc;
if (mode == '?' || mode == 'h')
usage();
if (mode == 'l')
return cmd_list(argc, argv);
if (mode == 'i')
return cmd_current();
if (mode != 'a' && mode != 'A' && mode != 'c' &&
mode != 'n' && mode != 'N' && mode != 'p' && mode != 'k') {
printf("%s: Invalid mode '-%c'\n", progname, mode);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
if (argc < 1) {
printf("%s: No session was specified.\n", progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
sockname = *argv;
++argv;
--argc;
expand_sockname();
if (mode == 'p') {
if (argc > 0) {
printf("%s: Invalid number of arguments.\n",
progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
return push_main();
}
if (mode == 'k') {
if (argc > 0) {
printf("%s: Invalid number of arguments.\n",
progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
return kill_main(0);
}
if (parse_options(&argc, &argv))
return 1;
if (mode != 'a')
argv = use_shell_if_no_cmd(argc, argv);
save_term();
if (dont_have_tty && mode != 'n' && mode != 'N') {
printf("%s: attaching to a session requires a "
"terminal.\n", progname);
return 1;
}
if (mode == 'a') {
if (argc > 0) {
printf("%s: Invalid number of arguments.\n",
progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
return attach_main(0);
}
if (mode == 'n')
return master_main(argv, 0, 0);
if (mode == 'N')
return master_main(argv, 0, 1);
if (mode == 'c') {
if (master_main(argv, 1, 0) != 0)
return 1;
return attach_main(0);
}
/* mode == 'A' */
if (attach_main(1) != 0) {
if (errno == ECONNREFUSED || errno == ENOENT) {
int saved_errno = errno;
replay_session_log(saved_errno);
if (saved_errno == ECONNREFUSED)
unlink(sockname);
if (master_main(argv, 1, 0) != 0)
return 1;
}
return attach_main(0);
}
return 0;
}
/* New command-based dispatch */
cmd = *argv;
++argv;
--argc;
if (is_cmd(cmd, "list", "l", "ls"))
return cmd_list(argc, argv);
if (is_cmd(cmd, "current", NULL, NULL))
return cmd_current();
if (is_cmd(cmd, "attach", "a", NULL))
return cmd_attach(argc, argv);
if (is_cmd(cmd, "new", "n", NULL))
return cmd_new(argc, argv);
if (is_cmd(cmd, "start", "s", NULL))
return cmd_start(argc, argv);
if (is_cmd(cmd, "run", NULL, NULL))
return cmd_run(argc, argv);
if (is_cmd(cmd, "push", "p", NULL))
return cmd_push(argc, argv);
if (is_cmd(cmd, "kill", "k", NULL))
return cmd_kill(argc, argv);
if (is_cmd(cmd, "clear", NULL, NULL))
return cmd_clear(argc, argv);
if (is_cmd(cmd, "tail", NULL, NULL))
return cmd_tail(argc, argv);
if (is_cmd(cmd, "rm", NULL, NULL))
return cmd_rm(argc, argv);
/* Smart default: treat first arg as session name → attach-or-create */
return cmd_open((char *)cmd, argc, argv);
}