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

1.50    ! nicm        1: /* $OpenBSD: cmd-queue.c,v 1.49 2017/04/21 14:01:19 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.43      nicm       69:
1.44      nicm       70:                item = next;
                     71:        } while (item != NULL);
1.43      nicm       72: }
                     73:
                     74: /* Insert an item. */
                     75: void
1.44      nicm       76: cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
1.43      nicm       77: {
                     78:        struct client           *c = after->client;
1.44      nicm       79:        struct cmdq_list        *queue = after->queue;
                     80:        struct cmdq_item        *next;
1.43      nicm       81:
                     82:        do {
1.44      nicm       83:                next = item->next;
                     84:                item->next = NULL;
1.43      nicm       85:
                     86:                if (c != NULL)
                     87:                        c->references++;
1.44      nicm       88:                item->client = c;
1.43      nicm       89:
1.44      nicm       90:                item->queue = queue;
1.43      nicm       91:                if (after->next != NULL)
1.44      nicm       92:                        TAILQ_INSERT_AFTER(queue, after->next, item, entry);
1.43      nicm       93:                else
1.44      nicm       94:                        TAILQ_INSERT_AFTER(queue, after, item, entry);
                     95:                after->next = item;
1.43      nicm       96:
1.44      nicm       97:                item = next;
                     98:        } while (item != NULL);
1.43      nicm       99: }
                    100:
                    101: /* Remove an item. */
                    102: static void
1.44      nicm      103: cmdq_remove(struct cmdq_item *item)
1.43      nicm      104: {
1.49      nicm      105:        if (item->shared != NULL && --item->shared->references == 0) {
                    106:                if (item->shared->formats != NULL)
                    107:                        format_free(item->shared->formats);
                    108:                free(item->shared);
                    109:        }
1.43      nicm      110:
1.44      nicm      111:        if (item->client != NULL)
                    112:                server_client_unref(item->client);
1.43      nicm      113:
1.44      nicm      114:        if (item->type == CMDQ_COMMAND)
                    115:                cmd_list_free(item->cmdlist);
1.43      nicm      116:
1.44      nicm      117:        TAILQ_REMOVE(item->queue, item, entry);
1.46      nicm      118:
                    119:        free((void *)item->name);
1.44      nicm      120:        free(item);
1.43      nicm      121: }
                    122:
                    123: /* Set command group. */
                    124: static u_int
                    125: cmdq_next_group(void)
                    126: {
                    127:        static u_int    group;
                    128:
                    129:        return (++group);
                    130: }
                    131:
                    132: /* Remove all subsequent items that match this item's group. */
                    133: static void
1.44      nicm      134: cmdq_remove_group(struct cmdq_item *item)
1.43      nicm      135: {
1.44      nicm      136:        struct cmdq_item        *this, *next;
1.43      nicm      137:
1.44      nicm      138:        this = TAILQ_NEXT(item, entry);
1.43      nicm      139:        while (this != NULL) {
                    140:                next = TAILQ_NEXT(this, entry);
1.44      nicm      141:                if (this->group == item->group)
1.43      nicm      142:                        cmdq_remove(this);
                    143:                this = next;
                    144:        }
                    145: }
                    146:
                    147: /* Get a command for the command queue. */
