[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.84

1.84    ! nicm        1: /* $OpenBSD: cmd-queue.c,v 1.83 2020/04/13 10:59:58 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);
                    336:                        if (m != NULL)
                    337:                                memcpy(&shared->mouse, m, sizeof shared->mouse);
1.84    ! nicm      338:                        shared->flags = flags;
1.82      nicm      339:                        last_group = group;
1.72      nicm      340:                }
1.82      nicm      341:                entry = cmd_get_entry(cmd);
1.43      nicm      342:
1.44      nicm      343:                item = xcalloc(1, sizeof *item);
1.82      nicm      344:                xasprintf(&item->name, "[%s/%p]", entry->name, item);
1.44      nicm      345:                item->type = CMDQ_COMMAND;
1.82      nicm      346:                item->group = group;
1.43      nicm      347:
1.49      nicm      348:                item->shared = shared;
1.44      nicm      349:                item->cmdlist = cmdlist;
                    350:                item->cmd = cmd;
1.68      nicm      351:
                    352:                log_debug("%s: %s group %u", __func__, item->name, item->group);
1.43      nicm      353:
1.49      nicm      354:                shared->references++;
1.43      nicm      355:                cmdlist->references++;
                    356:
                    357:                if (first == NULL)
1.44      nicm      358:                        first = item;
1.43      nicm      359:                if (last != NULL)
1.44      nicm      360:                        last->next = item;
                    361:                last = item;
1.82      nicm      362:
                    363:                cmd = cmd_list_next(cmd, &group);
1.43      nicm      364:        }
                    365:        return (first);
                    366: }
                    367:
1.54      nicm      368: /* Fill in flag for a command. */
                    369: static enum cmd_retval
                    370: cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs,
                    371:     const struct cmd_entry_flag *flag)
                    372: {
                    373:        const char      *value;
                    374:
                    375:        if (flag->flag == 0) {
                    376:                cmd_find_clear_state(fs, 0);
                    377:                return (CMD_RETURN_NORMAL);
                    378:        }
                    379:
1.82      nicm      380:        value = args_get(cmd_get_args(item->cmd), flag->flag);
1.54      nicm      381:        if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) {
                    382:                cmd_find_clear_state(fs, 0);
                    383:                return (CMD_RETURN_ERROR);
                    384:        }
                    385:        return (CMD_RETURN_NORMAL);
                    386: }
                    387:
1.43      nicm      388: /* Fire command on command queue. */
                    389: static enum cmd_retval
1.44      nicm      390: cmdq_fire_command(struct cmdq_item *item)
1.43      nicm      391: {
1.44      nicm      392:        struct client           *c = item->client;
1.72      nicm      393:        const char              *name = cmdq_name(c);
1.66      nicm      394:        struct cmdq_shared      *shared = item->shared;
1.44      nicm      395:        struct cmd              *cmd = item->cmd;
1.82      nicm      396:        const struct cmd_entry  *entry = cmd_get_entry(cmd);
1.43      nicm      397:        enum cmd_retval          retval;
                    398:        struct cmd_find_state   *fsp, fs;
                    399:        int                      flags;
1.72      nicm      400:        char                    *tmp;
                    401:
                    402:        if (log_get_level() > 1) {
                    403:                tmp = cmd_print(cmd);
                    404:                log_debug("%s %s: (%u) %s", __func__, name, item->group, tmp);
                    405:                free(tmp);
                    406:        }
1.43      nicm      407:
1.66      nicm      408:        flags = !!(shared->flags & CMDQ_SHARED_CONTROL);
1.44      nicm      409:        cmdq_guard(item, "begin", flags);
1.43      nicm      410:
1.53      nicm      411:        if (item->client == NULL)
                    412:                item->client = cmd_find_client(item, NULL, 1);
1.54      nicm      413:        retval = cmdq_find_flag(item, &item->source, &entry->source);
                    414:        if (retval == CMD_RETURN_ERROR)
                    415:                goto out;
                    416:        retval = cmdq_find_flag(item, &item->target, &entry->target);
                    417:        if (retval == CMD_RETURN_ERROR)
1.43      nicm      418:                goto out;
                    419:
1.54      nicm      420:        retval = entry->exec(cmd, item);
1.43      nicm      421:        if (retval == CMD_RETURN_ERROR)
                    422:                goto out;
                    423:
1.54      nicm      424:        if (entry->flags & CMD_AFTERHOOK) {
                    425:                if (cmd_find_valid_state(&item->target))
                    426:                        fsp = &item->target;
1.51      nicm      427:                else if (cmd_find_valid_state(&item->shared->current))
                    428:                        fsp = &item->shared->current;
1.58      nicm      429:                else if (cmd_find_from_client(&fs, item->client, 0) == 0)
1.43      nicm      430:                        fsp = &fs;
1.51      nicm      431:                else
                    432:                        goto out;
1.63      nicm      433:                cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name);
1.43      nicm      434:        }
                    435:
                    436: out:
