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