1.44      nicm      148: struct cmdq_item *
1.43      nicm      149: cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
                    150:     struct mouse_event *m, int flags)
                    151: {
1.44      nicm      152:        struct cmdq_item        *item, *first = NULL, *last = NULL;
                    153:        struct cmd              *cmd;
                    154:        u_int                    group = cmdq_next_group();
1.46      nicm      155:        char                    *tmp;
1.49      nicm      156:        struct cmdq_shared      *shared;
                    157:
                    158:        shared = xcalloc(1, sizeof *shared);
                    159:        if (current != NULL)
                    160:                cmd_find_copy_state(&shared->current, current);
                    161:        if (m != NULL)
                    162:                memcpy(&shared->mouse, m, sizeof shared->mouse);
1.43      nicm      163:
                    164:        TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
1.46      nicm      165:                xasprintf(&tmp, "command[%s]", cmd->entry->name);
                    166:
1.44      nicm      167:                item = xcalloc(1, sizeof *item);
1.46      nicm      168:                item->name = tmp;
1.44      nicm      169:                item->type = CMDQ_COMMAND;
1.46      nicm      170:
1.44      nicm      171:                item->group = group;
                    172:                item->flags = flags;
1.43      nicm      173:
1.49      nicm      174:                item->shared = shared;
1.44      nicm      175:                item->cmdlist = cmdlist;
                    176:                item->cmd = cmd;
1.43      nicm      177:
1.49      nicm      178:                shared->references++;
1.43      nicm      179:                cmdlist->references++;
                    180:
                    181:                if (first == NULL)
1.44      nicm      182:                        first = item;
1.43      nicm      183:                if (last != NULL)
1.44      nicm      184:                        last->next = item;
                    185:                last = item;
1.43      nicm      186:        }
                    187:        return (first);
                    188: }
                    189:
                    190: /* Fire command on command queue. */
                    191: static enum cmd_retval
1.44      nicm      192: cmdq_fire_command(struct cmdq_item *item)
1.43      nicm      193: {
1.44      nicm      194:        struct client           *c = item->client;
                    195:        struct cmd              *cmd = item->cmd;
1.43      nicm      196:        enum cmd_retval          retval;
                    197:        const char              *name;
                    198:        struct cmd_find_state   *fsp, fs;
                    199:        int                      flags;
                    200:
                    201:        flags = !!(cmd->flags & CMD_CONTROL);
1.44      nicm      202:        cmdq_guard(item, "begin", flags);
1.43      nicm      203:
1.44      nicm      204:        if (cmd_prepare_state(cmd, item) != 0) {
1.43      nicm      205:                retval = CMD_RETURN_ERROR;
                    206:                goto out;
                    207:        }
1.44      nicm      208:        if (item->client == NULL)
1.50    ! nicm      209:                item->client = cmd_find_client(item, NULL, 1);
1.43      nicm      210:
1.44      nicm      211:        retval = cmd->entry->exec(cmd, item);
1.43      nicm      212:        if (retval == CMD_RETURN_ERROR)
                    213:                goto out;
                    214:
                    215:        if (cmd->entry->flags & CMD_AFTERHOOK) {
                    216:                name = cmd->entry->name;
1.44      nicm      217:                if (cmd_find_valid_state(&item->state.tflag))
                    218:                        fsp = &item->state.tflag;
1.43      nicm      219:                else {
1.44      nicm      220:                        if (cmd_find_current(&fs, item, CMD_FIND_QUIET) != 0)
1.43      nicm      221:                                goto out;
                    222:                        fsp = &fs;
                    223:                }
1.44      nicm      224:                hooks_insert(fsp->s->hooks, item, fsp, "after-%s", name);
1.43      nicm      225:        }
                    226:
                    227: out:
1.44      nicm      228:        item->client = c;
1.43      nicm      229:        if (retval == CMD_RETURN_ERROR)
1.44      nicm      230:                cmdq_guard(item, "error", flags);
1.43      nicm      231:        else
1.44      nicm      232:                cmdq_guard(item, "end", flags);
1.43      nicm      233:        return (retval);
                    234: }
                    235:
                    236: /* Get a callback for the command queue. */
1.44      nicm      237: struct cmdq_item *
1.46      nicm      238: cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
1.1       nicm      239: {
1.44      nicm      240:        struct cmdq_item        *item;
1.46      nicm      241:        char                    *tmp;
                    242:
                    243:        xasprintf(&tmp, "callback[%s]", name);
1.1       nicm      244:
1.44      nicm      245:        item = xcalloc(1, sizeof *item);
1.46      nicm      246:        item->name = tmp;
1.44      nicm      247:        item->type = CMDQ_CALLBACK;
1.46      nicm      248:
1.44      nicm      249:        item->group = 0;
                    250:        item->flags = 0;
1.1       nicm      251:
1.44      nicm      252:        item->cb = cb;
                    253:        item->data = data;
1.1       nicm      254:
1.44      nicm      255:        return (item);
1.43      nicm      256: }
1.1       nicm      257:
1.43      nicm      258: /* Fire callback on callback queue. */
                    259: static enum cmd_retval
