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

1.12    ! nicm        1: /* $OpenBSD: server-msg.c,v 1.11 2009/08/11 17:18:35 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>
                     20:
                     21: #include <errno.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24: #include <time.h>
                     25: #include <unistd.h>
                     26:
                     27: #include "tmux.h"
                     28:
1.9       nicm       29: void   server_msg_command(struct client *, struct msg_command_data *);
                     30: void   server_msg_identify(struct client *, struct msg_identify_data *);
                     31: void   server_msg_resize(struct client *, struct msg_resize_data *);
                     32:
                     33: void printflike2 server_msg_command_error(struct cmd_ctx *, const char *, ...);
                     34: void printflike2 server_msg_command_print(struct cmd_ctx *, const char *, ...);
                     35: void printflike2 server_msg_command_info(struct cmd_ctx *, const char *, ...);
1.1       nicm       36:
                     37: int
                     38: server_msg_dispatch(struct client *c)
                     39: {
1.11      nicm       40:        struct imsg              imsg;
1.9       nicm       41:        struct msg_command_data  commanddata;
                     42:        struct msg_identify_data identifydata;
                     43:        struct msg_resize_data   resizedata;
                     44:        struct msg_unlock_data   unlockdata;
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.11      nicm       77:                        memcpy(&identifydata, imsg.data, sizeof identifydata);
1.9       nicm       78:
                     79:                        server_msg_identify(c, &identifydata);
                     80:                        break;
                     81:                case MSG_RESIZE:
1.11      nicm       82:                        if (datalen != sizeof resizedata)
1.9       nicm       83:                                fatalx("bad MSG_RESIZE size");
1.11      nicm       84:                        memcpy(&resizedata, imsg.data, sizeof resizedata);
1.9       nicm       85:
                     86:                        server_msg_resize(c, &resizedata);
                     87:                        break;
                     88:                case MSG_EXITING:
1.11      nicm       89:                        if (datalen != 0)
1.9       nicm       90:                                fatalx("bad MSG_EXITING size");
                     91:
                     92:                        c->session = NULL;
1.12    ! nicm       93:                        tty_close(&c->tty);
1.9       nicm       94:                        server_write_client(c, MSG_EXITED, NULL, 0);
                     95:                        break;
                     96:                case MSG_UNLOCK:
1.11      nicm       97:                        if (datalen != sizeof unlockdata)
1.9       nicm       98:                                fatalx("bad MSG_UNLOCK size");
1.11      nicm       99:                        memcpy(&unlockdata, imsg.data, sizeof unlockdata);
1.9       nicm      100:
                    101:                        unlockdata.pass[(sizeof unlockdata.pass) - 1] = '\0';
                    102:                        if (server_unlock(unlockdata.pass) != 0)
                    103:                                server_write_error(c, "bad password");
                    104:                        memset(&unlockdata, 0, sizeof unlockdata);
                    105:                        server_write_client(c, MSG_EXIT, NULL, 0);
                    106:                        break;
                    107:                case MSG_WAKEUP:
1.11      nicm      108:                        if (datalen != 0)
1.9       nicm      109:                                fatalx("bad MSG_WAKEUP size");
                    110:
                    111:                        c->flags &= ~CLIENT_SUSPENDED;
                    112:                        tty_start_tty(&c->tty);
                    113:                        server_redraw_client(c);
1.10      nicm      114:                        break;
                    115:                case MSG_ENVIRON:
1.11      nicm      116:                        if (datalen != sizeof environdata)
1.10      nicm      117:                                fatalx("bad MSG_ENVIRON size");
1.11      nicm      118:                        memcpy(&environdata, imsg.data, sizeof environdata);
1.10      nicm      119:
                    120:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    121:                        if (strchr(environdata.var, '=') != NULL)
                    122:                                environ_put(&c->environ, environdata.var);
1.9       nicm      123:                        break;
                    124:                default:
                    125:                        fatalx("unexpected message");
1.1       nicm      126:                }
1.11      nicm      127:
                    128:                imsg_free(&imsg);
1.1       nicm      129:        }
                    130: }
                    131:
                    132: void printflike2
1.9       nicm      133: server_msg_command_error(struct cmd_ctx *ctx, const char *fmt, ...)
1.1       nicm      134: {
1.7       nicm      135:        struct msg_print_data   data;
                    136:        va_list                 ap;
1.1       nicm      137:
                    138:        va_start(ap, fmt);
1.7       nicm      139:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1       nicm      140:        va_end(ap);
                    141:
1.7       nicm      142:        server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
1.1       nicm      143: }
                    144:
                    145: void printflike2
1.9       nicm      146: server_msg_command_print(struct cmd_ctx *ctx, const char *fmt, ...)
1.1       nicm      147: {
1.7       nicm      148:        struct msg_print_data   data;
                    149:        va_list                 ap;
1.1       nicm      150:
                    151:        va_start(ap, fmt);
1.7       nicm      152:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1       nicm      153:        va_end(ap);
                    154:
1.7       nicm      155:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1       nicm      156: }
                    157:
                    158: void printflike2
1.9       nicm      159: server_msg_command_info(struct cmd_ctx *ctx, const char *fmt, ...)
1.1       nicm      160: {
1.7       nicm      161:        struct msg_print_data   data;
                    162:        va_list                 ap;
1.1       nicm      163:
                    164:        if (be_quiet)
                    165:                return;
                    166:
                    167:        va_start(ap, fmt);
1.7       nicm      168:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1       nicm      169:        va_end(ap);
                    170:
1.7       nicm      171:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1       nicm      172: }
                    173:
1.6       nicm      174: void
1.9       nicm      175: server_msg_command(struct client *c, struct msg_command_data *data)
1.1       nicm      176: {
1.9       nicm      177:        struct cmd_ctx   ctx;
                    178:        struct cmd_list *cmdlist = NULL;
                    179:        struct cmd      *cmd;
                    180:        int              argc;
                    181:        char           **argv, *cause;
1.1       nicm      182:
                    183:        server_activity = time(NULL);
                    184:
1.9       nicm      185:        ctx.error = server_msg_command_error;
                    186:        ctx.print = server_msg_command_print;
                    187:        ctx.info = server_msg_command_info;
1.1       nicm      188:
1.9       nicm      189:        ctx.msgdata = data;
1.1       nicm      190:        ctx.curclient = NULL;
                    191:        ctx.cursession = NULL;
                    192:
                    193:        ctx.cmdclient = c;
                    194:
1.9       nicm      195:        argc = data->argc;
                    196:        data->argv[(sizeof data->argv) - 1] = '\0';
                    197:        if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
                    198:                server_msg_command_error(&ctx, "command too long");
1.7       nicm      199:                goto error;
                    200:        }
                    201:
                    202:        if (argc == 0) {
                    203:                argc = 1;
                    204:                argv = xcalloc(1, sizeof *argv);
                    205:                *argv = xstrdup("new-session");
                    206:        }
                    207:
                    208:        if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
1.9       nicm      209:                server_msg_command_error(&ctx, "%s", cause);
1.7       nicm      210:                cmd_free_argv(argc, argv);
                    211:                goto error;
                    212:        }
                    213:        cmd_free_argv(argc, argv);
                    214:
1.9       nicm      215:        if (data->pid != -1) {
1.1       nicm      216:                TAILQ_FOREACH(cmd, cmdlist, qentry) {
                    217:                        if (cmd->entry->flags & CMD_CANTNEST) {
1.9       nicm      218:                                server_msg_command_error(&ctx,
1.1       nicm      219:                                    "sessions should be nested with care. "
                    220:                                    "unset $TMUX to force");
1.7       nicm      221:                                goto error;
1.1       nicm      222:                        }
                    223:                }
                    224:        }
                    225:
                    226:        if (cmd_list_exec(cmdlist, &ctx) != 1)
                    227:                server_write_client(c, MSG_EXIT, NULL, 0);
                    228:        cmd_list_free(cmdlist);
1.7       nicm      229:        return;
                    230:
                    231: error:
                    232:        if (cmdlist != NULL)
                    233:                cmd_list_free(cmdlist);
                    234:        server_write_client(c, MSG_EXIT, NULL, 0);
1.1       nicm      235: }
                    236:
1.6       nicm      237: void
1.9       nicm      238: server_msg_identify(struct client *c, struct msg_identify_data *data)
1.1       nicm      239: {
1.9       nicm      240:        c->tty.sx = data->sx;
                    241:        c->tty.sy = data->sy;
1.1       nicm      242:
                    243:        c->cwd = NULL;
1.9       nicm      244:        data->cwd[(sizeof data->cwd) - 1] = '\0';
                    245:        if (*data->cwd != '\0')
                    246:                c->cwd = xstrdup(data->cwd);
                    247:
                    248:        data->tty[(sizeof data->tty) - 1] = '\0';
                    249:        data->term[(sizeof data->term) - 1] = '\0';
                    250:        tty_init(&c->tty, data->tty, data->term);
                    251:        if (data->flags & IDENTIFY_UTF8)
1.1       nicm      252:                c->tty.flags |= TTY_UTF8;
1.9       nicm      253:        if (data->flags & IDENTIFY_256COLOURS)
1.1       nicm      254:                c->tty.term_flags |= TERM_256COLOURS;
1.9       nicm      255:        else if (data->flags & IDENTIFY_88COLOURS)
1.1       nicm      256:                c->tty.term_flags |= TERM_88COLOURS;
1.9       nicm      257:        if (data->flags & IDENTIFY_HASDEFAULTS)
1.1       nicm      258:                c->tty.term_flags |= TERM_HASDEFAULTS;
1.5       nicm      259:
1.1       nicm      260:        c->flags |= CLIENT_TERMINAL;
                    261: }
                    262:
1.6       nicm      263: void
1.9       nicm      264: server_msg_resize(struct client *c, struct msg_resize_data *data)
1.1       nicm      265: {
1.9       nicm      266:        c->tty.sx = data->sx;
1.1       nicm      267:        if (c->tty.sx == 0)
                    268:                c->tty.sx = 80;
1.9       nicm      269:        c->tty.sy = data->sy;
1.1       nicm      270:        if (c->tty.sy == 0)
                    271:                c->tty.sy = 25;
                    272:
                    273:        c->tty.cx = UINT_MAX;
                    274:        c->tty.cy = UINT_MAX;
                    275:        c->tty.rupper = UINT_MAX;
                    276:        c->tty.rlower = UINT_MAX;
                    277:
                    278:        recalculate_sizes();
                    279:
                    280:        /* Always redraw this client. */
                    281:        server_redraw_client(c);
                    282: }