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

1.66    ! nicm        1: /* $OpenBSD: cmd-queue.c,v 1.65 2019/05/03 18: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.43      nicm       28: /* Global command queue. */
1.44      nicm       29: static struct cmdq_list global_queue = TAILQ_HEAD_INITIALIZER(global_queue);
1.24      nicm       30:
1.43      nicm       31: /* Get command queue name. */
                     32: static const char *
                     33: cmdq_name(struct client *c)
                     34: {
                     35:        static char     s[32];
                     36:
                     37:        if (c == NULL)
                     38:                return ("<global>");
                     39:        xsnprintf(s, sizeof s, "<%p>", c);
                     40:        return (s);
                     41: }
                     42:
                     43: /* Get command queue from client. */
1.44      nicm       44: static struct cmdq_list *
1.43      nicm       45: cmdq_get(struct client *c)
                     46: {
                     47:        if (c == NULL)
                     48:                return (&global_queue);
                     49:        return (&c->queue);
                     50: }
                     51:
                     52: /* Append an item. */
                     53: void
1.44      nicm       54: cmdq_append(struct client *c, struct cmdq_item *item)
1.43      nicm       55: {
1.44      nicm       56:        struct cmdq_list        *queue = cmdq_get(c);
                     57:        struct cmdq_item        *next;
1.43      nicm       58:
                     59:        do {
1.44      nicm       60:                next = item->next;
                     61:                item->next = NULL;
1.43      nicm       62:
                     63:                if (c != NULL)
                     64:                        c->references++;
1.44      nicm       65:                item->client = c;
1.43      nicm       66:
1.44      nicm       67:                item->queue = queue;
                     68:                TAILQ_INSERT_TAIL(queue, item, entry);
1.64      nicm       69:                log_debug("%s %s: %s", __func__, cmdq_name(c), item->name);
1.43      nicm       70:
1.44      nicm       71:                item = next;
                     72:        } while (item != NULL);
1.43      nicm       73: }
                     74:
                     75: /* Insert an item. */
                     76: void
1.44      nicm       77: cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
1.43      nicm       78: {
                     79:        struct client           *c = after->client;
1.44      nicm       80:        struct cmdq_list        *queue = after->queue;
                     81:        struct cmdq_item        *next;
1.43      nicm       82:
                     83:        do {
1.44      nicm       84:                next = item->next;
1.64      nicm       85:                item->next = after->next;
                     86:                after->next = item;
1.43      nicm       87:
                     88:                if (c != NULL)
                     89:                        c->references++;
1.44      nicm       90:                item->client = c;
1.43      nicm       91:
1.44      nicm       92:                item->queue = queue;
1.64      nicm       93:                TAILQ_INSERT_AFTER(queue, after, item, entry);
                     94:                log_debug("%s %s: %s after %s", __func__, cmdq_name(c),
                     95:                    item->name, after->name);
1.43      nicm       96:
1.65      nicm       97:                after = item;
1.44      nicm       98:                item = next;
                     99:        } while (item != NULL);
1.43      nicm      100: }
                    101:
1.63      nicm      102:
                    103: /* Insert a hook. */
                    104: void
                    105: cmdq_insert_hook(struct session *s, struct cmdq_item *item,
                    106:     struct cmd_find_state *fs, const char *fmt, ...)
                    107: {
                    108:        struct options                  *oo;
                    109:        va_list                          ap;
                    110:        char                            *name;
                    111:        struct cmdq_item                *new_item;
                    112:        struct options_entry            *o;
                    113:        struct options_array_item       *a;
                    114:        struct cmd_list                 *cmdlist;
                    115:
                    116:        if (item->flags & CMDQ_NOHOOKS)
                    117:                return;
                    118:        if (s == NULL)
                    119:                oo = global_s_options;
                    120:        else
                    121:                oo = s->options;
                    122:
                    123:        va_start(ap, fmt);
                    124:        xvasprintf(&name, fmt, ap);
                    125:        va_end(ap);
                    126:
                    127:        o = options_get(oo, name);
                    128:        if (o == NULL) {
                    129:                free(name);
                    130:                return;
                    131:        }
                    132:        log_debug("running hook %s (parent %p)", name, item);
                    133:
                    134:        a = options_array_first(o);
                    135:        while (a != NULL) {
                    136:                cmdlist = options_array_item_value(a)->cmdlist;
                    137:                if (cmdlist == NULL) {
                    138:                        a = options_array_next(a);
                    139:                        continue;
                    140:                }
                    141:
                    142:                new_item = cmdq_get_command(cmdlist, fs, NULL, CMDQ_NOHOOKS);
                    143:                cmdq_format(new_item, "hook", "%s", name);
                    144:                if (item != NULL) {
                    145:                        cmdq_insert_after(item, new_item);
                    146:                        item = new_item;
                    147:                } else
                    148:                        cmdq_append(NULL, new_item);
                    149:
                    150:                a = options_array_next(a);
                    151:        }
                    152:
                    153:        free(name);
                    154: }
                    155:
