Annotation of src/usr.bin/tmux/cmd-queue.c, Revision 1.88
1.88 ! nicm 1: /* $OpenBSD: cmd-queue.c,v 1.87 2020/04/13 14:46:04 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.84 nicm 28: /* Command queue flags. */
29: #define CMDQ_FIRED 0x1
30: #define CMDQ_WAITING 0x2
31:
1.83 nicm 32: /* Command queue item type. */
33: enum cmdq_type {
34: CMDQ_COMMAND,
35: CMDQ_CALLBACK,
36: };
37:
38: /* Command queue item. */
39: struct cmdq_item {
40: char *name;
41: struct cmdq_list *queue;
42: struct cmdq_item *next;
43:
44: struct client *client;
45:
46: enum cmdq_type type;
47: u_int group;
48:
49: u_int number;
50: time_t time;
51:
52: int flags;
53:
1.86 nicm 54: struct cmdq_state *state;
1.83 nicm 55: struct cmd_find_state source;
56: struct cmd_find_state target;
57:
58: struct cmd_list *cmdlist;
59: struct cmd *cmd;
60:
61: cmdq_cb cb;
62: void *data;
63:
64: TAILQ_ENTRY(cmdq_item) entry;
65: };
66: TAILQ_HEAD(cmdq_list, cmdq_item);
1.24 nicm 67:
1.87 nicm 68: /*
69: * Command queue state. This is the context for commands on the command queue.
70: * It holds information about how the commands were fired (the key and flags),
71: * any additional formats for the commands, and the current default target.
72: * Multiple commands can share the same state and a command may update the
73: * default target.
74: */
75: struct cmdq_state {
76: int references;
77: int flags;
78:
79: struct format_tree *formats;
80:
81: struct key_event event;
82: struct cmd_find_state current;
83: };
84:
1.43 nicm 85: /* Get command queue name. */
86: static const char *
87: cmdq_name(struct client *c)
88: {
1.70 nicm 89: static char s[256];
1.43 nicm 90:
91: if (c == NULL)
92: return ("<global>");
1.71 nicm 93: if (c->name != NULL)
94: xsnprintf(s, sizeof s, "<%s>", c->name);
95: else
96: xsnprintf(s, sizeof s, "<%p>", c);
1.43 nicm 97: return (s);
98: }
99:
100: /* Get command queue from client. */
1.44 nicm 101: static struct cmdq_list *
1.43 nicm 102: cmdq_get(struct client *c)
103: {
1.83 nicm 104: static struct cmdq_list *global_queue;
105:
106: if (c == NULL) {
107: if (global_queue == NULL)
108: global_queue = cmdq_new();
109: return (global_queue);
110: }
111: return (c->queue);
112: }
113:
114: /* Create a queue. */
115: struct cmdq_list *
116: cmdq_new(void)
117: {
118: struct cmdq_list *queue;
119:
120: queue = xcalloc (1, sizeof *queue);
121: TAILQ_INIT (queue);
122: return (queue);
123: }
124:
125: /* Free a queue. */
126: void
127: cmdq_free(struct cmdq_list *queue)
128: {
129: if (!TAILQ_EMPTY(queue))
130: fatalx("queue not empty");
131: free(queue);
132: }
133:
134: /* Get item name. */
135: const char *
136: cmdq_get_name(struct cmdq_item *item)
137: {
138: return (item->name);
139: }
140:
141: /* Get item client. */
142: struct client *
143: cmdq_get_client(struct cmdq_item *item)
144: {
145: return (item->client);
146: }
147:
1.88 ! nicm 148: /* Get item state. */
! 149: struct cmdq_state *
! 150: cmdq_get_state(struct cmdq_item *item)
! 151: {
! 152: return (item->state);
! 153: }
! 154:
1.83 nicm 155: /* Get item target. */
156: struct cmd_find_state *
157: cmdq_get_target(struct cmdq_item *item)
158: {
159: return (&item->target);
160: }
161:
162: /* Get item source. */
163: struct cmd_find_state *
164: cmdq_get_source(struct cmdq_item *item)
165: {
166: return (&item->source);
167: }
168:
1.87 nicm 169: /* Get state event. */
170: struct key_event *
171: cmdq_get_event(struct cmdq_item *item)
172: {
173: return (&item->state->event);
174: }
175:
176: /* Get state current target. */
177: struct cmd_find_state *
178: cmdq_get_current(struct cmdq_item *item)
179: {
180: return (&item->state->current);
181: }
182:
183: /* Get state flags. */
184: int
185: cmdq_get_flags(struct cmdq_item *item)
1.83 nicm 186: {
1.87 nicm 187: return (item->state->flags);
1.83 nicm 188: }
189:
1.88 ! nicm 190: /* Create a new state. */
! 191: struct cmdq_state *
! 192: cmdq_new_state(struct cmd_find_state *current, struct key_event *event,
! 193: int flags)
! 194: {
! 195: struct cmdq_state *state;
! 196:
! 197: state = xcalloc(1, sizeof *state);
! 198: state->references = 1;
! 199: state->flags = flags;
! 200:
! 201: if (event != NULL)
! 202: memcpy(&state->event, event, sizeof state->event);
! 203: else
! 204: state->event.key = KEYC_NONE;
! 205: if (current != NULL && cmd_find_valid_state(current))
! 206: cmd_find_copy_state(&state->current, current);
! 207: else
! 208: cmd_find_clear_state(&state->current, 0);
! 209:
! 210: return (state);
! 211: }
! 212:
! 213: /* Add a reference to a state. */
! 214: struct cmdq_state *
! 215: cmdq_link_state(struct cmdq_state *state)
! 216: {
! 217: state->references++;
! 218: return (state);
! 219: }
! 220:
! 221: /* Make a copy of a state. */
! 222: struct cmdq_state *
! 223: cmdq_copy_state(struct cmdq_state *state)
! 224: {
! 225: return (cmdq_new_state(&state->current, &state->event, state->flags));
! 226: }
! 227:
! 228: /* Free a state. */
! 229: void
! 230: cmdq_free_state(struct cmdq_state *state)
! 231: {
! 232: if (--state->references == 0)
! 233: free(state);
! 234: }
! 235:
! 236: /* Add a format to command queue. */
! 237: void
! 238: cmdq_add_format(struct cmdq_state *state, const char *key, const char *fmt, ...)
! 239: {
! 240: va_list ap;
! 241: char *value;
! 242:
! 243: va_start(ap, fmt);
! 244: xvasprintf(&value, fmt, ap);
! 245: va_end(ap);
! 246:
! 247: if (state->formats == NULL)
! 248: state->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
! 249: format_add(state->formats, key, "%s", value);
! 250:
! 251: free(value);
! 252: }
! 253:
1.83 nicm 254: /* Merge formats from item. */
255: void
256: cmdq_merge_formats(struct cmdq_item *item, struct format_tree *ft)
257: {
258: const struct cmd_entry *entry;
259:
260: if (item->cmd != NULL) {
261: entry = cmd_get_entry (item->cmd);
262: format_add(ft, "command", "%s", entry->name);
263: }
1.86 nicm 264: if (item->state->formats != NULL)
265: format_merge(ft, item->state->formats);
1.43 nicm 266: }
267:
268: /* Append an item. */
1.78 nicm 269: struct cmdq_item *
1.44 nicm 270: cmdq_append(struct client *c, struct cmdq_item *item)
1.43 nicm 271: {
1.44 nicm 272: struct cmdq_list *queue = cmdq_get(c);
273: struct cmdq_item *next;
1.43 nicm 274:
275: do {
1.44 nicm 276: next = item->next;
277: item->next = NULL;
1.43 nicm 278:
279: if (c != NULL)
280: c->references++;
1.44 nicm 281: item->client = c;
1.43 nicm 282:
1.44 nicm 283: item->queue = queue;
284: TAILQ_INSERT_TAIL(queue, item, entry);
1.64 nicm 285: log_debug("%s %s: %s", __func__, cmdq_name(c), item->name);
1.43 nicm 286:
1.44 nicm 287: item = next;
288: } while (item != NULL);
1.78 nicm 289: return (TAILQ_LAST(queue, cmdq_list));
1.43 nicm 290: }
291:
292: /* Insert an item. */
1.78 nicm 293: struct cmdq_item *
1.44 nicm 294: cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
1.43 nicm 295: {
296: struct client *c = after->client;
1.44 nicm 297: struct cmdq_list *queue = after->queue;
298: struct cmdq_item *next;
1.43 nicm 299:
300: do {
1.44 nicm 301: next = item->next;
1.64 nicm 302: item->next = after->next;
303: after->next = item;
1.43 nicm 304:
305: if (c != NULL)
306: c->references++;
1.44 nicm 307: item->client = c;
1.43 nicm 308:
1.44 nicm 309: item->queue = queue;
1.64 nicm 310: TAILQ_INSERT_AFTER(queue, after, item, entry);
311: log_debug("%s %s: %s after %s", __func__, cmdq_name(c),
312: item->name, after->name);
1.43 nicm 313:
1.65 nicm 314: after = item;
1.44 nicm 315: item = next;
316: } while (item != NULL);
1.78 nicm 317: return (after);
1.43 nicm 318: }
1.63 nicm 319:
320: /* Insert a hook. */
321: void
322: cmdq_insert_hook(struct session *s, struct cmdq_item *item,
1.88 ! nicm 323: struct cmd_find_state *current, const char *fmt, ...)
1.63 nicm 324: {
1.88 ! nicm 325: struct cmdq_state *state = item->state;
1.63 nicm 326: struct options *oo;
327: va_list ap;
328: char *name;
329: struct cmdq_item *new_item;
1.88 ! nicm 330: struct cmdq_state *new_state;
1.63 nicm 331: struct options_entry *o;
332: struct options_array_item *a;
333: struct cmd_list *cmdlist;
334:
1.86 nicm 335: if (item->state->flags & CMDQ_STATE_NOHOOKS)
1.63 nicm 336: return;
337: if (s == NULL)
338: oo = global_s_options;
339: else
340: oo = s->options;
341:
342: va_start(ap, fmt);
343: xvasprintf(&name, fmt, ap);
344: va_end(ap);
345:
346: o = options_get(oo, name);
347: if (o == NULL) {
348: free(name);
349: return;
350: }
351: log_debug("running hook %s (parent %p)", name, item);
352:
1.88 ! nicm 353: /*
! 354: * The hooks get a new state because they should not update the current
! 355: * target or formats for any subsequent commands.
! 356: */
! 357: new_state = cmdq_new_state(current, &state->event, CMDQ_STATE_NOHOOKS);
! 358: cmdq_add_format(new_state, "hook", "%s", name);
! 359:
1.63 nicm 360: a = options_array_first(o);
361: while (a != NULL) {
362: cmdlist = options_array_item_value(a)->cmdlist;
1.88 ! nicm 363: if (cmdlist != NULL) {
! 364: new_item = cmdq_get_command(cmdlist, new_state);
! 365: if (item != NULL)
! 366: item = cmdq_insert_after(item, new_item);
! 367: else
! 368: item = cmdq_append(NULL, new_item);
1.63 nicm 369: }
370: a = options_array_next(a);
371: }
372:
1.88 ! nicm 373: cmdq_free_state(new_state);
1.63 nicm 374: free(name);
1.74 nicm 375: }
376:
377: /* Continue processing command queue. */
378: void
379: cmdq_continue(struct cmdq_item *item)
380: {
381: item->flags &= ~CMDQ_WAITING;
1.63 nicm 382: }
383:
1.43 nicm 384: /* Remove an item. */
385: static void
1.44 nicm 386: cmdq_remove(struct cmdq_item *item)
1.43 nicm 387: {
1.44 nicm 388: if (item->client != NULL)
389: server_client_unref(item->client);
1.62 nicm 390: if (item->cmdlist != NULL)
1.44 nicm 391: cmd_list_free(item->cmdlist);
1.88 ! nicm 392: cmdq_free_state(item->state);
1.43 nicm 393:
1.44 nicm 394: TAILQ_REMOVE(item->queue, item, entry);
1.46 nicm 395:
1.64 nicm 396: free(item->name);
1.44 nicm 397: free(item);
1.43 nicm 398: }
399:
400: /* Remove all subsequent items that match this item's group. */
401: static void
1.44 nicm 402: cmdq_remove_group(struct cmdq_item *item)
1.43 nicm 403: {
1.44 nicm 404: struct cmdq_item *this, *next;
1.43 nicm 405:
1.69 nicm 406: if (item->group == 0)
407: return;
1.44 nicm 408: this = TAILQ_NEXT(item, entry);
1.43 nicm 409: while (this != NULL) {
410: next = TAILQ_NEXT(this, entry);
1.44 nicm 411: if (this->group == item->group)
1.43 nicm 412: cmdq_remove(this);
413: this = next;
414: }
415: }
416:
417: /* Get a command for the command queue. */
1.44 nicm 418: struct cmdq_item *
1.88 ! nicm 419: cmdq_get_command(struct cmd_list *cmdlist, struct cmdq_state *state)
1.43 nicm 420: {
1.44 nicm 421: struct cmdq_item *item, *first = NULL, *last = NULL;
422: struct cmd *cmd;
1.82 nicm 423: const struct cmd_entry *entry;
1.88 ! nicm 424: int created = 0;
1.49 nicm 425:
1.88 ! nicm 426: if (state == NULL) {
! 427: state = cmdq_new_state(NULL, NULL, 0);
! 428: created = 1;
! 429: }
! 430:
! 431: cmd = cmd_list_first(cmdlist);
1.82 nicm 432: while (cmd != NULL) {
433: entry = cmd_get_entry(cmd);
1.43 nicm 434:
1.44 nicm 435: item = xcalloc(1, sizeof *item);
1.82 nicm 436: xasprintf(&item->name, "[%s/%p]", entry->name, item);
1.44 nicm 437: item->type = CMDQ_COMMAND;
1.43 nicm 438:
1.88 ! nicm 439: item->group = cmd_get_group(cmd);
! 440: item->state = cmdq_link_state(state);
! 441:
1.44 nicm 442: item->cmdlist = cmdlist;
443: item->cmd = cmd;
1.68 nicm 444:
1.88 ! nicm 445: cmdlist->references++;
1.68 nicm 446: log_debug("%s: %s group %u", __func__, item->name, item->group);
1.43 nicm 447:
448: if (first == NULL)
1.44 nicm 449: first = item;
1.43 nicm 450: if (last != NULL)
1.44 nicm 451: last->next = item;
452: last = item;
1.82 nicm 453:
1.88 ! nicm 454: cmd = cmd_list_next(cmd);
1.43 nicm 455: }
1.88 ! nicm 456:
! 457: if (created)
! 458: cmdq_free_state(state);
1.43 nicm 459: return (first);
460: }
461:
1.54 nicm 462: /* Fill in flag for a command. */
463: static enum cmd_retval
464: cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs,
465: const struct cmd_entry_flag *flag)
466: {
467: const char *value;
468:
469: if (flag->flag == 0) {
470: cmd_find_clear_state(fs, 0);
471: return (CMD_RETURN_NORMAL);
472: }
473:
1.82 nicm 474: value = args_get(cmd_get_args(item->cmd), flag->flag);
1.54 nicm 475: if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) {
476: cmd_find_clear_state(fs, 0);
477: return (CMD_RETURN_ERROR);
478: }
479: return (CMD_RETURN_NORMAL);
480: }
481:
1.43 nicm 482: /* Fire command on command queue. */
483: static enum cmd_retval
1.44 nicm 484: cmdq_fire_command(struct cmdq_item *item)
1.43 nicm 485: {
1.44 nicm 486: struct client *c = item->client;
1.72 nicm 487: const char *name = cmdq_name(c);
1.86 nicm 488: struct cmdq_state *state = item->state;
1.44 nicm 489: struct cmd *cmd = item->cmd;
1.82 nicm 490: const struct cmd_entry *entry = cmd_get_entry(cmd);
1.43 nicm 491: enum cmd_retval retval;
492: struct cmd_find_state *fsp, fs;
493: int flags;
1.72 nicm 494: char *tmp;
495:
496: if (log_get_level() > 1) {
497: tmp = cmd_print(cmd);
498: log_debug("%s %s: (%u) %s", __func__, name, item->group, tmp);
499: free(tmp);
500: }
1.43 nicm 501:
1.86 nicm 502: flags = !!(state->flags & CMDQ_STATE_CONTROL);
1.44 nicm 503: cmdq_guard(item, "begin", flags);
1.43 nicm 504:
1.53 nicm 505: if (item->client == NULL)
506: item->client = cmd_find_client(item, NULL, 1);
1.54 nicm 507: retval = cmdq_find_flag(item, &item->source, &entry->source);
508: if (retval == CMD_RETURN_ERROR)
509: goto out;
510: retval = cmdq_find_flag(item, &item->target, &entry->target);
511: if (retval == CMD_RETURN_ERROR)
1.43 nicm 512: goto out;
513:
1.54 nicm 514: retval = entry->exec(cmd, item);
1.43 nicm 515: if (retval == CMD_RETURN_ERROR)
516: goto out;
517:
1.54 nicm 518: if (entry->flags & CMD_AFTERHOOK) {
519: if (cmd_find_valid_state(&item->target))
520: fsp = &item->target;
1.86 nicm 521: else if (cmd_find_valid_state(&item->state->current))
522: fsp = &item->state->current;
1.58 nicm 523: else if (cmd_find_from_client(&fs, item->client, 0) == 0)
1.43 nicm 524: fsp = &fs;
1.51 nicm 525: else
526: goto out;
1.63 nicm 527: cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name);
1.43 nicm 528: }
529:
530: out:
1.44 nicm 531: item->client = c;
1.43 nicm 532: if (retval == CMD_RETURN_ERROR)
1.44 nicm 533: cmdq_guard(item, "error", flags);
1.43 nicm 534: else
1.44 nicm 535: cmdq_guard(item, "end", flags);
1.43 nicm 536: return (retval);
537: }
538:
539: /* Get a callback for the command queue. */
1.44 nicm 540: struct cmdq_item *
1.46 nicm 541: cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
1.1 nicm 542: {
1.44 nicm 543: struct cmdq_item *item;
1.1 nicm 544:
1.44 nicm 545: item = xcalloc(1, sizeof *item);
1.64 nicm 546: xasprintf(&item->name, "[%s/%p]", name, item);
1.44 nicm 547: item->type = CMDQ_CALLBACK;
1.88 ! nicm 548:
1.44 nicm 549: item->group = 0;
1.88 ! nicm 550: item->state = cmdq_new_state(NULL, NULL, 0);
1.1 nicm 551:
1.44 nicm 552: item->cb = cb;
553: item->data = data;
1.1 nicm 554:
1.44 nicm 555: return (item);
1.67 nicm 556: }
557:
558: /* Generic error callback. */
559: static enum cmd_retval
560: cmdq_error_callback(struct cmdq_item *item, void *data)
561: {
562: char *error = data;
563:
564: cmdq_error(item, "%s", error);
565: free(error);
566:
567: return (CMD_RETURN_NORMAL);
568: }
569:
570: /* Get an error callback for the command queue. */
571: struct cmdq_item *
572: cmdq_get_error(const char *error)
573: {
574: return (cmdq_get_callback(cmdq_error_callback, xstrdup(error)));
1.43 nicm 575: }
1.1 nicm 576:
1.43 nicm 577: /* Fire callback on callback queue. */
578: static enum cmd_retval
1.44 nicm 579: cmdq_fire_callback(struct cmdq_item *item)
1.43 nicm 580: {
1.44 nicm 581: return (item->cb(item, item->data));
1.45 nicm 582: }
1.33 nicm 583:
1.43 nicm 584: /* Process next item on command queue. */
585: u_int
586: cmdq_next(struct client *c)
587: {
1.44 nicm 588: struct cmdq_list *queue = cmdq_get(c);
1.43 nicm 589: const char *name = cmdq_name(c);
1.44 nicm 590: struct cmdq_item *item;
1.43 nicm 591: enum cmd_retval retval;
592: u_int items = 0;
593: static u_int number;
1.1 nicm 594:
1.43 nicm 595: if (TAILQ_EMPTY(queue)) {
596: log_debug("%s %s: empty", __func__, name);
1.25 nicm 597: return (0);
598: }
1.44 nicm 599: if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43 nicm 600: log_debug("%s %s: waiting", __func__, name);
601: return (0);
602: }
603:
604: log_debug("%s %s: enter", __func__, name);
605: for (;;) {
1.44 nicm 606: item = TAILQ_FIRST(queue);
607: if (item == NULL)
1.43 nicm 608: break;
1.46 nicm 609: log_debug("%s %s: %s (%d), flags %x", __func__, name,
610: item->name, item->type, item->flags);
1.43 nicm 611:
612: /*
613: * Any item with the waiting flag set waits until an external
614: * event clears the flag (for example, a job - look at
615: * run-shell).
616: */
1.44 nicm 617: if (item->flags & CMDQ_WAITING)
1.43 nicm 618: goto waiting;
619:
620: /*
621: * Items are only fired once, once the fired flag is set, a
622: * waiting flag can only be cleared by an external event.
623: */
1.44 nicm 624: if (~item->flags & CMDQ_FIRED) {
625: item->time = time(NULL);
626: item->number = ++number;
1.43 nicm 627:
1.50 nicm 628: switch (item->type) {
1.44 nicm 629: case CMDQ_COMMAND:
630: retval = cmdq_fire_command(item);
1.43 nicm 631:
632: /*
633: * If a command returns an error, remove any
634: * subsequent commands in the same group.
635: */
636: if (retval == CMD_RETURN_ERROR)
1.44 nicm 637: cmdq_remove_group(item);
1.43 nicm 638: break;
1.44 nicm 639: case CMDQ_CALLBACK:
640: retval = cmdq_fire_callback(item);
1.43 nicm 641: break;
642: default:
643: retval = CMD_RETURN_ERROR;
644: break;
645: }
1.44 nicm 646: item->flags |= CMDQ_FIRED;
1.43 nicm 647:
648: if (retval == CMD_RETURN_WAIT) {
1.44 nicm 649: item->flags |= CMDQ_WAITING;
1.43 nicm 650: goto waiting;
651: }
652: items++;
653: }
1.44 nicm 654: cmdq_remove(item);
1.43 nicm 655: }
656:
657: log_debug("%s %s: exit (empty)", __func__, name);
658: return (items);
1.1 nicm 659:
1.43 nicm 660: waiting:
661: log_debug("%s %s: exit (wait)", __func__, name);
662: return (items);
663: }
664:
665: /* Print a guard line. */
666: void
1.44 nicm 667: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43 nicm 668: {
1.44 nicm 669: struct client *c = item->client;
1.76 nicm 670: long t = item->time;
671: u_int number = item->number;
1.43 nicm 672:
1.76 nicm 673: if (c != NULL && (c->flags & CLIENT_CONTROL))
674: file_print(c, "%%%s %ld %u %d\n", guard, t, number, flags);
1.1 nicm 675: }
676:
677: /* Show message from command. */
1.18 nicm 678: void
1.44 nicm 679: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 680: {
1.61 nicm 681: struct client *c = item->client;
682: struct window_pane *wp;
683: struct window_mode_entry *wme;
684: va_list ap;
685: char *tmp, *msg;
1.1 nicm 686:
687: va_start(ap, fmt);
1.76 nicm 688: xvasprintf(&msg, fmt, ap);
689: va_end(ap);
690:
691: log_debug("%s: %s", __func__, msg);
1.1 nicm 692:
693: if (c == NULL)
694: /* nothing */;
695: else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 696: if (~c->flags & CLIENT_UTF8) {
1.76 nicm 697: tmp = msg;
1.28 nicm 698: msg = utf8_sanitize(tmp);
699: free(tmp);
1.76 nicm 700: }
701: file_print(c, "%s\n", msg);
1.1 nicm 702: } else {
1.60 nicm 703: wp = c->session->curw->window->active;
1.61 nicm 704: wme = TAILQ_FIRST(&wp->modes);
1.81 nicm 705: if (wme == NULL || wme->mode != &window_view_mode) {
706: window_pane_set_mode(wp, NULL, &window_view_mode, NULL,
707: NULL);
708: }
1.77 nicm 709: window_copy_add(wp, "%s", msg);
1.1 nicm 710: }
711:
1.76 nicm 712: free(msg);
1.1 nicm 713: }
714:
715: /* Show error from command. */
1.18 nicm 716: void
1.44 nicm 717: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1 nicm 718: {
1.44 nicm 719: struct client *c = item->client;
720: struct cmd *cmd = item->cmd;
1.1 nicm 721: va_list ap;
1.82 nicm 722: char *msg, *tmp;
723: const char *file;
724: u_int line;
1.1 nicm 725:
726: va_start(ap, fmt);
1.76 nicm 727: xvasprintf(&msg, fmt, ap);
1.1 nicm 728: va_end(ap);
1.57 nicm 729:
730: log_debug("%s: %s", __func__, msg);
1.1 nicm 731:
1.82 nicm 732: if (c == NULL) {
733: cmd_get_source(cmd, &file, &line);
734: cfg_add_cause("%s:%u: %s", file, line, msg);
735: } else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28 nicm 736: if (~c->flags & CLIENT_UTF8) {
737: tmp = msg;
738: msg = utf8_sanitize(tmp);
739: free(tmp);
740: }
1.79 nicm 741: if (c->flags & CLIENT_CONTROL)
742: file_print(c, "%s\n", msg);
743: else
744: file_error(c, "%s\n", msg);
1.13 nicm 745: c->retval = 1;
1.1 nicm 746: } else {
747: *msg = toupper((u_char) *msg);
748: status_message_set(c, "%s", msg);
749: }
750:
751: free(msg);
752: }