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

1.69    ! nicm        1: /* $OpenBSD: cmd-queue.c,v 1.68 2019/05/23 11:13:30 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: /* Remove all subsequent items that match this item's group. */
                    179: static void
1.44      nicm      180: cmdq_remove_group(struct cmdq_item *item)
1.43      nicm      181: {
1.44      nicm      182:        struct cmdq_item        *this, *next;
1.43      nicm      183:
1.69    ! nicm      184:        if (item->group == 0)
        !           185:                return;
1.44      nicm      186:        this = TAILQ_NEXT(item, entry);
1.43      nicm      187:        while (this != NULL) {
                    188:                next = TAILQ_NEXT(this, entry);
1.44      nicm      189:                if (this->group == item->group)
1.43      nicm      190:                        cmdq_remove(this);
                    191:                this = next;
                    192:        }
                    193: }
                    194:
                    195: /* Get a command for the command queue. */
1.44      nicm      196: struct cmdq_item *
1.43      nicm      197: cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
                    198:     struct mouse_event *m, int flags)
                    199: {
1.44      nicm      200:        struct cmdq_item        *item, *first = NULL, *last = NULL;
                    201:        struct cmd              *cmd;
1.49      nicm      202:        struct cmdq_shared      *shared;
                    203:
                    204:        shared = xcalloc(1, sizeof *shared);
                    205:        if (current != NULL)
                    206:                cmd_find_copy_state(&shared->current, current);
1.52      nicm      207:        else
                    208:                cmd_find_clear_state(&shared->current, 0);
1.49      nicm      209:        if (m != NULL)
                    210:                memcpy(&shared->mouse, m, sizeof shared->mouse);
1.43      nicm      211:
                    212:        TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
1.44      nicm      213:                item = xcalloc(1, sizeof *item);
1.64      nicm      214:                xasprintf(&item->name, "[%s/%p]", cmd->entry->name, item);
1.44      nicm      215:                item->type = CMDQ_COMMAND;
1.46      nicm      216:
1.68      nicm      217:                item->group = cmd->group;
1.44      nicm      218:                item->flags = flags;
1.43      nicm      219:
1.49      nicm      220:                item->shared = shared;
1.44      nicm      221:                item->cmdlist = cmdlist;
                    222:                item->cmd = cmd;
1.68      nicm      223:
                    224:                log_debug("%s: %s group %u", __func__, item->name, item->group);
1.43      nicm      225:
1.49      nicm      226:                shared->references++;
1.43      nicm      227:                cmdlist->references++;
                    228:
                    229:                if (first == NULL)
1.44      nicm      230:                        first = item;
1.43      nicm      231:                if (last != NULL)
1.44      nicm      232:                        last->next = item;
                    233:                last = item;
1.43      nicm      234:        }
                    235:        return (first);
                    236: }
                    237:
1.54      nicm      238: /* Fill in flag for a command. */
                    239: static enum cmd_retval
                    240: cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs,
                    241:     const struct cmd_entry_flag *flag)
                    242: {
                    243:        const char      *value;
                    244:
                    245:        if (flag->flag == 0) {
                    246:                cmd_find_clear_state(fs, 0);
                    247:                return (CMD_RETURN_NORMAL);
                    248:        }
                    249:
                    250:        value = args_get(item->cmd->args, flag->flag);
                    251:        if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) {
                    252:                cmd_find_clear_state(fs, 0);
                    253:                return (CMD_RETURN_ERROR);
                    254:        }
                    255:        return (CMD_RETURN_NORMAL);
                    256: }
                    257:
1.43      nicm      258: /* Fire command on command queue. */
                    259: static enum cmd_retval
