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