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

1.29    ! nicm        1: /* $OpenBSD: cmd-queue.c,v 1.28 2015/11/12 11:10:50 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2013 Nicholas Marriott <nicm@users.sourceforge.net>
                      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.24      nicm       28: enum cmd_retval        cmdq_continue_one(struct cmd_q *);
                     29:
1.1       nicm       30: /* Create new command queue. */
                     31: struct cmd_q *
                     32: cmdq_new(struct client *c)
                     33: {
                     34:        struct cmd_q    *cmdq;
                     35:
                     36:        cmdq = xcalloc(1, sizeof *cmdq);
                     37:        cmdq->references = 1;
1.25      nicm       38:        cmdq->flags = 0;
1.1       nicm       39:
                     40:        cmdq->client = c;
1.11      nicm       41:        cmdq->client_exit = -1;
1.1       nicm       42:
                     43:        TAILQ_INIT(&cmdq->queue);
                     44:        cmdq->item = NULL;
                     45:        cmdq->cmd = NULL;
                     46:
                     47:        return (cmdq);
                     48: }
                     49:
                     50: /* Free command queue */
                     51: int
                     52: cmdq_free(struct cmd_q *cmdq)
                     53: {
1.25      nicm       54:        if (--cmdq->references != 0) {
                     55:                if (cmdq->flags & CMD_Q_DEAD)
                     56:                        return (1);
                     57:                return (0);
                     58:        }
1.1       nicm       59:
                     60:        cmdq_flush(cmdq);
                     61:        free(cmdq);
                     62:        return (1);
                     63: }
                     64:
                     65: /* Show message from command. */
1.18      nicm       66: void
1.1       nicm       67: cmdq_print(struct cmd_q *cmdq, const char *fmt, ...)
                     68: {
                     69:        struct client   *c = cmdq->client;
                     70:        struct window   *w;
                     71:        va_list          ap;
1.28      nicm       72:        char            *tmp, *msg;
1.1       nicm       73:
                     74:        va_start(ap, fmt);
                     75:
                     76:        if (c == NULL)
                     77:                /* nothing */;
                     78:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm       79:                if (~c->flags & CLIENT_UTF8) {
                     80:                        vasprintf(&tmp, fmt, ap);
                     81:                        msg = utf8_sanitize(tmp);
                     82:                        free(tmp);
                     83:                        evbuffer_add(c->stdout_data, msg, strlen(msg));
                     84:                        free(msg);
                     85:                } else
                     86:                        evbuffer_add_vprintf(c->stdout_data, fmt, ap);
1.1       nicm       87:                evbuffer_add(c->stdout_data, "\n", 1);
1.29    ! nicm       88:                server_client_push_stdout(c);
1.1       nicm       89:        } else {
                     90:                w = c->session->curw->window;
                     91:                if (w->active->mode != &window_copy_mode) {
                     92:                        window_pane_reset_mode(w->active);
                     93:                        window_pane_set_mode(w->active, &window_copy_mode);
                     94:                        window_copy_init_for_output(w->active);
                     95:                }
                     96:                window_copy_vadd(w->active, fmt, ap);
                     97:        }
                     98:
                     99:        va_end(ap);
                    100: }
                    101:
                    102: /* Show error from command. */
1.18      nicm      103: void
1.1       nicm      104: cmdq_error(struct cmd_q *cmdq, const char *fmt, ...)
                    105: {
                    106:        struct client   *c = cmdq->client;
                    107:        struct cmd      *cmd = cmdq->cmd;
                    108:        va_list          ap;
1.20      nicm      109:        char            *msg;
1.1       nicm      110:        size_t           msglen;
1.28      nicm      111:        char            *tmp;
1.1       nicm      112:
                    113:        va_start(ap, fmt);
                    114:        msglen = xvasprintf(&msg, fmt, ap);
                    115:        va_end(ap);
                    116:
1.20      nicm      117:        if (c == NULL)
                    118:                cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
                    119:        else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
1.28      nicm      120:                if (~c->flags & CLIENT_UTF8) {
                    121:                        tmp = msg;
                    122:                        msg = utf8_sanitize(tmp);
                    123:                        free(tmp);
                    124:                        msglen = strlen(msg);
                    125:                }
1.1       nicm      126:                evbuffer_add(c->stderr_data, msg, msglen);
                    127:                evbuffer_add(c->stderr_data, "\n", 1);
1.29    ! nicm      128:                server_client_push_stderr(c);
1.13      nicm      129:                c->retval = 1;
1.1       nicm      130:        } else {
                    131:                *msg = toupper((u_char) *msg);
                    132:                status_message_set(c, "%s", msg);
                    133:        }
                    134:
                    135:        free(msg);
                    136: }
                    137:
1.4       nicm      138: /* Print a guard line. */
1.21      nicm      139: void
1.10      nicm      140: cmdq_guard(struct cmd_q *cmdq, const char *guard, int flags)
1.4       nicm      141: {
                    142:        struct client   *c = cmdq->client;
                    143:
1.21      nicm      144:        if (c == NULL || !(c->flags & CLIENT_CONTROL))
                    145:                return;
1.4       nicm      146:
1.9       nicm      147:        evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
                    148:            (long) cmdq->time, cmdq->number, flags);
1.29    ! nicm      149:        server_client_push_stdout(c);
1.4       nicm      150: }
                    151:
1.1       nicm      152: /* Add command list to queue and begin processing if needed. */
                    153: void
