[BACK]Return to control.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/control.c, Revision 1.30

1.30    ! nicm        1: /* $OpenBSD: control.c,v 1.29 2020/04/13 15:55:51 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.17      nicm        4:  * Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  * Copyright (c) 2012 George Nachman <tmux@georgester.com>
                      6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     16:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     17:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19:
                     20: #include <sys/types.h>
                     21:
                     22: #include <event.h>
1.2       nicm       23: #include <stdlib.h>
1.1       nicm       24: #include <string.h>
1.10      nicm       25: #include <time.h>
1.1       nicm       26:
                     27: #include "tmux.h"
                     28:
                     29: /* Write a line. */
1.13      nicm       30: void
1.1       nicm       31: control_write(struct client *c, const char *fmt, ...)
                     32: {
1.25      nicm       33:        va_list ap;
1.1       nicm       34:
                     35:        va_start(ap, fmt);
1.25      nicm       36:        file_vprint(c, fmt, ap);
                     37:        file_print(c, "\n");
1.1       nicm       38:        va_end(ap);
                     39: }
                     40:
1.18      nicm       41: /* Control error callback. */
                     42: static enum cmd_retval
1.19      nicm       43: control_error(struct cmdq_item *item, void *data)
1.18      nicm       44: {
1.26      nicm       45:        struct client   *c = cmdq_get_client(item);
1.18      nicm       46:        char            *error = data;
                     47:
1.19      nicm       48:        cmdq_guard(item, "begin", 1);
1.18      nicm       49:        control_write(c, "parse error: %s", error);
1.19      nicm       50:        cmdq_guard(item, "error", 1);
1.18      nicm       51:
                     52:        free(error);
                     53:        return (CMD_RETURN_NORMAL);
                     54: }
                     55:
1.1       nicm       56: /* Control input callback. Read lines and fire commands. */
1.25      nicm       57: static void
                     58: control_callback(__unused struct client *c, __unused const char *path,
1.30    ! nicm       59:     int read_error, int closed, struct evbuffer *buffer, __unused void *data)
1.1       nicm       60: {
1.30    ! nicm       61:        char                    *line, *error;
1.29      nicm       62:        struct cmdq_state       *state;
1.30    ! nicm       63:        enum cmd_parse_status    status;
1.1       nicm       64:
1.30    ! nicm       65:        if (closed || read_error != 0)
1.1       nicm       66:                c->flags |= CLIENT_EXIT;
                     67:
                     68:        for (;;) {
1.25      nicm       69:                line = evbuffer_readln(buffer, NULL, EVBUFFER_EOL_LF);
1.1       nicm       70:                if (line == NULL)
                     71:                        break;
1.25      nicm       72:                log_debug("%s: %s", __func__, line);
1.1       nicm       73:                if (*line == '\0') { /* empty line exit */
1.24      nicm       74:                        free(line);
1.1       nicm       75:                        c->flags |= CLIENT_EXIT;
                     76:                        break;
                     77:                }
                     78:
1.30    ! nicm       79:                state = cmdq_new_state(NULL, NULL, CMDQ_STATE_CONTROL);
        !            80:                status = cmd_parse_and_append(line, NULL, c, state, &error);
        !            81:                if (status == CMD_PARSE_ERROR)
        !            82:                        cmdq_append(c, cmdq_get_callback(control_error, error));
        !            83:                cmdq_free_state(state);
1.1       nicm       84:
1.2       nicm       85:                free(line);
1.1       nicm       86:        }
1.25      nicm       87: }
                     88:
                     89: void
                     90: control_start(struct client *c)
                     91: {
                     92:        file_read(c, "-", control_callback, c);
                     93:
                     94:        if (c->flags & CLIENT_CONTROLCONTROL)
                     95:                file_print(c, "\033P1000p");
1.1       nicm       96: }