1.44      nicm      437:        item->client = c;
1.43      nicm      438:        if (retval == CMD_RETURN_ERROR)
1.44      nicm      439:                cmdq_guard(item, "error", flags);
1.43      nicm      440:        else
1.44      nicm      441:                cmdq_guard(item, "end", flags);
1.43      nicm      442:        return (retval);
                    443: }
                    444:
                    445: /* Get a callback for the command queue. */
1.44      nicm      446: struct cmdq_item *
1.46      nicm      447: cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
1.1       nicm      448: {
1.44      nicm      449:        struct cmdq_item        *item;
1.1       nicm      450:
1.44      nicm      451:        item = xcalloc(1, sizeof *item);
1.64      nicm      452:        xasprintf(&item->name, "[%s/%p]", name, item);
1.44      nicm      453:        item->type = CMDQ_CALLBACK;
                    454:        item->group = 0;
1.1       nicm      455:
1.44      nicm      456:        item->cb = cb;
                    457:        item->data = data;
1.1       nicm      458:
1.44      nicm      459:        return (item);
1.67      nicm      460: }
                    461:
                    462: /* Generic error callback. */
                    463: static enum cmd_retval
                    464: cmdq_error_callback(struct cmdq_item *item, void *data)
                    465: {
                    466:        char    *error = data;
                    467:
                    468:        cmdq_error(item, "%s", error);
                    469:        free(error);
                    470:
                    471:        return (CMD_RETURN_NORMAL);
                    472: }
                    473:
                    474: /* Get an error callback for the command queue. */
                    475: struct cmdq_item *
                    476: cmdq_get_error(const char *error)
                    477: {
                    478:        return (cmdq_get_callback(cmdq_error_callback, xstrdup(error)));
1.43      nicm      479: }
1.1       nicm      480:
1.43      nicm      481: /* Fire callback on callback queue. */
                    482: static enum cmd_retval
1.44      nicm      483: cmdq_fire_callback(struct cmdq_item *item)
1.43      nicm      484: {
1.44      nicm      485:        return (item->cb(item, item->data));
1.43      nicm      486: }
1.45      nicm      487:
                    488: /* Add a format to command queue. */
                    489: void
                    490: cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...)
                    491: {
1.49      nicm      492:        struct cmdq_shared      *shared = item->shared;
1.45      nicm      493:        va_list                  ap;
                    494:        char                    *value;
                    495:
                    496:        va_start(ap, fmt);
                    497:        xvasprintf(&value, fmt, ap);
                    498:        va_end(ap);
                    499:
1.49      nicm      500:        if (shared->formats == NULL)
1.55      nicm      501:                shared->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
1.49      nicm      502:        format_add(shared->formats, key, "%s", value);
1.45      nicm      503:
                    504:        free(value);
                    505: }
1.33      nicm      506:
1.43      nicm      507: /* Process next item on command queue. */
                    508: u_int
                    509: cmdq_next(struct client *c)
                    510: {
1.44      nicm      511:        struct cmdq_list        *queue = cmdq_get(c);
1.43      nicm      512:        const char              *name = cmdq_name(c);
1.44      nicm      513:        struct cmdq_item        *item;
1.43      nicm      514:        enum cmd_retval          retval;
                    515:        u_int                    items = 0;
                    516:        static u_int             number;
1.1       nicm      517:
1.43      nicm      518:        if (TAILQ_EMPTY(queue)) {
                    519:                log_debug("%s %s: empty", __func__, name);
1.25      nicm      520:                return (0);
                    521:        }
1.44      nicm      522:        if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43      nicm      523:                log_debug("%s %s: waiting", __func__, name);
                    524:                return (0);
                    525:        }
                    526:
                    527:        log_debug("%s %s: enter", __func__, name);
                    528:        for (;;) {
1.44      nicm      529:                item = TAILQ_FIRST(queue);
                    530:                if (item == NULL)
1.43      nicm      531:                        break;
1.46      nicm      532:                log_debug("%s %s: %s (%d), flags %x", __func__, name,
                    533:                    item->name, item->type, item->flags);
1.43      nicm      534:
                    535:                /*
                    536:                 * Any item with the waiting flag set waits until an external
                    537:                 * event clears the flag (for example, a job - look at
                    538:                 * run-shell).
                    539:                 */
1.44      nicm      540:                if (item->flags & CMDQ_WAITING)
1.43      nicm      541:                        goto waiting;
                    542:
                    543:                /*
                    544:                 * Items are only fired once, once the fired flag is set, a
                    545:                 * waiting flag can only be cleared by an external event.
                    546:                 */
1.44      nicm      547:                if (~item->flags & CMDQ_FIRED) {
                    548:                        item->time = time(NULL);
                    549:                        item->number = ++number;
1.43      nicm      550:
1.50      nicm      551:                        switch (item->type) {
1.44      nicm      552:                        case CMDQ_COMMAND:
                    553:                                retval = cmdq_fire_command(item);
1.43      nicm      554:
                    555:                                /*
                    556:                                 * If a command returns an error, remove any
                    557:                                 * subsequent commands in the same group.
                    558:                                 */
                    559:                                if (retval == CMD_RETURN_ERROR)
1.44      nicm      560:                                        cmdq_remove_group(item);
1.43      nicm      561:                                break;
1.44      nicm      562:                        case CMDQ_CALLBACK:
                    563:                                retval = cmdq_fire_callback(item);
1.43      nicm      564:                                break;
                    565:                        default:
                    566:                                retval = CMD_RETURN_ERROR;
                    567:                                break;
                    568:                        }
1.44      nicm      569:                        item->flags |= CMDQ_FIRED;
1.43      nicm      570:
                    571:                        if (retval == CMD_RETURN_WAIT) {
1.44      nicm      572:                                item->flags |= CMDQ_WAITING;
1.43      nicm      573:                                goto waiting;
                    574:                        }
                    575:                        items++;
                    576:                }
1.44      nicm      577:                cmdq_remove(item);
1.43      nicm      578:        }
                    579:
                    580:        log_debug("%s %s: exit (empty)", __func__, name);
                    581:        return (items);
