1
0
forked from x/ContextOS

Merge branch '0.2.0' into 0.3.0

This commit is contained in:
shaoying 2017-12-08 08:48:08 +08:00
commit 6cfef174ee
4 changed files with 100 additions and 82 deletions

View File

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

View File

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

View File

@ -1,8 +1,9 @@
package cli // {{{
// }}}
import ( // {{{
"bufio"
"context"
"bufio"
"fmt"
"io"
"os"
@ -29,6 +30,7 @@ type CLI struct {
login *ctx.Context
lex *ctx.Message
temp *ctx.Context
target *ctx.Context
m *ctx.Message
*ctx.Context
@ -95,53 +97,57 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
}
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
}
msg := m.Spawn(cli.target)
ls := []string{}
if cli.lex == nil {
ls = strings.Split(line, " ")
r := rune(ls[0][0])
if !unicode.IsNumber(r) || !unicode.IsLetter(r) || r == '$' || r == '_' {
if _, ok := cli.alias[string(r)]; ok {
if msg.Add("detail", ls[0][:1]); len(ls[0]) > 1 {
ls[0] = ls[0][1:]
} else {
ls = ls[1:]
}
}
if cli.temp != nil {
msg.Target, cli.temp = cli.temp, nil
}
for i := 0; i < len(ls); i++ {
ls[i] = strings.TrimSpace(ls[i])
if ls[i] == "" {
continue
}
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 {
if cli.lex != nil && len(ls[i]) > 1 {
switch ls[i][0] {
case '"', '\'':
ls[i] = ls[i][1 : len(ls[i])-1]
}
if ls[i][0] == '#' {
break
}
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 {
continue
}
}
}
msg.Add("detail", ls[i])
}
}
if len(ls) == 0 {
return true
}
msg.Wait = make(chan bool)
msg.Post(cli.Context)
@ -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.Configs = map[string]*ctx.Config{}
cli.Owner = nil
s := new(CLI)
s.Context = c
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.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 {
if len(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)
m.Assert(cli.lex != nil, "词法解析模块不存在")
cli.lex.Cmd("train", "[ \n\t]+", "1")
cli.lex.Cmd("train", "#[^\n]*\n", "1", "2")
cli.lex.Cmd("train", "[ \n\t]+", "void", "void")
cli.lex.Cmd("train", "#[^\n]*\n", "void", "void")
}
return x.Value
// }}}
@ -232,7 +240,6 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
}
cli.m = m
cli.Owner = nil
cli.Context.Master = cli.Context
cli.target = cli.Context
@ -274,7 +281,8 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
}
return true
} else {
if msg := m.Find("aaa", m.Target.Root); false && msg != nil {
if cli.Owner == nil {
if msg := m.Find("aaa", m.Target.Root); msg != nil {
username := ""
cli.echo("username>")
fmt.Fscanln(cli.in, &username)
@ -293,6 +301,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Cap("username", msg.Cap("username"))
}
}
m.Log("info", "%s: slaver terminal", cli.Name)
m.Log("info", "%s: open %s", cli.Name, m.Conf("init.sh"))
@ -307,13 +316,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
}
}
for m.Deal(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 {
for m.Deal(nil, func(msg *ctx.Message, arg ...string) bool {
cli.history = append(cli.history, map[string]string{
"time": time.Now().Format("15:04:05"),
"index": fmt.Sprintf("%d", len(cli.history)),
@ -416,6 +419,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
cli.target = v.Target
case m.Has("start"):
v.Set("detail", arg...).Target.Start(v)
cli.target = v.Target
case m.Has("switch"):
cli.target = v.Target
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"])
}
}
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 {
if msg.Target.Context != nil {
target := msg.Target
@ -453,6 +460,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
}
return true
})
case len(arg) > 0:
cli.next = strings.Join(arg, " ")
cli.temp = v.Target
default:
cli.target = v.Target
return ""
@ -473,6 +483,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
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 {
cli := c.Server.(*CLI) // {{{
switch len(arg) {
@ -555,7 +571,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
return ""
// }}}
}},
"void": &ctx.Command{Name: "", Help: "", Hand: nil},
},
Messages: make(chan *ctx.Message, 10),
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}
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 {
fmt.Printf("\n%s error: %v", msg.Target.Name, e)
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 {
@ -804,7 +805,7 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
}
m.Meta["result"] = nil
ret := x.Hand(m, c, key, arg...)
ret := x.Hand(m, s, key, arg...)
if ret != "" {
m.Echo(ret)
}
@ -1113,6 +1114,7 @@ var Index = &Context{Name: "ctx", Help: "根模块",
default:
switch arg[0] {
case "start":
m.Meta = nil
m.Set("detail", arg[1:]...).Target.Start(m)
case "stop":
m.Set("detail", arg[1:]...).Target.Close(m)
@ -1373,6 +1375,7 @@ func Start(args ...string) {
Pulse.Conf("root", args[3])
}
Index.Owner = Index.contexts["aaa"]
for _, m := range Pulse.Search("") {
m.Target.Begin(m)
}