forked from x/ContextOS
mac add lex添加了词法解析
This commit is contained in:
parent
c98127bc1a
commit
52089121a8
16
etc/init.sh
16
etc/init.sh
@ -1,8 +1,14 @@
|
||||
# @debug on
|
||||
~aaa
|
||||
# login root 94ca7394d007fa189cc4be0a2625d716 root
|
||||
@debug on
|
||||
~root aaa
|
||||
login root root
|
||||
|
||||
~root lex
|
||||
server start
|
||||
~root cli
|
||||
@lex lex
|
||||
|
||||
# login root 94ca7394d007fa189cc4be0a2625d716 root
|
||||
|
||||
# ~cli
|
||||
# remote slaver listen :9393 tcp
|
||||
|
||||
@ -10,8 +16,8 @@ login root root
|
||||
# login shy shy
|
||||
# userinfo add context hi hello nice
|
||||
# userinfo add command hi context
|
||||
~web
|
||||
listen
|
||||
# ~web
|
||||
# listen
|
||||
# ~demo
|
||||
# listen
|
||||
# ~home spawn test
|
||||
|
@ -27,6 +27,7 @@ type CLI struct {
|
||||
next string
|
||||
exit bool
|
||||
login *ctx.Context
|
||||
lex *ctx.Message
|
||||
|
||||
target *ctx.Context
|
||||
*ctx.Context
|
||||
@ -113,11 +114,24 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
||||
}
|
||||
|
||||
back:
|
||||
|
||||
line = strings.TrimSpace(line)
|
||||
if line[0] == '#' {
|
||||
return true
|
||||
}
|
||||
ls := strings.Split(line, " ")
|
||||
if cli.lex != nil {
|
||||
cli.lex.Cmd("split", line)
|
||||
ls = cli.lex.Meta["result"]
|
||||
for i := 0; i < len(ls); i++ {
|
||||
if ls[i][0] == '"' {
|
||||
ls[i] = ls[i][1 : len(ls[i])-1]
|
||||
}
|
||||
}
|
||||
if len(ls) == 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
msg := m.Spawn(cli.target)
|
||||
msg.Wait = make(chan bool)
|
||||
@ -139,7 +153,9 @@ back:
|
||||
}
|
||||
|
||||
for i := 0; i < len(ls); i++ {
|
||||
ls[i] = strings.TrimSpace(ls[i])
|
||||
if cli.lex == nil {
|
||||
ls[i] = strings.TrimSpace(ls[i])
|
||||
}
|
||||
|
||||
if ls[i][0] == '#' {
|
||||
break
|
||||
@ -181,6 +197,32 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
return x.Value
|
||||
}}
|
||||
|
||||
cli.Configs["lex"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "", Help: "屏蔽脚本输出的信息,yes:屏蔽,no:不屏蔽", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||
if len(arg) > 0 {
|
||||
cli, ok := m.Target.Server.(*CLI)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
cli.lex = m.Find(arg[0], m.Target.Root)
|
||||
if cli.lex == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
cli.lex.Target.Start(cli.lex)
|
||||
cli.lex.Cmd("train", "[ \n\t]+", "1")
|
||||
|
||||
cli.lex.Cmd("train", "[a-zA-Z][a-zA-Z0-9]*", "2", "2")
|
||||
|
||||
cli.lex.Cmd("train", "0x[0-9]+", "3", "2")
|
||||
cli.lex.Cmd("train", "[0-9]+", "3", "2")
|
||||
|
||||
cli.lex.Cmd("train", "\"[^\"]*\"", "4", "2")
|
||||
cli.lex.Cmd("train", "'[^']*'", "4", "2")
|
||||
|
||||
cli.lex.Cmd("train", "[~!@#$&*:]", "4", "2")
|
||||
}
|
||||
return ""
|
||||
}}
|
||||
cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息,yes:屏蔽,no:不屏蔽"}
|
||||
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 {
|
||||
@ -353,7 +395,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
|
||||
if s := m.Search(target, m.Get("args")); len(s) > 0 {
|
||||
ms = append(ms, s...)
|
||||
arg = arg[1:]
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
default:
|
||||
ms = append(ms, m.Spawn(target))
|
||||
}
|
||||
@ -361,11 +405,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
|
||||
for _, v := range ms {
|
||||
switch {
|
||||
case m.Has("start"):
|
||||
args := m.Meta["start"]
|
||||
v.Start(arg[0], args[1:]...)
|
||||
v.Start(arg[0], arg[1:]...)
|
||||
case m.Has("spawn"):
|
||||
args := m.Meta["spawn"]
|
||||
v.Target.Spawn(v, args[0]).Begin(v)
|
||||
v.Target.Spawn(v, arg[0]).Begin(v)
|
||||
cli.target = v.Target
|
||||
case m.Has("switch"):
|
||||
cli.target = v.Target
|
||||
|
@ -9,6 +9,7 @@ import ( // {{{
|
||||
"os/exec"
|
||||
"path"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -689,10 +690,14 @@ func (m *Message) Search(c *Context, name string) []*Message { // {{{
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Find(name string) *Message { // {{{
|
||||
func (m *Message) Find(name string, begin ...*Context) *Message { // {{{
|
||||
ns := strings.Split(name, ".")
|
||||
cs := m.Target.contexts
|
||||
old := m.Target.Name
|
||||
target := m.Target
|
||||
if len(begin) > 0 {
|
||||
target = begin[0]
|
||||
}
|
||||
cs := target.contexts
|
||||
old := target.Name
|
||||
|
||||
for _, v := range ns {
|
||||
if x, ok := cs[v]; ok {
|
||||
@ -788,8 +793,8 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
|
||||
}
|
||||
|
||||
m.AssertOne(m, true, func(m *Message) {
|
||||
m.Log("system", ":%v", arg)
|
||||
cmd := exec.Command(key, arg[1:]...)
|
||||
m.Log("system", ": %s %v", key, arg)
|
||||
cmd := exec.Command(key, arg...)
|
||||
v, e := cmd.CombinedOutput()
|
||||
if e != nil {
|
||||
m.Echo("%s\n", e)
|
||||
@ -1137,6 +1142,7 @@ var Index = &Context{Name: "ctx", Help: "根模块",
|
||||
switch arg[0] {
|
||||
case "start":
|
||||
go m.Set("detail", arg[1:]...).Target.Start(m)
|
||||
runtime.Gosched()
|
||||
case "stop":
|
||||
m.Set("detail", arg[1:]...).Target.Exit(m)
|
||||
case "switch":
|
||||
@ -1305,11 +1311,15 @@ var Index = &Context{Name: "ctx", Help: "根模块",
|
||||
m.Target.BackTrace(func(s *Context) bool {
|
||||
switch len(arg) {
|
||||
case 0:
|
||||
target := m.Target
|
||||
m.Target = s
|
||||
for k, v := range s.Caches {
|
||||
if m.Check(m.Target, "caches", k) {
|
||||
m.Echo("%s(%s): %s\n", k, v.Value, v.Name)
|
||||
m.Echo("%s(%s): %s\n", k, m.Cap(k), v.Name)
|
||||
}
|
||||
}
|
||||
m.Target = target
|
||||
|
||||
case 1:
|
||||
if v, ok := s.Caches[arg[0]]; ok {
|
||||
if m.Check(m.Target, "caches", arg[0]) {
|
||||
|
@ -11,6 +11,9 @@ import (
|
||||
_ "context/ssh"
|
||||
|
||||
_ "context/web"
|
||||
|
||||
_ "context/lex"
|
||||
|
||||
"os"
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user