[Sending this to bug-bash@gnu.org since the mail was rejected by bash-test...@cwru.edu.]

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu'
-DCONF_VENDOR='unknown' -DLOCALEDIR='/tmp/prefix/share/locale'
-DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -DDEBUG -DMALLOC_DEBUG -I. -I.
-I./include -I./lib   -g -O2 -Wno-parentheses -Wno-format-security
uname output: Linux penguin.cs.ucla.edu 4.4.6-300.fc23.x86_64 #1 SMP Wed
Mar 16 22:10:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-unknown-linux-gnu

Bash Version: 4.4
Patch Level: 0
Release Status: rc1

Description:
     Please see attached patch.

Repeat-By:
     Please see attached patch.

Fix:
     Please see attached patch.
From 75d409f43d09d1323416db90121bc6bf54cbc3ff Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Fri, 8 Apr 2016 11:22:39 -0700
Subject: [PATCH] Fix compatibility problem with INSIDE_EMACS etc.

The checks for deciding whether bash is being run in a GNU Emacs shell
window have been tweaked to better support how the future versions of
Emacs will set environment variables.

Here are details about the problem.  Bash's heuristic that infers
whether Bash is being run inside the Emacs 'M-x term' command will
have problems when running inside future versions of Emacs.  Although
Emacs 25 'M-x term' will continue to set both variables (e.g.,
EMACS='25.1 (term:0.96)' and INSIDE_EMACS='25.1,term:0.96') for
compatibility with Bash 4.3 and earlier, we would like Bash 4.4 and
later to work with future versions of Emacs that that set only
INSIDE_EMACS.

Problem reported by Phillip Lord in <http://bugs.gnu.org/20484#108>.

* shell.c (main): Tweak tests for whether we are running in an Emacs
shell window, to accommodate future Emacs versions better, and to
document and reflect historical practice more accurately.
---
 shell.c | 38 ++++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)

diff --git a/shell.c b/shell.c
index 63f139b..c91baf0 100644
--- a/shell.c
+++ b/shell.c
@@ -570,25 +570,47 @@ main (argc, argv, env)
   set_default_locale_vars ();
 
   /*
-   * M-x term -> TERM=eterm EMACS=22.1 (term:0.96)	(eterm)
-   * M-x shell -> TERM=dumb EMACS=t			(no line editing)
-   * M-x terminal -> TERM=emacs-em7955 EMACS=		(line editing)
+   * M-x term -> TERM='eterm-color' INSIDE_EMACS='25.1,term:0.96' (eterm)
+   * M-x shell -> TERM='dumb' INSIDE_EMACS='25.1,comint' (no line editing)
+   *
+   * Older versions of Emacs may set EMACS to 't' or to something like
+   * '22.1 (term:0.96)' instead of (or in addition to) setting INSIDE_EMACS.
+   * They may set TERM to 'eterm' instead of 'eterm-color'.  They may have
+   * a now-obsolete command that sets neither EMACS nor INSIDE_EMACS:
+   * M-x terminal -> TERM='emacs-em7955' (line editing)
    */
   if (interactive_shell)
     {
-      char *term, *emacs;
+      char *term, *emacs, *inside_emacs;
+      int emacs_term, in_emacs;
 
       term = get_string_value ("TERM");
       emacs = get_string_value ("EMACS");
+      inside_emacs = get_string_value ("INSIDE_EMACS");
+
+      if (inside_emacs)
+	{
+	  emacs_term = strstr (inside_emacs, ",term:") != 0;
+	  in_emacs = 1;
+	}
+      else if (emacs)
+	{
+	  /* Infer whether we are in an older Emacs.  Do not accept
+	     just any value for EMACS, as the value might be an
+	     executable file name or a shell command.  */
+	  emacs_term = strstr (emacs, " (term:") != 0;
+	  in_emacs = emacs_term || STREQ (emacs, "t");
+	}
+      else
+	emacs_term = in_emacs = 0;
 
       /* Not sure any emacs terminal emulator sets TERM=emacs any more */
       no_line_editing |= STREQ (term, "emacs");
-      no_line_editing |= emacs && emacs[0] == 't' && emacs[1] == '\0' && STREQ (term, "dumb");
-      no_line_editing |= get_string_value ("INSIDE_EMACS") != 0;
+      no_line_editing |= in_emacs && STREQ (term, "dumb");
 
       /* running_under_emacs == 2 for `eterm' */
-      running_under_emacs = (emacs != 0) || STREQN (term, "emacs", 5);
-      running_under_emacs += STREQN (term, "eterm", 5) && emacs && strstr (emacs, "term");
+      running_under_emacs = in_emacs || STREQN (term, "emacs", 5);
+      running_under_emacs += emacs_term && STREQN (term, "eterm", 5);
 
       if (running_under_emacs)
 	gnu_error_format = 1;
-- 
2.5.5

Reply via email to