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