Mercurial > hg > xemacs-beta
view lisp/process.el @ 853:2b6fa2618f76
[xemacs-hg @ 2002-05-28 08:44:22 by ben]
merge my stderr-proc ws
make-docfile.c: Fix places where we forget to check for EOF.
code-init.el: Don't use CRLF conversion by default on process output. CMD.EXE and
friends work both ways but Cygwin programs don't like the CRs.
code-process.el, multicast.el, process.el: Removed.
Improvements to call-process-internal:
-- allows a buffer to be specified for input and stderr output
-- use it on all systems
-- implement C-g as documented
-- clean up and comment
call-process-region uses new call-process facilities; no temp file.
remove duplicate funs in process.el.
comment exactly how coding systems work and fix various problems.
open-multicast-group now does similar coding-system frobbing to
open-network-stream.
dumped-lisp.el, faces.el, msw-faces.el: Fix some hidden errors due to code not being defined at the right time.
xemacs.mak: Add -DSTRICT.
================================================================
ALLOW SEPARATION OF STDOUT AND STDERR IN PROCESSES
================================================================
Standard output and standard error can be processed separately in
a process. Each can have its own buffer, its own mark in that buffer,
and its filter function. You can specify a separate buffer for stderr
in `start-process' to get things started, or use the new primitives:
set-process-stderr-buffer
process-stderr-buffer
process-stderr-mark
set-process-stderr-filter
process-stderr-filter
Also, process-send-region takes a 4th optional arg, a buffer.
Currently always uses a pipe() under Unix to read the error output.
(#### Would a PTY be better?)
sysdep.h, sysproc.h, unexfreebsd.c, unexsunos4.c, nt.c, emacs.c, callproc.c, symsinit.h, sysdep.c, Makefile.in.in, process-unix.c: Delete callproc.c. Move child_setup() to process-unix.c.
wait_for_termination() now only needed on a few really old systems.
console-msw.h, event-Xt.c, event-msw.c, event-stream.c, event-tty.c, event-unixoid.c, events.h, process-nt.c, process-unix.c, process.c, process.h, procimpl.h: Rewrite the process methods to handle a separate channel for
error input. Create Lstreams for reading in the error channel.
Many process methods need change. In general the changes are
fairly clear as they involve duplicating what's used for reading
the normal stdout and changing for stderr -- although tedious,
as such changes are required throughout the entire process code.
Rewrote the code that reads process output to do two loops, one
for stdout and one for stderr.
gpmevent.c, tooltalk.c: set_process_filter takes an argument for stderr.
================================================================
NEW ERROR-TRAPPING MECHANISM
================================================================
Totally rewrite error trapping code to be unified and support more
features. Basic function is call_trapping_problems(), which lets
you specify, by means of flags, what sorts of problems you want
trapped. these can include
-- quit
-- errors
-- throws past the function
-- creation of "display objects" (e.g. buffers)
-- deletion of already-existing "display objects" (e.g. buffers)
-- modification of already-existing buffers
-- entering the debugger
-- gc
-- errors->warnings (ala suspended errors)
etc. All other error funs rewritten in terms of this one.
Various older mechanisms removed or rewritten.
window.c, insdel.c, console.c, buffer.c, device.c, frame.c: When creating a display object, added call to
note_object_created(), for use with trapping_problems mechanism.
When deleting, call check_allowed_operation() and note_object
deleted().
The trapping-problems code records the objects created since the
call-trapping-problems began. Those objects can be deleted, but
none others (i.e. previously existing ones).
bytecode.c, cmdloop.c: internal_catch takes another arg.
eval.c: Add long comments describing the "five lists" used to maintain
state (backtrace, gcpro, specbind, etc.) in the Lisp engine.
backtrace.h, eval.c: Implement trapping-problems mechanism, eliminate old mechanisms or
redo in terms of new one.
frame.c, gutter.c: Flush out the concept of "critical display section", defined by
the in_display() var. Use an internal_bind() to get it reset,
rather than just doing it at end, because there may be a non-local
exit.
event-msw.c, event-stream.c, console-msw.h, device.c, dialog-msw.c, frame.c, frame.h, intl.c, toolbar.c, menubar-msw.c, redisplay.c, alloc.c, menubar-x.c: Make use of new trapping-errors stuff and rewrite code based on
old mechanisms.
glyphs-widget.c, redisplay.h: Protect calling Lisp in redisplay.
insdel.c: Protect hooks against deleting existing buffers.
frame-msw.c: Use EQ, not EQUAL in hash tables whose keys are just numbers.
Otherwise we run into stickiness in redisplay because
internal_equal() can QUIT.
================================================================
SIGNAL, C-G CHANGES
================================================================
Here we change the way that C-g interacts with event reading. The
idea is that a C-g occurring while we're reading a user event
should be read as C-g, but elsewhere should be a QUIT. The former
code did all sorts of bizarreness -- requiring that no QUIT occurs
anywhere in event-reading code (impossible to enforce given the
stuff called or Lisp code invoked), and having some weird system
involving enqueue/dequeue of a C-g and interaction with Vquit_flag
-- and it didn't work.
Now, we simply enclose all code where we want C-g read as an event
with {begin/end}_dont_check_for_quit(). This completely turns off
the mechanism that checks (and may remove or alter) C-g in the
read-ahead queues, so we just get the C-g normal.
Signal.c documents this very carefully.
cmdloop.c: Correct use of dont_check_for_quit to new scheme, remove old
out-of-date comments.
event-stream.c: Fix C-g handling to actually work.
device-x.c: Disable quit checking when err out.
signal.c: Cleanup. Add large descriptive comment.
process-unix.c, process-nt.c, sysdep.c: Use QUIT instead of REALLY_QUIT.
It's not necessary to use REALLY_QUIT and just confuses the issue.
lisp.h: Comment quit handlers.
================================================================
CONS CHANGES
================================================================
free_cons() now takes a Lisp_Object not the result of XCONS().
car and cdr have been renamed so that they don't get used directly;
go through XCAR(), XCDR() instead.
alloc.c, dired.c, editfns.c, emodules.c, fns.c, glyphs-msw.c, glyphs-x.c, glyphs.c, keymap.c, minibuf.c, search.c, eval.c, lread.c, lisp.h: Correct free_cons calling convention: now takes Lisp_Object,
not Lisp_Cons
chartab.c: Eliminate direct use of ->car, ->cdr, should be black box.
callint.c: Rewrote using EXTERNAL_LIST_LOOP to avoid use of Lisp_Cons.
================================================================
USE INTERNAL-BIND-*
================================================================
eval.c: Cleanups of these funs.
alloc.c, fileio.c, undo.c, specifier.c, text.c, profile.c, lread.c, redisplay.c, menubar-x.c, macros.c: Rewrote to use internal_bind_int() and internal_bind_lisp_object()
in place of whatever varied and cumbersome mechanisms were
formerly there.
================================================================
SPECBIND SANITY
================================================================
backtrace.h: - Improved comments
backtrace.h, bytecode.c, eval.c: Add new mechanism check_specbind_stack_sanity() for sanity
checking code each time the catchlist or specbind stack change.
Removed older prototype of same mechanism.
================================================================
MISC
================================================================
lisp.h, insdel.c, window.c, device.c, console.c, buffer.c: Fleshed out authorship.
device-msw.c: Correct bad Unicode-ization.
print.c: Be more careful when not initialized or in fatal error handling.
search.c: Eliminate running_asynch_code, an FSF holdover.
alloc.c: Added comments about gc-cons-threshold.
dialog-x.c: Use begin_gc_forbidden() around code to build up a widget value
tree, like in menubar-x.c.
gui.c: Use Qunbound not Qnil as the default for
gethash.
lisp-disunion.h, lisp-union.h: Added warnings on use of VOID_TO_LISP().
lisp.h: Use ERROR_CHECK_STRUCTURES to turn on
ERROR_CHECK_TRAPPING_PROBLEMS and ERROR_CHECK_TYPECHECK
lisp.h: Add assert_with_message.
lisp.h: Add macros for gcproing entire arrays. (You could do this before
but it required manual twiddling the gcpro structure.)
lisp.h: Add prototypes for new functions defined elsewhere.
author | ben |
---|---|
date | Tue, 28 May 2002 08:45:36 +0000 |
parents | a634e3b7acc8 |
children | 84762348c6f9 |
line wrap: on
line source
;;; process.el --- commands for subprocesses; split out of simple.el ;; Copyright (C) 1985-7, 1993,4, 1997 Free Software Foundation, Inc. ;; Copyright (C) 1995, 2000, 2001, 2002 Ben Wing. ;; Author: Ben Wing ;; Maintainer: XEmacs Development Team ;; Keywords: internal, processes, dumped ;; This file is part of XEmacs. ;; XEmacs is free software; you can redistribute it and/or modify it ;; under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; XEmacs is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with XEmacs; see the file COPYING. If not, write to the ;; Free Software Foundation, 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;; Synched up with: FSF 19.30, except for setenv/getenv (synched with FSF ;;; 21.0.105). ;;; Authorship: ;; Created 1995 by Ben Wing during Mule work -- some commands split out ;; of simple.el and wrappers of *-internal functions created so they could ;; be redefined in a Mule world. ;; Lisp definition of call-process-internal added Mar. 2000 by Ben Wing. ;;; Commentary: ;; This file is dumped with XEmacs. ;;; Code: (defgroup processes nil "Process, subshell, compilation, and job control support." :group 'external :group 'development) (defgroup processes-basics nil "Basic stuff dealing with processes." :group 'processes) (defgroup execute nil "Executing external commands." :group 'processes) ;; This may be changed to "/c" in win32-native.el. (defvar shell-command-switch "-c" "Switch used to have the shell execute its command line argument.") (defun start-process-shell-command (name buffer &rest args) "Start a program in a subprocess. Return the process object for it. NAME is name for process. It is modified if necessary to make it unique. BUFFER is the buffer or (buffer-name) to associate with the process. Process output goes at end of that buffer, unless you specify an output stream or filter function to handle the output. BUFFER may be also nil, meaning that this process is not associated with any buffer Third arg is command name, the name of a shell command. Remaining arguments are the arguments for the command. Wildcards and redirection are handled as usual in the shell." ;; We used to use `exec' to replace the shell with the command, ;; but that failed to handle (...) and semicolon, etc. (start-process name buffer shell-file-name shell-command-switch (mapconcat #'identity args " "))) (defun call-process-internal (program &optional infile buffer display &rest args) "Internal function to call PROGRAM synchronously in separate process. Lisp callers should use `call-process' or `call-process-region'. The program's input comes from file INFILE (nil means `/dev/null'). XEmacs feature: INFILE can also be a list of (BUFFER [START [END]]), i.e. a list of one to three elements, consisting of a buffer and optionally a start position or start and end position. In this case, input comes from the buffer, starting from START (defaults to the beginning of the buffer) and ending at END (defaults to the end of the buffer). Insert output in BUFFER before point; t means current buffer; nil for BUFFER means discard it; 0 means discard and don't wait. If BUFFER is a string, then find or create a buffer with that name, then insert the output in that buffer, before point. BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, REAL-BUFFER says what to do with standard output, as above, while STDERR-FILE says what to do with standard error in the child. STDERR-FILE may be nil (discard standard error output), t (mix it with ordinary output), a file name string, or (XEmacs feature) a buffer object. If STDERR-FILE is a buffer object (but not the name of a buffer, since that would be interpreted as a file), the standard error output will be inserted into the buffer before point. Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted. Remaining arguments are strings passed as command arguments to PROGRAM. If BUFFER is 0, returns immediately with value nil. Otherwise waits for PROGRAM to terminate and returns a numeric exit status or a signal description string. If you quit, the process is first killed with SIGINT, then with SIGKILL if you quit again before the process exits. Coding systems for the process are the same as for `start-process-internal'." (let (proc inbuf errbuf kill-inbuf kill-errbuf no-wait start end) ;; first set up an unwind-protect to clean everything up. this will: ;; ;; -- kill the process. (when we're not waiting for it to finish, we ;; set PROC to nil when we're ready to exit so this doesn't happen -- ;; if we're interrupted before we're ready to exit, we should still ;; kill the process) ;; -- kill temporary buffers created to handle I/O to or from a file. ;; KILL-INBUF/KILL-ERRBUF tell us if we should do so. ;; ;; note that we need to be *very* careful in this code to handle C-g ;; at any point. (unwind-protect (progn ;; first handle INFILE. (cond ((stringp infile) (setq infile (expand-file-name infile)) (setq kill-inbuf t) (setq inbuf (generate-new-buffer "*call-process*")) ;; transfer the exact contents of the file to the process. ;; we do that by reading in and writing out in ;; binary. #### is this even correct? should we be doing ;; the same thing with stderr? if so we'd need a way of ;; controlling the stderr coding system separate from ;; everything else. (with-current-buffer inbuf ;; Make sure this works with jka-compr (let ((file-name-handler-alist nil)) (insert-file-contents-internal infile nil nil nil nil 'binary)) (setq start (point-min) end (point-max)))) ((consp infile) (setq inbuf (get-buffer (car infile))) (setq start (or (nth 1 infile) (point-min inbuf))) (setq end (or (nth 2 infile) (point-max inbuf)))) ((null infile) nil) (t (error 'wrong-type-argument "Must be filename or (BUFFER [START [END]])" infile))) ;; now handle BUFFER (let ((stderr (if (consp buffer) (second buffer) t))) (if (consp buffer) (setq buffer (car buffer))) (setq buffer (cond ((null buffer) nil) ((eq buffer t) (current-buffer)) ;; use integerp for compatibility with existing ;; call-process rmsism. ((integerp buffer) (setq no-wait t) nil) (t (get-buffer-create buffer)))) (when (and stderr (not (eq t stderr))) ;; both ERRBUF and STDERR being non-nil indicates to the ;; code below that STDERR is a file and we should write ;; ERRBUF to it; so clear out STDERR if we don't want this. (if (bufferp stderr) (setq errbuf stderr stderr nil) (setq stderr (expand-file-name stderr)) (setq kill-errbuf t) (setq errbuf (generate-new-buffer "*call-process*")))) ;; now start process. using a pty causes all sorts of ;; weirdness, at least under cygwin, when there's input. #### i ;; don't know what's going wrong and whether it's a cygwin-only ;; problem. suffice to say that there were NO pty connections ;; in the old version. (let ((process-connection-type nil)) (setq proc (apply 'start-process-internal "*call-process*" (if (eq t stderr) buffer (list buffer errbuf)) program args))) ;; see comment above where the data was read from the file. (if kill-inbuf (set-process-output-coding-system proc 'binary)) ;; point mark/stderr-mark at the right place (by default it's ;; end of buffer). (if buffer (set-marker (process-mark proc) (point buffer) buffer)) (if errbuf (set-marker (process-stderr-mark proc) (point errbuf) errbuf)) ;; now do I/O, very carefully! the unwind-protect makes sure ;; to clear out the sentinel, since it does a `throw', which would ;; have no catch (or writes to a file -- we only want this on ;; normal exit) (unwind-protect ;; if not NO-WAIT, set a sentinel to return the exit ;; status. it will throw to this catch so we can exit ;; properly. (catch 'call-process-done (set-process-sentinel proc (if no-wait ;; we're trying really really hard to emulate ;; the old call-process, which would save the ;; stderr to a file even if discarding output. so ;; we set a sentinel to save the output when ;; we finish. ;; ;; #### not clear if we should be doing this. ;; ;; NOTE NOTE NOTE: Due to the total bogosity of ;; dynamic scoping, and the lack of closures, we ;; have to be careful how we write the first ;; sentinel below since it may be executed after ;; this function has returned -- thus we fake a ;; closure. (This doesn't apply to the second one, ;; which only gets executed within the ;; unwind-protect.) (if (and errbuf stderr) (set-process-sentinel proc `(lambda (proc status) (set-process-sentinel proc nil) (with-current-buffer ,errbuf (write-region-internal 1 (1+ (buffer-size)) ,stderr nil 'major-rms-kludge-city nil coding-system-for-write)) (kill-buffer ,errbuf)))) ;; normal sentinel: maybe write out stderr and return ;; status. #'(lambda (proc status) (when (and errbuf stderr) (with-current-buffer errbuf (write-region-internal 1 (1+ (buffer-size)) stderr nil 'major-rms-kludge-city nil coding-system-for-write))) (cond ((eq 'exit (process-status proc)) (set-process-sentinel proc nil) (throw 'call-process-done (process-exit-status proc))) ((eq 'signal (process-status proc)) (set-process-sentinel proc nil) (throw 'call-process-done status)))))) (if (not no-wait) ;; we're waiting. send the input and loop forever, ;; handling process output and maybe redisplaying. ;; exit happens through the sentinel or C-g. if ;; C-g, send SIGINT the first time, EOF if not ;; already done so (might make the process exit), ;; and keep waiting. Another C-g will exit the ;; whole function, and the unwind-protect will ;; kill the process. (Hence the documented semantics ;; of SIGINT/SIGKILL.) (let (eof-sent) (condition-case nil (progn (when inbuf (process-send-region proc start end inbuf)) (process-send-eof proc) (setq eof-sent t) (while t (accept-process-output proc) (if display (sit-for 0)))) (quit (process-send-signal 'SIGINT proc) (unless eof-sent (process-send-eof proc)) (while t (accept-process-output proc) (if display (sit-for 0)))))) ;; discard and no wait: send the input, set PROC ;; and ERRBUF to nil so that the unwind-protect ;; forms don't erase the sentinel, kill the process, ;; or kill ERRBUF (the sentinel does that), and exit. (when inbuf (process-send-region proc start end inbuf)) (process-send-eof proc) (setq errbuf nil) (setq proc nil))) (if proc (set-process-sentinel proc nil))))) ;; unwind-protect forms. (if (and inbuf kill-inbuf) (kill-buffer inbuf)) (if (and errbuf kill-errbuf) (kill-buffer errbuf)) (condition-case nil (if (and proc (process-live-p proc)) (kill-process proc)) (error nil))))) (defun shell-command (command &optional output-buffer) "Execute string COMMAND in inferior shell; display output, if any. If COMMAND ends in ampersand, execute it asynchronously. The output appears in the buffer `*Async Shell Command*'. That buffer is in shell mode. Otherwise, COMMAND is executed synchronously. The output appears in the buffer `*Shell Command Output*'. If the output is one line, it is displayed in the echo area *as well*, but it is nonetheless available in buffer `*Shell Command Output*', even though that buffer is not automatically displayed. If there is no output, or if output is inserted in the current buffer, then `*Shell Command Output*' is deleted. The optional second argument OUTPUT-BUFFER, if non-nil, says to put the output in some other buffer. If OUTPUT-BUFFER is a buffer or buffer name, put the output there. If OUTPUT-BUFFER is not a buffer and not nil, insert output in current buffer. (This cannot be done asynchronously.) In either case, the output is inserted after point (leaving mark after it)." (interactive (list (read-shell-command "Shell command: ") current-prefix-arg)) (if (and output-buffer (not (or (bufferp output-buffer) (stringp output-buffer)))) (progn (barf-if-buffer-read-only) (push-mark nil (not (interactive-p))) ;; We do not use -f for csh; we will not support broken use of ;; .cshrcs. Even the BSD csh manual says to use ;; "if ($?prompt) exit" before things which are not useful ;; non-interactively. Besides, if someone wants their other ;; aliases for shell commands then they can still have them. (call-process shell-file-name nil t nil shell-command-switch command) (exchange-point-and-mark t)) ;; Preserve the match data in case called from a program. (save-match-data (if (string-match "[ \t]*&[ \t]*$" command) ;; Command ending with ampersand means asynchronous. (progn (if-fboundp 'background (background (substring command 0 (match-beginning 0))) (error 'unimplemented "backgrounding a shell command requires package `background'"))) (shell-command-on-region (point) (point) command output-buffer))))) ;; We have a sentinel to prevent insertion of a termination message ;; in the buffer itself. (defun shell-command-sentinel (process signal) (if (memq (process-status process) '(exit signal)) (message "%s: %s." (car (cdr (cdr (process-command process)))) (substring signal 0 -1)))) (defun shell-command-on-region (start end command &optional output-buffer replace) "Execute string COMMAND in inferior shell with region as input. Normally display output (if any) in temp buffer `*Shell Command Output*'; Prefix arg means replace the region with it. The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE. If REPLACE is non-nil, that means insert the output in place of text from START to END, putting point and mark around it. If the output is one line, it is displayed in the echo area, but it is nonetheless available in buffer `*Shell Command Output*' even though that buffer is not automatically displayed. If there is no output, or if output is inserted in the current buffer, then `*Shell Command Output*' is deleted. If the optional fourth argument OUTPUT-BUFFER is non-nil, that says to put the output in some other buffer. If OUTPUT-BUFFER is a buffer or buffer name, put the output there. If OUTPUT-BUFFER is not a buffer and not nil, insert output in the current buffer. In either case, the output is inserted after point (leaving mark after it)." (interactive (let ((string ;; Do this before calling region-beginning ;; and region-end, in case subprocess output ;; relocates them while we are in the minibuffer. (read-shell-command "Shell command on region: "))) ;; call-interactively recognizes region-beginning and ;; region-end specially, leaving them in the history. (list (region-beginning) (region-end) string current-prefix-arg current-prefix-arg))) (if (or replace (and output-buffer (not (or (bufferp output-buffer) (stringp output-buffer))))) ;; Replace specified region with output from command. (let ((swap (and replace (< start end)))) ;; Don't muck with mark unless REPLACE says we should. (goto-char start) (and replace (push-mark)) (call-process-region start end shell-file-name t t nil shell-command-switch command) (let ((shell-buffer (get-buffer "*Shell Command Output*"))) (and shell-buffer (not (eq shell-buffer (current-buffer))) (kill-buffer shell-buffer))) ;; Don't muck with mark unless REPLACE says we should. (and replace swap (exchange-point-and-mark t))) ;; No prefix argument: put the output in a temp buffer, ;; replacing its entire contents. (let ((buffer (get-buffer-create (or output-buffer "*Shell Command Output*"))) (success nil) (exit-status nil) (directory default-directory)) (unwind-protect (if (eq buffer (current-buffer)) ;; If the input is the same buffer as the output, ;; delete everything but the specified region, ;; then replace that region with the output. (progn (setq buffer-read-only nil) (delete-region (max start end) (point-max)) (delete-region (point-min) (max start end)) (setq exit-status (call-process-region (point-min) (point-max) shell-file-name t t nil shell-command-switch command)) (setq success t)) ;; Clear the output buffer, ;; then run the command with output there. (save-excursion (set-buffer buffer) (setq buffer-read-only nil) ;; XEmacs change (setq default-directory directory) (erase-buffer)) (setq exit-status (call-process-region start end shell-file-name nil buffer nil shell-command-switch command)) (setq success t)) ;; Report the amount of output. (let ((lines (save-excursion (set-buffer buffer) (if (= (buffer-size) 0) 0 (count-lines (point-min) (point-max)))))) (cond ((= lines 0) (if success (display-message 'command (if (eql exit-status 0) "(Shell command succeeded with no output)" "(Shell command failed with no output)"))) (kill-buffer buffer)) ((and success (= lines 1)) (message "%s" (save-excursion (set-buffer buffer) (goto-char (point-min)) (buffer-substring (point) (progn (end-of-line) (point)))))) (t (set-window-start (display-buffer buffer) 1)))))))) (defun shell-quote-argument (argument) "Quote an argument for passing as argument to an inferior shell." (if (and (eq system-type 'windows-nt) (let ((progname (downcase (file-name-nondirectory shell-file-name)))) (or (equal progname "command.com") (equal progname "cmd.exe")))) ;; the expectation is that you can take the result of ;; shell-quote-argument and pass it to as an arg to ;; (start-process shell-quote-argument ...) and have it end ;; up as-is in the program's argv[] array. to do this, we ;; need to protect against both the shell's and the program's ;; quoting conventions (and our own conventions in ;; mswindows-construct-process-command-line!). Putting quotes ;; around shell metachars gets through the last two, and applying ;; the normal VC runtime quoting works with practically all apps. (declare-fboundp (mswindows-quote-one-vc-runtime-arg argument t)) (if (equal argument "") "\"\"" ;; Quote everything except POSIX filename characters. ;; This should be safe enough even for really weird shells. (let ((result "") (start 0) end) (while (string-match "[^-0-9a-zA-Z_./]" argument start) (setq end (match-beginning 0) result (concat result (substring argument start end) "\\" (substring argument end (1+ end))) start (1+ end))) (concat result (substring argument start)))))) (defun shell-command-to-string (command) "Execute shell command COMMAND and return its output as a string." (with-output-to-string (call-process shell-file-name nil t nil shell-command-switch command))) (defalias 'exec-to-string 'shell-command-to-string) ;; History list for environment variable names. (defvar read-envvar-name-history nil) (defun read-envvar-name (prompt &optional mustmatch) "Read environment variable name, prompting with PROMPT. Optional second arg MUSTMATCH, if non-nil, means require existing envvar name. If it is also not t, RET does not exit if it does non-null completion." (completing-read prompt (mapcar (function (lambda (enventry) (list (substring enventry 0 (string-match "=" enventry))))) process-environment) nil mustmatch nil 'read-envvar-name-history)) ;; History list for VALUE argument to setenv. (defvar setenv-history nil) (defun setenv (variable &optional value unset) "Set the value of the environment variable named VARIABLE to VALUE. VARIABLE should be a string. VALUE is optional; if not provided or is `nil', the environment variable VARIABLE will be removed. Interactively, a prefix argument means to unset the variable. Interactively, the current value (if any) of the variable appears at the front of the history list when you type in the new value. This function works by modifying `process-environment'." (interactive (if current-prefix-arg (list (read-envvar-name "Clear environment variable: " 'exact) nil t) (let ((var (read-envvar-name "Set environment variable: " nil))) ;; Here finally we specify the args to call setenv with. (list var (read-from-minibuffer (format "Set %s to value: " var) nil nil nil 'setenv-history (getenv var)))))) (if unset (setq value nil)) (if (string-match "=" variable) (error "Environment variable name `%s' contains `='" variable) (let ((pattern (concat "\\`" (regexp-quote (concat variable "=")))) (case-fold-search nil) (scan process-environment) found) (if (string-equal "TZ" variable) (set-time-zone-rule value)) (while scan (cond ((string-match pattern (car scan)) (setq found t) (if (eq nil value) (setq process-environment (delq (car scan) process-environment)) (setcar scan (concat variable "=" value))) (setq scan nil))) (setq scan (cdr scan))) (or found (if value (setq process-environment (cons (concat variable "=" value) process-environment))))))) ;; already in C. Can't move it to Lisp too easily because it's needed ;; extremely early in the Lisp loadup sequence. ; (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)) (provide 'env) ;; Yuck. Formerly the above were in env.el, which did this ;; provide. ;;; process.el ends here