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