1.23      nicm      154: cmdq_run(struct cmd_q *cmdq, struct cmd_list *cmdlist, struct mouse_event *m)
1.1       nicm      155: {
1.23      nicm      156:        cmdq_append(cmdq, cmdlist, m);
1.1       nicm      157:
                    158:        if (cmdq->item == NULL) {
                    159:                cmdq->cmd = NULL;
                    160:                cmdq_continue(cmdq);
                    161:        }
                    162: }
                    163:
                    164: /* Add command list to queue. */
                    165: void
1.23      nicm      166: cmdq_append(struct cmd_q *cmdq, struct cmd_list *cmdlist, struct mouse_event *m)
1.1       nicm      167: {
                    168:        struct cmd_q_item       *item;
                    169:
                    170:        item = xcalloc(1, sizeof *item);
                    171:        item->cmdlist = cmdlist;
                    172:        TAILQ_INSERT_TAIL(&cmdq->queue, item, qentry);
                    173:        cmdlist->references++;
1.23      nicm      174:
                    175:        if (m != NULL)
                    176:                memcpy(&item->mouse, m, sizeof item->mouse);
                    177:        else
                    178:                item->mouse.valid = 0;
1.1       nicm      179: }
                    180:
1.24      nicm      181: /* Process one command. */
                    182: enum cmd_retval
                    183: cmdq_continue_one(struct cmd_q *cmdq)
                    184: {
                    185:        struct cmd      *cmd = cmdq->cmd;
                    186:        enum cmd_retval  retval;
                    187:        char             tmp[1024];
                    188:        int              flags = !!(cmd->flags & CMD_CONTROL);
                    189:
                    190:        cmd_print(cmd, tmp, sizeof tmp);
                    191:        log_debug("cmdq %p: %s", cmdq, tmp);
                    192:
                    193:        cmdq->time = time(NULL);
                    194:        cmdq->number++;
                    195:
                    196:        cmdq_guard(cmdq, "begin", flags);
                    197:
                    198:        retval = cmd->entry->exec(cmd, cmdq);
                    199:
                    200:        if (retval == CMD_RETURN_ERROR)
                    201:                cmdq_guard(cmdq, "error", flags);
                    202:        else
                    203:                cmdq_guard(cmdq, "end", flags);
                    204:        return (retval);
                    205: }
                    206:
1.1       nicm      207: /* Continue processing command queue. Returns 1 if finishes empty. */
                    208: int
                    209: cmdq_continue(struct cmd_q *cmdq)
                    210: {
1.26      nicm      211:        struct client           *c = cmdq->client;
1.1       nicm      212:        struct cmd_q_item       *next;
                    213:        enum cmd_retval          retval;
1.24      nicm      214:        int                      empty;
1.1       nicm      215:
1.22      nicm      216:        cmdq->references++;
1.1       nicm      217:        notify_disable();
1.26      nicm      218:
1.27      nicm      219:        log_debug("continuing cmdq %p: flags %#x, client %p", cmdq, cmdq->flags,
                    220:            c);
1.1       nicm      221:
                    222:        empty = TAILQ_EMPTY(&cmdq->queue);
                    223:        if (empty)
                    224:                goto empty;
                    225:
                    226:        if (cmdq->item == NULL) {
                    227:                cmdq->item = TAILQ_FIRST(&cmdq->queue);
                    228:                cmdq->cmd = TAILQ_FIRST(&cmdq->item->cmdlist->list);
                    229:        } else
                    230:                cmdq->cmd = TAILQ_NEXT(cmdq->cmd, qentry);
                    231:
                    232:        do {
                    233:                while (cmdq->cmd != NULL) {
1.24      nicm      234:                        retval = cmdq_continue_one(cmdq);
1.1       nicm      235:                        if (retval == CMD_RETURN_ERROR)
                    236:                                break;
                    237:                        if (retval == CMD_RETURN_WAIT)
                    238:                                goto out;
                    239:                        if (retval == CMD_RETURN_STOP) {
                    240:                                cmdq_flush(cmdq);
                    241:                                goto empty;
                    242:                        }
                    243:                        cmdq->cmd = TAILQ_NEXT(cmdq->cmd, qentry);
                    244:                }
1.19      nicm      245:                next = TAILQ_NEXT(cmdq->item, qentry);
1.1       nicm      246:
                    247:                TAILQ_REMOVE(&cmdq->queue, cmdq->item, qentry);
                    248:                cmd_list_free(cmdq->item->cmdlist);
                    249:                free(cmdq->item);
                    250:
                    251:                cmdq->item = next;
                    252:                if (cmdq->item != NULL)
                    253:                        cmdq->cmd = TAILQ_FIRST(&cmdq->item->cmdlist->list);
                    254:        } while (cmdq->item != NULL);
                    255:
                    256: empty:
1.11      nicm      257:        if (cmdq->client_exit > 0)
1.1       nicm      258:                cmdq->client->flags |= CLIENT_EXIT;
                    259:        if (cmdq->emptyfn != NULL)
1.22      nicm      260:                cmdq->emptyfn(cmdq);
1.1       nicm      261:        empty = 1;
                    262:
                    263: out:
                    264:        notify_enable();
1.22      nicm      265:        cmdq_free(cmdq);
                    266:
1.1       nicm      267:        return (empty);
                    268: }
                    269:
                    270: /* Flush command queue. */
                    271: void
                    272: cmdq_flush(struct cmd_q *cmdq)
                    273: {
                    274:        struct cmd_q_item       *item, *item1;
                    275:
                    276:        TAILQ_FOREACH_SAFE(item, &cmdq->queue, qentry, item1) {
                    277:                TAILQ_REMOVE(&cmdq->queue, item, qentry);
                    278:                cmd_list_free(item->cmdlist);
                    279:                free(item);
                    280:        }
                    281:        cmdq->item = NULL;
                    282: }