Annotation of src/usr.bin/tmux/cmd-queue.c, Revision 1.45
1.45 ! nicm 1: /* $OpenBSD: cmd-queue.c,v 1.44 2016/10/16 19:04:05 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.45 ! nicm 105: if (item->formats != NULL)
! 106: format_free(item->formats);
1.43 nicm 107:
1.44 nicm 108: if (item->client != NULL)
109: server_client_unref(item->client);
1.43 nicm 110:
1.44 nicm 111: if (item->type == CMDQ_COMMAND)
112: cmd_list_free(item->cmdlist);
1.43 nicm 113:
1.44 nicm 114: TAILQ_REMOVE(item->queue, item, entry);
115: free(item);
1.43 nicm 116: }
117:
118: /* Set command group. */
119: static u_int
120: cmdq_next_group(void)
121: {
122: static u_int group;
123:
124: return (++group);
125: }
126:
127: /* Remove all subsequent items that match this item's group. */
128: static void
1.44 nicm 129: cmdq_remove_group(struct cmdq_item *item)
1.43 nicm 130: {
1.44 nicm 131: struct cmdq_item *this, *next;
1.43 nicm 132:
1.44 nicm 133: this = TAILQ_NEXT(item, entry);
1.43 nicm 134: while (this != NULL) {
135: next = TAILQ_NEXT(this, entry);
1.44 nicm 136: if (this->group == item->group)
1.43 nicm 137: cmdq_remove(this);
138: this = next;
139: }
140: }
141:
142: /* Get a command for the command queue. */
1.44 nicm 143: struct cmdq_item *
1.43 nicm 144: cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
145: struct mouse_event *m, int flags)
146: {
1.44 nicm 147: struct cmdq_item *item, *first = NULL, *last = NULL;
148: struct cmd *cmd;
149: u_int group = cmdq_next_group();
1.43 nicm 150:
151: TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
1.44 nicm 152: item = xcalloc(1, sizeof *item);
153: item->type = CMDQ_COMMAND;
154: item->group = group;
155: item->flags = flags;
1.43 nicm 156:
1.44 nicm 157: item->cmdlist = cmdlist;
158: item->cmd = cmd;
1.43 nicm 159:
160: if (current != NULL)
1.44 nicm 161: cmd_find_copy_state(&item->current, current);
1.43 nicm 162: if (m != NULL)
1.44 nicm 163: memcpy(&item->mouse, m, sizeof item->mouse);
1.43 nicm 164: cmdlist->references++;
165:
166: if (first == NULL)
1.44 nicm 167: first = item;
1.43 nicm 168: if (last != NULL)
1.44 nicm 169: last->next = item;
170: last = item;
1.43 nicm 171: }
172: return (first);
173: }
174:
175: /* Fire command on command queue. */
176: static enum cmd_retval
1.44 nicm 177: cmdq_fire_command(struct cmdq_item *item)
1.43 nicm 178: {
1.44 nicm 179: struct client *c = item->client;
180: struct cmd *cmd = item->cmd;
1.43 nicm 181: enum cmd_retval retval;
182: const char *name;
183: struct cmd_find_state *fsp, fs;
184: int flags;
185:
186: flags = !!(cmd->flags & CMD_CONTROL);
1.44 nicm 187: cmdq_guard(item, "begin", flags);
1.43 nicm 188:
1.44 nicm 189: if (cmd_prepare_state(cmd, item) != 0) {
1.43 nicm 190: retval = CMD_RETURN_ERROR;
191: goto out;
192: }
1.44 nicm 193: if (item->client == NULL)
194: item->client = cmd_find_client(item, NULL, CMD_FIND_QUIET);
1.43 nicm 195:
1.44 nicm 196: retval = cmd->entry->exec(cmd, item);
1.43 nicm 197: if (retval == CMD_RETURN_ERROR)
198: goto out;
199:
200: if (cmd->entry->flags & CMD_AFTERHOOK) {
201: name = cmd->entry->name;
1.44 nicm 202: if (cmd_find_valid_state(&item->state.tflag))
203: fsp = &item->state.tflag;
1.43 nicm 204: else {
1.44 nicm 205: if (cmd_find_current(&fs, item, CMD_FIND_QUIET) != 0)
1.43 nicm 206: goto out;
207: fsp = &fs;
208: }
1.44 nicm 209: hooks_insert(fsp->s->hooks, item, fsp, "after-%s", name);
1.43 nicm 210: }
211:
212: out:
1.44 nicm 213: item->client = c;
1.43 nicm 214: if (retval == CMD_RETURN_ERROR)
1.44 nicm 215: cmdq_guard(item, "error", flags);
1.43 nicm 216: else
1.44 nicm 217: cmdq_guard(item, "end", flags);
1.43 nicm 218: return (retval);
219: }
220:
221: /* Get a callback for the command queue. */
1.44 nicm 222: struct cmdq_item *
223: cmdq_get_callback(cmdq_cb cb, void *data)
1.1 nicm 224: {
1.44 nicm 225: struct cmdq_item *item;
1.1 nicm 226:
1.44 nicm 227: item = xcalloc(1, sizeof *item);
228: item->type = CMDQ_CALLBACK;
229: item->group = 0;
230: item->flags = 0;
1.1 nicm 231:
1.44 nicm 232: item->cb = cb;
233: item->data = data;
1.1 nicm 234:
1.44 nicm 235: return (item);
1.43 nicm 236: }
1.1 nicm 237:
1.43 nicm 238: /* Fire callback on callback queue. */
239: static enum cmd_retval
1.44 nicm 240: cmdq_fire_callback(struct cmdq_item *item)
1.43 nicm 241: {
1.44 nicm 242: return (item->cb(item, item->data));
1.43 nicm 243: }
1.45 ! nicm 244:
! 245: /* Add a format to command queue. */
! 246: void
! 247: cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...)
! 248: {
! 249: va_list ap;
! 250: struct cmdq_item *loop;
! 251: char *value;
! 252:
! 253: va_start(ap, fmt);
! 254: xvasprintf(&value, fmt, ap);
! 255: va_end(ap);
! 256:
! 257: for (loop = item; loop != NULL; loop = item->next) {
! 258: if (loop->formats == NULL)
! 259: loop->formats = format_create(NULL, 0);
! 260: format_add(loop->formats, key, "%s", value);
! 261: }
! 262:
! 263: free(value);
! 264: }
! 265:
1.33 nicm 266:
1.43 nicm 267: /* Process next item on command queue. */
268: u_int
269: cmdq_next(struct client *c)
270: {
1.44 nicm 271: struct cmdq_list *queue = cmdq_get(c);
1.43 nicm 272: const char *name = cmdq_name(c);
1.44 nicm 273: struct cmdq_item *item;
1.43 nicm 274: enum cmd_retval retval;
275: u_int items = 0;
276: static u_int number;
1.1 nicm 277:
1.43 nicm 278: if (TAILQ_EMPTY(queue)) {
279: log_debug("%s %s: empty", __func__, name);
1.25 nicm 280: return (0);
281: }
1.44 nicm 282: if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43 nicm 283: log_debug("%s %s: waiting", __func__, name);
284: return (0);
285: }
286:
287: log_debug("%s %s: enter", __func__, name);
288: for (;;) {
1.44 nicm 289: item = TAILQ_FIRST(queue);
290: if (item == NULL)
1.43 nicm 291: break;
292: log_debug("%s %s: type %d, flags %x", __func__, name,
1.44 nicm 293: item->type, item->flags);
1.43 nicm 294:
295: /*
296: * Any item with the waiting flag set waits until an external
297: * event clears the flag (for example, a job - look at
298: * run-shell).
299: */
1.44 nicm 300: if (item->flags & CMDQ_WAITING)
1.43 nicm 301: goto waiting;
302:
303: /*
304: * Items are only fired once, once the fired flag is set, a
305: * waiting flag can only be cleared by an external event.
306: */
1.44 nicm 307: if (~item->flags & CMDQ_FIRED) {
308: item->time = time(NULL);
309: item->number = ++number;
1.43 nicm 310:
1.44 nicm 311: switch (item->type)
1.43 nicm 312: {
1.44 nicm 313: case CMDQ_COMMAND:
314: retval = cmdq_fire_command(item);
1.43 nicm 315:
316: /*
317: * If a command returns an error, remove any
318: * subsequent commands in the same group.
319: */
320: if (retval == CMD_RETURN_ERROR)
1.44 nicm 321: cmdq_remove_group(item);
1.43 nicm 322: break;
1.44 nicm 323: case CMDQ_CALLBACK:
324: retval = cmdq_fire_callback(item);
1.43 nicm 325: break;
326: default:
327: retval = CMD_RETURN_ERROR;
328: break;
329: }
1.44 nicm 330: item->flags |= CMDQ_FIRED;
1.43 nicm 331:
332: if (retval == CMD_RETURN_WAIT) {
1.44 nicm 333: item->flags |= CMDQ_WAITING;
1.43 nicm 334: goto waiting;
335: }
336: items++;
337: }
1.44 nicm 338: cmdq_remove(item);
1.43 nicm 339: }
340:
341: log_debug("%s %s: exit (empty)", __func__, name);
342: return (items);
1.1 nicm 343:
1.43 nicm 344: waiting:
345: log_debug("%s %s: exit (wait)", __func__, name);
346: return (items);
347: }
348:
349: /* Print a guard line. */
350: void
1.44 nicm 351: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43 nicm 352: {
1.44 nicm 353: struct client *c = item->client;
1.43 nicm 354:
355: if (c == NULL || !(c->flags & CLIENT_CONTROL))
356: return;
357:
358: evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
1.44 nicm 359: (long)item->time, item->number, flags);
1.43 nicm 360: server_client_push_stdout(c);
1.1 nicm 361: }
362:
363: /* Show message from command. */
1.18 nicm 364: void
1.44 nicm 365: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 366: {
1.44 nicm 367: struct client *c = item->client;
1.1 nicm 368: struct window *w;
369: va_list ap;
1.28 nicm 370: char *tmp, *msg;
1.1 nicm 371:
372: va_start(ap, fmt);
373:
374: if (c == NULL)
375: /* nothing */;
376: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 377: if (~c->flags & CLIENT_UTF8) {
1.37 nicm 378: xvasprintf(&tmp, fmt, ap);
1.28 nicm 379: msg = utf8_sanitize(tmp);
380: free(tmp);
381: evbuffer_add(c->stdout_data, msg, strlen(msg));
382: free(msg);
383: } else
384: evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1 nicm 385: evbuffer_add(c->stdout_data, "\n", 1);
1.29 nicm 386: server_client_push_stdout(c);
1.1 nicm 387: } else {
388: w = c->session->curw->window;
389: if (w->active->mode != &window_copy_mode) {
390: window_pane_reset_mode(w->active);
391: window_pane_set_mode(w->active, &window_copy_mode);
392: window_copy_init_for_output(w->active);
393: }
394: window_copy_vadd(w->active, fmt, ap);
395: }
396:
397: va_end(ap);
398: }
399:
400: /* Show error from command. */
1.18 nicm 401: void
1.44 nicm 402: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 403: {
1.44 nicm 404: struct client *c = item->client;
405: struct cmd *cmd = item->cmd;
1.1 nicm 406: va_list ap;
1.20 nicm 407: char *msg;
1.1 nicm 408: size_t msglen;
1.28 nicm 409: char *tmp;
1.1 nicm 410:
411: va_start(ap, fmt);
412: msglen = xvasprintf(&msg, fmt, ap);
413: va_end(ap);
414:
1.20 nicm 415: if (c == NULL)
416: cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
417: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 418: if (~c->flags & CLIENT_UTF8) {
419: tmp = msg;
420: msg = utf8_sanitize(tmp);
421: free(tmp);
422: msglen = strlen(msg);
423: }
1.1 nicm 424: evbuffer_add(c->stderr_data, msg, msglen);
425: evbuffer_add(c->stderr_data, "\n", 1);
1.29 nicm 426: server_client_push_stderr(c);
1.13 nicm 427: c->retval = 1;
1.1 nicm 428: } else {
429: *msg = toupper((u_char) *msg);
430: status_message_set(c, "%s", msg);
431: }
432:
433: free(msg);
434: }