Change terminal-overrides to a server option (now that we have them), it
[tmux-openbsd.git] / cmd-queue.c
blobcaa80afe738ee82f1600d422489bb424ea23259f
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2013 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>
22 #include <stdlib.h>
23 #include <time.h>
25 #include "tmux.h"
27 /* Create new command queue. */
28 struct cmd_q *
29 cmdq_new(struct client *c)
31 struct cmd_q *cmdq;
33 cmdq = xcalloc(1, sizeof *cmdq);
34 cmdq->references = 1;
35 cmdq->dead = 0;
37 cmdq->client = c;
38 cmdq->client_exit = -1;
40 TAILQ_INIT(&cmdq->queue);
41 cmdq->item = NULL;
42 cmdq->cmd = NULL;
44 return (cmdq);
47 /* Free command queue */
48 int
49 cmdq_free(struct cmd_q *cmdq)
51 if (--cmdq->references != 0)
52 return (cmdq->dead);
54 cmdq_flush(cmdq);
55 free(cmdq);
56 return (1);
59 /* Show message from command. */
60 void printflike2
61 cmdq_print(struct cmd_q *cmdq, const char *fmt, ...)
63 struct client *c = cmdq->client;
64 struct window *w;
65 va_list ap;
67 va_start(ap, fmt);
69 if (c == NULL)
70 /* nothing */;
71 else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
72 evbuffer_add_vprintf(c->stdout_data, fmt, ap);
74 evbuffer_add(c->stdout_data, "\n", 1);
75 server_push_stdout(c);
76 } else {
77 w = c->session->curw->window;
78 if (w->active->mode != &window_copy_mode) {
79 window_pane_reset_mode(w->active);
80 window_pane_set_mode(w->active, &window_copy_mode);
81 window_copy_init_for_output(w->active);
83 window_copy_vadd(w->active, fmt, ap);
86 va_end(ap);
89 /* Show info from command. */
90 void printflike2
91 cmdq_info(struct cmd_q *cmdq, const char *fmt, ...)
93 struct client *c = cmdq->client;
94 va_list ap;
95 char *msg;
97 if (options_get_number(&global_options, "quiet"))
98 return;
100 va_start(ap, fmt);
102 if (c == NULL)
103 /* nothing */;
104 else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
105 evbuffer_add_vprintf(c->stdout_data, fmt, ap);
107 evbuffer_add(c->stdout_data, "\n", 1);
108 server_push_stdout(c);
109 } else {
110 xvasprintf(&msg, fmt, ap);
111 *msg = toupper((u_char) *msg);
112 status_message_set(c, "%s", msg);
113 free(msg);
116 va_end(ap);
120 /* Show error from command. */
121 void printflike2
122 cmdq_error(struct cmd_q *cmdq, const char *fmt, ...)
124 struct client *c = cmdq->client;
125 struct cmd *cmd = cmdq->cmd;
126 va_list ap;
127 char *msg, *cause;
128 size_t msglen;
130 va_start(ap, fmt);
131 msglen = xvasprintf(&msg, fmt, ap);
132 va_end(ap);
134 if (c == NULL) {
135 xasprintf(&cause, "%s:%u: %s", cmd->file, cmd->line, msg);
136 ARRAY_ADD(&cfg_causes, cause);
137 } else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
138 evbuffer_add(c->stderr_data, msg, msglen);
139 evbuffer_add(c->stderr_data, "\n", 1);
141 server_push_stderr(c);
142 c->retval = 1;
143 } else {
144 *msg = toupper((u_char) *msg);
145 status_message_set(c, "%s", msg);
148 free(msg);
151 /* Print a guard line. */
153 cmdq_guard(struct cmd_q *cmdq, const char *guard, int flags)
155 struct client *c = cmdq->client;
157 if (c == NULL)
158 return 0;
159 if (!(c->flags & CLIENT_CONTROL))
160 return 0;
162 evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
163 (long) cmdq->time, cmdq->number, flags);
164 server_push_stdout(c);
165 return 1;
168 /* Add command list to queue and begin processing if needed. */
169 void
170 cmdq_run(struct cmd_q *cmdq, struct cmd_list *cmdlist)
172 cmdq_append(cmdq, cmdlist);
174 if (cmdq->item == NULL) {
175 cmdq->cmd = NULL;
176 cmdq_continue(cmdq);
180 /* Add command list to queue. */
181 void
182 cmdq_append(struct cmd_q *cmdq, struct cmd_list *cmdlist)
184 struct cmd_q_item *item;
186 item = xcalloc(1, sizeof *item);
187 item->cmdlist = cmdlist;
188 TAILQ_INSERT_TAIL(&cmdq->queue, item, qentry);
189 cmdlist->references++;
192 /* Continue processing command queue. Returns 1 if finishes empty. */
194 cmdq_continue(struct cmd_q *cmdq)
196 struct cmd_q_item *next;
197 enum cmd_retval retval;
198 int empty, guard, flags;
199 char s[1024];
201 notify_disable();
203 empty = TAILQ_EMPTY(&cmdq->queue);
204 if (empty)
205 goto empty;
207 if (cmdq->item == NULL) {
208 cmdq->item = TAILQ_FIRST(&cmdq->queue);
209 cmdq->cmd = TAILQ_FIRST(&cmdq->item->cmdlist->list);
210 } else
211 cmdq->cmd = TAILQ_NEXT(cmdq->cmd, qentry);
213 do {
214 next = TAILQ_NEXT(cmdq->item, qentry);
216 while (cmdq->cmd != NULL) {
217 cmd_print(cmdq->cmd, s, sizeof s);
218 log_debug("cmdq %p: %s (client %d)", cmdq, s,
219 cmdq->client != NULL ? cmdq->client->ibuf.fd : -1);
221 cmdq->time = time(NULL);
222 cmdq->number++;
224 flags = !!(cmdq->cmd->flags & CMD_CONTROL);
225 guard = cmdq_guard(cmdq, "begin", flags);
227 retval = cmdq->cmd->entry->exec(cmdq->cmd, cmdq);
229 if (guard) {
230 if (retval == CMD_RETURN_ERROR)
231 cmdq_guard(cmdq, "error", flags);
232 else
233 cmdq_guard(cmdq, "end", flags);
236 if (retval == CMD_RETURN_ERROR)
237 break;
238 if (retval == CMD_RETURN_WAIT)
239 goto out;
240 if (retval == CMD_RETURN_STOP) {
241 cmdq_flush(cmdq);
242 goto empty;
245 cmdq->cmd = TAILQ_NEXT(cmdq->cmd, qentry);
248 TAILQ_REMOVE(&cmdq->queue, cmdq->item, qentry);
249 cmd_list_free(cmdq->item->cmdlist);
250 free(cmdq->item);
252 cmdq->item = next;
253 if (cmdq->item != NULL)
254 cmdq->cmd = TAILQ_FIRST(&cmdq->item->cmdlist->list);
255 } while (cmdq->item != NULL);
257 empty:
258 if (cmdq->client_exit > 0)
259 cmdq->client->flags |= CLIENT_EXIT;
260 if (cmdq->emptyfn != NULL)
261 cmdq->emptyfn(cmdq); /* may free cmdq */
262 empty = 1;
264 out:
265 notify_enable();
266 return (empty);
269 /* Flush command queue. */
270 void
271 cmdq_flush(struct cmd_q *cmdq)
273 struct cmd_q_item *item, *item1;
275 TAILQ_FOREACH_SAFE(item, &cmdq->queue, qentry, item1) {
276 TAILQ_REMOVE(&cmdq->queue, item, qentry);
277 cmd_list_free(item->cmdlist);
278 free(item);
280 cmdq->item = NULL;