1.44      nicm      260: cmdq_fire_command(struct cmdq_item *item)
1.43      nicm      261: {
1.44      nicm      262:        struct client           *c = item->client;
1.66      nicm      263:        struct cmdq_shared      *shared = item->shared;
1.44      nicm      264:        struct cmd              *cmd = item->cmd;
1.54      nicm      265:        const struct cmd_entry  *entry = cmd->entry;
1.43      nicm      266:        enum cmd_retval          retval;
                    267:        struct cmd_find_state   *fsp, fs;
                    268:        int                      flags;
                    269:
1.66      nicm      270:        flags = !!(shared->flags & CMDQ_SHARED_CONTROL);
1.44      nicm      271:        cmdq_guard(item, "begin", flags);
1.43      nicm      272:
1.53      nicm      273:        if (item->client == NULL)
                    274:                item->client = cmd_find_client(item, NULL, 1);
1.54      nicm      275:        retval = cmdq_find_flag(item, &item->source, &entry->source);
                    276:        if (retval == CMD_RETURN_ERROR)
                    277:                goto out;
                    278:        retval = cmdq_find_flag(item, &item->target, &entry->target);
                    279:        if (retval == CMD_RETURN_ERROR)
1.43      nicm      280:                goto out;
                    281:
1.54      nicm      282:        retval = entry->exec(cmd, item);
1.43      nicm      283:        if (retval == CMD_RETURN_ERROR)
                    284:                goto out;
                    285:
1.54      nicm      286:        if (entry->flags & CMD_AFTERHOOK) {
                    287:                if (cmd_find_valid_state(&item->target))
                    288:                        fsp = &item->target;
1.51      nicm      289:                else if (cmd_find_valid_state(&item->shared->current))
                    290:                        fsp = &item->shared->current;
1.58      nicm      291:                else if (cmd_find_from_client(&fs, item->client, 0) == 0)
1.43      nicm      292:                        fsp = &fs;
1.51      nicm      293:                else
                    294:                        goto out;
1.63      nicm      295:                cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name);
1.43      nicm      296:        }
                    297:
                    298: out:
1.44      nicm      299:        item->client = c;
1.43      nicm      300:        if (retval == CMD_RETURN_ERROR)
1.44      nicm      301:                cmdq_guard(item, "error", flags);
1.43      nicm      302:        else
1.44      nicm      303:                cmdq_guard(item, "end", flags);
1.43      nicm      304:        return (retval);
                    305: }
                    306:
                    307: /* Get a callback for the command queue. */
1.44      nicm      308: struct cmdq_item *
1.46      nicm      309: cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
1.1       nicm      310: {
1.44      nicm      311:        struct cmdq_item        *item;
1.1       nicm      312:
1.44      nicm      313:        item = xcalloc(1, sizeof *item);
1.64      nicm      314:        xasprintf(&item->name, "[%s/%p]", name, item);
1.44      nicm      315:        item->type = CMDQ_CALLBACK;
1.46      nicm      316:
1.44      nicm      317:        item->group = 0;
                    318:        item->flags = 0;
1.1       nicm      319:
1.44      nicm      320:        item->cb = cb;
                    321:        item->data = data;
1.1       nicm      322:
1.44      nicm      323:        return (item);
1.67      nicm      324: }
                    325:
                    326: /* Generic error callback. */
                    327: static enum cmd_retval
                    328: cmdq_error_callback(struct cmdq_item *item, void *data)
                    329: {
                    330:        char    *error = data;
                    331:
                    332:        cmdq_error(item, "%s", error);
                    333:        free(error);
                    334:
                    335:        return (CMD_RETURN_NORMAL);
                    336: }
                    337:
                    338: /* Get an error callback for the command queue. */
                    339: struct cmdq_item *
                    340: cmdq_get_error(const char *error)
                    341: {
                    342:        return (cmdq_get_callback(cmdq_error_callback, xstrdup(error)));
1.43      nicm      343: }
1.1       nicm      344:
1.43      nicm      345: /* Fire callback on callback queue. */
                    346: static enum cmd_retval
