elvish/eval/util.go

61 lines
1.0 KiB
Go
Raw Normal View History

2016-01-31 09:11:10 +08:00
package eval
2016-02-19 05:52:05 +08:00
import (
"os"
"strings"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/util"
)
2016-01-31 09:11:10 +08:00
func throw(e error) {
util.Throw(e)
2016-01-31 09:11:10 +08:00
}
2016-02-15 21:18:27 +08:00
func maybeThrow(err error) {
if err != nil {
2016-02-20 07:48:13 +08:00
util.Throw(err)
2016-02-15 21:18:27 +08:00
}
}
2016-02-19 05:52:05 +08:00
func mustGetHome(uname string) string {
dir, err := util.GetHome(uname)
if err != nil {
throw(err)
}
return dir
}
// parseVariable parses a variable name.
func parseVariable(qname string) (splice bool, ns string, name string) {
if strings.HasPrefix(qname, "@") {
splice = true
qname = qname[1:]
if qname == "" {
qname = "args"
}
}
i := strings.IndexRune(qname, ':')
if i == -1 {
return splice, "", qname
}
return splice, qname[:i], qname[i+1:]
}
func makeFlag(m parse.RedirMode) int {
switch m {
case parse.Read:
return os.O_RDONLY
case parse.Write:
return os.O_WRONLY | os.O_CREATE
case parse.ReadWrite:
return os.O_RDWR | os.O_CREATE
case parse.Append:
return os.O_WRONLY | os.O_CREATE | os.O_APPEND
default:
// XXX should report parser bug
panic("bad RedirMode; parser bug")
}
}