1.44      nicm      260: cmdq_fire_callback(struct cmdq_item *item)
1.43      nicm      261: {
1.44      nicm      262:        return (item->cb(item, item->data));
1.43      nicm      263: }
1.45      nicm      264:
                    265: /* Add a format to command queue. */
                    266: void
                    267: cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...)
                    268: {
1.49      nicm      269:        struct cmdq_shared      *shared = item->shared;
1.45      nicm      270:        va_list                  ap;
                    271:        char                    *value;
                    272:
                    273:        va_start(ap, fmt);
                    274:        xvasprintf(&value, fmt, ap);
                    275:        va_end(ap);
                    276:
1.49      nicm      277:        if (shared->formats == NULL)
                    278:                shared->formats = format_create(NULL, FORMAT_NONE, 0);
                    279:        format_add(shared->formats, key, "%s", value);
1.45      nicm      280:
                    281:        free(value);
                    282: }
1.33      nicm      283:
1.43      nicm      284: /* Process next item on command queue. */
                    285: u_int
                    286: cmdq_next(struct client *c)
                    287: {
1.44      nicm      288:        struct cmdq_list        *queue = cmdq_get(c);
1.43      nicm      289:        const char              *name = cmdq_name(c);
1.44      nicm      290:        struct cmdq_item        *item;
1.43      nicm      291:        enum cmd_retval          retval;
                    292:        u_int                    items = 0;
                    293:        static u_int             number;
1.1       nicm      294:
1.43      nicm      295:        if (TAILQ_EMPTY(queue)) {
                    296:                log_debug("%s %s: empty", __func__, name);
1.25      nicm      297:                return (0);
                    298:        }
1.44      nicm      299:        if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
1.43      nicm      300:                log_debug("%s %s: waiting", __func__, name);
                    301:                return (0);
                    302:        }
                    303:
                    304:        log_debug("%s %s: enter", __func__, name);
                    305:        for (;;) {
1.44      nicm      306:                item = TAILQ_FIRST(queue);
                    307:                if (item == NULL)
1.43      nicm      308:                        break;
1.46      nicm      309:                log_debug("%s %s: %s (%d), flags %x", __func__, name,
                    310:                    item->name, item->type, item->flags);
1.43      nicm      311:
                    312:                /*
                    313:                 * Any item with the waiting flag set waits until an external
                    314:                 * event clears the flag (for example, a job - look at
                    315:                 * run-shell).
                    316:                 */
1.44      nicm      317:                if (item->flags & CMDQ_WAITING)
1.43      nicm      318:                        goto waiting;
                    319:
                    320:                /*
                    321:                 * Items are only fired once, once the fired flag is set, a
                    322:                 * waiting flag can only be cleared by an external event.
                    323:                 */
1.44      nicm      324:                if (~item->flags & CMDQ_FIRED) {
                    325:                        item->time = time(NULL);
                    326:                        item->number = ++number;
1.43      nicm      327:
1.50    ! nicm      328:                        switch (item->type) {
1.44      nicm      329:                        case CMDQ_COMMAND:
                    330:                                retval = cmdq_fire_command(item);
1.43      nicm      331:
                    332:                                /*
                    333:                                 * If a command returns an error, remove any
                    334:                                 * subsequent commands in the same group.
                    335:                                 */
                    336:                                if (retval == CMD_RETURN_ERROR)
1.44      nicm      337:                                        cmdq_remove_group(item);
1.43      nicm      338:                                break;
1.44      nicm      339:                        case CMDQ_CALLBACK:
                    340:                                retval = cmdq_fire_callback(item);
1.43      nicm      341:                                break;
                    342:                        default:
                    343:                                retval = CMD_RETURN_ERROR;
                    344:                                break;
                    345:                        }
1.44      nicm      346:                        item->flags |= CMDQ_FIRED;
1.43      nicm      347:
                    348:                        if (retval == CMD_RETURN_WAIT) {
1.44      nicm      349:                                item->flags |= CMDQ_WAITING;
1.43      nicm      350:                                goto waiting;
                    351:                        }
                    352:                        items++;
                    353:                }
1.44      nicm      354:                cmdq_remove(item);
1.43      nicm      355:        }
                    356:
                    357:        log_debug("%s %s: exit (empty)", __func__, name);
                    358:        return (items);