1.44      nicm      347: cmdq_fire_callback(struct cmdq_item *item)
1.43      nicm      348: {
1.44      nicm      349:        return (item->cb(item, item->data));
1.43      nicm      350: }
1.45      nicm      351:
                    352: /* Add a format to command queue. */
                    353: void
                    354: cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...)
                    355: {
1.49      nicm      356:        struct cmdq_shared      *shared = item->shared;
1.45      nicm      357:        va_list                  ap;
                    358:        char                    *value;
                    359:
                    360:        va_start(ap, fmt);
                    361:        xvasprintf(&value, fmt, ap);
                    362:        va_end(ap);
                    363:
1.49      nicm      364:        if (shared->formats == NULL)
1.55      nicm      365:                shared->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
1.49      nicm      366:        format_add(shared->formats, key, "%s", value);
1.45      nicm      367:
                    368:        free(value);
                    369: }
1.33      nicm      370:
1.43      nicm      371: /* Process next item on command queue. */
                    372: u_int
                    373: cmdq_next(struct client *c)
                    374: {
1.44      nicm      375:        struct cmdq_list        *queue = cmdq_get(c);
1.43      nicm      376:        const char              *name = cmdq_name(c);
1.44      nicm      377:        struct cmdq_item        *item;
1.43      nicm      378:        enum cmd_retval          retval;
                    379:        u_int                    items = 0;
                    380:        static u_int             number;
1.1       nicm      381:
1.43      nicm      382:        if (TAILQ_EMPTY(queue)) {
                    383:                log_debug("%s %s: empty", __func__, name);
1.25      nicm      384:                return (0);
                    385:        }
1.44      nicm      386:        if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43      nicm      387:                log_debug("%s %s: waiting", __func__, name);
                    388:                return (0);
                    389:        }
                    390:
                    391:        log_debug("%s %s: enter", __func__, name);
                    392:        for (;;) {
1.44      nicm      393:                item = TAILQ_FIRST(queue);
                    394:                if (item == NULL)
1.43      nicm      395:                        break;
1.46      nicm      396:                log_debug("%s %s: %s (%d), flags %x", __func__, name,
                    397:                    item->name, item->type, item->flags);
1.43      nicm      398:
                    399:                /*
                    400:                 * Any item with the waiting flag set waits until an external
                    401:                 * event clears the flag (for example, a job - look at
                    402:                 * run-shell).
                    403:                 */
1.44      nicm      404:                if (item->flags & CMDQ_WAITING)
1.43      nicm      405:                        goto waiting;
                    406:
                    407:                /*
                    408:                 * Items are only fired once, once the fired flag is set, a
                    409:                 * waiting flag can only be cleared by an external event.
                    410:                 */
1.44      nicm      411:                if (~item->flags & CMDQ_FIRED) {
                    412:                        item->time = time(NULL);
                    413:                        item->number = ++number;
1.43      nicm      414:
1.50      nicm      415:                        switch (item->type) {
1.44      nicm      416:                        case CMDQ_COMMAND:
                    417:                                retval = cmdq_fire_command(item);
1.43      nicm      418:
                    419:                                /*
                    420:                                 * If a command returns an error, remove any
                    421:                                 * subsequent commands in the same group.
                    422:                                 */
                    423:                                if (retval == CMD_RETURN_ERROR)
1.44      nicm      424:                                        cmdq_remove_group(item);
1.43      nicm      425:                                break;
1.44      nicm      426:                        case CMDQ_CALLBACK:
                    427:                                retval = cmdq_fire_callback(item);
1.43      nicm      428:                                break;
                    429:                        default:
                    430:                                retval = CMD_RETURN_ERROR;
                    431:                                break;
                    432:                        }
1.44      nicm      433:                        item->flags |= CMDQ_FIRED;
1.43      nicm      434:
                    435:                        if (retval == CMD_RETURN_WAIT) {
1.44      nicm      436:                                item->flags |= CMDQ_WAITING;
1.43      nicm      437:                                goto waiting;
                    438:                        }
                    439:                        items++;
                    440:                }
1.44      nicm      441:                cmdq_remove(item);
1.43      nicm      442:        }
                    443:
                    444:        log_debug("%s %s: exit (empty)", __func__, name);
                    445:        return (items);
