diff src/process.c @ 814:a634e3b7acc8

[xemacs-hg @ 2002-04-14 12:41:59 by ben] latest changes TODO.ben-mule-21-5: Update. make-docfile.c: Add basic support for handling ISO 2022 doc strings -- we parse the basic charset designation sequences so we know whether we're in ASCII and have to pay attention to end quotes and such. Reformat code according to coding standards. abbrev.el: Add `global-abbrev-mode', which turns on or off abbrev-mode in all buffers. Added `defining-abbrev-turns-on-abbrev-mode' -- if non-nil, defining an abbrev through an interactive function will automatically turn on abbrev-mode, either globally or locally depending on the command. This is the "what you'd expect" behavior. indent.el: general function for indenting a balanced expression in a mode-correct way. Works similar to indent-region in that a mode can specify a specific command to do the whole operation; if not, figure out the region using forward-sexp and indent each line using indent-according-to-mode. keydefs.el: Removed. Modify M-C-backslash to do indent-region-or-balanced-expression. Make S-Tab just insert a TAB char, like it's meant to do. make-docfile.el: Now that we're using the call-process-in-lisp, we need to load an extra file win32-native.el because we're running a bare temacs. menubar-items.el: Totally redo the Cmds menu so that most used commands appear directly on the menu and less used commands appear in submenus. The old way may have been very pretty, but rather impractical. process.el: Under Windows, don't ever use old-call-process-internal, even in batch mode. We can do processes in batch mode. subr.el: Someone recoded truncate-string-to-width, saying "the FSF version is too complicated and does lots of hard-to-understand stuff" but the resulting recoded version was *totally* wrong! it misunderstood the basic point of this function, which is work in *columns* not chars. i dumped ours and copied the version from FSF 21.1. Also added truncate-string-with-continuation-dots, since this idiom is used often. config.inc.samp, xemacs.mak: Separate out debug and optimize flags. Remove all vestiges of USE_MINIMAL_TAGBITS, USE_INDEXED_LRECORD_IMPLEMENTATION, and GUNG_HO, since those ifdefs have long been removed. Make error-checking support actually work. Some rearrangement of config.inc.samp to make it more logical. Remove callproc.c and ntproc.c from xemacs.mak, no longer used. Make pdump the default. lisp.h: Add support for strong type-checking of Bytecount, Bytebpos, Charcount, Charbpos, and others, by making them classes, overloading the operators to provide integer-like operation and carefully controlling what operations are allowed. Not currently enabled in C++ builds because there are still a number of compile errors, and it won't really work till we merge in my "8-bit-Mule" workspace, in which I make use of the new types Charxpos, Bytexpos, Memxpos, representing a "position" either in a buffer or a string. (This is especially important in the extent code.) abbrev.c, alloc.c, eval.c, buffer.c, buffer.h, editfns.c, fns.c, text.h: Warning fixes, some of them related to new C++ strict type checking of Bytecount, Charbpos, etc. dired.c: Caught an actual error due to strong type checking -- char len being passed when should be byte len. alloc.c, backtrace.h, bytecode.c, bytecode.h, eval.c, sysdep.c: Further optimize Ffuncall: -- process arg list at compiled-function creation time, converting into an array for extra-quick access at funcall time. -- rewrite funcall_compiled_function to use it, and inline this function. -- change the order of check for magic stuff in SPECBIND_FAST_UNSAFE to be faster. -- move the check for need to garbage collect into the allocation code, so only a single flag needs to be checked in funcall. buffer.c, symbols.c: add debug funs to check on mule optimization info in buffers and strings. eval.c, emacs.c, text.c, regex.c, scrollbar-msw.c, search.c: Fix evil crashes due to eistrings not properly reinitialized under pdump. Redo a bit some of the init routines; convert some complex_vars_of() into simple vars_of(), because they didn't need complex processing. callproc.c, emacs.c, event-stream.c, nt.c, process.c, process.h, sysdep.c, sysdep.h, syssignal.h, syswindows.h, ntproc.c: Delete. Hallelujah, praise the Lord, there is no god but Allah!!! fix so that processes can be invoked in bare temacs -- thereby eliminating any need for callproc.c. (currently only eliminated under NT.) remove all crufty and unnecessary old process code in ntproc.c and elsewhere. move non-callproc-specific stuff (mostly environment) into process.c, so callproc.c can be left out under NT. console-tty.c, doc.c, file-coding.c, file-coding.h, lstream.c, lstream.h: fix doc string handling so it works with Japanese, etc docs. change handling of "character mode" so callers don't have to manually set it (quite error-prone). event-msw.c: spacing fixes. lread.c: eliminate unused crufty vintage-19 "FSF defun hack" code. lrecord.h: improve pdump description docs. buffer.c, ntheap.c, unexnt.c, win32.c, emacs.c: Mule-ize some unexec and startup code. It was pseudo-Mule-ized before by simply always calling the ...A versions of functions, but that won't cut it -- eventually we want to be able to run properly even if XEmacs has been installed in a Japanese directory. (The current problem is the timing of the loading of the Unicode tables; this will eventually be fixed.) Go through and fix various other places where the code was not Mule-clean. Provide a function mswindows_get_module_file_name() to get our own name without resort to PATH_MAX and such. Add a big comment in main() about the problem with Unicode table load timing that I just alluded to. emacs.c: When error-checking is enabled (interpreted as "user is developing XEmacs"), don't ask user to "pause to read messages" when a fatal error has occurred, because it will wedge if we are in an inner modal loop (typically when a menu is popped up) and make us unable to get a useful stack trace in the debugger. text.c: Correct update_entirely_ascii_p_flag to actually work. lisp.h, symsinit.h: declarations for above changes.
author ben
date Sun, 14 Apr 2002 12:43:31 +0000
parents 2b676dc88c66
children 39b1580316c8
line wrap: on
line diff
--- a/src/process.c	Sat Apr 13 20:44:53 2002 +0000
+++ b/src/process.c	Sun Apr 14 12:43:31 2002 +0000
@@ -21,18 +21,20 @@
 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  */
 
-/* This file has been Mule-ized except for `start-process-internal',
-   `open-network-stream-internal' and `open-multicast-group-internal'. */
+/* This file has been Mule-ized. */
 
 /* This file has been split into process.c and process-unix.c by
    Kirill M. Katsnelson <kkm@kis.ru>, so please bash him and not
-   the original author(s) */
+   the original author(s).
+
+   Non-synch-subprocess stuff (mostly process environment) moved from
+   callproc.c, 4-3-02, Ben Wing. */
 
 #include <config.h>
 