1.1       nicm      359:
1.43      nicm      360: waiting:
                    361:        log_debug("%s %s: exit (wait)", __func__, name);
                    362:        return (items);
                    363: }
                    364:
                    365: /* Print a guard line. */
                    366: void
1.44      nicm      367: cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
1.43      nicm      368: {
1.44      nicm      369:        struct client   *c = item->client;
1.43      nicm      370:
                    371:        if (c == NULL || !(c->flags & CLIENT_CONTROL))
                    372:                return;
                    373:
                    374:        evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
1.44      nicm      375:            (long)item->time, item->number, flags);
1.43      nicm      376:        server_client_push_stdout(c);
1.1       nicm      377: }
                    378:
                    379: /* Show message from command. */
1.18      nicm      380: void
1.44      nicm      381: cmdq_print(struct cmdq_item *item, const char *fmt, ...)
1.1       nicm      382: {
1.44      nicm      383:        struct client   *c = item->client;
1.1       nicm      384:        struct window   *w;
                    385:        va_list          ap;
1.28      nicm      386:        char            *tmp, *msg;
1.1       nicm      387:
                    388:        va_start(ap, fmt);
                    389:
                    390:        if (c == NULL)
                    391:                /* nothing */;
                    392:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      393:                if (~c->flags & CLIENT_UTF8) {
1.37      nicm      394:                        xvasprintf(&tmp, fmt, ap);
1.28      nicm      395:                        msg = utf8_sanitize(tmp);
                    396:                        free(tmp);
                    397:                        evbuffer_add(c->stdout_data, msg, strlen(msg));
                    398:                        free(msg);
                    399:                } else
                    400:                        evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1       nicm      401:                evbuffer_add(c->stdout_data, "\n", 1);
1.29      nicm      402:                server_client_push_stdout(c);
1.1       nicm      403:        } else {
                    404:                w = c->session->curw->window;
                    405:                if (w->active->mode != &window_copy_mode) {
                    406:                        window_pane_reset_mode(w->active);
                    407:                        window_pane_set_mode(w->active, &window_copy_mode);
                    408:                        window_copy_init_for_output(w->active);
                    409:                }
                    410:                window_copy_vadd(w->active, fmt, ap);
                    411:        }
                    412:
                    413:        va_end(ap);
                    414: }
                    415:
                    416: /* Show error from command. */
1.18      nicm      417: void
1.44      nicm      418: cmdq_error(struct cmdq_item *item, const char *fmt, ...)
1.1       nicm      419: {
1.44      nicm      420:        struct client   *c = item->client;
                    421:        struct cmd      *cmd = item->cmd;
1.1       nicm      422:        va_list          ap;
1.20      nicm      423:        char            *msg;
1.1       nicm      424:        size_t           msglen;
1.28      nicm      425:        char            *tmp;
1.1       nicm      426:
                    427:        va_start(ap, fmt);
                    428:        msglen = xvasprintf(&msg, fmt, ap);
                    429:        va_end(ap);
                    430:
1.20      nicm      431:        if (c == NULL)
                    432:                cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
                    433:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      434:                if (~c->flags & CLIENT_UTF8) {
                    435:                        tmp = msg;
                    436:                        msg = utf8_sanitize(tmp);
                    437:                        free(tmp);
                    438:                        msglen = strlen(msg);
                    439:                }
1.1       nicm      440:                evbuffer_add(c->stderr_data, msg, msglen);
                    441:                evbuffer_add(c->stderr_data, "\n", 1);
1.29      nicm      442:                server_client_push_stderr(c);
1.13      nicm      443:                c->retval = 1;
1.1       nicm      444:        } else {
                    445:                *msg = toupper((u_char) *msg);
                    446:                status_message_set(c, "%s", msg);
                    447:        }
                    448:
                    449:        free(msg);
                    450: }