2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2010-11-08 14:25:01 -07:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// General environment variables.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
2017-12-04 11:57:48 -07:00
|
|
|
import (
|
|
|
|
"internal/testlog"
|
|
|
|
"syscall"
|
|
|
|
)
|
2011-05-02 13:38:13 -06:00
|
|
|
|
2010-11-08 14:25:01 -07:00
|
|
|
// Expand replaces ${var} or $var in the string based on the mapping function.
|
2012-12-17 08:37:02 -07:00
|
|
|
// For example, os.ExpandEnv(s) is equivalent to os.Expand(s, os.Getenv).
|
2010-11-08 14:25:01 -07:00
|
|
|
func Expand(s string, mapping func(string) string) string {
|
2018-04-12 13:15:58 -06:00
|
|
|
var buf []byte
|
2010-11-08 14:25:01 -07:00
|
|
|
// ${} is all ASCII, so bytes are fine for this operation.
|
2010-11-08 16:13:35 -07:00
|
|
|
i := 0
|
|
|
|
for j := 0; j < len(s); j++ {
|
|
|
|
if s[j] == '$' && j+1 < len(s) {
|
2018-04-12 13:15:58 -06:00
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, 0, 2*len(s))
|
|
|
|
}
|
2011-10-12 14:42:04 -06:00
|
|
|
buf = append(buf, s[i:j]...)
|
2010-11-08 16:13:35 -07:00
|
|
|
name, w := getShellName(s[j+1:])
|
2018-06-29 09:59:04 -06:00
|
|
|
if name == "" && w > 0 {
|
|
|
|
// Encountered invalid syntax; eat the
|
|
|
|
// characters.
|
|
|
|
} else if name == "" {
|
|
|
|
// Valid syntax, but $ was not followed by a
|
|
|
|
// name. Leave the dollar character untouched.
|
2018-03-28 02:15:26 -06:00
|
|
|
buf = append(buf, s[j])
|
|
|
|
} else {
|
|
|
|
buf = append(buf, mapping(name)...)
|
|
|
|
}
|
2010-11-08 16:13:35 -07:00
|
|
|
j += w
|
|
|
|
i = j + 1
|
2010-11-08 14:25:01 -07:00
|
|
|
}
|
|
|
|
}
|
2018-04-12 13:15:58 -06:00
|
|
|
if buf == nil {
|
|
|
|
return s
|
|
|
|
}
|
2010-11-08 16:13:35 -07:00
|
|
|
return string(buf) + s[i:]
|
2010-11-08 14:25:01 -07:00
|
|
|
}
|
|
|
|
|
2012-02-09 20:16:15 -07:00
|
|
|
// ExpandEnv replaces ${var} or $var in the string according to the values
|
2016-03-01 16:21:55 -07:00
|
|
|
// of the current environment variables. References to undefined
|
2010-11-08 14:25:01 -07:00
|
|
|
// variables are replaced by the empty string.
|
2012-02-09 20:16:15 -07:00
|
|
|
func ExpandEnv(s string) string {
|
2010-11-08 14:25:01 -07:00
|
|
|
return Expand(s, Getenv)
|
|
|
|
}
|
|
|
|
|
2015-02-16 19:04:07 -07:00
|
|
|
// isShellSpecialVar reports whether the character identifies a special
|
2010-11-08 14:25:01 -07:00
|
|
|
// shell variable such as $*.
|
|
|
|
func isShellSpecialVar(c uint8) bool {
|
|
|
|
switch c {
|
2016-07-31 10:10:35 -06:00
|
|
|
case '*', '#', '$', '@', '!', '?', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
2010-11-08 14:25:01 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isAlphaNum reports whether the byte is an ASCII letter, number, or underscore
|
|
|
|
func isAlphaNum(c uint8) bool {
|
|
|
|
return c == '_' || '0' <= c && c <= '9' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'
|
|
|
|
}
|
|
|
|
|
2015-02-16 17:16:06 -07:00
|
|
|
// getShellName returns the name that begins the string and the number of bytes
|
2016-03-01 16:21:55 -07:00
|
|
|
// consumed to extract it. If the name is enclosed in {}, it's part of a ${}
|
2010-11-08 14:25:01 -07:00
|
|
|
// expansion and two more bytes are needed than the length of the name.
|
|
|
|
func getShellName(s string) (string, int) {
|
|
|
|
switch {
|
|
|
|
case s[0] == '{':
|
|
|
|
if len(s) > 2 && isShellSpecialVar(s[1]) && s[2] == '}' {
|
|
|
|
return s[1:2], 3
|
|
|
|
}
|
|
|
|
// Scan to closing brace
|
|
|
|
for i := 1; i < len(s); i++ {
|
|
|
|
if s[i] == '}' {
|
2018-06-29 09:59:04 -06:00
|
|
|
if i == 1 {
|
|
|
|
return "", 2 // Bad syntax; eat "${}"
|
|
|
|
}
|
2010-11-08 14:25:01 -07:00
|
|
|
return s[1:i], i + 1
|
|
|
|
}
|
|
|
|
}
|
2018-06-29 09:59:04 -06:00
|
|
|
return "", 1 // Bad syntax; eat "${"
|
2010-11-08 14:25:01 -07:00
|
|
|
case isShellSpecialVar(s[0]):
|
|
|
|
return s[0:1], 1
|
|
|
|
}
|
|
|
|
// Scan alphanumerics.
|
|
|
|
var i int
|
|
|
|
for i = 0; i < len(s) && isAlphaNum(s[i]); i++ {
|
|
|
|
}
|
|
|
|
return s[:i], i
|
|
|
|
}
|
2011-11-14 12:06:50 -07:00
|
|
|
|
|
|
|
// Getenv retrieves the value of the environment variable named by the key.
|
|
|
|
// It returns the value, which will be empty if the variable is not present.
|
2016-08-17 23:26:51 -06:00
|
|
|
// To distinguish between an empty value and an unset value, use LookupEnv.
|
2011-11-14 12:06:50 -07:00
|
|
|
func Getenv(key string) string {
|
2017-12-04 11:57:48 -07:00
|
|
|
testlog.Getenv(key)
|
2012-02-18 22:18:13 -07:00
|
|
|
v, _ := syscall.Getenv(key)
|
2011-11-14 12:06:50 -07:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2015-05-05 12:17:08 -06:00
|
|
|
// LookupEnv retrieves the value of the environment variable named
|
|
|
|
// by the key. If the variable is present in the environment the
|
|
|
|
// value (which may be empty) is returned and the boolean is true.
|
|
|
|
// Otherwise the returned value will be empty and the boolean will
|
|
|
|
// be false.
|
|
|
|
func LookupEnv(key string) (string, bool) {
|
2017-12-04 11:57:48 -07:00
|
|
|
testlog.Getenv(key)
|
2015-05-05 12:17:08 -06:00
|
|
|
return syscall.Getenv(key)
|
|
|
|
}
|
|
|
|
|
2011-11-14 12:06:50 -07:00
|
|
|
// Setenv sets the value of the environment variable named by the key.
|
|
|
|
// It returns an error, if any.
|
|
|
|
func Setenv(key, value string) error {
|
|
|
|
err := syscall.Setenv(key, value)
|
|
|
|
if err != nil {
|
|
|
|
return NewSyscallError("setenv", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-01 12:17:15 -06:00
|
|
|
// Unsetenv unsets a single environment variable.
|
|
|
|
func Unsetenv(key string) error {
|
|
|
|
return syscall.Unsetenv(key)
|
|
|
|
}
|
|
|
|
|
2011-11-14 12:06:50 -07:00
|
|
|
// Clearenv deletes all environment variables.
|
|
|
|
func Clearenv() {
|
|
|
|
syscall.Clearenv()
|
|
|
|
}
|
|
|
|
|
2012-02-09 20:16:15 -07:00
|
|
|
// Environ returns a copy of strings representing the environment,
|
2011-11-14 12:06:50 -07:00
|
|
|
// in the form "key=value".
|
|
|
|
func Environ() []string {
|
|
|
|
return syscall.Environ()
|
|
|
|
}
|