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