2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
2015-03-04 11:18:26 +00:00
|
|
|
//
|
2015-07-07 00:54:22 +00:00
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2015-03-04 11:18:26 +00:00
|
|
|
//
|
2015-07-07 00:54:22 +00:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2015-03-04 11:18:26 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU General Public License for more details.
|
2015-03-04 11:18:26 +00:00
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-23 13:48:53 +00:00
|
|
|
|
2014-06-23 11:20:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-03-06 11:18:44 +00:00
|
|
|
"bufio"
|
2015-03-06 02:38:19 +00:00
|
|
|
"fmt"
|
2015-04-22 22:11:11 +00:00
|
|
|
"math/big"
|
2014-08-11 14:24:35 +00:00
|
|
|
"os"
|
2015-05-26 22:52:02 +00:00
|
|
|
"os/signal"
|
2015-05-12 12:24:11 +00:00
|
|
|
"path/filepath"
|
2015-07-31 09:51:18 +00:00
|
|
|
"regexp"
|
2015-05-12 13:20:53 +00:00
|
|
|
"strings"
|
2015-05-12 12:24:11 +00:00
|
|
|
|
2015-06-18 16:23:13 +00:00
|
|
|
"sort"
|
|
|
|
|
2015-03-15 06:31:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2015-04-02 19:14:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-04-01 11:29:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/docserver"
|
2015-03-30 23:51:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/natspec"
|
2015-06-23 14:48:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/registrar"
|
2014-12-14 18:23:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2015-03-15 06:31:40 +00:00
|
|
|
re "github.com/ethereum/go-ethereum/jsre"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2015-06-17 14:22:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc/api"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc/codec"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc/comms"
|
2015-06-23 14:48:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc/shared"
|
2015-03-04 11:18:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/xeth"
|
2015-03-06 02:38:19 +00:00
|
|
|
"github.com/peterh/liner"
|
2015-03-24 16:49:28 +00:00
|
|
|
"github.com/robertkrimen/otto"
|
2014-06-23 11:20:59 +00:00
|
|
|
)
|
|
|
|
|
2015-07-31 09:51:18 +00:00
|
|
|
var passwordRegexp = regexp.MustCompile("personal.[nu]")
|
|
|
|
|
|
|
|
const passwordRepl = ""
|
|
|
|
|
2015-03-10 01:00:57 +00:00
|
|
|
type prompter interface {
|
|
|
|
AppendHistory(string)
|
|
|
|
Prompt(p string) (string, error)
|
|
|
|
PasswordPrompt(p string) (string, error)
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:33:59 +00:00
|
|
|
type dumbterm struct{ r *bufio.Reader }
|
2015-03-10 01:00:57 +00:00
|
|
|
|
2015-03-10 01:33:59 +00:00
|
|
|
func (r dumbterm) Prompt(p string) (string, error) {
|
2015-03-10 01:00:57 +00:00
|
|
|
fmt.Print(p)
|
2015-05-26 22:52:02 +00:00
|
|
|
line, err := r.r.ReadString('\n')
|
|
|
|
return strings.TrimSuffix(line, "\n"), err
|
2014-06-23 11:20:59 +00:00
|
|
|
}
|
2015-03-06 02:38:19 +00:00
|
|
|
|
2015-03-10 01:33:59 +00:00
|
|
|
func (r dumbterm) PasswordPrompt(p string) (string, error) {
|
2015-03-10 01:00:57 +00:00
|
|
|
fmt.Println("!! Unsupported terminal, password will echo.")
|
|
|
|
fmt.Print(p)
|
|
|
|
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
|
|
fmt.Println()
|
|
|
|
return input, err
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:33:59 +00:00
|
|
|
func (r dumbterm) AppendHistory(string) {}
|
2015-03-10 01:00:57 +00:00
|
|
|
|
|
|
|
type jsre struct {
|
2015-06-23 14:48:33 +00:00
|
|
|
ds *docserver.DocServer
|
2015-04-22 13:55:01 +00:00
|
|
|
re *re.JSRE
|
|
|
|
ethereum *eth.Ethereum
|
|
|
|
xeth *xeth.XEth
|
2015-04-22 22:11:11 +00:00
|
|
|
wait chan *big.Int
|
2015-04-22 13:55:01 +00:00
|
|
|
ps1 string
|
|
|
|
atexit func()
|
|
|
|
corsDomain string
|
2015-06-17 14:22:35 +00:00
|
|
|
client comms.EthereumClient
|
2015-03-10 01:00:57 +00:00
|
|
|
prompter
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 14:22:35 +00:00
|
|
|
var (
|
|
|
|
loadedModulesMethods map[string][]string
|
|
|
|
)
|
|
|
|
|
|
|
|
func keywordCompleter(line string) []string {
|
|
|
|
results := make([]string, 0)
|
|
|
|
|
|
|
|
if strings.Contains(line, ".") {
|
|
|
|
elements := strings.Split(line, ".")
|
|
|
|
if len(elements) == 2 {
|
|
|
|
module := elements[0]
|
|
|
|
partialMethod := elements[1]
|
|
|
|
if methods, found := loadedModulesMethods[module]; found {
|
|
|
|
for _, method := range methods {
|
|
|
|
if strings.HasPrefix(method, partialMethod) { // e.g. debug.se
|
|
|
|
results = append(results, module+"."+method)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for module, methods := range loadedModulesMethods {
|
|
|
|
if line == module { // user typed in full module name, show all methods
|
|
|
|
for _, method := range methods {
|
|
|
|
results = append(results, module+"."+method)
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(module, line) { // partial method name, e.g. admi
|
|
|
|
results = append(results, module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func apiWordCompleter(line string, pos int) (head string, completions []string, tail string) {
|
2015-09-06 14:25:55 +00:00
|
|
|
if len(line) == 0 || pos == 0 {
|
2015-06-17 14:22:35 +00:00
|
|
|
return "", nil, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for i = pos - 1; i > 0; i-- {
|
|
|
|
if line[i] == '.' || (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i >= 3 && line[i] == '3' && line[i-3] == 'w' && line[i-2] == 'e' && line[i-1] == 'b' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i += 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
begin := line[:i]
|
|
|
|
keyword := line[i:pos]
|
|
|
|
end := line[pos:]
|
|
|
|
|
|
|
|
completionWords := keywordCompleter(keyword)
|
|
|
|
return begin, completionWords, end
|
|
|
|
}
|
|
|
|
|
2015-09-25 11:08:48 +00:00
|
|
|
func newLightweightJSRE(libPath string, client comms.EthereumClient, datadir string, interactive bool) *jsre {
|
2015-06-18 16:23:13 +00:00
|
|
|
js := &jsre{ps1: "> "}
|
|
|
|
js.wait = make(chan *big.Int)
|
|
|
|
js.client = client
|
2015-06-23 14:48:33 +00:00
|
|
|
js.ds = docserver.New("/")
|
2015-06-18 16:23:13 +00:00
|
|
|
|
|
|
|
// update state in separare forever blocks
|
|
|
|
js.re = re.New(libPath)
|
2015-08-07 07:56:49 +00:00
|
|
|
if err := js.apiBindings(js); err != nil {
|
2015-06-18 16:23:13 +00:00
|
|
|
utils.Fatalf("Unable to initialize console - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !liner.TerminalSupported() || !interactive {
|
|
|
|
js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
|
|
|
|
} else {
|
|
|
|
lr := liner.NewLiner()
|
2015-09-25 11:08:48 +00:00
|
|
|
js.withHistory(datadir, func(hist *os.File) { lr.ReadHistory(hist) })
|
2015-06-18 16:23:13 +00:00
|
|
|
lr.SetCtrlCAborts(true)
|
|
|
|
js.loadAutoCompletion()
|
|
|
|
lr.SetWordCompleter(apiWordCompleter)
|
|
|
|
lr.SetTabCompletionStyle(liner.TabPrints)
|
|
|
|
js.prompter = lr
|
|
|
|
js.atexit = func() {
|
2015-09-25 11:08:48 +00:00
|
|
|
js.withHistory(datadir, func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
|
2015-06-18 16:23:13 +00:00
|
|
|
lr.Close()
|
|
|
|
close(js.wait)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return js
|
|
|
|
}
|
|
|
|
|
2015-06-17 14:22:35 +00:00
|
|
|
func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, client comms.EthereumClient, interactive bool, f xeth.Frontend) *jsre {
|
2015-03-10 01:00:57 +00:00
|
|
|
js := &jsre{ethereum: ethereum, ps1: "> "}
|
2015-04-22 13:55:01 +00:00
|
|
|
// set default cors domain used by startRpc from CLI flag
|
|
|
|
js.corsDomain = corsDomain
|
2015-04-22 22:11:11 +00:00
|
|
|
if f == nil {
|
|
|
|
f = js
|
|
|
|
}
|
2015-06-23 14:48:33 +00:00
|
|
|
js.ds = docserver.New("/")
|
2015-04-22 22:11:11 +00:00
|
|
|
js.xeth = xeth.New(ethereum, f)
|
|
|
|
js.wait = js.xeth.UpdateState()
|
2015-06-17 14:22:35 +00:00
|
|
|
js.client = client
|
|
|
|
if clt, ok := js.client.(*comms.InProcClient); ok {
|
2015-06-22 10:47:32 +00:00
|
|
|
if offeredApis, err := api.ParseApiString(shared.AllApis, codec.JSON, js.xeth, ethereum); err == nil {
|
|
|
|
clt.Initialize(api.Merge(offeredApis...))
|
|
|
|
}
|
2015-06-17 14:22:35 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 22:11:11 +00:00
|
|
|
// update state in separare forever blocks
|
2015-03-15 06:31:40 +00:00
|
|
|
js.re = re.New(libPath)
|
2015-06-17 14:22:35 +00:00
|
|
|
if err := js.apiBindings(f); err != nil {
|
|
|
|
utils.Fatalf("Unable to connect - %v", err)
|
|
|
|
}
|
2015-03-10 01:00:57 +00:00
|
|
|
|
2015-03-24 14:09:06 +00:00
|
|
|
if !liner.TerminalSupported() || !interactive {
|
2015-03-10 01:33:59 +00:00
|
|
|
js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
|
2015-03-06 11:18:44 +00:00
|
|
|
} else {
|
|
|
|
lr := liner.NewLiner()
|
2015-09-25 11:08:48 +00:00
|
|
|
js.withHistory(ethereum.DataDir, func(hist *os.File) { lr.ReadHistory(hist) })
|
2015-03-10 13:31:54 +00:00
|
|
|
lr.SetCtrlCAborts(true)
|
2015-06-17 14:22:35 +00:00
|
|
|
js.loadAutoCompletion()
|
|
|
|
lr.SetWordCompleter(apiWordCompleter)
|
|
|
|
lr.SetTabCompletionStyle(liner.TabPrints)
|
2015-03-10 01:00:57 +00:00
|
|
|
js.prompter = lr
|
2015-03-10 13:31:54 +00:00
|
|
|
js.atexit = func() {
|
2015-09-25 11:08:48 +00:00
|
|
|
js.withHistory(ethereum.DataDir, func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
|
2015-03-10 13:31:54 +00:00
|
|
|
lr.Close()
|
2015-04-22 22:11:11 +00:00
|
|
|
close(js.wait)
|
2015-03-10 13:31:54 +00:00
|
|
|
}
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
2015-03-10 01:00:57 +00:00
|
|
|
return js
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 14:22:35 +00:00
|
|
|
func (self *jsre) loadAutoCompletion() {
|
2015-06-18 16:23:13 +00:00
|
|
|
if modules, err := self.supportedApis(); err == nil {
|
2015-06-17 14:22:35 +00:00
|
|
|
loadedModulesMethods = make(map[string][]string)
|
|
|
|
for module, _ := range modules {
|
|
|
|
loadedModulesMethods[module] = api.AutoCompletion[module]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 12:04:18 +00:00
|
|
|
func (self *jsre) batch(statement string) {
|
2015-08-14 11:06:34 +00:00
|
|
|
err := self.re.EvalAndPrettyPrint(statement)
|
2015-06-19 12:04:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.atexit != nil {
|
|
|
|
self.atexit()
|
|
|
|
}
|
|
|
|
|
|
|
|
self.re.Stop(false)
|
|
|
|
}
|
|
|
|
|
2015-06-18 16:23:13 +00:00
|
|
|
// show summary of current geth instance
|
|
|
|
func (self *jsre) welcome() {
|
2015-08-11 16:14:46 +00:00
|
|
|
self.re.Run(`
|
|
|
|
(function () {
|
|
|
|
console.log('instance: ' + web3.version.client);
|
|
|
|
console.log(' datadir: ' + admin.datadir);
|
|
|
|
console.log("coinbase: " + eth.coinbase);
|
|
|
|
var ts = 1000 * eth.getBlock(eth.blockNumber).timestamp;
|
|
|
|
console.log("at block: " + eth.blockNumber + " (" + new Date(ts) + ")");
|
|
|
|
})();
|
|
|
|
`)
|
2015-06-18 16:23:13 +00:00
|
|
|
if modules, err := self.supportedApis(); err == nil {
|
|
|
|
loadedModules := make([]string, 0)
|
|
|
|
for api, version := range modules {
|
|
|
|
loadedModules = append(loadedModules, fmt.Sprintf("%s:%s", api, version))
|
2015-06-17 14:22:35 +00:00
|
|
|
}
|
2015-06-18 16:23:13 +00:00
|
|
|
sort.Strings(loadedModules)
|
2015-08-11 16:14:46 +00:00
|
|
|
fmt.Println("modules:", strings.Join(loadedModules, " "))
|
2015-06-17 14:22:35 +00:00
|
|
|
}
|
2015-06-18 16:23:13 +00:00
|
|
|
}
|
2015-06-17 14:22:35 +00:00
|
|
|
|
2015-06-18 16:23:13 +00:00
|
|
|
func (self *jsre) supportedApis() (map[string]string, error) {
|
|
|
|
return self.client.SupportedModules()
|
2015-06-17 14:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *jsre) apiBindings(f xeth.Frontend) error {
|
2015-06-18 16:23:13 +00:00
|
|
|
apis, err := js.supportedApis()
|
2015-06-17 14:22:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiNames := make([]string, 0, len(apis))
|
|
|
|
for a, _ := range apis {
|
|
|
|
apiNames = append(apiNames, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
apiImpl, err := api.ParseApiString(strings.Join(apiNames, ","), codec.JSON, js.xeth, js.ethereum)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Unable to determine supported api's: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-07 07:56:49 +00:00
|
|
|
jeth := rpc.NewJeth(api.Merge(apiImpl...), js.re, js.client, f)
|
2015-06-17 14:22:35 +00:00
|
|
|
js.re.Set("jeth", struct{}{})
|
|
|
|
t, _ := js.re.Get("jeth")
|
|
|
|
jethObj := t.Object()
|
|
|
|
|
|
|
|
jethObj.Set("send", jeth.Send)
|
|
|
|
jethObj.Set("sendAsync", jeth.Send)
|
|
|
|
|
|
|
|
err = js.re.Compile("bignumber.js", re.BigNumber_JS)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Error loading bignumber.js: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = js.re.Compile("ethereum.js", re.Web3_JS)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Error loading web3.js: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:14:46 +00:00
|
|
|
_, err = js.re.Run("var web3 = require('web3');")
|
2015-06-17 14:22:35 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Error requiring web3: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:14:46 +00:00
|
|
|
_, err = js.re.Run("web3.setProvider(jeth)")
|
2015-06-17 14:22:35 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Error setting web3 provider: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// load only supported API's in javascript runtime
|
|
|
|
shortcuts := "var eth = web3.eth; "
|
|
|
|
for _, apiName := range apiNames {
|
2015-06-22 11:54:13 +00:00
|
|
|
if apiName == shared.Web3ApiName {
|
2015-06-17 14:22:35 +00:00
|
|
|
continue // manually mapped
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = js.re.Compile(fmt.Sprintf("%s.js", apiName), api.Javascript(apiName)); err == nil {
|
|
|
|
shortcuts += fmt.Sprintf("var %s = web3.%s; ", apiName, apiName)
|
|
|
|
} else {
|
|
|
|
utils.Fatalf("Error loading %s.js: %v", apiName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:14:46 +00:00
|
|
|
_, err = js.re.Run(shortcuts)
|
2015-06-17 14:22:35 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Error setting namespaces: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:14:46 +00:00
|
|
|
js.re.Run(`var GlobalRegistrar = eth.contract(` + registrar.GlobalRegistrarAbi + `); registrar = GlobalRegistrar.at("` + registrar.GlobalRegistrarAddr + `");`)
|
2015-06-17 14:22:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-31 06:28:12 +00:00
|
|
|
func (self *jsre) ConfirmTransaction(tx string) bool {
|
2015-04-08 11:22:31 +00:00
|
|
|
if self.ethereum.NatSpec {
|
2015-06-23 14:48:33 +00:00
|
|
|
notice := natspec.GetNotice(self.xeth, tx, self.ds)
|
2015-04-08 11:22:31 +00:00
|
|
|
fmt.Println(notice)
|
2015-04-22 22:11:11 +00:00
|
|
|
answer, _ := self.Prompt("Confirm Transaction [y/n]")
|
2015-04-08 11:22:31 +00:00
|
|
|
return strings.HasPrefix(strings.Trim(answer, " "), "y")
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 01:00:57 +00:00
|
|
|
func (self *jsre) UnlockAccount(addr []byte) bool {
|
|
|
|
fmt.Printf("Please unlock account %x.\n", addr)
|
2015-03-10 01:33:59 +00:00
|
|
|
pass, err := self.PasswordPrompt("Passphrase: ")
|
2015-03-06 02:38:19 +00:00
|
|
|
if err != nil {
|
2015-03-10 01:00:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// TODO: allow retry
|
2015-04-02 19:14:25 +00:00
|
|
|
if err := self.ethereum.AccountManager().Unlock(common.BytesToAddress(addr), pass); err != nil {
|
2015-03-10 01:00:57 +00:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
fmt.Println("Account is now unlocked for this session.")
|
|
|
|
return true
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:00:57 +00:00
|
|
|
func (self *jsre) exec(filename string) error {
|
2015-03-15 06:31:40 +00:00
|
|
|
if err := self.re.Exec(filename); err != nil {
|
2015-04-22 00:31:59 +00:00
|
|
|
self.re.Stop(false)
|
2015-03-10 01:00:57 +00:00
|
|
|
return fmt.Errorf("Javascript Error: %v", err)
|
|
|
|
}
|
2015-04-22 00:31:59 +00:00
|
|
|
self.re.Stop(true)
|
2015-03-10 01:00:57 +00:00
|
|
|
return nil
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 01:00:57 +00:00
|
|
|
func (self *jsre) interactive() {
|
2015-05-26 22:52:02 +00:00
|
|
|
// Read input lines.
|
|
|
|
prompt := make(chan string)
|
|
|
|
inputln := make(chan string)
|
|
|
|
go func() {
|
|
|
|
defer close(inputln)
|
|
|
|
for {
|
|
|
|
line, err := self.Prompt(<-prompt)
|
|
|
|
if err != nil {
|
2015-08-14 09:31:29 +00:00
|
|
|
if err == liner.ErrPromptAborted { // ctrl-C
|
|
|
|
self.resetPrompt()
|
|
|
|
inputln <- ""
|
|
|
|
continue
|
|
|
|
}
|
2015-05-26 22:52:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inputln <- line
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
2015-05-26 22:52:02 +00:00
|
|
|
}()
|
|
|
|
// Wait for Ctrl-C, too.
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, os.Interrupt)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if self.atexit != nil {
|
|
|
|
self.atexit()
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
2015-05-26 22:52:02 +00:00
|
|
|
self.re.Stop(false)
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
prompt <- self.ps1
|
|
|
|
select {
|
|
|
|
case <-sig:
|
|
|
|
fmt.Println("caught interrupt, exiting")
|
|
|
|
return
|
|
|
|
case input, ok := <-inputln:
|
|
|
|
if !ok || indentCount <= 0 && input == "exit" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if input == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
str += input + "\n"
|
|
|
|
self.setIndent()
|
|
|
|
if indentCount <= 0 {
|
2015-07-31 09:51:18 +00:00
|
|
|
hist := hidepassword(str[:len(str)-1])
|
|
|
|
if len(hist) > 0 {
|
|
|
|
self.AppendHistory(hist)
|
|
|
|
}
|
2015-05-26 22:52:02 +00:00
|
|
|
self.parseInput(str)
|
|
|
|
str = ""
|
2015-03-06 02:38:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 09:51:18 +00:00
|
|
|
func hidepassword(input string) string {
|
|
|
|
if passwordRegexp.MatchString(input) {
|
|
|
|
return passwordRepl
|
|
|
|
} else {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 11:08:48 +00:00
|
|
|
func (self *jsre) withHistory(datadir string, op func(*os.File)) {
|
2015-06-18 16:23:13 +00:00
|
|
|
hist, err := os.OpenFile(filepath.Join(datadir, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm)
|
2015-03-10 01:00:57 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to open history file: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
op(hist)
|
|
|
|
hist.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *jsre) parseInput(code string) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("[native] error", r)
|
2015-03-06 11:18:44 +00:00
|
|
|
}
|
|
|
|
}()
|
2015-08-11 16:14:46 +00:00
|
|
|
if err := self.re.EvalAndPrettyPrint(code); err != nil {
|
2015-03-24 16:49:28 +00:00
|
|
|
if ottoErr, ok := err.(*otto.Error); ok {
|
|
|
|
fmt.Println(ottoErr.String())
|
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2015-03-10 01:00:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 11:18:44 +00:00
|
|
|
|
2015-03-10 01:00:57 +00:00
|
|
|
var indentCount = 0
|
|
|
|
var str = ""
|
2015-03-06 11:18:44 +00:00
|
|
|
|
2015-08-14 09:31:29 +00:00
|
|
|
func (self *jsre) resetPrompt() {
|
|
|
|
indentCount = 0
|
|
|
|
str = ""
|
|
|
|
self.ps1 = "> "
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:00:57 +00:00
|
|
|
func (self *jsre) setIndent() {
|
|
|
|
open := strings.Count(str, "{")
|
|
|
|
open += strings.Count(str, "(")
|
|
|
|
closed := strings.Count(str, "}")
|
|
|
|
closed += strings.Count(str, ")")
|
|
|
|
indentCount = open - closed
|
|
|
|
if indentCount <= 0 {
|
|
|
|
self.ps1 = "> "
|
|
|
|
} else {
|
|
|
|
self.ps1 = strings.Join(make([]string, indentCount*2), "..")
|
|
|
|
self.ps1 += " "
|
2015-03-06 11:18:44 +00:00
|
|
|
}
|
|
|
|
}
|