-#if !defined (NO_SUBPROCESSES)
-
-/* The entire file is within this conditional */
+#if defined (NO_SUBPROCESSES)
+#error "We don't support this anymore."
+#endif
 
 #include "lisp.h"
 
@@ -52,8 +54,8 @@
 
 #include "sysfile.h"
 #include "sysproc.h"
+#include "syssignal.h"
 #include "systime.h"
-#include "syssignal.h" /* Always include before systty.h */
 #include "systty.h"
 #include "syswait.h"
 
@@ -105,7 +107,6 @@
 /* List of process objects. */
 Lisp_Object Vprocess_list;
 
-extern Lisp_Object Vlisp_EXEC_SUFFIXES;
 Lisp_Object Vnull_device;
 
 /* Cons of coding systems used to initialize process I/O on a newly-
@@ -117,6 +118,18 @@
 
 Fixnum debug_process_io;
 
+Lisp_Object Vshell_file_name;
+
+/* The environment to pass to all subprocesses when they are started.
+   This is in the semi-bogus format of ("VAR=VAL" "VAR2=VAL2" ... )
+ */
+Lisp_Object Vprocess_environment;
+
+/* Make sure egetenv() not called too soon */
+int env_initted;
+
+Lisp_Object Vlisp_EXEC_SUFFIXES;
+
 
 
 static Lisp_Object
@@ -527,7 +540,6 @@
   p->coding_instream =
     make_coding_input_stream (XLSTREAM (p->pipe_instream), incode,
 			      CODING_DECODE, 0);
-  Lstream_set_character_mode (XLSTREAM (p->coding_instream));
   p->coding_outstream =
     make_coding_output_stream (XLSTREAM (p->pipe_outstream), outcode,
 			       CODING_ENCODE, 0);
@@ -583,7 +595,6 @@
        (int nargs, Lisp_Object *args))
 {
   /* This function can call lisp */
-  /* !!#### This function has not been Mule-ized */
   Lisp_Object buffer, name, program, process, current_dir;
   Lisp_Object tem;
   int speccount = specpdl_depth ();
@@ -665,8 +676,7 @@
   process = make_process_internal (name);
 
   XPROCESS (process)->buffer = buffer;
-  XPROCESS (process)->command = Flist (nargs - 2,
-				    args + 2);
+  XPROCESS (process)->command = Flist (nargs - 2, args + 2);
 
   /* Make the process marker point into the process buffer (if any).  */
   if (!NILP (buffer))
@@ -2048,20 +2058,6 @@
   return XPROCESS (process)->kill_without_query ? Qt : Qnil;
 }
 
-
-/* This is not named init_process in order to avoid a conflict with NS 3.3 */
-void
-init_xemacs_process (void)
-{
-  MAYBE_PROCMETH (init_process, ());
-
-  Vprocess_list = Qnil;
-
-  if (usid_to_process)
-    clrhash (usid_to_process);
-  else
-    usid_to_process = make_hash_table (32);
-}
 
 #if 0
 
