Annotation of src/usr.bin/tmux/cmd-queue.c, Revision 1.47
1.47 ! nicm 1: /* $OpenBSD: cmd-queue.c,v 1.46 2016/10/18 08:46:43 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: }
1.33 nicm 277:
1.43 nicm 278: /* Process next item on command queue. */
279: u_int
280: cmdq_next(struct client *c)
281: {
1.44 nicm 282: struct cmdq_list *queue = cmdq_get(c);
1.43 nicm 283: const char *name = cmdq_name(c);
1.44 nicm 284: struct cmdq_item *item;
1.43 nicm 285: enum cmd_retval retval;
286: u_int items = 0;
287: static u_int number;
1.1 nicm 288:
1.43 nicm 289: if (TAILQ_EMPTY(queue)) {
290: log_debug("%s %s: empty", __func__, name);
1.25 nicm 291: return (0);
292: }
1.44 nicm 293: if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43 nicm 294: log_debug("%s %s: waiting", __func__, name);
295: return (0);
296: }
297:
298: log_debug("%s %s: enter", __func__, name);
299: for (;;) {
1.44 nicm 300: item = TAILQ_FIRST(queue);
301: if (item == NULL)
1.43 nicm 302: break;
1.46 nicm 303: log_debug("%s %s: %s (%d), flags %x", __func__, name,
304: item->name, item->type, item->flags);
1.43 nicm 305:
306: /*
307: * Any item with the waiting flag set waits until an external
308: * event clears the flag (for example, a job - look at
309: * run-shell).
310: */
1.44 nicm 311: if (item->flags & CMDQ_WAITING)
1.43 nicm 312: goto waiting;
313:
314: /*
315: * Items are only fired once, once the fired flag is set, a
316: * waiting flag can only be cleared by an external event.
317: */
1.44 nicm 318: if (~item->flags & CMDQ_FIRED) {
319: item->time = time(NULL);
320: item->number = ++number;
1.43 nicm 321:
1.44 nicm 322: switch (item->type)
1.43 nicm 323: {
1.44 nicm 324: case CMDQ_COMMAND:
325: retval = cmdq_fire_command(item);
1.43 nicm 326:
327: /*
328: * If a command returns an error, remove any
329: * subsequent commands in the same group.
330: */
331: if (retval == CMD_RETURN_ERROR)
1.44 nicm 332: cmdq_remove_group(item);
1.43 nicm 333: break;
1.44 nicm 334: case CMDQ_CALLBACK:
335: retval = cmdq_fire_callback(item);
1.43 nicm 336: break;
337: default:
338: retval = CMD_RETURN_ERROR;
339: break;
340: }
1.44 nicm 341: item->flags |= CMDQ_FIRED;
1.43 nicm 342:
343: if (retval == CMD_RETURN_WAIT) {
1.44 nicm 344: item->flags |= CMDQ_WAITING;
1.43 nicm 345: goto waiting;
346: }
347: items++;
348: }
1.44 nicm 349: cmdq_remove(item);
1.43 nicm 350: }
351:
352: log_debug("%s %s: exit (empty)", __func__, name);
353: return (items);
1.1 nicm 354:
1.43 nicm 355: waiting:
356: log_debug("%s %s: exit (wait)", __func__, name);
357: return (items);
358: }
359:
360: /* Print a guard line. */
361: void
1.44 nicm 362: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43 nicm 363: {
1.44 nicm 364: struct client *c = item->client;
1.43 nicm 365:
366: if (c == NULL || !(c->flags & CLIENT_CONTROL))
367: return;
368:
369: evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
1.44 nicm 370: (long)item->time, item->number, flags);
1.43 nicm 371: server_client_push_stdout(c);
1.1 nicm 372: }
373:
374: /* Show message from command. */
1.18 nicm 375: void
1.44 nicm 376: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 377: {
1.44 nicm 378: struct client *c = item->client;
1.1 nicm 379: struct window *w;
380: va_list ap;
1.28 nicm 381: char *tmp, *msg;
1.1 nicm 382:
383: va_start(ap, fmt);
384:
385: if (c == NULL)
386: /* nothing */;
387: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 388: if (~c->flags & CLIENT_UTF8) {
1.37 nicm 389: xvasprintf(&tmp, fmt, ap);
1.28 nicm 390: msg = utf8_sanitize(tmp);
391: free(tmp);
392: evbuffer_add(c->stdout_data, msg, strlen(msg));
393: free(msg);
394: } else
395: evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1 nicm 396: evbuffer_add(c->stdout_data, "\n", 1);
1.29 nicm 397: server_client_push_stdout(c);
1.1 nicm 398: } else {
399: w = c->session->curw->window;
400: if (w->active->mode != &window_copy_mode) {
401: window_pane_reset_mode(w->active);
402: window_pane_set_mode(w->active, &window_copy_mode);
403: window_copy_init_for_output(w->active);
404: }
405: window_copy_vadd(w->active, fmt, ap);
406: }
407:
408: va_end(ap);
409: }
410:
411: /* Show error from command. */
1.18 nicm 412: void
1.44 nicm 413: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 414: {
1.44 nicm 415: struct client *c = item->client;
416: struct cmd *cmd = item->cmd;
1.1 nicm 417: va_list ap;
1.20 nicm 418: char *msg;
1.1 nicm 419: size_t msglen;
1.28 nicm 420: char *tmp;
1.1 nicm 421:
422: va_start(ap, fmt);
423: msglen = xvasprintf(&msg, fmt, ap);
424: va_end(ap);
425:
1.20 nicm 426: if (c == NULL)
427: cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
428: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 429: if (~c->flags & CLIENT_UTF8) {
430: tmp = msg;
431: msg = utf8_sanitize(tmp);
432: free(tmp);
433: msglen = strlen(msg);
434: }
1.1 nicm 435: evbuffer_add(c->stderr_data, msg, msglen);
436: evbuffer_add(c->stderr_data, "\n", 1);
1.29 nicm 437: server_client_push_stderr(c);
1.13 nicm 438: c->retval = 1;
1.1 nicm 439: } else {
440: *msg = toupper((u_char) *msg);
441: status_message_set(c, "%s", msg);
442: }
443:
444: free(msg);
445: }