Revision: 2826
          http://tmux.svn.sourceforge.net/tmux/?rev=2826&view=rev
Author:   tcunha
Date:     2012-06-18 15:23:01 +0000 (Mon, 18 Jun 2012)
Log Message:
-----------
Sync OpenBSD patchset 1138:

Add a skeleton mode to tmux (called "control mode") that let's tmux
commands be sent and output received on stdout. This can be used to
integrate with other terminal emulators and should allow some other
things to be made simpler later. More to come so doesn't do much yet and
deliberately not documented.

Modified Paths:
--------------
    trunk/client.c
    trunk/cmd-list.c
    trunk/server-client.c
    trunk/server-fn.c
    trunk/tmux.c
    trunk/tmux.h

Added Paths:
-----------
    trunk/control.c

Modified: trunk/client.c
===================================================================
--- trunk/client.c      2012-06-18 15:17:55 UTC (rev 2825)
+++ trunk/client.c      2012-06-18 15:23:01 UTC (rev 2826)
@@ -169,6 +169,7 @@
        pid_t                    ppid;
        enum msgtype             msg;
        char                    *cause;
+       struct termios           tio, saved_tio;
 
        /* Set up the initial command. */
        cmdflags = 0;
@@ -235,6 +236,23 @@
        setblocking(STDIN_FILENO, 0);
        event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST,
            client_stdin_callback, NULL);
+       if (flags & IDENTIFY_TERMIOS) {
+               if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
+                       fprintf(stderr, "tcgetattr failed: %s\n",
+                           strerror(errno));
+                       return (1);
+               }
+               cfmakeraw(&tio);
+               tio.c_iflag = ICRNL|IXANY;
+               tio.c_oflag = OPOST|ONLCR;
+               tio.c_lflag = NOKERNINFO;
+               tio.c_cflag = CREAD|CS8|HUPCL;
+               tio.c_cc[VMIN] = 1;
+               tio.c_cc[VTIME] = 0;
+               cfsetispeed(&tio, cfgetispeed(&saved_tio));
+               cfsetospeed(&tio, cfgetospeed(&saved_tio));
+               tcsetattr(STDIN_FILENO, TCSANOW, &tio);
+       }
 
        /* Establish signal handlers. */
        set_signals(client_signal);
@@ -275,7 +293,8 @@
                ppid = getppid();
                if (client_exittype == MSG_DETACHKILL && ppid > 1)
                        kill(ppid, SIGHUP);
-       }
+       } else if (flags & IDENTIFY_TERMIOS)
+               tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
        setblocking(STDIN_FILENO, 1);
        return (client_exitval);
 }
@@ -515,6 +534,12 @@
 
                        shell_exec(shelldata.shell, shellcmd);
                        /* NOTREACHED */
+               case MSG_DETACH:
+                       client_write_server(MSG_EXITING, NULL, 0);
+                       break;
+               case MSG_EXITED:
+                       imsg_free(&imsg);
+                       return (-1);
                default:
                        fatalx("unexpected message");
                }

Modified: trunk/cmd-list.c
===================================================================
--- trunk/cmd-list.c    2012-06-18 15:17:55 UTC (rev 2825)
+++ trunk/cmd-list.c    2012-06-18 15:23:01 UTC (rev 2826)
@@ -81,12 +81,24 @@
 int
 cmd_list_exec(struct cmd_list *cmdlist, struct cmd_ctx *ctx)
 {
+       struct client   *c = ctx->curclient;
        struct cmd      *cmd;
-       int              n, retval;
+       int              n, retval, guards;
 
+       guards = 0;
+       if (c != NULL && c->session != NULL)
+               guards = c->flags & CLIENT_CONTROL;
+
        retval = 0;
        TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
-               if ((n = cmd_exec(cmd, ctx)) == -1)
+               if (guards)
+                       ctx->print(ctx, "%%begin");
+               n = cmd_exec(cmd, ctx);
+               if (guards)
+                       ctx->print(ctx, "%%end");
+
+               /* Return of -1 is an error. */
+               if (n == -1)
                        return (-1);
 
                /*

Added: trunk/control.c
===================================================================
--- trunk/control.c                             (rev 0)
+++ trunk/control.c     2012-06-18 15:23:01 UTC (rev 2826)
@@ -0,0 +1,121 @@
+/* $Id$ */
+
+/*
+ * Copyright (c) 2012 Nicholas Marriott <[email protected]>
+ * Copyright (c) 2012 George Nachman <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <event.h>
+#include <string.h>
+
+#include "tmux.h"
+
+void printflike2 control_msg_error(struct cmd_ctx *, const char *, ...);
+void printflike2 control_msg_print(struct cmd_ctx *, const char *, ...);
+void printflike2 control_msg_info(struct cmd_ctx *, const char *, ...);
+void printflike2 control_write(struct client *, const char *, ...);
+
+/* Command error callback. */
+void printflike2
+control_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
+{
+       struct client   *c = ctx->curclient;
+       va_list          ap;
+
+       va_start(ap, fmt);
+       evbuffer_add_vprintf(c->stdout_data, fmt, ap);
+       va_end(ap);
+
+       evbuffer_add(c->stdout_data, "\n", 1);
+       server_push_stdout(c);
+}
+
+/* Command print callback. */
+void printflike2
+control_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
+{
+       struct client   *c = ctx->curclient;
+       va_list          ap;
+
+       va_start(ap, fmt);
+       evbuffer_add_vprintf(c->stdout_data, fmt, ap);
+       va_end(ap);
+
+       evbuffer_add(c->stdout_data, "\n", 1);
+       server_push_stdout(c);
+}
+
+/* Command info callback. */
+void printflike2
+control_msg_info(unused struct cmd_ctx *ctx, unused const char *fmt, ...)
+{
+}
+
+/* Write a line. */
+void printflike2
+control_write(struct client *c, const char *fmt, ...)
+{
+       va_list          ap;
+
+       va_start(ap, fmt);
+       evbuffer_add_vprintf(c->stdout_data, fmt, ap);
+       va_end(ap);
+
+       evbuffer_add(c->stdout_data, "\n", 1);
+       server_push_stdout(c);
+}
+
+/* Control input callback. Read lines and fire commands. */
+void
+control_callback(struct client *c, int closed, unused void *data)
+{
+       char            *line, *cause;
+       struct cmd_ctx   ctx;
+       struct cmd_list *cmdlist;
+
+       if (closed)
+               c->flags |= CLIENT_EXIT;
+
+       for (;;) {
+               line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF);
+               if (line == NULL)
+                       break;
+               if (*line == '\0') { /* empty line exit */
+                       c->flags |= CLIENT_EXIT;
+                       break;
+               }
+
+               ctx.msgdata = NULL;
+               ctx.cmdclient = NULL;
+               ctx.curclient = c;
+
+               ctx.error = control_msg_error;
+               ctx.print = control_msg_print;
+               ctx.info = control_msg_info;
+
+               if (cmd_string_parse(line, &cmdlist, &cause) != 0) {
+                       control_write(c, "%%error in line \"%s\": %s", line,
+                           cause);
+                       xfree(cause);
+               } else {
+                       cmd_list_exec(cmdlist, &ctx);
+                       cmd_list_free(cmdlist);
+               }
+
+               xfree(line);
+       }
+}


