xterm ttle patch v898 v2

Tribhuvan loka at rcn.com
Tue Jan 7 06:50:17 UTC 2003


This is a modification of a patch Tomas Stylbo sent me a couple of days
ago.
Changes:
1) cut down initial delay from 5 seconds to 1 second.
2) inserted #ifdef for SA_INTERRUPT as some platforms
    don't implement that in <signal.h> (notably Solaris-x86)
3) changed code in store_xterm_title, see: if ( i < 5 ) ...
4) added option for displaying user at host in title bar
   I still have to add explicit support for NATIVE_WIN32
   (ie: correct environment variables for USER and HOSTNAME)

TODO:
1) the problem on remote host is still not resolved
   (ie: store and restore - the title function works fine
    on remote machines, but _restoring_ old title does not.
    The delay on initialization of the store_xterm_title
    is fine (though it could be reduced to a fraction of a second)
    but I think it should be introduced in _restore_ when
    mc is exiting/cleaning up. I couldn't quite figure out how to
    do that.
2)  I'd like to create a dialog box for these xtra features, so
    that while they are compiled in by default - they can be
    switched on and off on-the-fly for buggy/cheap terminal
    environments.

Question for Thomas Stylbo: what platform are you testing on?

I'm testing on Solaris 8 & 9 x86 w/ X11R6-4.1.0, 4.2.0,
 openwindows and openwindows with XF86 porting.
 Terminals: xterm, rxvt, gnome terminal for the moment

Will report back full testing results a little later....

Tribhuvan
<loka at rcn dot com>

diff -purN:

--- mc-4.6.0-pre2/src/main.c	2002-12-26 11:38:37.000000000 -0500
+++ mc-4.6.0-pre2.new/src/main.c	2003-01-06 06:31:17.077067000 -0500
@@ -30,7 +30,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
-
 #include <sys/stat.h>
 
 #ifdef HAVE_UNISTD_H
@@ -296,6 +295,10 @@ char *mc_home;
 
 char cmd_buf[512];
 
