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