1.43      nicm      156: /* Remove an item. */
                    157: static void
1.44      nicm      158: cmdq_remove(struct cmdq_item *item)
1.43      nicm      159: {
1.49      nicm      160:        if (item->shared != NULL && --item->shared->references == 0) {
                    161:                if (item->shared->formats != NULL)
                    162:                        format_free(item->shared->formats);
                    163:                free(item->shared);
                    164:        }
1.43      nicm      165:
1.44      nicm      166:        if (item->client != NULL)
                    167:                server_client_unref(item->client);
1.43      nicm      168:
1.62      nicm      169:        if (item->cmdlist != NULL)
1.44      nicm      170:                cmd_list_free(item->cmdlist);
1.43      nicm      171:
1.44      nicm      172:        TAILQ_REMOVE(item->queue, item, entry);
1.46      nicm      173:
1.64      nicm      174:        free(item->name);
1.44      nicm      175:        free(item);
1.43      nicm      176: }
                    177:
                    178: /* Set command group. */
                    179: static u_int
                    180: cmdq_next_group(void)
                    181: {
                    182:        static u_int    group;
                    183:
                    184:        return (++group);
                    185: }
                    186:
                    187: /* Remove all subsequent items that match this item's group. */
                    188: static void
1.44      nicm      189: cmdq_remove_group(struct cmdq_item *item)
1.43      nicm      190: {
1.44      nicm      191:        struct cmdq_item        *this, *next;
1.43      nicm      192:
1.44      nicm      193:        this = TAILQ_NEXT(item, entry);
1.43      nicm      194:        while (this != NULL) {
                    195:                next = TAILQ_NEXT(this, entry);
1.44      nicm      196:                if (this->group == item->group)
1.43      nicm      197:                        cmdq_remove(this);
                    198:                this = next;
                    199:        }
                    200: }
                    201:
                    202: /* Get a command for the command queue. */
1.44      nicm      203: struct cmdq_item *
1.43      nicm      204: cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
                    205:     struct mouse_event *m, int flags)
                    206: {
1.44      nicm      207:        struct cmdq_item        *item, *first = NULL, *last = NULL;
                    208:        struct cmd              *cmd;
                    209:        u_int                    group = cmdq_next_group();
1.49      nicm      210:        struct cmdq_shared      *shared;
                    211:
                    212:        shared = xcalloc(1, sizeof *shared);
                    213:        if (current != NULL)
                    214:                cmd_find_copy_state(&shared->current, current);
1.52      nicm      215:        else
                    216:                cmd_find_clear_state(&shared->current, 0);
1.49      nicm      217:        if (m != NULL)
                    218:                memcpy(&shared->mouse, m, sizeof shared->mouse);
1.43      nicm      219:
                    220:        TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
1.44      nicm      221:                item = xcalloc(1, sizeof *item);
1.64      nicm      222:                xasprintf(&item->name, "[%s/%p]", cmd->entry->name, item);
1.44      nicm      223:                item->type = CMDQ_COMMAND;
1.46      nicm      224:
1.44      nicm      225:                item->group = group;
                    226:                item->flags = flags;
1.43      nicm      227:
1.49      nicm      228:                item->shared = shared;
1.44      nicm      229:                item->cmdlist = cmdlist;
                    230:                item->cmd = cmd;
1.43      nicm      231:
1.49      nicm      232:                shared->references++;
1.43      nicm      233:                cmdlist->references++;
                    234:
                    235:                if (first == NULL)
1.44      nicm      236:                        first = item;
1.43      nicm      237:                if (last != NULL)
1.44      nicm      238:                        last->next = item;
                    239:                last = item;
1.43      nicm      240:        }
                    241:        return (first);
                    242: }
                    243:
1.54      nicm      244: /* Fill in flag for a command. */
                    245: static enum cmd_retval
                    246: cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs,
                    247:     const struct cmd_entry_flag *flag)
                    248: {
                    249:        const char      *value;
                    250:
                    251:        if (flag->flag == 0) {
                    252:                cmd_find_clear_state(fs, 0);
                    253:                return (CMD_RETURN_NORMAL);
                    254:        }
                    255:
                    256:        value = args_get(item->cmd->args, flag->flag);
                    257:        if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) {
                    258:                cmd_find_clear_state(fs, 0);
                    259:                return (CMD_RETURN_ERROR);
                    260:        }
                    261:        return (CMD_RETURN_NORMAL);
                    262: }
                    263:
1.43      nicm      264: /* Fire command on command queue. */
                    265: static enum cmd_retval
1.44      nicm      266: cmdq_fire_command(struct cmdq_item *item)
1.43      nicm      267: {
1.44      nicm      268:        struct client           *c = item->client;
1.66    ! nicm      269:        struct cmdq_shared      *shared = item->shared;
1.44      nicm      270:        struct cmd              *cmd = item->cmd;
1.54      nicm      271:        const struct cmd_entry  *entry = cmd->entry;
1.43      nicm      272:        enum cmd_retval          retval;
                    273:        struct cmd_find_state   *fsp, fs;
                    274:        int                      flags;
                    275:
1.66    ! nicm      276:        flags = !!(shared->flags & CMDQ_SHARED_CONTROL);
1.44      nicm      277:        cmdq_guard(item, "begin", flags);
1.43      nicm      278:
1.53      nicm      279:        if (item->client == NULL)
                    280:                item->client = cmd_find_client(item, NULL, 1);
1.54      nicm      281:        retval = cmdq_find_flag(item, &item->source, &entry->source);
                    282:        if (retval == CMD_RETURN_ERROR)
                    283:                goto out;
                    284:        retval = cmdq_find_flag(item, &item->target, &entry->target);
                    285:        if (retval == CMD_RETURN_ERROR)
1.43      nicm      286:                goto out;
                    287:
1.54      nicm      288:        retval = entry->exec(cmd, item);
1.43      nicm      289:        if (retval == CMD_RETURN_ERROR)
                    290:                goto out;
                    291:
1.54      nicm      292:        if (entry->flags & CMD_AFTERHOOK) {
                    293:                if (cmd_find_valid_state(&item->target))
                    294:                        fsp = &item->target;
1.51      nicm      295:                else if (cmd_find_valid_state(&item->shared->current))
                    296:                        fsp = &item->shared->current;
1.58      nicm      297:                else if (cmd_find_from_client(&fs, item->client, 0) == 0)
1.43      nicm      298:                        fsp = &fs;
1.51      nicm      299:                else
                    300:                        goto out;
1.63      nicm      301:                cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name);
1.43      nicm      302:        }
                    303:
                    304: out:
1.44      nicm      305:        item->client = c;
1.43      nicm      306:        if (retval == CMD_RETURN_ERROR)
1.44      nicm      307:                cmdq_guard(item, "error", flags);
1.43      nicm      308:        else
1.44      nicm      309:                cmdq_guard(item, "end", flags);
1.43      nicm      310:        return (retval);
                    311: }
                    312:
                    313: /* Get a callback for the command queue. */
1.44      nicm      314: struct cmdq_item *
1.46      nicm      315: cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
1.1       nicm      316: {
1.44      nicm      317:        struct cmdq_item        *item;
1.1       nicm      318:
1.44      nicm      319:        item = xcalloc(1, sizeof *item);
1.64      nicm      320:        xasprintf(&item->name, "[%s/%p]", name, item);
1.44      nicm      321:        item->type = CMDQ_CALLBACK;
1.46      nicm      322:
1.44      nicm      323:        item->group = 0;
                    324:        item->flags = 0;
1.1       nicm      325:
1.44      nicm      326:        item->cb = cb;
                    327:        item->data = data;
1.1       nicm      328:
1.44      nicm      329:        return (item);
1.43      nicm      330: }
1.1       nicm      331:
1.43      nicm      332: /* Fire callback on callback queue. */
                    333: static enum cmd_retval
