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