1.1       nicm      446:
1.43      nicm      447: waiting:
                    448:        log_debug("%s %s: exit (wait)", __func__, name);
                    449:        return (items);
                    450: }
                    451:
                    452: /* Print a guard line. */
                    453: void
1.44      nicm      454: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43      nicm      455: {
1.44      nicm      456:        struct client   *c = item->client;
1.43      nicm      457:
                    458:        if (c == NULL || !(c->flags & CLIENT_CONTROL))
                    459:                return;
                    460:
                    461:        evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
1.44      nicm      462:            (long)item->time, item->number, flags);
1.43      nicm      463:        server_client_push_stdout(c);
1.1       nicm      464: }
                    465:
                    466: /* Show message from command. */
1.18      nicm      467: void
1.44      nicm      468: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1       nicm      469: {
1.61      nicm      470:        struct client                   *c = item->client;
                    471:        struct window_pane              *wp;
                    472:        struct window_mode_entry        *wme;
                    473:        va_list                          ap;
                    474:        char                            *tmp, *msg;
1.1       nicm      475:
                    476:        va_start(ap, fmt);
                    477:
                    478:        if (c == NULL)
                    479:                /* nothing */;
                    480:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      481:                if (~c->flags & CLIENT_UTF8) {
1.37      nicm      482:                        xvasprintf(&tmp, fmt, ap);
1.28      nicm      483:                        msg = utf8_sanitize(tmp);
                    484:                        free(tmp);
                    485:                        evbuffer_add(c->stdout_data, msg, strlen(msg));
                    486:                        free(msg);
                    487:                } else
                    488:                        evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1       nicm      489:                evbuffer_add(c->stdout_data, "\n", 1);
1.29      nicm      490:                server_client_push_stdout(c);
1.1       nicm      491:        } else {
1.60      nicm      492:                wp = c->session->curw->window->active;
1.61      nicm      493:                wme = TAILQ_FIRST(&wp->modes);
                    494:                if (wme == NULL || wme->mode != &window_view_mode)
1.60      nicm      495:                        window_pane_set_mode(wp, &window_view_mode, NULL, NULL);
                    496:                window_copy_vadd(wp, fmt, ap);
1.1       nicm      497:        }
                    498:
                    499:        va_end(ap);
                    500: }
                    501:
                    502: /* Show error from command. */
1.18      nicm      503: void
1.44      nicm      504: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1       nicm      505: {
1.44      nicm      506:        struct client   *c = item->client;
                    507:        struct cmd      *cmd = item->cmd;
1.1       nicm      508:        va_list          ap;
1.20      nicm      509:        char            *msg;
1.1       nicm      510:        size_t           msglen;
1.28      nicm      511:        char            *tmp;
1.1       nicm      512:
                    513:        va_start(ap, fmt);
                    514:        msglen = xvasprintf(&msg, fmt, ap);
                    515:        va_end(ap);
1.57      nicm      516:
                    517:        log_debug("%s: %s", __func__, msg);
1.1       nicm      518:
1.20      nicm      519:        if (c == NULL)
                    520:                cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
                    521:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      522:                if (~c->flags & CLIENT_UTF8) {
                    523:                        tmp = msg;
                    524:                        msg = utf8_sanitize(tmp);
                    525:                        free(tmp);
                    526:                        msglen = strlen(msg);
                    527:                }
1.1       nicm      528:                evbuffer_add(c->stderr_data, msg, msglen);
                    529:                evbuffer_add(c->stderr_data, "\n", 1);
1.29      nicm      530:                server_client_push_stderr(c);
1.13      nicm      531:                c->retval = 1;
1.1       nicm      532:        } else {
                    533:                *msg = toupper((u_char) *msg);
                    534:                status_message_set(c, "%s", msg);
                    535:        }
                    536:
                    537:        free(msg);
                    538: }