diff --git a/arguments.c b/arguments.c index 2b8ebc85..d4e5e53f 100644 --- a/arguments.c +++ b/arguments.c @@ -20,6 +20,7 @@ #include #include +#include #include "tmux.h" @@ -77,7 +78,6 @@ struct args * args_parse(const char *template, int argc, char **argv) { struct args *args; - char *ptr; int opt; args = xcalloc(1, sizeof *args); @@ -88,7 +88,7 @@ args_parse(const char *template, int argc, char **argv) while ((opt = getopt(argc, argv, template)) != -1) { if (opt < 0) continue; - if (opt == '?' || (ptr = strchr(template, opt)) == NULL) { + if (opt == '?' || strchr(template, opt) == NULL) { args_free(args); return (NULL); } @@ -204,19 +204,15 @@ args_set(struct args *args, u_char ch, const char *value) /* Replace existing argument. */ if ((entry = args_find(args, ch)) != NULL) { free(entry->value); - if (value != NULL) - entry->value = xstrdup(value); - else - entry->value = NULL; - return; + entry->value = NULL; + } else { + entry = xcalloc(1, sizeof *entry); + entry->flag = ch; + RB_INSERT(args_tree, &args->tree, entry); } - entry = xcalloc(1, sizeof *entry); - entry->flag = ch; if (value != NULL) entry->value = xstrdup(value); - - RB_INSERT(args_tree, &args->tree, entry); } /* Get argument value. Will be NULL if it isn't present. */ diff --git a/client.c b/client.c index 26c095c0..277efb65 100644 --- a/client.c +++ b/client.c @@ -118,10 +118,15 @@ retry: close(fd); xasprintf(&lockfile, "%s.lock", path); - if ((lockfd = client_get_lock(lockfile)) == -1) + if ((lockfd = client_get_lock(lockfile)) == -1) { + free(lockfile); goto retry; - if (unlink(path) != 0 && errno != ENOENT) + } + if (unlink(path) != 0 && errno != ENOENT) { + free(lockfile); + close(lockfd); return (-1); + } fd = server_start(lockfd, lockfile); free(lockfile); close(lockfd); @@ -232,7 +237,8 @@ client_main(int argc, char **argv, int flags) /* Initialise the client socket and start the server. */ fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER); if (fd == -1) { - fprintf(stderr, "failed to connect to server\n"); + fprintf(stderr, "failed to connect to server: %s\n", + strerror(errno)); return (1); } diff --git a/cmd-attach-session.c b/cmd-attach-session.c index e4c0b232..8094d78b 100644 --- a/cmd-attach-session.c +++ b/cmd-attach-session.c @@ -47,6 +47,9 @@ cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag, { struct session *s; struct client *c; + struct winlink *wl = NULL; + struct window *w = NULL; + struct window_pane *wp = NULL; const char *update; char *cause; u_int i; @@ -59,12 +62,31 @@ cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag, return (CMD_RETURN_ERROR); } - if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) - return (CMD_RETURN_ERROR); + if (tflag == NULL) { + if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) + return (CMD_RETURN_ERROR); + } else if (tflag[strcspn(tflag, ":.")] != '\0') { + if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) + return (CMD_RETURN_ERROR); + } else { + if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) + return (CMD_RETURN_ERROR); + w = cmd_lookup_windowid(tflag); + if (w == NULL && (wp = cmd_lookup_paneid(tflag)) != NULL) + w = wp->window; + if (w != NULL) + wl = winlink_find_by_window(&s->windows, w); + } if (cmdq->client == NULL) return (CMD_RETURN_NORMAL); + if (wl != NULL) { + if (wp != NULL) + window_set_active_pane(wp->window, wp); + session_set_current(s, wl); + } + if (cmdq->client->session != NULL) { if (dflag) { /* diff --git a/cmd-new-session.c b/cmd-new-session.c index ad083a44..15e411d0 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -107,13 +107,16 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) cp = format_expand(ft, args_get(args, 'c')); format_free(ft); - fd = open(cp, O_RDONLY|O_DIRECTORY); - free(cp); - if (fd == -1) { - cmdq_error(cmdq, "bad working directory: %s", - strerror(errno)); - return (CMD_RETURN_ERROR); - } + if (cp != NULL && *cp != '\0') { + fd = open(cp, O_RDONLY|O_DIRECTORY); + free(cp); + if (fd == -1) { + cmdq_error(cmdq, "bad working directory: %s", + strerror(errno)); + return (CMD_RETURN_ERROR); + } + } else if (cp != NULL) + free(cp); cwd = fd; } else if (c != NULL && c->session == NULL) cwd = c->cwd; diff --git a/cmd-new-window.c b/cmd-new-window.c index 5c2cbe40..58a5eb65 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -82,6 +82,37 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) } detached = args_has(args, 'd'); + if (args->argc == 0) + cmd = options_get_string(&s->options, "default-command"); + else + cmd = args->argv[0]; + + if (args_has(args, 'c')) { + ft = format_create(); + if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL) + format_client(ft, c); + format_session(ft, s); + format_winlink(ft, s, s->curw); + format_window_pane(ft, s->curw->window->active); + cp = format_expand(ft, args_get(args, 'c')); + format_free(ft); + + if (cp != NULL && *cp != '\0') { + fd = open(cp, O_RDONLY|O_DIRECTORY); + free(cp); + if (fd == -1) { + cmdq_error(cmdq, "bad working directory: %s", + strerror(errno)); + return (CMD_RETURN_ERROR); + } + } else if (cp != NULL) + free(cp); + cwd = fd; + } else if (cmdq->client != NULL && cmdq->client->session == NULL) + cwd = cmdq->client->cwd; + else + cwd = s->cwd; + wl = NULL; if (idx != -1) wl = winlink_find_by_index(&s->windows, idx); @@ -102,34 +133,6 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) } } - if (args->argc == 0) - cmd = options_get_string(&s->options, "default-command"); - else - cmd = args->argv[0]; - - if (args_has(args, 'c')) { - ft = format_create(); - if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL) - format_client(ft, c); - format_session(ft, s); - format_winlink(ft, s, s->curw); - format_window_pane(ft, s->curw->window->active); - cp = format_expand(ft, args_get(args, 'c')); - format_free(ft); - - fd = open(cp, O_RDONLY|O_DIRECTORY); - free(cp); - if (fd == -1) { - cmdq_error(cmdq, "bad working directory: %s", - strerror(errno)); - return (CMD_RETURN_ERROR); - } - cwd = fd; - } else if (cmdq->client != NULL && cmdq->client->session == NULL) - cwd = cmdq->client->cwd; - else - cwd = s->cwd; - if (idx == -1) idx = -1 - options_get_number(&s->options, "base-index"); wl = session_new(s, args_get(args, 'n'), cmd, cwd, idx, &cause); diff --git a/cmd-queue.c b/cmd-queue.c index c5905bdb..17955b82 100644 --- a/cmd-queue.c +++ b/cmd-queue.c @@ -69,9 +69,7 @@ cmdq_print(struct cmd_q *cmdq, const char *fmt, ...) if (c == NULL) /* nothing */; else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { - va_start(ap, fmt); evbuffer_add_vprintf(c->stdout_data, fmt, ap); - va_end(ap); evbuffer_add(c->stdout_data, "\n", 1); server_push_stdout(c); @@ -104,9 +102,7 @@ cmdq_info(struct cmd_q *cmdq, const char *fmt, ...) if (c == NULL) /* nothing */; else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { - va_start(ap, fmt); evbuffer_add_vprintf(c->stdout_data, fmt, ap); - va_end(ap); evbuffer_add(c->stdout_data, "\n", 1); server_push_stdout(c); diff --git a/cmd-split-window.c b/cmd-split-window.c index 4bb069f0..c43cb96b 100644 --- a/cmd-split-window.c +++ b/cmd-split-window.c @@ -94,13 +94,16 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) cp = format_expand(ft, args_get(args, 'c')); format_free(ft); - fd = open(cp, O_RDONLY|O_DIRECTORY); - free(cp); - if (fd == -1) { - cmdq_error(cmdq, "bad working directory: %s", - strerror(errno)); - return (CMD_RETURN_ERROR); - } + if (cp != NULL && *cp != '\0') { + fd = open(cp, O_RDONLY|O_DIRECTORY); + free(cp); + if (fd == -1) { + cmdq_error(cmdq, "bad working directory: %s", + strerror(errno)); + return (CMD_RETURN_ERROR); + } + } else if (cp != NULL) + free(cp); cwd = fd; } else if (cmdq->client != NULL && cmdq->client->session == NULL) cwd = cmdq->client->cwd; diff --git a/cmd-switch-client.c b/cmd-switch-client.c index d101c52b..3d97c5b7 100644 --- a/cmd-switch-client.c +++ b/cmd-switch-client.c @@ -59,9 +59,13 @@ cmd_switch_client_key_binding(struct cmd *self, int key) enum cmd_retval cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct client *c; - struct session *s; + struct args *args = self->args; + struct client *c; + struct session *s; + struct winlink *wl = NULL; + struct window *w = NULL; + struct window_pane *wp = NULL; + const char *tflag; if ((c = cmd_find_client(cmdq, args_get(args, 'c'), 0)) == NULL) return (CMD_RETURN_ERROR); @@ -76,7 +80,7 @@ cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq) } } - s = NULL; + tflag = args_get(args, 't'); if (args_has(args, 'n')) { if ((s = session_next_session(c->session)) == NULL) { cmdq_error(cmdq, "can't find next session"); @@ -94,10 +98,33 @@ cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq) cmdq_error(cmdq, "can't find last session"); return (CMD_RETURN_ERROR); } - } else - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); + } else { + if (tflag == NULL) { + if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) + return (CMD_RETURN_ERROR); + } else if (tflag[strcspn(tflag, ":.")] != '\0') { + if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) + return (CMD_RETURN_ERROR); + } else { + if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) + return (CMD_RETURN_ERROR); + w = cmd_lookup_windowid(tflag); + if (w == NULL && + (wp = cmd_lookup_paneid(tflag)) != NULL) + w = wp->window; + if (w != NULL) + wl = winlink_find_by_window(&s->windows, w); + } + + if (cmdq->client == NULL) + return (CMD_RETURN_NORMAL); + + if (wl != NULL) { + if (wp != NULL) + window_set_active_pane(wp->window, wp); + session_set_current(s, wl); + } + } if (c->session != NULL) c->last_session = c->session; diff --git a/cmd.c b/cmd.c index 414c9067..5e6b93aa 100644 --- a/cmd.c +++ b/cmd.c @@ -125,9 +125,7 @@ struct session *cmd_lookup_session(const char *, int *); struct session *cmd_lookup_session_id(const char *); struct winlink *cmd_lookup_window(struct session *, const char *, int *); int cmd_lookup_index(struct session *, const char *, int *); -struct window_pane *cmd_lookup_paneid(const char *); struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *); -struct window *cmd_lookup_windowid(const char *); struct session *cmd_window_session(struct cmd_q *, struct window *, struct winlink **); struct winlink *cmd_find_window_offset(const char *, struct session *, int *); diff --git a/format.c b/format.c index de6d8c84..10ac613e 100644 --- a/format.c +++ b/format.c @@ -321,6 +321,13 @@ format_expand(struct format_tree *ft, const char *fmt) break; fmt += n + 1; continue; + case '#': + while (len - off < 2) { + buf = xrealloc(buf, 2, len); + len *= 2; + } + buf[off++] = '#'; + continue; default: s = NULL; if (ch >= 'A' && ch <= 'Z') diff --git a/grid.c b/grid.c index 9e800243..15c3018b 100644 --- a/grid.c +++ b/grid.c @@ -124,7 +124,7 @@ grid_compare(struct grid *ga, struct grid *gb) struct grid_cell *gca, *gcb; u_int xx, yy; - if (ga->sx != gb->sx || ga->sy != ga->sy) + if (ga->sx != gb->sx || ga->sy != gb->sy) return (1); for (yy = 0; yy < ga->sy; yy++) { @@ -644,7 +644,7 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx, if (trim) { while (off > 0 && buf[off - 1] == ' ') off--; - } + } buf[off] = '\0'; return (buf); diff --git a/options.c b/options.c index 7360906b..d70550f1 100644 --- a/options.c +++ b/options.c @@ -26,7 +26,7 @@ /* * Option handling; each option has a name, type and value and is stored in - * a splay tree. + * a red-black tree. */ RB_GENERATE(options_tree, options_entry, entry, options_cmp); diff --git a/screen.c b/screen.c index 39720f4e..7bfc0154 100644 --- a/screen.c +++ b/screen.c @@ -111,12 +111,8 @@ screen_set_cursor_colour(struct screen *s, const char *colour_string) void screen_set_title(struct screen *s, const char *title) { - char tmp[BUFSIZ]; - - strlcpy(tmp, title, sizeof tmp); - free(s->title); - s->title = xstrdup(tmp); + s->title = xstrdup(title); } /* Resize screen. */ diff --git a/tmux.1 b/tmux.1 index 1146faf8..075bb1ea 100644 --- a/tmux.1 +++ b/tmux.1 @@ -3015,6 +3015,9 @@ for example .Ql #{session_name} . Some variables also have an shorter alias such as .Ql #S . +.Ql ## +is replaced by a single +.Ql # . Conditionals are also accepted by prefixing with .Ql \&? and separating two alternatives with a comma; diff --git a/tmux.c b/tmux.c index 1e6edd90..f5be1de4 100644 --- a/tmux.c +++ b/tmux.c @@ -205,8 +205,9 @@ int main(int argc, char **argv) { struct passwd *pw; - char *s, *path, *label, *home, **var, tmp[MAXPATHLEN]; + char *s, *path, *label, **var, tmp[MAXPATHLEN]; char in[256]; + const char *home; long long pid; int opt, flags, quiet, keys, session; @@ -331,11 +332,15 @@ main(int argc, char **argv) pw = getpwuid(getuid()); if (pw != NULL) home = pw->pw_dir; + else + home = NULL; } - xasprintf(&cfg_file, "%s/.tmux.conf", home); - if (access(cfg_file, R_OK) != 0 && errno == ENOENT) { - free(cfg_file); - cfg_file = NULL; + if (home != NULL) { + xasprintf(&cfg_file, "%s/.tmux.conf", home); + if (access(cfg_file, R_OK) != 0 && errno == ENOENT) { + free(cfg_file); + cfg_file = NULL; + } } } @@ -367,7 +372,11 @@ main(int argc, char **argv) } } free(label); - strlcpy(socket_path, path, sizeof socket_path); + + if (strlcpy(socket_path, path, sizeof socket_path) >= sizeof socket_path) { + fprintf(stderr, "socket path too long: %s\n", path); + exit(1); + } free(path); #ifdef HAVE_SETPROCTITLE diff --git a/tmux.h b/tmux.h index dbc2c491..784b6da9 100644 --- a/tmux.h +++ b/tmux.h @@ -1732,6 +1732,8 @@ int cmd_find_index(struct cmd_q *, const char *, struct winlink *cmd_find_pane(struct cmd_q *, const char *, struct session **, struct window_pane **); char *cmd_template_replace(const char *, const char *, int); +struct window *cmd_lookup_windowid(const char *); +struct window_pane *cmd_lookup_paneid(const char *); extern const struct cmd_entry *cmd_table[]; extern const struct cmd_entry cmd_attach_session_entry; extern const struct cmd_entry cmd_bind_key_entry; diff --git a/tty-acs.c b/tty-acs.c index e85888c3..ae84f0d4 100644 --- a/tty-acs.c +++ b/tty-acs.c @@ -30,38 +30,38 @@ struct tty_acs_entry { const char *string; }; const struct tty_acs_entry tty_acs_table[] = { - { '+', "\342\206\222" }, - { ',', "\342\206\220" }, - { '-', "\342\206\221" }, - { '.', "\342\206\223" }, - { '0', "\342\226\256" }, - { '`', "\342\227\206" }, - { 'a', "\342\226\222" }, - { 'f', "\302\260" }, - { 'g', "\302\261" }, - { 'h', "\342\226\222" }, - { 'i', "\342\230\203" }, - { 'j', "\342\224\230" }, - { 'k', "\342\224\220" }, - { 'l', "\342\224\214" }, - { 'm', "\342\224\224" }, - { 'n', "\342\224\274" }, - { 'o', "\342\216\272" }, - { 'p', "\342\216\273" }, - { 'q', "\342\224\200" }, - { 'r', "\342\216\274" }, - { 's', "\342\216\275" }, - { 't', "\342\224\234" }, - { 'u', "\342\224\244" }, - { 'v', "\342\224\264" }, - { 'w', "\342\224\254" }, - { 'x', "\342\224\202" }, - { 'y', "\342\211\244" }, - { 'z', "\342\211\245" }, - { '{', "\317\200" }, - { '|', "\342\211\240" }, - { '}', "\302\243" }, - { '~', "\302\267" } + { '+', "\342\206\222" }, /* arrow pointing right */ + { ',', "\342\206\220" }, /* arrow pointing left */ + { '-', "\342\206\221" }, /* arrow pointing up */ + { '.', "\342\206\223" }, /* arrow pointing down */ + { '0', "\342\226\256" }, /* solid square block */ + { '`', "\342\227\206" }, /* diamond */ + { 'a', "\342\226\222" }, /* checker board (stipple) */ + { 'f', "\302\260" }, /* degree symbol */ + { 'g', "\302\261" }, /* plus/minus */ + { 'h', "\342\226\222" }, /* board of squares */ + { 'i', "\342\230\203" }, /* lantern symbol */ + { 'j', "\342\224\230" }, /* lower right corner */ + { 'k', "\342\224\220" }, /* upper right corner */ + { 'l', "\342\224\214" }, /* upper left corner */ + { 'm', "\342\224\224" }, /* lower left corner */ + { 'n', "\342\224\274" }, /* large plus or crossover */ + { 'o', "\342\216\272" }, /* scan line 1 */ + { 'p', "\342\216\273" }, /* scan line 3 */ + { 'q', "\342\224\200" }, /* horizontal line */ + { 'r', "\342\216\274" }, /* scan line 7 */ + { 's', "\342\216\275" }, /* scan line 9 */ + { 't', "\342\224\234" }, /* tee pointing right */ + { 'u', "\342\224\244" }, /* tee pointing left */ + { 'v', "\342\224\264" }, /* tee pointing up */ + { 'w', "\342\224\254" }, /* tee pointing down */ + { 'x', "\342\224\202" }, /* vertical line */ + { 'y', "\342\211\244" }, /* less-than-or-equal-to */ + { 'z', "\342\211\245" }, /* greater-than-or-equal-to */ + { '{', "\317\200" }, /* greek pi */ + { '|', "\342\211\240" }, /* not-equal */ + { '}', "\302\243" }, /* UK pound sign */ + { '~', "\302\267" } /* bullet */ }; int diff --git a/window-choose.c b/window-choose.c index 572581a5..456e4602 100644 --- a/window-choose.c +++ b/window-choose.c @@ -345,8 +345,7 @@ window_choose_collapse(struct window_pane *wp, struct session *s) * assign the actual result we want to render and copy the new one over * the top of it. */ - for (i = 0; i < ARRAY_LENGTH(&data->list); i++) - { + for (i = 0; i < ARRAY_LENGTH(&data->list); i++) { item = &ARRAY_ITEM(&data->list, i); wcd = item->wcd; diff --git a/window-copy.c b/window-copy.c index 37b65c87..0831d0f2 100644 --- a/window-copy.c +++ b/window-copy.c @@ -1030,7 +1030,7 @@ window_copy_search_up(struct window_pane *wp, const char *searchstr) cis = 1; for (ptr = searchstr; *ptr != '\0'; ptr++) { - if (*ptr != tolower(*ptr)) { + if (*ptr != tolower((u_char)*ptr)) { cis = 0; break; } @@ -1097,7 +1097,7 @@ window_copy_search_down(struct window_pane *wp, const char *searchstr) cis = 1; for (ptr = searchstr; *ptr != '\0'; ptr++) { - if (*ptr != tolower(*ptr)) { + if (*ptr != tolower((u_char)*ptr)) { cis = 0; break; }