+
+(defun substitute-env-vars (string)
+ "Substitute environment variables referred to in STRING.
+`$FOO' where FOO is an environment variable name means to substitute
+the value of that variable. The variable name should be terminated
+with a character not a letter, digit or underscore; otherwise, enclose
+the entire variable name in braces. For instance, in `ab$cd-x',
+`$cd' is treated as an environment variable.
+
+Use `$$' to insert a single dollar sign."
+ (let ((start 0))
+ (while (string-match
+ (eval-when-compile
+ (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
+ (and "${" (submatch (minimal-match (0+ anything))) "}")
+ "$$")))
+ string start)
+ (cond ((match-beginning 1)
+ (let ((value (getenv (match-string 1 string))))
+ (setq string (replace-match (or value "") t t string)
+ start (+ (match-beginning 0) (length value)))))
+ ((match-beginning 2)
+ (let ((value (getenv (match-string 2 string))))
+ (setq string (replace-match (or value "") t t string)
+ start (+ (match-beginning 0) (length value)))))
+ (t
+ (setq string (replace-match "$" t t string)
+ start (+ (match-beginning 0) 1)))))
+ string))
+
+;; Fixme: Should `process-environment' be recoded if LC_CTYPE &c is set?
+
+(defun setenv (variable &optional value substitute-env-vars)