1.1       nicm      582:
1.43      nicm      583: waiting:
                    584:        log_debug("%s %s: exit (wait)", __func__, name);
                    585:        return (items);
                    586: }
                    587:
                    588: /* Print a guard line. */
                    589: void
1.44      nicm      590: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43      nicm      591: {
1.44      nicm      592:        struct client   *c = item->client;
1.76      nicm      593:        long             t = item->time;
                    594:        u_int            number = item->number;
1.43      nicm      595:
1.76      nicm      596:        if (c != NULL && (c->flags & CLIENT_CONTROL))
                    597:                file_print(c, "%%%s %ld %u %d\n", guard, t, number, flags);
1.1       nicm      598: }
                    599:
                    600: /* Show message from command. */
1.18      nicm      601: void
1.44      nicm      602: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1       nicm      603: {
1.61      nicm      604:        struct client                   *c = item->client;
                    605:        struct window_pane              *wp;
                    606:        struct window_mode_entry        *wme;
                    607:        va_list                          ap;
                    608:        char                            *tmp, *msg;
1.1       nicm      609:
                    610:        va_start(ap, fmt);
1.76      nicm      611:        xvasprintf(&msg, fmt, ap);
                    612:        va_end(ap);
                    613:
                    614:        log_debug("%s: %s", __func__, msg);
1.1       nicm      615:
                    616:        if (c == NULL)
                    617:                /* nothing */;
                    618:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      619:                if (~c->flags & CLIENT_UTF8) {
1.76      nicm      620:                        tmp = msg;
1.28      nicm      621:                        msg = utf8_sanitize(tmp);
                    622:                        free(tmp);
1.76      nicm      623:                }
                    624:                file_print(c, "%s\n", msg);
1.1       nicm      625:        } else {
1.60      nicm      626:                wp = c->session->curw->window->active;
1.61      nicm      627:                wme = TAILQ_FIRST(&wp->modes);
1.81      nicm      628:                if (wme == NULL || wme->mode != &window_view_mode) {
                    629:                        window_pane_set_mode(wp, NULL, &window_view_mode, NULL,
                    630:                            NULL);
                    631:                }
1.77      nicm      632:                window_copy_add(wp, "%s", msg);
1.1       nicm      633:        }
                    634:
1.76      nicm      635:        free(msg);
1.1       nicm      636: }
                    637:
                    638: /* Show error from command. */
1.18      nicm      639: void
1.44      nicm      640: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1       nicm      641: {
1.44      nicm      642:        struct client   *c = item->client;
                    643:        struct cmd      *cmd = item->cmd;
1.1       nicm      644:        va_list          ap;
1.82      nicm      645:        char            *msg, *tmp;
                    646:        const char      *file;
                    647:        u_int            line;
1.1       nicm      648:
                    649:        va_start(ap, fmt);
1.76      nicm      650:        xvasprintf(&msg, fmt, ap);
1.1       nicm      651:        va_end(ap);
1.57      nicm      652:
                    653:        log_debug("%s: %s", __func__, msg);
1.1       nicm      654:
1.82      nicm      655:        if (c == NULL) {
                    656:                cmd_get_source(cmd, &file, &line);
                    657:                cfg_add_cause("%s:%u: %s", file, line, msg);
                    658:        } else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      659:                if (~c->flags & CLIENT_UTF8) {
                    660:                        tmp = msg;
                    661:                        msg = utf8_sanitize(tmp);
                    662:                        free(tmp);
                    663:                }
1.79      nicm      664:                if (c->flags & CLIENT_CONTROL)
                    665:                        file_print(c, "%s\n", msg);
                    666:                else
                    667:                        file_error(c, "%s\n", msg);
1.13      nicm      668:                c->retval = 1;
1.1       nicm      669:        } else {
                    670:                *msg = toupper((u_char) *msg);
                    671:                status_message_set(c, "%s", msg);
                    672:        }
                    673:
                    674:        free(msg);
                    675: }