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

1.28    ! nicm        1: /* $OpenBSD: control.c,v 1.27 2020/04/13 14:04:25 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,
                     59:     int error, int closed, struct evbuffer *buffer, __unused void *data)
1.1       nicm       60: {
1.22      nicm       61:        char                    *line;
1.19      nicm       62:        struct cmdq_item        *item;
1.22      nicm       63:        struct cmd_parse_result *pr;
1.1       nicm       64:
1.25      nicm       65:        if (closed || 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.22      nicm       79:                pr = cmd_parse_from_string(line, NULL);
                     80:                switch (pr->status) {
                     81:                case CMD_PARSE_EMPTY:
                     82:                        break;
                     83:                case CMD_PARSE_ERROR:
                     84:                        item = cmdq_get_callback(control_error, pr->error);
1.19      nicm       85:                        cmdq_append(c, item);
1.22      nicm       86:                        break;
                     87:                case CMD_PARSE_SUCCESS:
1.28    ! nicm       88:                        item = cmdq_get_command(pr->cmdlist, NULL, NULL,
        !            89:                            CMDQ_STATE_CONTROL);
1.19      nicm       90:                        cmdq_append(c, item);
1.22      nicm       91:                        cmd_list_free(pr->cmdlist);
                     92:                        break;
1.1       nicm       93:                }
                     94:
1.2       nicm       95:                free(line);
1.1       nicm       96:        }
1.25      nicm       97: }
                     98:
                     99: void
                    100: control_start(struct client *c)
                    101: {
                    102:        file_read(c, "-", control_callback, c);
                    103:
                    104:        if (c->flags & CLIENT_CONTROLCONTROL)
                    105:                file_print(c, "\033P1000p");
1.1       nicm      106: }