[BACK]Return to cmd-queue.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-queue.c, Revision 1.85

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