Annotation of src/usr.bin/tmux/cmd-queue.c, Revision 1.63
1.63 ! nicm 1: /* $OpenBSD: cmd-queue.c,v 1.62 2019/04/17 14:37:48 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:
1.63 ! nicm 101:
! 102: /* Insert a hook. */
! 103: void
! 104: cmdq_insert_hook(struct session *s, struct cmdq_item *item,
! 105: struct cmd_find_state *fs, const char *fmt, ...)
! 106: {
! 107: struct options *oo;
! 108: va_list ap;
! 109: char *name;
! 110: struct cmdq_item *new_item;
! 111: struct options_entry *o;
! 112: struct options_array_item *a;
! 113: struct cmd_list *cmdlist;
! 114:
! 115: if (item->flags & CMDQ_NOHOOKS)
! 116: return;
! 117: if (s == NULL)
! 118: oo = global_s_options;
! 119: else
! 120: oo = s->options;
! 121:
! 122: va_start(ap, fmt);
! 123: xvasprintf(&name, fmt, ap);
! 124: va_end(ap);
! 125:
! 126: o = options_get(oo, name);
! 127: if (o == NULL) {
! 128: free(name);
! 129: return;
! 130: }
! 131: log_debug("running hook %s (parent %p)", name, item);
! 132:
! 133: a = options_array_first(o);
! 134: while (a != NULL) {
! 135: cmdlist = options_array_item_value(a)->cmdlist;
! 136: if (cmdlist == NULL) {
! 137: a = options_array_next(a);
! 138: continue;
! 139: }
! 140:
! 141: new_item = cmdq_get_command(cmdlist, fs, NULL, CMDQ_NOHOOKS);
! 142: cmdq_format(new_item, "hook", "%s", name);
! 143: if (item != NULL) {
! 144: cmdq_insert_after(item, new_item);
! 145: item = new_item;
! 146: } else
! 147: cmdq_append(NULL, new_item);
! 148:
! 149: a = options_array_next(a);
! 150: }
! 151:
! 152: free(name);
! 153: }
! 154:
1.43 nicm 155: /* Remove an item. */
156: static void
1.44 nicm 157: cmdq_remove(struct cmdq_item *item)
1.43 nicm 158: {
1.49 nicm 159: if (item->shared != NULL && --item->shared->references == 0) {
160: if (item->shared->formats != NULL)
161: format_free(item->shared->formats);
162: free(item->shared);
163: }
1.43 nicm 164:
1.44 nicm 165: if (item->client != NULL)
166: server_client_unref(item->client);
1.43 nicm 167:
1.62 nicm 168: if (item->cmdlist != NULL)
1.44 nicm 169: cmd_list_free(item->cmdlist);
1.43 nicm 170:
1.44 nicm 171: TAILQ_REMOVE(item->queue, item, entry);
1.46 nicm 172:
173: free((void *)item->name);
1.44 nicm 174: free(item);
1.43 nicm 175: }
176:
177: /* Set command group. */
178: static u_int
179: cmdq_next_group(void)
180: {
181: static u_int group;
182:
183: return (++group);
184: }
185:
186: /* Remove all subsequent items that match this item's group. */
187: static void
1.44 nicm 188: cmdq_remove_group(struct cmdq_item *item)
1.43 nicm 189: {
1.44 nicm 190: struct cmdq_item *this, *next;
1.43 nicm 191:
1.44 nicm 192: this = TAILQ_NEXT(item, entry);
1.43 nicm 193: while (this != NULL) {
194: next = TAILQ_NEXT(this, entry);
1.44 nicm 195: if (this->group == item->group)
1.43 nicm 196: cmdq_remove(this);
197: this = next;
198: }
199: }
200:
201: /* Get a command for the command queue. */
1.44 nicm 202: struct cmdq_item *
1.43 nicm 203: cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
204: struct mouse_event *m, int flags)
205: {
1.44 nicm 206: struct cmdq_item *item, *first = NULL, *last = NULL;
207: struct cmd *cmd;
208: u_int group = cmdq_next_group();
1.46 nicm 209: char *tmp;
1.49 nicm 210: struct cmdq_shared *shared;
211:
212: shared = xcalloc(1, sizeof *shared);
213: if (current != NULL)
214: cmd_find_copy_state(&shared->current, current);
1.52 nicm 215: else
216: cmd_find_clear_state(&shared->current, 0);
1.49 nicm 217: if (m != NULL)
218: memcpy(&shared->mouse, m, sizeof shared->mouse);
1.43 nicm 219:
220: TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
1.46 nicm 221: xasprintf(&tmp, "command[%s]", cmd->entry->name);
222:
1.44 nicm 223: item = xcalloc(1, sizeof *item);
1.46 nicm 224: item->name = tmp;
1.44 nicm 225: item->type = CMDQ_COMMAND;
1.46 nicm 226:
1.44 nicm 227: item->group = group;
228: item->flags = flags;
1.43 nicm 229:
1.49 nicm 230: item->shared = shared;
1.44 nicm 231: item->cmdlist = cmdlist;
232: item->cmd = cmd;
1.43 nicm 233:
1.49 nicm 234: shared->references++;
1.43 nicm 235: cmdlist->references++;
236:
237: if (first == NULL)
1.44 nicm 238: first = item;
1.43 nicm 239: if (last != NULL)
1.44 nicm 240: last->next = item;
241: last = item;
1.43 nicm 242: }
243: return (first);
244: }
245:
1.54 nicm 246: /* Fill in flag for a command. */
247: static enum cmd_retval
248: cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs,
249: const struct cmd_entry_flag *flag)
250: {
251: const char *value;
252:
253: if (flag->flag == 0) {
254: cmd_find_clear_state(fs, 0);
255: return (CMD_RETURN_NORMAL);
256: }
257:
258: value = args_get(item->cmd->args, flag->flag);
259: if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) {
260: cmd_find_clear_state(fs, 0);
261: return (CMD_RETURN_ERROR);
262: }
263: return (CMD_RETURN_NORMAL);
264: }
265:
1.43 nicm 266: /* Fire command on command queue. */
267: static enum cmd_retval
1.44 nicm 268: cmdq_fire_command(struct cmdq_item *item)
1.43 nicm 269: {
1.44 nicm 270: struct client *c = item->client;
271: struct cmd *cmd = item->cmd;
1.54 nicm 272: const struct cmd_entry *entry = cmd->entry;
1.43 nicm 273: enum cmd_retval retval;
274: struct cmd_find_state *fsp, fs;
275: int flags;
276:
277: flags = !!(cmd->flags & CMD_CONTROL);
1.44 nicm 278: cmdq_guard(item, "begin", flags);
1.43 nicm 279:
1.53 nicm 280: if (item->client == NULL)
281: item->client = cmd_find_client(item, NULL, 1);
1.54 nicm 282: retval = cmdq_find_flag(item, &item->source, &entry->source);
283: if (retval == CMD_RETURN_ERROR)
284: goto out;
285: retval = cmdq_find_flag(item, &item->target, &entry->target);
286: if (retval == CMD_RETURN_ERROR)
1.43 nicm 287: goto out;
288:
1.54 nicm 289: retval = entry->exec(cmd, item);
1.43 nicm 290: if (retval == CMD_RETURN_ERROR)
291: goto out;
292:
1.54 nicm 293: if (entry->flags & CMD_AFTERHOOK) {
294: if (cmd_find_valid_state(&item->target))
295: fsp = &item->target;
1.51 nicm 296: else if (cmd_find_valid_state(&item->shared->current))
297: fsp = &item->shared->current;
1.58 nicm 298: else if (cmd_find_from_client(&fs, item->client, 0) == 0)
1.43 nicm 299: fsp = &fs;
1.51 nicm 300: else
301: goto out;
1.63 ! nicm 302: cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name);
1.43 nicm 303: }
304:
305: out:
1.44 nicm 306: item->client = c;
1.43 nicm 307: if (retval == CMD_RETURN_ERROR)
1.44 nicm 308: cmdq_guard(item, "error", flags);
1.43 nicm 309: else
1.44 nicm 310: cmdq_guard(item, "end", flags);
1.43 nicm 311: return (retval);
312: }
313:
314: /* Get a callback for the command queue. */
1.44 nicm 315: struct cmdq_item *
1.46 nicm 316: cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
1.1 nicm 317: {
1.44 nicm 318: struct cmdq_item *item;
1.46 nicm 319: char *tmp;
320:
321: xasprintf(&tmp, "callback[%s]", name);
1.1 nicm 322:
1.44 nicm 323: item = xcalloc(1, sizeof *item);
1.46 nicm 324: item->name = tmp;
1.44 nicm 325: item->type = CMDQ_CALLBACK;
1.46 nicm 326:
1.44 nicm 327: item->group = 0;
328: item->flags = 0;
1.1 nicm 329:
1.44 nicm 330: item->cb = cb;
331: item->data = data;
1.1 nicm 332:
1.44 nicm 333: return (item);
1.43 nicm 334: }
1.1 nicm 335:
1.43 nicm 336: /* Fire callback on callback queue. */
337: static enum cmd_retval
1.44 nicm 338: cmdq_fire_callback(struct cmdq_item *item)
1.43 nicm 339: {
1.44 nicm 340: return (item->cb(item, item->data));
1.43 nicm 341: }
1.45 nicm 342:
343: /* Add a format to command queue. */
344: void
345: cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...)
346: {
1.49 nicm 347: struct cmdq_shared *shared = item->shared;
1.45 nicm 348: va_list ap;
349: char *value;
350:
351: va_start(ap, fmt);
352: xvasprintf(&value, fmt, ap);
353: va_end(ap);
354:
1.49 nicm 355: if (shared->formats == NULL)
1.55 nicm 356: shared->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
1.49 nicm 357: format_add(shared->formats, key, "%s", value);
1.45 nicm 358:
359: free(value);
360: }
1.33 nicm 361:
1.43 nicm 362: /* Process next item on command queue. */
363: u_int
364: cmdq_next(struct client *c)
365: {
1.44 nicm 366: struct cmdq_list *queue = cmdq_get(c);
1.43 nicm 367: const char *name = cmdq_name(c);
1.44 nicm 368: struct cmdq_item *item;
1.43 nicm 369: enum cmd_retval retval;
370: u_int items = 0;
371: static u_int number;
1.1 nicm 372:
1.43 nicm 373: if (TAILQ_EMPTY(queue)) {
374: log_debug("%s %s: empty", __func__, name);
1.25 nicm 375: return (0);
376: }
1.44 nicm 377: if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43 nicm 378: log_debug("%s %s: waiting", __func__, name);
379: return (0);
380: }
381:
382: log_debug("%s %s: enter", __func__, name);
383: for (;;) {
1.44 nicm 384: item = TAILQ_FIRST(queue);
385: if (item == NULL)
1.43 nicm 386: break;
1.46 nicm 387: log_debug("%s %s: %s (%d), flags %x", __func__, name,
388: item->name, item->type, item->flags);
1.43 nicm 389:
390: /*
391: * Any item with the waiting flag set waits until an external
392: * event clears the flag (for example, a job - look at
393: * run-shell).
394: */
1.44 nicm 395: if (item->flags & CMDQ_WAITING)
1.43 nicm 396: goto waiting;
397:
398: /*
399: * Items are only fired once, once the fired flag is set, a
400: * waiting flag can only be cleared by an external event.
401: */
1.44 nicm 402: if (~item->flags & CMDQ_FIRED) {
403: item->time = time(NULL);
404: item->number = ++number;
1.43 nicm 405:
1.50 nicm 406: switch (item->type) {
1.44 nicm 407: case CMDQ_COMMAND:
408: retval = cmdq_fire_command(item);
1.43 nicm 409:
410: /*
411: * If a command returns an error, remove any
412: * subsequent commands in the same group.
413: */
414: if (retval == CMD_RETURN_ERROR)
1.44 nicm 415: cmdq_remove_group(item);
1.43 nicm 416: break;
1.44 nicm 417: case CMDQ_CALLBACK:
418: retval = cmdq_fire_callback(item);
1.43 nicm 419: break;
420: default:
421: retval = CMD_RETURN_ERROR;
422: break;
423: }
1.44 nicm 424: item->flags |= CMDQ_FIRED;
1.43 nicm 425:
426: if (retval == CMD_RETURN_WAIT) {
1.44 nicm 427: item->flags |= CMDQ_WAITING;
1.43 nicm 428: goto waiting;
429: }
430: items++;
431: }
1.44 nicm 432: cmdq_remove(item);
1.43 nicm 433: }
434:
435: log_debug("%s %s: exit (empty)", __func__, name);
436: return (items);
1.1 nicm 437:
1.43 nicm 438: waiting:
439: log_debug("%s %s: exit (wait)", __func__, name);
440: return (items);
441: }
442:
443: /* Print a guard line. */
444: void
1.44 nicm 445: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43 nicm 446: {
1.44 nicm 447: struct client *c = item->client;
1.43 nicm 448:
449: if (c == NULL || !(c->flags & CLIENT_CONTROL))
450: return;
451:
452: evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
1.44 nicm 453: (long)item->time, item->number, flags);
1.43 nicm 454: server_client_push_stdout(c);
1.1 nicm 455: }
456:
457: /* Show message from command. */
1.18 nicm 458: void
1.44 nicm 459: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 460: {
1.61 nicm 461: struct client *c = item->client;
462: struct window_pane *wp;
463: struct window_mode_entry *wme;
464: va_list ap;
465: char *tmp, *msg;
1.1 nicm 466:
467: va_start(ap, fmt);
468:
469: if (c == NULL)
470: /* nothing */;
471: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 472: if (~c->flags & CLIENT_UTF8) {
1.37 nicm 473: xvasprintf(&tmp, fmt, ap);
1.28 nicm 474: msg = utf8_sanitize(tmp);
475: free(tmp);
476: evbuffer_add(c->stdout_data, msg, strlen(msg));
477: free(msg);
478: } else
479: evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1 nicm 480: evbuffer_add(c->stdout_data, "\n", 1);
1.29 nicm 481: server_client_push_stdout(c);
1.1 nicm 482: } else {
1.60 nicm 483: wp = c->session->curw->window->active;
1.61 nicm 484: wme = TAILQ_FIRST(&wp->modes);
485: if (wme == NULL || wme->mode != &window_view_mode)
1.60 nicm 486: window_pane_set_mode(wp, &window_view_mode, NULL, NULL);
487: window_copy_vadd(wp, fmt, ap);
1.1 nicm 488: }
489:
490: va_end(ap);
491: }
492:
493: /* Show error from command. */
1.18 nicm 494: void
1.44 nicm 495: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 496: {
1.44 nicm 497: struct client *c = item->client;
498: struct cmd *cmd = item->cmd;
1.1 nicm 499: va_list ap;
1.20 nicm 500: char *msg;
1.1 nicm 501: size_t msglen;
1.28 nicm 502: char *tmp;
1.1 nicm 503:
504: va_start(ap, fmt);
505: msglen = xvasprintf(&msg, fmt, ap);
506: va_end(ap);
1.57 nicm 507:
508: log_debug("%s: %s", __func__, msg);
1.1 nicm 509:
1.20 nicm 510: if (c == NULL)
511: cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
512: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 513: if (~c->flags & CLIENT_UTF8) {
514: tmp = msg;
515: msg = utf8_sanitize(tmp);
516: free(tmp);
517: msglen = strlen(msg);
518: }
1.1 nicm 519: evbuffer_add(c->stderr_data, msg, msglen);
520: evbuffer_add(c->stderr_data, "\n", 1);
1.29 nicm 521: server_client_push_stderr(c);
1.13 nicm 522: c->retval = 1;
1.1 nicm 523: } else {
524: *msg = toupper((u_char) *msg);
525: status_message_set(c, "%s", msg);
526: }
527:
528: free(msg);
529: }