1.44      nicm      334: cmdq_fire_callback(struct cmdq_item *item)
1.43      nicm      335: {
1.44      nicm      336:        return (item->cb(item, item->data));
1.43      nicm      337: }
1.45      nicm      338:
                    339: /* Add a format to command queue. */
                    340: void
                    341: cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...)
                    342: {
1.49      nicm      343:        struct cmdq_shared      *shared = item->shared;
1.45      nicm      344:        va_list                  ap;
                    345:        char                    *value;
                    346:
                    347:        va_start(ap, fmt);
                    348:        xvasprintf(&value, fmt, ap);
                    349:        va_end(ap);
                    350:
1.49      nicm      351:        if (shared->formats == NULL)
1.55      nicm      352:                shared->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
1.49      nicm      353:        format_add(shared->formats, key, "%s", value);
1.45      nicm      354:
                    355:        free(value);
                    356: }
1.33      nicm      357:
1.43      nicm      358: /* Process next item on command queue. */
                    359: u_int
                    360: cmdq_next(struct client *c)
                    361: {
1.44      nicm      362:        struct cmdq_list        *queue = cmdq_get(c);
1.43      nicm      363:        const char              *name = cmdq_name(c);
1.44      nicm      364:        struct cmdq_item        *item;
1.43      nicm      365:        enum cmd_retval          retval;
                    366:        u_int                    items = 0;
                    367:        static u_int             number;
1.1       nicm      368:
1.43      nicm      369:        if (TAILQ_EMPTY(queue)) {
                    370:                log_debug("%s %s: empty", __func__, name);
1.25      nicm      371:                return (0);
                    372:        }
1.44      nicm      373:        if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43      nicm      374:                log_debug("%s %s: waiting", __func__, name);
                    375:                return (0);
                    376:        }
                    377:
                    378:        log_debug("%s %s: enter", __func__, name);
                    379:        for (;;) {
1.44      nicm      380:                item = TAILQ_FIRST(queue);
                    381:                if (item == NULL)
1.43      nicm      382:                        break;
1.46      nicm      383:                log_debug("%s %s: %s (%d), flags %x", __func__, name,
                    384:                    item->name, item->type, item->flags);
1.43      nicm      385:
                    386:                /*
                    387:                 * Any item with the waiting flag set waits until an external
                    388:                 * event clears the flag (for example, a job - look at
                    389:                 * run-shell).
                    390:                 */
1.44      nicm      391:                if (item->flags & CMDQ_WAITING)
1.43      nicm      392:                        goto waiting;
                    393:
                    394:                /*
                    395:                 * Items are only fired once, once the fired flag is set, a
                    396:                 * waiting flag can only be cleared by an external event.
                    397:                 */
1.44      nicm      398:                if (~item->flags & CMDQ_FIRED) {
                    399:                        item->time = time(NULL);
                    400:                        item->number = ++number;
1.43      nicm      401:
1.50      nicm      402:                        switch (item->type) {
1.44      nicm      403:                        case CMDQ_COMMAND:
                    404:                                retval = cmdq_fire_command(item);
1.43      nicm      405:
                    406:                                /*
                    407:                                 * If a command returns an error, remove any
                    408:                                 * subsequent commands in the same group.
                    409:                                 */
                    410:                                if (retval == CMD_RETURN_ERROR)
1.44      nicm      411:                                        cmdq_remove_group(item);
1.43      nicm      412:                                break;
1.44      nicm      413:                        case CMDQ_CALLBACK:
                    414:                                retval = cmdq_fire_callback(item);
1.43      nicm      415:                                break;
                    416:                        default:
                    417:                                retval = CMD_RETURN_ERROR;
                    418:                                break;
                    419:                        }
1.44      nicm      420:                        item->flags |= CMDQ_FIRED;
1.43      nicm      421:
                    422:                        if (retval == CMD_RETURN_WAIT) {
1.44      nicm      423:                                item->flags |= CMDQ_WAITING;
1.43      nicm      424:                                goto waiting;
                    425:                        }
                    426:                        items++;
                    427:                }
1.44      nicm      428:                cmdq_remove(item);
1.43      nicm      429:        }
                    430:
                    431:        log_debug("%s %s: exit (empty)", __func__, name);
                    432:        return (items);
