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

Annotation of src/usr.bin/tmux/server-msg.c, Revision 1.23

1.23    ! nicm        1: /* $OpenBSD: server-msg.c,v 1.22 2009/09/24 07:02:56 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 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>
1.19      nicm       20: #include <sys/ioctl.h>
1.1       nicm       21:
                     22: #include <errno.h>
1.21      nicm       23: #include <paths.h>
1.1       nicm       24: #include <stdlib.h>
                     25: #include <string.h>
                     26: #include <time.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "tmux.h"
                     30:
1.9       nicm       31: void   server_msg_command(struct client *, struct msg_command_data *);
1.13      nicm       32: void   server_msg_identify(struct client *, struct msg_identify_data *, int);
1.21      nicm       33: void   server_msg_shell(struct client *);
1.9       nicm       34:
                     35: void printflike2 server_msg_command_error(struct cmd_ctx *, const char *, ...);
                     36: void printflike2 server_msg_command_print(struct cmd_ctx *, const char *, ...);
                     37: void printflike2 server_msg_command_info(struct cmd_ctx *, const char *, ...);
1.1       nicm       38:
                     39: int
                     40: server_msg_dispatch(struct client *c)
                     41: {
1.11      nicm       42:        struct imsg              imsg;
1.9       nicm       43:        struct msg_command_data  commanddata;
                     44:        struct msg_identify_data identifydata;
1.10      nicm       45:        struct msg_environ_data  environdata;
1.11      nicm       46:        ssize_t                  n, datalen;
                     47:
                     48:         if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                     49:                 return (-1);
1.1       nicm       50:
                     51:        for (;;) {
1.11      nicm       52:                if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
                     53:                        return (-1);
                     54:                if (n == 0)
1.1       nicm       55:                        return (0);
1.11      nicm       56:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                     57:
                     58:                if (imsg.hdr.peerid != PROTOCOL_VERSION) {
                     59:                        server_write_client(c, MSG_VERSION, NULL, 0);
                     60:                        c->flags |= CLIENT_BAD;
                     61:                        imsg_free(&imsg);
                     62:                        continue;
                     63:                }
1.1       nicm       64:
1.11      nicm       65:                log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
                     66:                switch (imsg.hdr.type) {
1.9       nicm       67:                case MSG_COMMAND:
1.11      nicm       68:                        if (datalen != sizeof commanddata)
1.9       nicm       69:                                fatalx("bad MSG_COMMAND size");
1.11      nicm       70:                        memcpy(&commanddata, imsg.data, sizeof commanddata);
1.9       nicm       71:
                     72:                        server_msg_command(c, &commanddata);
                     73:                        break;
                     74:                case MSG_IDENTIFY:
1.11      nicm       75:                        if (datalen != sizeof identifydata)
1.9       nicm       76:                                fatalx("bad MSG_IDENTIFY size");
1.18      nicm       77:                        if (imsg.fd == -1)
                     78:                                fatalx("MSG_IDENTIFY missing fd");
1.11      nicm       79:                        memcpy(&identifydata, imsg.data, sizeof identifydata);
1.9       nicm       80:
1.13      nicm       81:                        server_msg_identify(c, &identifydata, imsg.fd);
1.9       nicm       82:                        break;
                     83:                case MSG_RESIZE:
1.19      nicm       84:                        if (datalen != 0)
1.9       nicm       85:                                fatalx("bad MSG_RESIZE size");
                     86:
1.19      nicm       87:                        tty_resize(&c->tty);
                     88:                        recalculate_sizes();
                     89:                        server_redraw_client(c);
1.9       nicm       90:                        break;
                     91:                case MSG_EXITING:
1.11      nicm       92:                        if (datalen != 0)
1.9       nicm       93:                                fatalx("bad MSG_EXITING size");
                     94:
                     95:                        c->session = NULL;
1.12      nicm       96:                        tty_close(&c->tty);
1.9       nicm       97:                        server_write_client(c, MSG_EXITED, NULL, 0);
                     98:                        break;
1.20      nicm       99:                case MSG_WAKEUP:
1.9       nicm      100:                case MSG_UNLOCK:
1.11      nicm      101:                        if (datalen != 0)
1.9       nicm      102:                                fatalx("bad MSG_WAKEUP size");
                    103:
                    104:                        c->flags &= ~CLIENT_SUSPENDED;
                    105:                        tty_start_tty(&c->tty);
                    106:                        server_redraw_client(c);
1.22      nicm      107:                        recalculate_sizes();
1.23    ! nicm      108:                        if (c->session != NULL)
        !           109:                                c->session->activity = time(NULL);
1.10      nicm      110:                        break;
                    111:                case MSG_ENVIRON:
1.11      nicm      112:                        if (datalen != sizeof environdata)
1.10      nicm      113:                                fatalx("bad MSG_ENVIRON size");
1.11      nicm      114:                        memcpy(&environdata, imsg.data, sizeof environdata);
1.10      nicm      115:
                    116:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    117:                        if (strchr(environdata.var, '=') != NULL)
                    118:                                environ_put(&c->environ, environdata.var);
1.9       nicm      119:                        break;
1.21      nicm      120:                case MSG_SHELL:
                    121:                        if (datalen != 0)
                    122:                                fatalx("bad MSG_SHELL size");
                    123:
                    124:                        server_msg_shell(c);
                    125:                        break;
1.9       nicm      126:                default:
                    127:                        fatalx("unexpected message");
1.1       nicm      128:                }
1.11      nicm      129:
                    130:                imsg_free(&imsg);
1.1       nicm      131:        }
                    132: }
                    133:
                    134: void printflike2
1.9       nicm      135: server_msg_command_error(struct cmd_ctx *ctx, const char *fmt, ...)
1.1       nicm      136: {
1.7       nicm      137:        struct msg_print_data   data;
                    138:        va_list                 ap;
1.1       nicm      139:
                    140:        va_start(ap, fmt);
1.7       nicm      141:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1       nicm      142:        va_end(ap);
                    143:
1.7       nicm      144:        server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
1.1       nicm      145: }
                    146:
                    147: void printflike2
1.9       nicm      148: server_msg_command_print(struct cmd_ctx *ctx, const char *fmt, ...)
1.1       nicm      149: {
1.7       nicm      150:        struct msg_print_data   data;
                    151:        va_list                 ap;
1.1       nicm      152:
                    153:        va_start(ap, fmt);
1.7       nicm      154:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1       nicm      155:        va_end(ap);
                    156:
1.7       nicm      157:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1       nicm      158: }
                    159:
                    160: void printflike2
1.9       nicm      161: server_msg_command_info(struct cmd_ctx *ctx, const char *fmt, ...)
1.1       nicm      162: {
1.7       nicm      163:        struct msg_print_data   data;
                    164:        va_list                 ap;
1.1       nicm      165:
                    166:        if (be_quiet)
                    167:                return;
                    168:
                    169:        va_start(ap, fmt);
1.7       nicm      170:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1       nicm      171:        va_end(ap);
                    172:
1.7       nicm      173:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1       nicm      174: }
                    175:
1.6       nicm      176: void
1.9       nicm      177: server_msg_command(struct client *c, struct msg_command_data *data)
1.1       nicm      178: {
1.9       nicm      179:        struct cmd_ctx   ctx;
                    180:        struct cmd_list *cmdlist = NULL;
                    181:        struct cmd      *cmd;
                    182:        int              argc;
                    183:        char           **argv, *cause;
1.1       nicm      184:
1.23    ! nicm      185:        if (c->session != NULL)
        !           186:                c->session->activity = time(NULL);
1.1       nicm      187:
1.9       nicm      188:        ctx.error = server_msg_command_error;
                    189:        ctx.print = server_msg_command_print;
                    190:        ctx.info = server_msg_command_info;
1.1       nicm      191:
1.9       nicm      192:        ctx.msgdata = data;
1.1       nicm      193:        ctx.curclient = NULL;
                    194:
                    195:        ctx.cmdclient = c;
                    196:
1.9       nicm      197:        argc = data->argc;
                    198:        data->argv[(sizeof data->argv) - 1] = '\0';
                    199:        if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
                    200:                server_msg_command_error(&ctx, "command too long");
1.7       nicm      201:                goto error;
                    202:        }
                    203:
                    204:        if (argc == 0) {
                    205:                argc = 1;
                    206:                argv = xcalloc(1, sizeof *argv);
                    207:                *argv = xstrdup("new-session");
                    208:        }
                    209:
                    210:        if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
1.9       nicm      211:                server_msg_command_error(&ctx, "%s", cause);
1.7       nicm      212:                cmd_free_argv(argc, argv);
                    213:                goto error;
                    214:        }
                    215:        cmd_free_argv(argc, argv);
                    216:
1.9       nicm      217:        if (data->pid != -1) {
1.1       nicm      218:                TAILQ_FOREACH(cmd, cmdlist, qentry) {
                    219:                        if (cmd->entry->flags & CMD_CANTNEST) {
1.9       nicm      220:                                server_msg_command_error(&ctx,
1.1       nicm      221:                                    "sessions should be nested with care. "
                    222:                                    "unset $TMUX to force");
1.7       nicm      223:                                goto error;
1.1       nicm      224:                        }
                    225:                }
                    226:        }
                    227:
                    228:        if (cmd_list_exec(cmdlist, &ctx) != 1)
                    229:                server_write_client(c, MSG_EXIT, NULL, 0);
                    230:        cmd_list_free(cmdlist);
1.7       nicm      231:        return;
                    232:
                    233: error:
                    234:        if (cmdlist != NULL)
                    235:                cmd_list_free(cmdlist);
                    236:        server_write_client(c, MSG_EXIT, NULL, 0);
1.1       nicm      237: }
                    238:
1.6       nicm      239: void
1.13      nicm      240: server_msg_identify(struct client *c, struct msg_identify_data *data, int fd)
1.1       nicm      241: {
                    242:        c->cwd = NULL;
1.9       nicm      243:        data->cwd[(sizeof data->cwd) - 1] = '\0';
                    244:        if (*data->cwd != '\0')
                    245:                c->cwd = xstrdup(data->cwd);
                    246:
                    247:        data->term[(sizeof data->term) - 1] = '\0';
1.18      nicm      248:        tty_init(&c->tty, fd, data->term);
1.9       nicm      249:        if (data->flags & IDENTIFY_UTF8)
1.1       nicm      250:                c->tty.flags |= TTY_UTF8;
1.9       nicm      251:        if (data->flags & IDENTIFY_256COLOURS)
1.1       nicm      252:                c->tty.term_flags |= TERM_256COLOURS;
1.9       nicm      253:        else if (data->flags & IDENTIFY_88COLOURS)
1.1       nicm      254:                c->tty.term_flags |= TERM_88COLOURS;
1.9       nicm      255:        if (data->flags & IDENTIFY_HASDEFAULTS)
1.1       nicm      256:                c->tty.term_flags |= TERM_HASDEFAULTS;
1.18      nicm      257:
1.19      nicm      258:        tty_resize(&c->tty);
1.5       nicm      259:
1.1       nicm      260:        c->flags |= CLIENT_TERMINAL;
1.21      nicm      261: }
                    262:
                    263: void
                    264: server_msg_shell(struct client *c)
                    265: {
                    266:        struct msg_shell_data    data;
                    267:        const char              *shell;
                    268:
                    269:        shell = options_get_string(&global_s_options, "default-shell");
                    270:
                    271:        if (*shell == '\0' || areshell(shell))
                    272:                shell = _PATH_BSHELL;
                    273:        if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
                    274:                strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
                    275:
                    276:        server_write_client(c, MSG_SHELL, &data, sizeof data);
                    277:        c->flags |= CLIENT_BAD; /* it will die after exec */
1.1       nicm      278: }