+/* logname at hostname for xterm_title */
+int xterm_title_who_flag = 1;
+unsigned char *who_xterm_title = NULL;
+
 WPanel *
 get_current_panel (void)
 {
@@ -1839,17 +1842,39 @@ midnight_callback (struct Dlg_head *h, i
 void
 update_xterm_title_path (void)
 {
-    unsigned char *p, *s;
-
-    if (xterm_flag && xterm_title) {
-	p = s = g_strdup (strip_home_and_password (cpanel->cwd));
-	do {
-	    if (*s <= 32)
-		*s = '?';
-	} while (*++s);
-	fprintf (stdout, "\33]0;mc - %s\7", p);
-	fflush (stdout);
-	g_free (p);
+    if ( !xterm_title_who_flag ) {
+                                      /* without logname at hostname */
+        unsigned char *p, *s;
+
+	if (xterm_flag && xterm_title) {
+	    p = s = g_strdup (strip_home_and_password (cpanel->cwd));
+	    do {
+	        if (*s <= 32)
+		    *s = '?';
+	    } while (*++s);
+	    fprintf (stdout, "\33]0;mc - %s\7", p);
+	    g_free (p);
+	    fflush (stdout);
+	} 
+    } else {
+                                      /* with logname at hostname */
+        unsigned char *p, *s, *w;
+	unsigned char o[255];
+
+	if (xterm_flag && xterm_title) {
+	    w = who_xterm_title;
+	    p = s = g_strdup (strip_home_and_password (cpanel->cwd));
+	    do {
+	        if (*s <= 32)
+		    *s = '?';
+	    } while (*++s);
+	    sprintf (o, "%s  %s",w , p);
+	    fprintf (stdout, "\33]0;%s\7", o);
+	    g_free (p);
+	    g_free (w);
+	    g_free (o);
+	    fflush (stdout);
+	}
     }
 }
 
@@ -2406,11 +2431,120 @@ compatibility_move_mc_files (void)
 }
 #endif				/* NATIVE_WIN32 */
 
+/* Retrieve and store xterm title */
+
+static void 
+xterm_sig_alrm_h (int sig) {
+    /* Do nothing.
+     * The only purpose of this signal is to interrupt the 
+     * read system call in xterm_read_title(). */
+}
+
+static char *
+store_xterm_title (void)
+{
+    static unsigned char title[256];
+    struct sigaction sa;
+    int cnt;
+    char *cmd = "\033[21t";
+    int e;
+    int tty;
+    
+    if ((tty = open("/dev/tty", O_RDWR | O_NOCTTY)) == -1) {
+      return (NULL);
+    }
+    /* Make sure we will not be blocked infinitely. */
+    sa.sa_handler = xterm_sig_alrm_h;
+#   ifdef SA_INTERRUPT
+    sa.sa_flags = SA_INTERRUPT | SA_RESETHAND;
+#   else
+    sa.sa_flags = 0;
+    siginterrupt(SIGALRM, 1);
+#   endif
+    
+    sigaction(SIGALRM, &sa, NULL);
+    alarm(1);
+    
+    /* Write the request for window title. */
+    e = strlen(cmd); 
+    while ((cnt = write(tty, cmd, e)) > 0) {
+        e -= cnt;
+        cmd += cnt;
+    }
+    if (cnt == -1) {
+        close(tty);
+        return (NULL);
+    }
+    else {
+        /* Read the response. */
+        int i = 0;
+        char ch;
+        int seen_esc = 0;
+        while (i < sizeof(title) && read(tty, &ch, 1) > 0) {
+            if (seen_esc == 0) {
+                if (ch == '\033')
+                    seen_esc = 1;
+                else
+                    continue;
+            }
+            if (ch == '\x9c')  /* ST */
+                break;
+            title[i++] = ch;
+        }
+        close(tty);
+	if (i < 5)
+	  return NULL;
+	title[i-2] = '\0';
+	return title+3;
+    }
+}
+
+unsigned char *
+xterm_title_get_who (void)
+{
+  static unsigned char *who, *at, *where; 
+
+  if ( getenv("LOGNAME") == "" ) {
+    if ( getenv("USER") == "" )
+      who = "mc";
+    else
+      who = getenv("USER");
+  } else
+  who = getenv ("LOGNAME");
+  at = "@";
+  if ( getenv("HOSTNAME") == "" ) {
+    if ( getenv("SESSION_SVR") == "" )
+      where = "";
+    else
+      where = getenv("SESSION_SVR");
+  } else
+    where = getenv("HOSTNAME");
+
+    strcat ( who, at );
+    strcat ( who, where );
+    
+    if (!who || !(*who))
+      { who = "mc - "; return who; }
+    else
+      return who;
+}
+
+/* Restore xterm title to stored value */
+static void
+restore_xterm_title (const unsigned char *title)
+{
+    if (title != NULL) {
+      printf ("\e]0;%s\a", title);
+      fflush(stdout);
+    }
+}	
+
 int
 main (int argc, char *argv[])
 {
     /* if on, it displays the information that files have been moved to ~/.mc */
     int show_change_notice = 0;
+    unsigned char *old_xterm_title = NULL;
 
     /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
     setlocale (LC_ALL, "");
@@ -2513,9 +2647,20 @@ main (int argc, char *argv[])
 #endif				/* HAVE_SUBSHELL_SUPPORT */
 	prompt = (geteuid () == 0) ? "# " : "$ ";
 
+    /* Get and store xterm_title */
+    if (xterm_flag)
+        old_xterm_title = store_xterm_title();
+
+    /* Set logname at hostname string for use in xterm_title */
+    who_xterm_title = xterm_title_get_who ();
+
     /* Program main loop */
     do_nc ();
 
+    /* Restore xterm_title */
+    if (xterm_flag && xterm_title)
+        restore_xterm_title(old_xterm_title);
+
     /* Save the tree store */
     tree_store_save ();
 





More information about the mc-devel mailing list