OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [os/] [env.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2010 The Go Authors.  All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// General environment variables.
6
 
7
package os
8
 
9
import (
10
        "errors"
11
        "syscall"
12
)
13
 
14
// Expand replaces ${var} or $var in the string based on the mapping function.
15
// Invocations of undefined variables are replaced with the empty string.
16
func Expand(s string, mapping func(string) string) string {
17
        buf := make([]byte, 0, 2*len(s))
18
        // ${} is all ASCII, so bytes are fine for this operation.
19
        i := 0
20
        for j := 0; j < len(s); j++ {
21
                if s[j] == '$' && j+1 < len(s) {
22
                        buf = append(buf, s[i:j]...)
23
                        name, w := getShellName(s[j+1:])
24
                        buf = append(buf, mapping(name)...)
25
                        j += w
26
                        i = j + 1
27
                }
28
        }
29
        return string(buf) + s[i:]
30
}
31
 
32
// ShellExpand replaces ${var} or $var in the string according to the values
33
// of the operating system's environment variables.  References to undefined
34
// variables are replaced by the empty string.
35
func ShellExpand(s string) string {
36
        return Expand(s, Getenv)
37
}
38
 
39
// isSpellSpecialVar reports whether the character identifies a special
40
// shell variable such as $*.
41
func isShellSpecialVar(c uint8) bool {
42
        switch c {
43
        case '*', '#', '$', '@', '!', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
44
                return true
45
        }
46
        return false
47
}
48
 
49
// isAlphaNum reports whether the byte is an ASCII letter, number, or underscore
50
func isAlphaNum(c uint8) bool {
51
        return c == '_' || '0' <= c && c <= '9' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'
52
}
53
 
54
// getName returns the name that begins the string and the number of bytes
55
// consumed to extract it.  If the name is enclosed in {}, it's part of a ${}
56
// expansion and two more bytes are needed than the length of the name.
57
func getShellName(s string) (string, int) {
58
        switch {
59
        case s[0] == '{':
60
                if len(s) > 2 && isShellSpecialVar(s[1]) && s[2] == '}' {
61
                        return s[1:2], 3
62
                }
63
                // Scan to closing brace
64
                for i := 1; i < len(s); i++ {
65
                        if s[i] == '}' {
66
                                return s[1:i], i + 1
67
                        }
68
                }
69
                return "", 1 // Bad syntax; just eat the brace.
70
        case isShellSpecialVar(s[0]):
71
                return s[0:1], 1
72
        }
73
        // Scan alphanumerics.
74
        var i int
75
        for i = 0; i < len(s) && isAlphaNum(s[i]); i++ {
76
        }
77
        return s[:i], i
78
}
79
 
80
// ENOENV is the error indicating that an environment variable does not exist.
81
var ENOENV = errors.New("no such environment variable")
82
 
83
// Getenverror retrieves the value of the environment variable named by the key.
84
// It returns the value and an error, if any.
85
func Getenverror(key string) (value string, err error) {
86
        if len(key) == 0 {
87
                return "", EINVAL
88
        }
89
        val, found := syscall.Getenv(key)
90
        if !found {
91
                return "", ENOENV
92
        }
93
        return val, nil
94
}
95
 
96
// Getenv retrieves the value of the environment variable named by the key.
97
// It returns the value, which will be empty if the variable is not present.
98
func Getenv(key string) string {
99
        v, _ := Getenverror(key)
100
        return v
101
}
102
 
103
// Setenv sets the value of the environment variable named by the key.
104
// It returns an error, if any.
105
func Setenv(key, value string) error {
106
        err := syscall.Setenv(key, value)
107
        if err != nil {
108
                return NewSyscallError("setenv", err)
109
        }
110
        return nil
111
}
112
 
113
// Clearenv deletes all environment variables.
114
func Clearenv() {
115
        syscall.Clearenv()
116
}
117
 
118
// Environ returns an array of strings representing the environment,
119
// in the form "key=value".
120
func Environ() []string {
121
        return syscall.Environ()
122
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.