Do not call event_del() for signals after fork(), just use sigaction()
[tmux-openbsd.git] / cmd-choose-client.c
blobcd5ab9ea4df3e99e945227e3d5a1af5f889016d1
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <ctype.h>
23 #include "tmux.h"
26 * Enter choice mode to choose a client.
29 int cmd_choose_client_exec(struct cmd *, struct cmd_ctx *);
31 void cmd_choose_client_callback(void *, int);
32 void cmd_choose_client_free(void *);
34 const struct cmd_entry cmd_choose_client_entry = {
35 "choose-client", NULL,
36 CMD_TARGET_WINDOW_USAGE " [template]",
37 CMD_ARG01, "",
38 cmd_target_init,
39 cmd_target_parse,
40 cmd_choose_client_exec,
41 cmd_target_free,
42 cmd_target_print
45 struct cmd_choose_client_data {
46 struct client *client;
47 char *template;
50 int
51 cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
53 struct cmd_target_data *data = self->data;
54 struct cmd_choose_client_data *cdata;
55 struct winlink *wl;
56 struct client *c;
57 u_int i, idx, cur;
59 if (ctx->curclient == NULL) {
60 ctx->error(ctx, "must be run interactively");
61 return (-1);
64 if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
65 return (-1);
67 if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
68 return (0);
70 cur = idx = 0;
71 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
72 c = ARRAY_ITEM(&clients, i);
73 if (c == NULL || c->session == NULL)
74 continue;
75 if (c == ctx->curclient)
76 cur = idx;
77 idx++;
79 window_choose_add(wl->window->active, i,
80 "%s: %s [%ux%u %s]%s", c->tty.path,
81 c->session->name, c->tty.sx, c->tty.sy,
82 c->tty.termname, c->tty.flags & TTY_UTF8 ? " (utf8)" : "");
85 cdata = xmalloc(sizeof *cdata);
86 if (data->arg != NULL)
87 cdata->template = xstrdup(data->arg);
88 else
89 cdata->template = xstrdup("detach-client -t '%%'");
90 cdata->client = ctx->curclient;
91 cdata->client->references++;
93 window_choose_ready(wl->window->active,
94 cur, cmd_choose_client_callback, cmd_choose_client_free, cdata);
96 return (0);
99 void
100 cmd_choose_client_callback(void *data, int idx)
102 struct cmd_choose_client_data *cdata = data;
103 struct client *c;
104 struct cmd_list *cmdlist;
105 struct cmd_ctx ctx;
106 char *template, *cause;
108 if (idx == -1)
109 return;
110 if (cdata->client->flags & CLIENT_DEAD)
111 return;
113 if ((u_int) idx > ARRAY_LENGTH(&clients) - 1)
114 return;
115 c = ARRAY_ITEM(&clients, idx);
116 if (c == NULL || c->session == NULL)
117 return;
118 template = cmd_template_replace(cdata->template, c->tty.path, 1);
120 if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
121 if (cause != NULL) {
122 *cause = toupper((u_char) *cause);
123 status_message_set(c, "%s", cause);
124 xfree(cause);
126 xfree(template);
127 return;
129 xfree(template);
131 ctx.msgdata = NULL;
132 ctx.curclient = cdata->client;
134 ctx.error = key_bindings_error;
135 ctx.print = key_bindings_print;
136 ctx.info = key_bindings_info;
138 ctx.cmdclient = NULL;
140 cmd_list_exec(cmdlist, &ctx);
141 cmd_list_free(cmdlist);
144 void
145 cmd_choose_client_free(void *data)
147 struct cmd_choose_client_data *cdata = data;
149 cdata->client->references--;
150 xfree(cdata->template);
151 xfree(cdata);