Annotation of src/usr.bin/tmux/cmd-queue.c, Revision 1.44
1.44 ! nicm 1: /* $OpenBSD: cmd-queue.c,v 1.43 2016/10/16 17:55:14 nicm Exp $ */
1.1 nicm 2:
3: /*
1.34 nicm 4: * Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <ctype.h>
22: #include <stdlib.h>
1.23 nicm 23: #include <string.h>
1.7 nicm 24: #include <time.h>
1.1 nicm 25:
26: #include "tmux.h"
27:
1.43 nicm 28: /* Global command queue. */
1.44 ! nicm 29: static struct cmdq_list global_queue = TAILQ_HEAD_INITIALIZER(global_queue);
1.24 nicm 30:
1.43 nicm 31: /* Get command queue name. */
32: static const char *
33: cmdq_name(struct client *c)
34: {
35: static char s[32];
36:
37: if (c == NULL)
38: return ("<global>");
39: xsnprintf(s, sizeof s, "<%p>", c);
40: return (s);
41: }
42:
43: /* Get command queue from client. */
1.44 ! nicm 44: static struct cmdq_list *
1.43 nicm 45: cmdq_get(struct client *c)
46: {
47: if (c == NULL)
48: return (&global_queue);
49: return (&c->queue);
50: }
51:
52: /* Append an item. */
53: void
1.44 ! nicm 54: cmdq_append(struct client *c, struct cmdq_item *item)
1.43 nicm 55: {
1.44 ! nicm 56: struct cmdq_list *queue = cmdq_get(c);
! 57: struct cmdq_item *next;
1.43 nicm 58:
59: do {
1.44 ! nicm 60: next = item->next;
! 61: item->next = NULL;
1.43 nicm 62:
63: if (c != NULL)
64: c->references++;
1.44 ! nicm 65: item->client = c;
1.43 nicm 66:
1.44 ! nicm 67: item->queue = queue;
! 68: TAILQ_INSERT_TAIL(queue, item, entry);
1.43 nicm 69:
1.44 ! nicm 70: item = next;
! 71: } while (item != NULL);
1.43 nicm 72: }
73:
74: /* Insert an item. */
75: void
1.44 ! nicm 76: cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
1.43 nicm 77: {
78: struct client *c = after->client;
1.44 ! nicm 79: struct cmdq_list *queue = after->queue;
! 80: struct cmdq_item *next;
1.43 nicm 81:
82: do {
1.44 ! nicm 83: next = item->next;
! 84: item->next = NULL;
1.43 nicm 85:
86: if (c != NULL)
87: c->references++;
1.44 ! nicm 88: item->client = c;
1.43 nicm 89:
1.44 ! nicm 90: item->queue = queue;
1.43 nicm 91: if (after->next != NULL)
1.44 ! nicm 92: TAILQ_INSERT_AFTER(queue, after->next, item, entry);
1.43 nicm 93: else
1.44 ! nicm 94: TAILQ_INSERT_AFTER(queue, after, item, entry);
! 95: after->next = item;
1.43 nicm 96:
1.44 ! nicm 97: item = next;
! 98: } while (item != NULL);
1.43 nicm 99: }
100:
101: /* Remove an item. */
102: static void
1.44 ! nicm 103: cmdq_remove(struct cmdq_item *item)
1.43 nicm 104: {
1.44 ! nicm 105: free((void *)item->hook);
1.43 nicm 106:
1.44 ! nicm 107: if (item->client != NULL)
! 108: server_client_unref(item->client);
1.43 nicm 109:
1.44 ! nicm 110: if (item->type == CMDQ_COMMAND)
! 111: cmd_list_free(item->cmdlist);
1.43 nicm 112:
1.44 ! nicm 113: TAILQ_REMOVE(item->queue, item, entry);
! 114: free(item);
1.43 nicm 115: }
116:
117: /* Set command group. */
118: static u_int
119: cmdq_next_group(void)
120: {
121: static u_int group;
122:
123: return (++group);
124: }
125:
126: /* Remove all subsequent items that match this item's group. */
127: static void
1.44 ! nicm 128: cmdq_remove_group(struct cmdq_item *item)
1.43 nicm 129: {
1.44 ! nicm 130: struct cmdq_item *this, *next;
1.43 nicm 131:
1.44 ! nicm 132: this = TAILQ_NEXT(item, entry);
1.43 nicm 133: while (this != NULL) {
134: next = TAILQ_NEXT(this, entry);
1.44 ! nicm 135: if (this->group == item->group)
1.43 nicm 136: cmdq_remove(this);
137: this = next;
138: }
139: }
140:
141: /* Get a command for the command queue. */
1.44 ! nicm 142: struct cmdq_item *
1.43 nicm 143: cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
144: struct mouse_event *m, int flags)
145: {
1.44 ! nicm 146: struct cmdq_item *item, *first = NULL, *last = NULL;
! 147: struct cmd *cmd;
! 148: u_int group = cmdq_next_group();
1.43 nicm 149:
150: TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
1.44 ! nicm 151: item = xcalloc(1, sizeof *item);
! 152: item->type = CMDQ_COMMAND;
! 153: item->group = group;
! 154: item->flags = flags;
1.43 nicm 155:
1.44 ! nicm 156: item->cmdlist = cmdlist;
! 157: item->cmd = cmd;
1.43 nicm 158:
159: if (current != NULL)
1.44 ! nicm 160: cmd_find_copy_state(&item->current, current);
1.43 nicm 161: if (m != NULL)
1.44 ! nicm 162: memcpy(&item->mouse, m, sizeof item->mouse);
1.43 nicm 163: cmdlist->references++;
164:
165: if (first == NULL)
1.44 ! nicm 166: first = item;
1.43 nicm 167: if (last != NULL)
1.44 ! nicm 168: last->next = item;
! 169: last = item;
1.43 nicm 170: }
171: return (first);
172: }
173:
174: /* Fire command on command queue. */
175: static enum cmd_retval
1.44 ! nicm 176: cmdq_fire_command(struct cmdq_item *item)
1.43 nicm 177: {
1.44 ! nicm 178: struct client *c = item->client;
! 179: struct cmd *cmd = item->cmd;
1.43 nicm 180: enum cmd_retval retval;
181: const char *name;
182: struct cmd_find_state *fsp, fs;
183: int flags;
184:
185: flags = !!(cmd->flags & CMD_CONTROL);
1.44 ! nicm 186: cmdq_guard(item, "begin", flags);
1.43 nicm 187:
1.44 ! nicm 188: if (cmd_prepare_state(cmd, item) != 0) {
1.43 nicm 189: retval = CMD_RETURN_ERROR;
190: goto out;
191: }
1.44 ! nicm 192: if (item->client == NULL)
! 193: item->client = cmd_find_client(item, NULL, CMD_FIND_QUIET);
1.43 nicm 194:
1.44 ! nicm 195: retval = cmd->entry->exec(cmd, item);
1.43 nicm 196: if (retval == CMD_RETURN_ERROR)
197: goto out;
198:
199: if (cmd->entry->flags & CMD_AFTERHOOK) {
200: name = cmd->entry->name;
1.44 ! nicm 201: if (cmd_find_valid_state(&item->state.tflag))
! 202: fsp = &item->state.tflag;
1.43 nicm 203: else {
1.44 ! nicm 204: if (cmd_find_current(&fs, item, CMD_FIND_QUIET) != 0)
1.43 nicm 205: goto out;
206: fsp = &fs;
207: }
1.44 ! nicm 208: hooks_insert(fsp->s->hooks, item, fsp, "after-%s", name);
1.43 nicm 209: }
210:
211: out:
1.44 ! nicm 212: item->client = c;
1.43 nicm 213: if (retval == CMD_RETURN_ERROR)
1.44 ! nicm 214: cmdq_guard(item, "error", flags);
1.43 nicm 215: else
1.44 ! nicm 216: cmdq_guard(item, "end", flags);
1.43 nicm 217: return (retval);
218: }
219:
220: /* Get a callback for the command queue. */
1.44 ! nicm 221: struct cmdq_item *
! 222: cmdq_get_callback(cmdq_cb cb, void *data)
1.1 nicm 223: {
1.44 ! nicm 224: struct cmdq_item *item;
1.1 nicm 225:
1.44 ! nicm 226: item = xcalloc(1, sizeof *item);
! 227: item->type = CMDQ_CALLBACK;
! 228: item->group = 0;
! 229: item->flags = 0;
1.1 nicm 230:
1.44 ! nicm 231: item->cb = cb;
! 232: item->data = data;
1.1 nicm 233:
1.44 ! nicm 234: return (item);
1.43 nicm 235: }
1.1 nicm 236:
1.43 nicm 237: /* Fire callback on callback queue. */
238: static enum cmd_retval
1.44 ! nicm 239: cmdq_fire_callback(struct cmdq_item *item)
1.43 nicm 240: {
1.44 ! nicm 241: return (item->cb(item, item->data));
1.43 nicm 242: }
1.33 nicm 243:
1.43 nicm 244: /* Process next item on command queue. */
245: u_int
246: cmdq_next(struct client *c)
247: {
1.44 ! nicm 248: struct cmdq_list *queue = cmdq_get(c);
1.43 nicm 249: const char *name = cmdq_name(c);
1.44 ! nicm 250: struct cmdq_item *item;
1.43 nicm 251: enum cmd_retval retval;
252: u_int items = 0;
253: static u_int number;
1.1 nicm 254:
1.43 nicm 255: if (TAILQ_EMPTY(queue)) {
256: log_debug("%s %s: empty", __func__, name);
1.25 nicm 257: return (0);
258: }
1.44 ! nicm 259: if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43 nicm 260: log_debug("%s %s: waiting", __func__, name);
261: return (0);
262: }
263:
264: log_debug("%s %s: enter", __func__, name);
265: for (;;) {
1.44 ! nicm 266: item = TAILQ_FIRST(queue);
! 267: if (item == NULL)
1.43 nicm 268: break;
269: log_debug("%s %s: type %d, flags %x", __func__, name,
1.44 ! nicm 270: item->type, item->flags);
1.43 nicm 271:
272: /*
273: * Any item with the waiting flag set waits until an external
274: * event clears the flag (for example, a job - look at
275: * run-shell).
276: */
1.44 ! nicm 277: if (item->flags & CMDQ_WAITING)
1.43 nicm 278: goto waiting;
279:
280: /*
281: * Items are only fired once, once the fired flag is set, a
282: * waiting flag can only be cleared by an external event.
283: */
1.44 ! nicm 284: if (~item->flags & CMDQ_FIRED) {
! 285: item->time = time(NULL);
! 286: item->number = ++number;
1.43 nicm 287:
1.44 ! nicm 288: switch (item->type)
1.43 nicm 289: {
1.44 ! nicm 290: case CMDQ_COMMAND:
! 291: retval = cmdq_fire_command(item);
1.43 nicm 292:
293: /*
294: * If a command returns an error, remove any
295: * subsequent commands in the same group.
296: */
297: if (retval == CMD_RETURN_ERROR)
1.44 ! nicm 298: cmdq_remove_group(item);
1.43 nicm 299: break;
1.44 ! nicm 300: case CMDQ_CALLBACK:
! 301: retval = cmdq_fire_callback(item);
1.43 nicm 302: break;
303: default:
304: retval = CMD_RETURN_ERROR;
305: break;
306: }
1.44 ! nicm 307: item->flags |= CMDQ_FIRED;
1.43 nicm 308:
309: if (retval == CMD_RETURN_WAIT) {
1.44 ! nicm 310: item->flags |= CMDQ_WAITING;
1.43 nicm 311: goto waiting;
312: }
313: items++;
314: }
1.44 ! nicm 315: cmdq_remove(item);
1.43 nicm 316: }
317:
318: log_debug("%s %s: exit (empty)", __func__, name);
319: return (items);
1.1 nicm 320:
1.43 nicm 321: waiting:
322: log_debug("%s %s: exit (wait)", __func__, name);
323: return (items);
324: }
325:
326: /* Print a guard line. */
327: void
1.44 ! nicm 328: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43 nicm 329: {
1.44 ! nicm 330: struct client *c = item->client;
1.43 nicm 331:
332: if (c == NULL || !(c->flags & CLIENT_CONTROL))
333: return;
334:
335: evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
1.44 ! nicm 336: (long)item->time, item->number, flags);
1.43 nicm 337: server_client_push_stdout(c);
1.1 nicm 338: }
339:
340: /* Show message from command. */
1.18 nicm 341: void
1.44 ! nicm 342: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 343: {
1.44 ! nicm 344: struct client *c = item->client;
1.1 nicm 345: struct window *w;
346: va_list ap;
1.28 nicm 347: char *tmp, *msg;
1.1 nicm 348:
349: va_start(ap, fmt);
350:
351: if (c == NULL)
352: /* nothing */;
353: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 354: if (~c->flags & CLIENT_UTF8) {
1.37 nicm 355: xvasprintf(&tmp, fmt, ap);
1.28 nicm 356: msg = utf8_sanitize(tmp);
357: free(tmp);
358: evbuffer_add(c->stdout_data, msg, strlen(msg));
359: free(msg);
360: } else
361: evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1 nicm 362: evbuffer_add(c->stdout_data, "\n", 1);
1.29 nicm 363: server_client_push_stdout(c);
1.1 nicm 364: } else {
365: w = c->session->curw->window;
366: if (w->active->mode != &window_copy_mode) {
367: window_pane_reset_mode(w->active);
368: window_pane_set_mode(w->active, &window_copy_mode);
369: window_copy_init_for_output(w->active);
370: }
371: window_copy_vadd(w->active, fmt, ap);
372: }
373:
374: va_end(ap);
375: }
376:
377: /* Show error from command. */
1.18 nicm 378: void
1.44 ! nicm 379: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 380: {
1.44 ! nicm 381: struct client *c = item->client;
! 382: struct cmd *cmd = item->cmd;
1.1 nicm 383: va_list ap;
1.20 nicm 384: char *msg;
1.1 nicm 385: size_t msglen;
1.28 nicm 386: char *tmp;
1.1 nicm 387:
388: va_start(ap, fmt);
389: msglen = xvasprintf(&msg, fmt, ap);
390: va_end(ap);
391:
1.20 nicm 392: if (c == NULL)
393: cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
394: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 395: if (~c->flags & CLIENT_UTF8) {
396: tmp = msg;
397: msg = utf8_sanitize(tmp);
398: free(tmp);
399: msglen = strlen(msg);
400: }
1.1 nicm 401: evbuffer_add(c->stderr_data, msg, msglen);
402: evbuffer_add(c->stderr_data, "\n", 1);
1.29 nicm 403: server_client_push_stderr(c);
1.13 nicm 404: c->retval = 1;
1.1 nicm 405: } else {
406: *msg = toupper((u_char) *msg);
407: status_message_set(c, "%s", msg);
408: }
409:
410: free(msg);
411: }