@@ -2076,6 +2072,238 @@
 
 #endif /* 0 */
 
+
+static int
+getenv_internal (const Intbyte *var,
+		 Bytecount varlen,
+		 Intbyte **value,
+		 Bytecount *valuelen)
+{
+  Lisp_Object scan;
+
+  assert (env_initted);
+
+  for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
+    {
+      Lisp_Object entry = XCAR (scan);
+
+      if (STRINGP (entry)
+	  && XSTRING_LENGTH (entry) > varlen
+	  && XSTRING_BYTE (entry, varlen) == '='
+#ifdef WIN32_NATIVE
+	  /* NT environment variables are case insensitive.  */
+	  && ! memicmp (XSTRING_DATA (entry), var, varlen)
+#else  /* not WIN32_NATIVE */
+	  && ! memcmp (XSTRING_DATA (entry), var, varlen)
+#endif /* not WIN32_NATIVE */
+	  )
+	{
+	  *value    = XSTRING_DATA   (entry) + (varlen + 1);
+	  *valuelen = XSTRING_LENGTH (entry) - (varlen + 1);
+	  return 1;
+	}
+    }
+
+  return 0;
+}
+
+static void
+putenv_internal (const Intbyte *var,
+		 Bytecount varlen,
+		 const Intbyte *value,
+		 Bytecount valuelen)
+{
+  Lisp_Object scan;
+
+  assert (env_initted);
+
+  for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
+    {
+      Lisp_Object entry = XCAR (scan);
+
+      if (STRINGP (entry)
+	  && XSTRING_LENGTH (entry) > varlen
+	  && XSTRING_BYTE (entry, varlen) == '='
+#ifdef WIN32_NATIVE
+	  /* NT environment variables are case insensitive.  */
+	  && ! memicmp (XSTRING_DATA (entry), var, varlen)
+#else  /* not WIN32_NATIVE */
+	  && ! memcmp (XSTRING_DATA (entry), var, varlen)
+#endif /* not WIN32_NATIVE */
+	  )
+	{
+	  XCAR (scan) = concat3 (make_string (var, varlen),
+				 build_string ("="),
+				 make_string (value, valuelen));
+	  return;
+	}
+    }
+
+  Vprocess_environment = Fcons (concat3 (make_string (var, varlen),
+					 build_string ("="),
+					 make_string (value, valuelen)),
+				Vprocess_environment);
+}
+
+/* NOTE:
+
+   FSF has this as a Lisp function, as follows.  Generally moving things
+   out of C and into Lisp is a good idea, but in this case the Lisp
+   function is used so early in the startup sequence that it would be ugly
+   to rearrange the early dumped code to accommodate this.
+   
+(defun getenv (variable)
+  "Get the value of environment variable VARIABLE.
+VARIABLE should be a string.  Value is nil if VARIABLE is undefined in
+the environment.  Otherwise, value is a string.
+
+This function consults the variable `process-environment'
+for its value."
+  (interactive (list (read-envvar-name "Get environment variable: " t)))
+  (let ((value (getenv-internal variable)))
+    (when (interactive-p)
+      (message "%s" (if value value "Not set")))
+    value))
+*/
+
+DEFUN ("getenv", Fgetenv, 1, 2, "sEnvironment variable: \np", /*
+Return the value of environment variable VAR, as a string.
+VAR is a string, the name of the variable.
+When invoked interactively, prints the value in the echo area.
+*/
+       (var, interactivep))
+{
+  Intbyte *value;
+  Bytecount valuelen;
+  Lisp_Object v = Qnil;
+  struct gcpro gcpro1;
+
+  CHECK_STRING (var);
+  GCPRO1 (v);
+  if (getenv_internal (XSTRING_DATA (var), XSTRING_LENGTH (var),
+		       &value, &valuelen))
+    v = make_string (value, valuelen);
+  if (!NILP (interactivep))
+    {
+      if (NILP (v))
+	message ("%s not defined in environment", XSTRING_DATA (var));
+      else
+	/* #### Should use Fprin1_to_string or Fprin1 to handle string
+           containing quotes correctly.  */
+	message ("\"%s\"", value);
+    }
+  RETURN_UNGCPRO (v);
+}
+
+/* A version of getenv that consults Vprocess_environment, easily
+   callable from C.
+
+   (At init time, Vprocess_environment is initialized from the
+   environment, stored in the global variable environ. [Note that
+   at startup time, `environ' should be the same as the envp parameter
+   passed to main(); however, later calls to putenv() may change
+   `environ', making the envp parameter inaccurate.] Calls to getenv()
+   and putenv() consult and modify `environ'.  However, once
+   Vprocess_environment is initted, XEmacs C code should *NEVER* call
+   getenv() or putenv() directly, because (1) Lisp code that modifies
+   the environment only modifies Vprocess_environment, not `environ';
+   and (2) Vprocess_environment is in internal format but `environ'
+   is in some external format, and getenv()/putenv() are not Mule-
+   encapsulated.
+
+   WARNING: This value points into Lisp string data and thus will become
+   invalid after a GC. */
+
+Intbyte *
+egetenv (const CIntbyte *var)
+{
+  /* This cannot GC -- 7-28-00 ben */
+  Intbyte *value;
+  Bytecount valuelen;
+
+  if (getenv_internal ((const Intbyte *) var, strlen (var), &value, &valuelen))
+    return value;
+  else
+    return 0;
+}
+
+void
+eputenv (const CIntbyte *var, const CIntbyte *value)
+{
+  putenv_internal ((Intbyte *) var, strlen (var), (Intbyte *) value,
+		   strlen (value));
+}
+
+
+/* This is not named init_process in order to avoid a conflict with NS 3.3 */
+void
+init_xemacs_process (void)
+{
+  /* This function can GC */
+
+  MAYBE_PROCMETH (init_process, ());
+
+  Vprocess_list = Qnil;
+
+  if (usid_to_process)
+    clrhash (usid_to_process);
+  else
+    usid_to_process = make_hash_table (32);
+  
+  {
+    /* jwz: always initialize Vprocess_environment, so that egetenv()
+       works in temacs. */
+    char **envp;
+    Vprocess_environment = Qnil;
+    for (envp = environ; envp && *envp; envp++)
+      Vprocess_environment =
+	Fcons (build_ext_string (*envp, Qnative), Vprocess_environment);
+    /* This gets set back to 0 in disksave_object_finalization() */
+    env_initted = 1;
+  }
+
+  {
+    /* Initialize shell-file-name from environment variables or best guess. */
+#ifdef WIN32_NATIVE
+    const Intbyte *shell = egetenv ("SHELL");
+    if (!shell) shell = egetenv ("COMSPEC");
+    /* Should never happen! */
+    if (!shell) shell =
+      (Intbyte *) (GetVersion () & 0x80000000 ? "command" : "cmd");
+#else /* not WIN32_NATIVE */
+    const Intbyte *shell = egetenv ("SHELL");
+    if (!shell) shell = (Intbyte *) "/bin/sh";
+#endif
+
+#if 0 /* defined (WIN32_NATIVE) */
+    /* BAD BAD BAD.  We do not wanting to be passing an XEmacs-created
+       SHELL var down to some inferior Cygwin process, which might get
+       screwed up.
+	 
+       There are a few broken apps (eterm/term.el, eterm/tshell.el,
+       os-utils/terminal.el, texinfo/tex-mode.el) where this will
+       cause problems.  Those broken apps don't look at
+       shell-file-name, instead just at explicit-shell-file-name,
+       ESHELL and SHELL.  They are apparently attempting to borrow
+       what `M-x shell' uses, but that latter also looks at
+       shell-file-name.  What we want is for all of these apps to look
+       at shell-file-name, so that the user can change the value of
+       shell-file-name and everything will work out hunky-dorey.
+       */
+    
+    if (!egetenv ("SHELL"))
+      {
+	Intbyte *faux_var = alloca_array (Intbyte, 7 + qxestrlen (shell));
+	qxesprintf (faux_var, "SHELL=%s", shell);
+	Vprocess_environment = Fcons (build_intstring (faux_var),
+				      Vprocess_environment);
+      }
+#endif /* 0 */
+
+    Vshell_file_name = build_intstring (shell);
+  }
+}
+
 void
 syms_of_process (void)
 {
@@ -2144,6 +2372,7 @@
   DEFSUBR (Fset_process_output_coding_system);
   DEFSUBR (Fprocess_coding_system);
   DEFSUBR (Fset_process_coding_system);
+  DEFSUBR (Fgetenv);
 }
 
 void
@@ -2232,6 +2461,20 @@
 */ );
   network_stream_blocking_port_list = Qnil;
 #endif	/* PROCESS_IO_BLOCKING */
-}
+
+  /* This function can GC */
+  DEFVAR_LISP ("shell-file-name", &Vshell_file_name /*
+*File name to load inferior shells from.
+Initialized from the SHELL environment variable.
+*/ );
 
-#endif /* not NO_SUBPROCESSES */
+  DEFVAR_LISP ("process-environment", &Vprocess_environment /*
+List of environment variables for subprocesses to inherit.
+Each element should be a string of the form ENVVARNAME=VALUE.
+The environment which Emacs inherits is placed in this variable
+when Emacs starts.
+*/ );
+
+  Vlisp_EXEC_SUFFIXES = build_string (EXEC_SUFFIXES);
+  staticpro (&Vlisp_EXEC_SUFFIXES);
+}