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