1
0
forked from x/ContextOS

Merge branch '0.1.0' into 0.2.0

This commit is contained in:
shaoying 2017-12-08 08:46:59 +08:00
commit 71c9b4a49d
4 changed files with 100 additions and 82 deletions

View File

@ -1,12 +1,18 @@
# @debug on # @debug on
~lex start
source etc/lex.sh source etc/lex.sh
~cli @lex lex
~root mdb ~mdb
open chat chat "chat:chat@/chat" mysql open chat chat "chat:chat@/chat" mysql
~chat
query "select * from userinfo"
~root aaa return
login shy shy ~aaa
login root root login root root
login shy shy
~root cli ~root cli
remote slaver listen ":9393" tcp remote slaver listen ":9393" tcp

View File

@ -1,12 +1,6 @@
~root lex train [a-zA-Z][a-zA-Z0-9]*
server start train 0x[0-9]+
train [a-zA-Z][a-zA-Z0-9]* 2 2 train [0-9]+
train 0x[0-9]+ 3 2 train "[^"]*"
train [0-9]+ 3 2 train '[^']*'
train "[^"]*" 4 2 train [~!@#$&*:]
train '[^']*' 4 2
train [~!@#$&*:] 4 2
~root cli
@lex lex

View File

@ -1,8 +1,9 @@
package cli // {{{ package cli // {{{
// }}} // }}}
import ( // {{{ import ( // {{{
"bufio"
"context" "context"
"bufio"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -29,6 +30,7 @@ type CLI struct {
login *ctx.Context login *ctx.Context
lex *ctx.Message lex *ctx.Message
temp *ctx.Context
target *ctx.Context target *ctx.Context
m *ctx.Message m *ctx.Message
*ctx.Context *ctx.Context
@ -95,52 +97,56 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
} }
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if line[0] == '#' { if len(line) == 0 || line[0] == '#' {
return true
}
ls := []string{}
if cli.lex == nil {
ls = strings.Split(line, " ")
} else {
lex := m.Spawn(cli.lex.Target)
m.Assert(lex.Cmd("split", line, "void"))
ls = lex.Meta["result"]
}
if len(ls) == 0 {
return true return true
} }
msg := m.Spawn(cli.target) msg := m.Spawn(cli.target)
ls := []string{} if cli.temp != nil {
if cli.lex == nil { msg.Target, cli.temp = cli.temp, nil
ls = strings.Split(line, " ") }
r := rune(ls[0][0]) for i := 0; i < len(ls); i++ {
if !unicode.IsNumber(r) || !unicode.IsLetter(r) || r == '$' || r == '_' { ls[i] = strings.TrimSpace(ls[i])
if _, ok := cli.alias[string(r)]; ok { if ls[i] == "" {
if msg.Add("detail", ls[0][:1]); len(ls[0]) > 1 { continue
ls[0] = ls[0][1:] }
if ls[i][0] == '#' {
break
}
if cli.lex != nil && len(ls[i]) > 1 {
switch ls[i][0] {
case '"', '\'':
ls[i] = ls[i][1 : len(ls[i])-1]
}
}
if r := rune(ls[i][0]); r == '$' || r == '_' || (!unicode.IsNumber(r) && !unicode.IsLetter(r)) {
if c, ok := cli.alias[string(r)]; ok {
if msg.Add("detail", c); len(ls[i]) > 1 {
ls[i] = ls[i][1:]
} else { } else {
ls = ls[1:] continue
} }
} }
} }
for i := 0; i < len(ls); i++ { msg.Add("detail", ls[i])
ls[i] = strings.TrimSpace(ls[i])
if ls[i][0] == '#' {
break
}
if ls[i] != "" {
msg.Add("detail", ls[i])
}
}
} else {
lex := m.Spawn(cli.lex.Target)
m.Assert(lex.Cmd("split", line))
ls = lex.Meta["result"]
for i := 0; i < len(ls); i++ {
if ls[i][0] == '"' && len(ls[i]) > 1 {
ls[i] = ls[i][1 : len(ls[i])-1]
}
if ls[i][0] == '#' {
break
}
msg.Add("detail", ls[i])
}
}
if len(ls) == 0 {
return true
} }
msg.Wait = make(chan bool) msg.Wait = make(chan bool)
@ -158,6 +164,8 @@ func (cli *CLI) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
c.Caches = map[string]*ctx.Cache{} c.Caches = map[string]*ctx.Cache{}
c.Configs = map[string]*ctx.Config{} c.Configs = map[string]*ctx.Config{}
cli.Owner = nil
s := new(CLI) s := new(CLI)
s.Context = c s.Context = c
return s return s
@ -169,7 +177,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
cli.Caches["nhistory"] = &ctx.Cache{Name: "历史命令数量", Value: "0", Help: "当前终端已经执行命令的数量"} cli.Caches["nhistory"] = &ctx.Cache{Name: "历史命令数量", Value: "0", Help: "当前终端已经执行命令的数量"}
cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息yes:屏蔽no:不屏蔽"} cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息yes:屏蔽no:不屏蔽"}
cli.Configs["default"] = &ctx.Config{Name: "默认的搜索起点(root/back/home)", Value: "home", Help: "模块搜索的默认起点root:从根模块back:从父模块home:从当前模块"} cli.Configs["default"] = &ctx.Config{Name: "默认的搜索起点(root/back/home)", Value: "root", Help: "模块搜索的默认起点root:从根模块back:从父模块home:从当前模块"}
cli.Configs["PS1"] = &ctx.Config{Name: "命令行提示符(target/detail)", Value: "target", Help: "命令行提示符target:显示当前模块detail:显示详细信息", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string { cli.Configs["PS1"] = &ctx.Config{Name: "命令行提示符(target/detail)", Value: "target", Help: "命令行提示符target:显示当前模块detail:显示详细信息", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
if len(arg) > 0 { // {{{ if len(arg) > 0 { // {{{
return arg[0] return arg[0]
@ -220,8 +228,8 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
cli.lex = m.Find(arg[0], m.Target.Root) cli.lex = m.Find(arg[0], m.Target.Root)
m.Assert(cli.lex != nil, "词法解析模块不存在") m.Assert(cli.lex != nil, "词法解析模块不存在")
cli.lex.Cmd("train", "[ \n\t]+", "1") cli.lex.Cmd("train", "[ \n\t]+", "void", "void")
cli.lex.Cmd("train", "#[^\n]*\n", "1", "2") cli.lex.Cmd("train", "#[^\n]*\n", "void", "void")
} }
return x.Value return x.Value
// }}} // }}}
@ -232,7 +240,6 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
} }
cli.m = m cli.m = m
cli.Owner = nil
cli.Context.Master = cli.Context cli.Context.Master = cli.Context
cli.target = cli.Context cli.target = cli.Context
@ -274,24 +281,26 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
} }
return true return true
} else { } else {
if msg := m.Find("aaa", m.Target.Root); false && msg != nil { if cli.Owner == nil {
username := "" if msg := m.Find("aaa", m.Target.Root); msg != nil {
cli.echo("username>") username := ""
fmt.Fscanln(cli.in, &username) cli.echo("username>")
fmt.Fscanln(cli.in, &username)
password := "" password := ""
cli.echo("password>") cli.echo("password>")
fmt.Fscanln(cli.in, &password) fmt.Fscanln(cli.in, &password)
if msg.Cmd("login", username, password) == "" { if msg.Cmd("login", username, password) == "" {
cli.echo("登录失败") cli.echo("登录失败")
m.Cmd("exit") m.Cmd("exit")
cli.out.Close() cli.out.Close()
cli.in.Close() cli.in.Close()
return false return false
}
m.Cap("username", msg.Cap("username"))
} }
m.Cap("username", msg.Cap("username"))
} }
m.Log("info", "%s: slaver terminal", cli.Name) m.Log("info", "%s: slaver terminal", cli.Name)
@ -307,13 +316,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
} }
} }
for m.Deal(func(msg *ctx.Message, arg ...string) bool { for m.Deal(nil, func(msg *ctx.Message, arg ...string) bool {
if a, ok := cli.alias[arg[0]]; ok {
arg[0] = a
}
return true
}, func(msg *ctx.Message, arg ...string) bool {
cli.history = append(cli.history, map[string]string{ cli.history = append(cli.history, map[string]string{
"time": time.Now().Format("15:04:05"), "time": time.Now().Format("15:04:05"),
"index": fmt.Sprintf("%d", len(cli.history)), "index": fmt.Sprintf("%d", len(cli.history)),
@ -416,6 +419,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
cli.target = v.Target cli.target = v.Target
case m.Has("start"): case m.Has("start"):
v.Set("detail", arg...).Target.Start(v) v.Set("detail", arg...).Target.Start(v)
cli.target = v.Target
case m.Has("switch"): case m.Has("switch"):
cli.target = v.Target cli.target = v.Target
case m.Has("close"): case m.Has("close"):
@ -437,7 +441,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
m.Echo(" %s(%d): -> %s %v\n", k, v.Code, v.Target.Name, v.Meta["detail"]) m.Echo(" %s(%d): -> %s %v\n", k, v.Code, v.Target.Name, v.Meta["detail"])
} }
} }
case m.Has("list") || cli.target == v.Target: case m.Has("list") || len(m.Meta["detail"]) == 1 || (len(arg) == 0 && cli.target == v.Target):
if len(m.Meta["detail"]) == 1 {
v.Target = cli.target
}
m.Travel(v.Target, func(msg *ctx.Message) bool { m.Travel(v.Target, func(msg *ctx.Message) bool {
if msg.Target.Context != nil { if msg.Target.Context != nil {
target := msg.Target target := msg.Target
@ -453,6 +460,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
} }
return true return true
}) })
case len(arg) > 0:
cli.next = strings.Join(arg, " ")
cli.temp = v.Target
default: default:
cli.target = v.Target cli.target = v.Target
return "" return ""
@ -473,6 +483,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
return "" return ""
// }}} // }}}
}}, }},
"return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
cli := c.Server.(*CLI) // {{{
cli.bio.Discard(cli.bio.Buffered())
return ""
// }}}
}},
"alias": &ctx.Command{Name: "alias [short [long]]", Help: "查看日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { "alias": &ctx.Command{Name: "alias [short [long]]", Help: "查看日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
cli := c.Server.(*CLI) // {{{ cli := c.Server.(*CLI) // {{{
switch len(arg) { switch len(arg) {
@ -555,7 +571,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
return "" return ""
// }}} // }}}
}}, }},
"void": &ctx.Command{Name: "", Help: "", Hand: nil},
}, },
Messages: make(chan *ctx.Message, 10), Messages: make(chan *ctx.Message, 10),
Index: map[string]*ctx.Context{ Index: map[string]*ctx.Context{

View File

@ -131,6 +131,7 @@ func (c *Context) Begin(m *Message) *Context { // {{{
} }
} }
c.Owner = m.Master.Owner
c.Requests = []*Message{m} c.Requests = []*Message{m}
if c.Server != nil { if c.Server != nil {
@ -513,7 +514,7 @@ func (m *Message) AssertOne(msg *Message, safe bool, hand ...func(msg *Message))
if msg.Conf("debug") == "on" && e != io.EOF { if msg.Conf("debug") == "on" && e != io.EOF {
fmt.Printf("\n%s error: %v", msg.Target.Name, e) fmt.Printf("\n%s error: %v", msg.Target.Name, e)
debug.PrintStack() debug.PrintStack()
fmt.Printf("%s error: %v\n", msg.Target.Name, e) fmt.Printf("%s error: %v\n\n", msg.Target.Name, e)
} }
if e == io.EOF { if e == io.EOF {
@ -804,7 +805,7 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
} }
m.Meta["result"] = nil m.Meta["result"] = nil
ret := x.Hand(m, c, key, arg...) ret := x.Hand(m, s, key, arg...)
if ret != "" { if ret != "" {
m.Echo(ret) m.Echo(ret)
} }
@ -1113,6 +1114,7 @@ var Index = &Context{Name: "ctx", Help: "根模块",
default: default:
switch arg[0] { switch arg[0] {
case "start": case "start":
m.Meta = nil
m.Set("detail", arg[1:]...).Target.Start(m) m.Set("detail", arg[1:]...).Target.Start(m)
case "stop": case "stop":
m.Set("detail", arg[1:]...).Target.Close(m) m.Set("detail", arg[1:]...).Target.Close(m)
@ -1373,6 +1375,7 @@ func Start(args ...string) {
Pulse.Conf("root", args[3]) Pulse.Conf("root", args[3])
} }
Index.Owner = Index.contexts["aaa"]
for _, m := range Pulse.Search("") { for _, m := range Pulse.Search("") {
m.Target.Begin(m) m.Target.Begin(m)
} }