Property changes on: trunk/control.c
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: trunk/server-client.c
===================================================================
--- trunk/server-client.c       2012-06-18 15:17:55 UTC (rev 2825)
+++ trunk/server-client.c       2012-06-18 15:23:01 UTC (rev 2826)
@@ -107,6 +107,9 @@
        struct options  *oo = s != NULL ? &s->options : &global_s_options;
        char            *overrides;
 
+       if (c->flags & CLIENT_CONTROL)
+               return (0);
+
        if (!(c->flags & CLIENT_TERMINAL)) {
                *cause = xstrdup ("not a terminal");
                return (-1);
@@ -893,6 +896,17 @@
        if (*data->cwd != '\0')
                c->cwd = xstrdup(data->cwd);
 
+       if (data->flags & IDENTIFY_CONTROL) {
+               c->stdin_callback = control_callback;
+               c->flags |= (CLIENT_CONTROL|CLIENT_SUSPENDED);
+
+               c->tty.fd = -1;
+               c->tty.log_fd = -1;
+
+               close(fd);
+               return;
+       }
+
        if (!isatty(fd))
            return;
        data->term[(sizeof data->term) - 1] = '\0';

Modified: trunk/server-fn.c
===================================================================
--- trunk/server-fn.c   2012-06-18 15:17:55 UTC (rev 2825)
+++ trunk/server-fn.c   2012-06-18 15:23:01 UTC (rev 2826)
@@ -49,6 +49,8 @@
 void
 server_write_ready(struct client *c)
 {
+       if (c->flags & CLIENT_CONTROL)
+               return;
        server_write_client(c, MSG_READY, NULL, 0);
 }
 

Modified: trunk/tmux.c
===================================================================
--- trunk/tmux.c        2012-06-18 15:17:55 UTC (rev 2825)
+++ trunk/tmux.c        2012-06-18 15:23:01 UTC (rev 2826)
@@ -244,7 +244,7 @@
        quiet = flags = 0;
        label = path = NULL;
        login_shell = (**argv == '-');
-       while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUvV")) != -1) {
+       while ((opt = getopt(argc, argv, "28c:Cdf:lL:qS:uUvV")) != -1) {
                switch (opt) {
                case '2':
                        flags |= IDENTIFY_256COLOURS;
@@ -259,6 +259,12 @@
                                xfree(shell_cmd);
                        shell_cmd = xstrdup(optarg);
                        break;
+               case 'C':
+                       if (flags & IDENTIFY_CONTROL)
+                               flags |= IDENTIFY_TERMIOS;
+                       else
+                               flags |= IDENTIFY_CONTROL;
+                       break;
                case 'V':
                        printf("%s %s\n", __progname, VERSION);
                        exit(0);

Modified: trunk/tmux.h
===================================================================
--- trunk/tmux.h        2012-06-18 15:17:55 UTC (rev 2825)
+++ trunk/tmux.h        2012-06-18 15:23:01 UTC (rev 2826)
@@ -429,6 +429,8 @@
 #define IDENTIFY_UTF8 0x1
 #define IDENTIFY_256COLOURS 0x2
 #define IDENTIFY_88COLOURS 0x4
+#define IDENTIFY_CONTROL 0x8
+#define IDENTIFY_TERMIOS 0x10
        int             flags;
 };
 
@@ -1228,6 +1230,7 @@
 #define CLIENT_BORDERS 0x400
 #define CLIENT_READONLY 0x800
 #define CLIENT_REDRAWWINDOW 0x1000
+#define CLIENT_CONTROL 0x2000
        int              flags;
 
        struct event     identify_timer;
@@ -2127,6 +2130,9 @@
 void set_signals(void(*)(int, short, void *));
 void clear_signals(int);
 
+/* control.c */
+void control_callback(struct client *, int, void*);
+
 /* session.c */
 extern struct sessions sessions;
 extern struct sessions dead_sessions;

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
tmux-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tmux-cvs

Reply via email to