1.1       nicm      433:
1.43      nicm      434: waiting:
                    435:        log_debug("%s %s: exit (wait)", __func__, name);
                    436:        return (items);
                    437: }
                    438:
                    439: /* Print a guard line. */
                    440: void
1.44      nicm      441: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43      nicm      442: {
1.44      nicm      443:        struct client   *c = item->client;
1.43      nicm      444:
                    445:        if (c == NULL || !(c->flags & CLIENT_CONTROL))
                    446:                return;
                    447:
                    448:        evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
1.44      nicm      449:            (long)item->time, item->number, flags);
1.43      nicm      450:        server_client_push_stdout(c);
1.1       nicm      451: }
                    452:
                    453: /* Show message from command. */
1.18      nicm      454: void
1.44      nicm      455: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1       nicm      456: {
1.61      nicm      457:        struct client                   *c = item->client;
                    458:        struct window_pane              *wp;
                    459:        struct window_mode_entry        *wme;
                    460:        va_list                          ap;
                    461:        char                            *tmp, *msg;
1.1       nicm      462:
                    463:        va_start(ap, fmt);
                    464:
                    465:        if (c == NULL)
                    466:                /* nothing */;
                    467:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      468:                if (~c->flags & CLIENT_UTF8) {
1.37      nicm      469:                        xvasprintf(&tmp, fmt, ap);
1.28      nicm      470:                        msg = utf8_sanitize(tmp);
                    471:                        free(tmp);
                    472:                        evbuffer_add(c->stdout_data, msg, strlen(msg));
                    473:                        free(msg);
                    474:                } else
                    475:                        evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1       nicm      476:                evbuffer_add(c->stdout_data, "\n", 1);
1.29      nicm      477:                server_client_push_stdout(c);
1.1       nicm      478:        } else {
1.60      nicm      479:                wp = c->session->curw->window->active;
1.61      nicm      480:                wme = TAILQ_FIRST(&wp->modes);
                    481:                if (wme == NULL || wme->mode != &window_view_mode)
1.60      nicm      482:                        window_pane_set_mode(wp, &window_view_mode, NULL, NULL);
                    483:                window_copy_vadd(wp, fmt, ap);
1.1       nicm      484:        }
                    485:
                    486:        va_end(ap);
                    487: }
                    488:
                    489: /* Show error from command. */
1.18      nicm      490: void
1.44      nicm      491: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1       nicm      492: {
1.44      nicm      493:        struct client   *c = item->client;
                    494:        struct cmd      *cmd = item->cmd;
1.1       nicm      495:        va_list          ap;
1.20      nicm      496:        char            *msg;
1.1       nicm      497:        size_t           msglen;
1.28      nicm      498:        char            *tmp;
1.1       nicm      499:
                    500:        va_start(ap, fmt);
                    501:        msglen = xvasprintf(&msg, fmt, ap);
                    502:        va_end(ap);
1.57      nicm      503:
                    504:        log_debug("%s: %s", __func__, msg);
1.1       nicm      505:
1.20      nicm      506:        if (c == NULL)
                    507:                cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
                    508:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      509:                if (~c->flags & CLIENT_UTF8) {
                    510:                        tmp = msg;
                    511:                        msg = utf8_sanitize(tmp);
                    512:                        free(tmp);
                    513:                        msglen = strlen(msg);
                    514:                }
1.1       nicm      515:                evbuffer_add(c->stderr_data, msg, msglen);
                    516:                evbuffer_add(c->stderr_data, "\n", 1);
1.29      nicm      517:                server_client_push_stderr(c);
1.13      nicm      518:                c->retval = 1;
1.1       nicm      519:        } else {
                    520:                *msg = toupper((u_char) *msg);
                    521:                status_message_set(c, "%s", msg);
                    522:        }
                    523:
                    524:        free(msg);
                    525: }