2013-10-01 21:34:49 +08:00
|
|
|
package eval
|
|
|
|
|
2014-04-02 10:51:09 +08:00
|
|
|
// Builtin functions.
|
|
|
|
|
2013-10-02 22:10:07 +08:00
|
|
|
import (
|
2016-01-31 09:21:46 +08:00
|
|
|
"errors"
|
2014-01-16 09:24:14 +08:00
|
|
|
"fmt"
|
2016-03-17 22:28:00 +08:00
|
|
|
"math/rand"
|
2016-11-06 17:47:20 +08:00
|
|
|
"net"
|
2018-01-05 12:07:11 +08:00
|
|
|
"path/filepath"
|
2016-02-10 01:18:18 +08:00
|
|
|
"time"
|
2016-02-14 04:05:35 +08:00
|
|
|
|
2018-02-15 17:14:05 +08:00
|
|
|
"github.com/elves/elvish/eval/vals"
|
2013-10-02 22:10:07 +08:00
|
|
|
)
|
|
|
|
|
2017-12-17 13:20:03 +08:00
|
|
|
// Builtins that have not been put into their own groups go here.
|
|
|
|
|
|
|
|
var ErrArgs = errors.New("args error")
|
|
|
|
|
2015-01-23 06:19:27 +08:00
|
|
|
func init() {
|
2018-02-07 03:39:40 +08:00
|
|
|
addBuiltinFns(map[string]interface{}{
|
2018-02-05 15:35:49 +08:00
|
|
|
"nop": nop,
|
|
|
|
"kind-of": kindOf,
|
|
|
|
"constantly": constantly,
|
2015-03-01 21:43:35 +08:00
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
"-source": source,
|
2017-04-11 08:16:43 +08:00
|
|
|
|
|
|
|
// Time
|
2018-02-05 15:35:49 +08:00
|
|
|
"esleep": sleep,
|
|
|
|
"-time": _time,
|
2017-04-11 08:16:43 +08:00
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
"-ifaddrs": _ifaddrs,
|
2017-12-17 13:20:03 +08:00
|
|
|
})
|
2018-02-05 15:35:49 +08:00
|
|
|
|
2016-03-17 22:30:15 +08:00
|
|
|
// For rand and randint.
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
2013-10-01 21:34:49 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 21:25:17 +08:00
|
|
|
func nop(opts RawOptions, args ...interface{}) {
|
2018-02-05 15:35:49 +08:00
|
|
|
// Do nothing
|
2015-03-01 21:43:35 +08:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
func kindOf(fm *Frame, args ...interface{}) {
|
|
|
|
out := fm.ports[1].Chan
|
2015-01-20 05:04:37 +08:00
|
|
|
for _, a := range args {
|
2018-02-15 17:14:05 +08:00
|
|
|
out <- vals.Kind(a)
|
2015-01-20 05:04:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
func constantly(args ...interface{}) Callable {
|
2017-04-11 08:16:43 +08:00
|
|
|
// XXX Repr of this fn is not right
|
2018-02-06 16:02:27 +08:00
|
|
|
return NewBuiltinFn(
|
2017-04-11 08:16:43 +08:00
|
|
|
"created by constantly",
|
2018-02-05 15:35:49 +08:00
|
|
|
func(fm *Frame) {
|
|
|
|
out := fm.ports[1].Chan
|
2017-04-11 08:16:43 +08:00
|
|
|
for _, v := range args {
|
|
|
|
out <- v
|
|
|
|
}
|
|
|
|
},
|
2018-02-05 15:35:49 +08:00
|
|
|
)
|
2015-01-24 02:22:40 +08:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
func source(fm *Frame, fname string) error {
|
2018-01-05 12:07:11 +08:00
|
|
|
abs, err := filepath.Abs(fname)
|
2018-02-05 15:35:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fm.Source(fname, abs)
|
2017-04-11 08:16:43 +08:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
func sleep(fm *Frame, t float64) error {
|
2016-02-23 00:39:03 +08:00
|
|
|
d := time.Duration(float64(time.Second) * t)
|
|
|
|
select {
|
2018-02-05 15:35:49 +08:00
|
|
|
case <-fm.Interrupts():
|
|
|
|
return ErrInterrupted
|
2016-02-23 00:39:03 +08:00
|
|
|
case <-time.After(d):
|
2018-02-05 15:35:49 +08:00
|
|
|
return nil
|
2016-02-23 00:39:03 +08:00
|
|
|
}
|
2016-02-10 01:18:18 +08:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
func _time(ec *Frame, f Callable) error {
|
2017-04-11 08:16:43 +08:00
|
|
|
t0 := time.Now()
|
2018-01-21 19:05:36 +08:00
|
|
|
err := f.Call(ec, NoArgs, NoOpts)
|
2017-04-11 08:16:43 +08:00
|
|
|
t1 := time.Now()
|
|
|
|
|
|
|
|
dt := t1.Sub(t0)
|
|
|
|
fmt.Fprintln(ec.ports[1].File, dt)
|
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-11-06 17:47:20 +08:00
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
func _ifaddrs(fm *Frame) error {
|
2016-11-06 17:47:20 +08:00
|
|
|
addrs, err := net.InterfaceAddrs()
|
2018-02-05 15:35:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out := fm.ports[1].Chan
|
2016-11-06 17:47:20 +08:00
|
|
|
for _, addr := range addrs {
|
2018-01-25 09:40:15 +08:00
|
|
|
out <- addr.String()
|
2016-11-06 17:47:20 +08:00
|
|
|
}
|
2018-02-05 15:35:49 +08:00
|
|
|
return nil
|
2016-11-06 17:47:20 +08:00
|
|
|
}
|