Annotation of src/usr.bin/tmux/cmd-queue.c, Revision 1.52
1.52 ! nicm 1: /* $OpenBSD: cmd-queue.c,v 1.51 2017/04/21 20:26:34 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:
192: /* Fire command on command queue. */
193: static enum cmd_retval
1.44 nicm 194: cmdq_fire_command(struct cmdq_item *item)
1.43 nicm 195: {
1.44 nicm 196: struct client *c = item->client;
197: struct cmd *cmd = item->cmd;
1.43 nicm 198: enum cmd_retval retval;
199: const char *name;
200: struct cmd_find_state *fsp, fs;
201: int flags;
202:
203: flags = !!(cmd->flags & CMD_CONTROL);
1.44 nicm 204: cmdq_guard(item, "begin", flags);
1.43 nicm 205:
1.44 nicm 206: if (cmd_prepare_state(cmd, item) != 0) {
1.43 nicm 207: retval = CMD_RETURN_ERROR;
208: goto out;
209: }
1.52 ! nicm 210:
1.44 nicm 211: if (item->client == NULL)
1.50 nicm 212: item->client = cmd_find_client(item, NULL, 1);
1.43 nicm 213:
1.44 nicm 214: retval = cmd->entry->exec(cmd, item);
1.43 nicm 215: if (retval == CMD_RETURN_ERROR)
216: goto out;
217:
218: if (cmd->entry->flags & CMD_AFTERHOOK) {
219: name = cmd->entry->name;
1.44 nicm 220: if (cmd_find_valid_state(&item->state.tflag))
221: fsp = &item->state.tflag;
1.51 nicm 222: else if (cmd_find_valid_state(&item->shared->current))
223: fsp = &item->shared->current;
224: else if (cmd_find_from_client(&fs, item->client) == 0)
1.43 nicm 225: fsp = &fs;
1.51 nicm 226: else
227: goto out;
1.44 nicm 228: hooks_insert(fsp->s->hooks, item, fsp, "after-%s", name);
1.43 nicm 229: }
230:
231: out:
1.44 nicm 232: item->client = c;
1.43 nicm 233: if (retval == CMD_RETURN_ERROR)
1.44 nicm 234: cmdq_guard(item, "error", flags);
1.43 nicm 235: else
1.44 nicm 236: cmdq_guard(item, "end", flags);
1.43 nicm 237: return (retval);
238: }
239:
240: /* Get a callback for the command queue. */
1.44 nicm 241: struct cmdq_item *
1.46 nicm 242: cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
1.1 nicm 243: {
1.44 nicm 244: struct cmdq_item *item;
1.46 nicm 245: char *tmp;
246:
247: xasprintf(&tmp, "callback[%s]", name);
1.1 nicm 248:
1.44 nicm 249: item = xcalloc(1, sizeof *item);
1.46 nicm 250: item->name = tmp;
1.44 nicm 251: item->type = CMDQ_CALLBACK;
1.46 nicm 252:
1.44 nicm 253: item->group = 0;
254: item->flags = 0;
1.1 nicm 255:
1.44 nicm 256: item->cb = cb;
257: item->data = data;
1.1 nicm 258:
1.44 nicm 259: return (item);
1.43 nicm 260: }
1.1 nicm 261:
1.43 nicm 262: /* Fire callback on callback queue. */
263: static enum cmd_retval
1.44 nicm 264: cmdq_fire_callback(struct cmdq_item *item)
1.43 nicm 265: {
1.44 nicm 266: return (item->cb(item, item->data));
1.43 nicm 267: }
1.45 nicm 268:
269: /* Add a format to command queue. */
270: void
271: cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...)
272: {
1.49 nicm 273: struct cmdq_shared *shared = item->shared;
1.45 nicm 274: va_list ap;
275: char *value;
276:
277: va_start(ap, fmt);
278: xvasprintf(&value, fmt, ap);
279: va_end(ap);
280:
1.49 nicm 281: if (shared->formats == NULL)
282: shared->formats = format_create(NULL, FORMAT_NONE, 0);
283: format_add(shared->formats, key, "%s", value);
1.45 nicm 284:
285: free(value);
286: }
1.33 nicm 287:
1.43 nicm 288: /* Process next item on command queue. */
289: u_int
290: cmdq_next(struct client *c)
291: {
1.44 nicm 292: struct cmdq_list *queue = cmdq_get(c);
1.43 nicm 293: const char *name = cmdq_name(c);
1.44 nicm 294: struct cmdq_item *item;
1.43 nicm 295: enum cmd_retval retval;
296: u_int items = 0;
297: static u_int number;
1.1 nicm 298:
1.43 nicm 299: if (TAILQ_EMPTY(queue)) {
300: log_debug("%s %s: empty", __func__, name);
1.25 nicm 301: return (0);
302: }
1.44 nicm 303: if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43 nicm 304: log_debug("%s %s: waiting", __func__, name);
305: return (0);
306: }
307:
308: log_debug("%s %s: enter", __func__, name);
309: for (;;) {
1.44 nicm 310: item = TAILQ_FIRST(queue);
311: if (item == NULL)
1.43 nicm 312: break;
1.46 nicm 313: log_debug("%s %s: %s (%d), flags %x", __func__, name,
314: item->name, item->type, item->flags);
1.43 nicm 315:
316: /*
317: * Any item with the waiting flag set waits until an external
318: * event clears the flag (for example, a job - look at
319: * run-shell).
320: */
1.44 nicm 321: if (item->flags & CMDQ_WAITING)
1.43 nicm 322: goto waiting;
323:
324: /*
325: * Items are only fired once, once the fired flag is set, a
326: * waiting flag can only be cleared by an external event.
327: */
1.44 nicm 328: if (~item->flags & CMDQ_FIRED) {
329: item->time = time(NULL);
330: item->number = ++number;
1.43 nicm 331:
1.50 nicm 332: switch (item->type) {
1.44 nicm 333: case CMDQ_COMMAND:
334: retval = cmdq_fire_command(item);
1.43 nicm 335:
336: /*
337: * If a command returns an error, remove any
338: * subsequent commands in the same group.
339: */
340: if (retval == CMD_RETURN_ERROR)
1.44 nicm 341: cmdq_remove_group(item);
1.43 nicm 342: break;
1.44 nicm 343: case CMDQ_CALLBACK:
344: retval = cmdq_fire_callback(item);
1.43 nicm 345: break;
346: default:
347: retval = CMD_RETURN_ERROR;
348: break;
349: }
1.44 nicm 350: item->flags |= CMDQ_FIRED;
1.43 nicm 351:
352: if (retval == CMD_RETURN_WAIT) {
1.44 nicm 353: item->flags |= CMDQ_WAITING;
1.43 nicm 354: goto waiting;
355: }
356: items++;
357: }
1.44 nicm 358: cmdq_remove(item);
1.43 nicm 359: }
360:
361: log_debug("%s %s: exit (empty)", __func__, name);
362: return (items);
1.1 nicm 363:
1.43 nicm 364: waiting:
365: log_debug("%s %s: exit (wait)", __func__, name);
366: return (items);
367: }
368:
369: /* Print a guard line. */
370: void
1.44 nicm 371: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43 nicm 372: {
1.44 nicm 373: struct client *c = item->client;
1.43 nicm 374:
375: if (c == NULL || !(c->flags & CLIENT_CONTROL))
376: return;
377:
378: evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
1.44 nicm 379: (long)item->time, item->number, flags);
1.43 nicm 380: server_client_push_stdout(c);
1.1 nicm 381: }
382:
383: /* Show message from command. */
1.18 nicm 384: void
1.44 nicm 385: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 386: {
1.44 nicm 387: struct client *c = item->client;
1.1 nicm 388: struct window *w;
389: va_list ap;
1.28 nicm 390: char *tmp, *msg;
1.1 nicm 391:
392: va_start(ap, fmt);
393:
394: if (c == NULL)
395: /* nothing */;
396: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 397: if (~c->flags & CLIENT_UTF8) {
1.37 nicm 398: xvasprintf(&tmp, fmt, ap);
1.28 nicm 399: msg = utf8_sanitize(tmp);
400: free(tmp);
401: evbuffer_add(c->stdout_data, msg, strlen(msg));
402: free(msg);
403: } else
404: evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1 nicm 405: evbuffer_add(c->stdout_data, "\n", 1);
1.29 nicm 406: server_client_push_stdout(c);
1.1 nicm 407: } else {
408: w = c->session->curw->window;
409: if (w->active->mode != &window_copy_mode) {
410: window_pane_reset_mode(w->active);
411: window_pane_set_mode(w->active, &window_copy_mode);
412: window_copy_init_for_output(w->active);
413: }
414: window_copy_vadd(w->active, fmt, ap);
415: }
416:
417: va_end(ap);
418: }
419:
420: /* Show error from command. */
1.18 nicm 421: void
1.44 nicm 422: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 423: {
1.44 nicm 424: struct client *c = item->client;
425: struct cmd *cmd = item->cmd;
1.1 nicm 426: va_list ap;
1.20 nicm 427: char *msg;
1.1 nicm 428: size_t msglen;
1.28 nicm 429: char *tmp;
1.1 nicm 430:
431: va_start(ap, fmt);
432: msglen = xvasprintf(&msg, fmt, ap);
433: va_end(ap);
434:
1.20 nicm 435: if (c == NULL)
436: cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
437: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 438: if (~c->flags & CLIENT_UTF8) {
439: tmp = msg;
440: msg = utf8_sanitize(tmp);
441: free(tmp);
442: msglen = strlen(msg);
443: }
1.1 nicm 444: evbuffer_add(c->stderr_data, msg, msglen);
445: evbuffer_add(c->stderr_data, "\n", 1);
1.29 nicm 446: server_client_push_stderr(c);
1.13 nicm 447: c->retval = 1;
1.1 nicm 448: } else {
449: *msg = toupper((u_char) *msg);
450: status_message_set(c, "%s", msg);
451: }
452:
453: free(msg);
454: }