mirror of
https://shylinux.com/x/ContextOS
synced 2025-04-26 01:04:06 +08:00
tce pro ctx
This commit is contained in:
parent
c8e8b174a1
commit
3292ce386e
@ -1,3 +1,4 @@
|
||||
var a = 0
|
||||
source etc/local.shy
|
||||
~aaa login root root
|
||||
~aaa
|
||||
login root root
|
||||
|
@ -69,6 +69,18 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
cli.Caches["back"] = &ctx.Cache{Name: "前一条指令", Value: "", Help: "前一条指令"}
|
||||
cli.Caches["next"] = &ctx.Cache{Name: "下一条指令", Value: "", Help: "下一条指令"}
|
||||
|
||||
cli.Configs["target"] = &ctx.Config{Name: "词法解析器", Value: "cli", Help: "命令行词法解析器", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||
if len(arg) > 0 && len(arg[0]) > 0 { // {{{
|
||||
cli, ok := m.Target().Server.(*CLI)
|
||||
m.Assert(ok, "模块类型错误")
|
||||
|
||||
target := m.Find(arg[0])
|
||||
cli.target = target.Target()
|
||||
return arg[0]
|
||||
}
|
||||
return x.Value
|
||||
// }}}
|
||||
}}
|
||||
cli.Configs["lex"] = &ctx.Config{Name: "词法解析器", Value: "", Help: "命令行词法解析器", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||
if len(arg) > 0 && len(arg[0]) > 0 { // {{{
|
||||
cli, ok := m.Target().Server.(*CLI)
|
||||
@ -102,11 +114,11 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
yac.Cmd("train", "void", "void", "[\t ]+")
|
||||
|
||||
yac.Cmd("train", "key", "key", "[A-Za-z_][A-Za-z_0-9]*")
|
||||
yac.Cmd("train", "num", "num", "mul{", "0", "[1-9][0-9]*", "0[0-9]+", "0x[0-9]+", "}")
|
||||
yac.Cmd("train", "num", "num", "mul{", "0", "-?[1-9][0-9]*", "0[0-9]+", "0x[0-9]+", "}")
|
||||
yac.Cmd("train", "str", "str", "mul{", "\"[^\"]*\"", "'[^']*'", "}")
|
||||
|
||||
yac.Cmd("train", "tran", "tran", "mul{", "@", "$", "}", "opt{", "[a-zA-Z0-9_]+", "}")
|
||||
yac.Cmd("train", "word", "word", "mul{", "~", "!", "tran", "str", "[a-zA-Z0-9_/.:]+", "}")
|
||||
yac.Cmd("train", "word", "word", "mul{", "~", "!", "=", "tran", "str", "[a-zA-Z0-9_/\\-.:]+", "}")
|
||||
|
||||
yac.Cmd("train", "op1", "op1", "mul{", "$", "@", "}")
|
||||
yac.Cmd("train", "op1", "op1", "mul{", "-z", "-n", "}")
|
||||
@ -129,6 +141,9 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
yac.Cmd("train", "stm", "function", "function", "rep{", "key", "}")
|
||||
yac.Cmd("train", "stm", "return", "return", "rep{", "exp", "}")
|
||||
|
||||
yac.Cmd("train", "cmd", "cmd", "cache", "rep{", "word", "}")
|
||||
yac.Cmd("train", "cmd", "cmd", "cache", "key", "rep{", "word", "}")
|
||||
yac.Cmd("train", "cmd", "cmd", "cache", "key", "opt{", "=", "exp", "}")
|
||||
yac.Cmd("train", "cmd", "cmd", "rep{", "word", "}")
|
||||
yac.Cmd("train", "tran", "tran", "$", "(", "cmd", ")")
|
||||
|
||||
@ -341,10 +356,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
}
|
||||
|
||||
if m.Options("parse") || !m.Options("time_format") {
|
||||
m.Echo("%d", t.Unix())
|
||||
m.Echo(" ")
|
||||
}
|
||||
m.Echo("%d000", t.Unix())
|
||||
} else {
|
||||
m.Echo(t.Format(f))
|
||||
}
|
||||
// }}}
|
||||
}},
|
||||
"express": &ctx.Command{Name: "express exp", Help: "表达式运算", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
@ -661,18 +676,23 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
} // }}}
|
||||
}},
|
||||
"source": &ctx.Command{Name: "source file", Help: "运行脚本, file: 脚本文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
target := m.Target() // {{{
|
||||
if _, ok := m.Source().Server.(*CLI); ok {
|
||||
target = m.Source()
|
||||
cli, ok := m.Source().Server.(*CLI) // {{{
|
||||
if !ok {
|
||||
cli, ok = m.Target().Server.(*CLI)
|
||||
}
|
||||
|
||||
if !m.Caps("skip") {
|
||||
msg := m.Spawn(target)
|
||||
msg := m.Spawn(cli)
|
||||
msg.Start(fmt.Sprintf("%s_%d_%s", key, msg.Optioni("level", msg.Capi("level")+1), arg[0]), "脚本文件", arg[0])
|
||||
<-msg.Target().Exit
|
||||
m.Copy(msg, "result").Copy(msg, "append")
|
||||
nfs := msg.Sesss("nfs")
|
||||
nfs.Target().Close(nfs)
|
||||
|
||||
sub, _ := msg.Target().Server.(*CLI)
|
||||
if sub.target != msg.Target() {
|
||||
cli.target = sub.target
|
||||
}
|
||||
} // }}}
|
||||
}},
|
||||
"return": &ctx.Command{Name: "return result...", Help: "结束脚本, rusult: 返回值", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
|
@ -13,6 +13,7 @@ import ( // {{{
|
||||
"path"
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -2796,42 +2797,54 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
// }}}
|
||||
}},
|
||||
"command": &Command{
|
||||
Name: "command [all] add [key [name help]]", Help: "查看或修改命令",
|
||||
Form: map[string]int{"all": 0, "delete": 0, "void": 0, "condition": -1},
|
||||
Name: "command [all|add cmd arg...|list [begin [end]]|test [begin [end]]|delete cmd]", Help: "查看或修改命令",
|
||||
Form: map[string]int{"delete": 0, "condition": -1},
|
||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
all := m.Has("all") // {{{
|
||||
if len(arg) == 0 {
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s comands:\n", m.target.Name)
|
||||
keys := []string{}
|
||||
for k, _ := range m.target.Commands {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
for k, x := range m.target.Commands {
|
||||
if m.Check(m.target, "commands", k) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
m.Echo("%s: %s\n", k, m.target.Commands[k].Name)
|
||||
}
|
||||
m.Echo("%s: %s\n", k, x.Name)
|
||||
}
|
||||
}
|
||||
return all
|
||||
})
|
||||
return
|
||||
}
|
||||
switch arg[0] {
|
||||
case "all":
|
||||
keys := []string{}
|
||||
values := map[string]*Command{}
|
||||
for s := m.target; s != nil; s = s.context {
|
||||
for k, v := range s.Commands {
|
||||
if _, ok := values[k]; !ok {
|
||||
keys = append(keys, k)
|
||||
values[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
m.Echo("%s: %s\n", k, values[k].Name)
|
||||
}
|
||||
return
|
||||
case "add":
|
||||
if m.target.Caches == nil {
|
||||
m.target.Caches = map[string]*Cache{}
|
||||
}
|
||||
if _, ok := m.target.Caches["list_count"]; !ok {
|
||||
m.target.Caches["list_count"] = &Cache{Name: "list_count", Value: "0", Help: "list_count"}
|
||||
m.target.Caches["list_begin"] = &Cache{Name: "list_begin", Value: "0", Help: "list_begin"}
|
||||
}
|
||||
|
||||
if m.target.Commands == nil {
|
||||
m.target.Commands = map[string]*Command{}
|
||||
}
|
||||
|
||||
if _, ok := m.target.Caches["part"]; !ok {
|
||||
m.target.Caches["part"] = &Cache{Name: "part", Value: "0", Help: "part"}
|
||||
}
|
||||
m.target.Commands[m.Cap("part")] = &Command{
|
||||
Name: strings.Join(arg[1:], " "), Help: "part", Hand: func(m *Message, c *Context, key string, args ...string) {
|
||||
m.target.Commands[m.Cap("list_count")] = &Command{
|
||||
Name: strings.Join(arg[1:], " "),
|
||||
Help: "list_command",
|
||||
Hand: func(m *Message, c *Context, key string, args ...string) {
|
||||
list := []string{}
|
||||
j := 0
|
||||
for i := 1; i < len(arg); i++ {
|
||||
@ -2844,14 +2857,14 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
list = append(list, args[j:]...)
|
||||
|
||||
msg := m.Spawn(m.target).Cmd(list)
|
||||
msg := m.Spawn().Cmd(list)
|
||||
m.Copy(msg, "result").Copy(msg, "append")
|
||||
},
|
||||
}
|
||||
m.Capi("part", 1)
|
||||
m.Capi("list_count", 1)
|
||||
return
|
||||
case "list":
|
||||
begin, end := m.Confi("command_list_base"), m.Capi("part")
|
||||
begin, end := m.Capi("list_begin"), m.Capi("list_count")
|
||||
if len(arg) > 1 {
|
||||
n, e := strconv.Atoi(arg[1])
|
||||
m.Assert(e)
|
||||
@ -2869,7 +2882,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
return
|
||||
case "test":
|
||||
begin, end := 0, m.Capi("part")
|
||||
begin, end := 0, m.Capi("list_count")
|
||||
if len(arg) > 1 {
|
||||
n, e := strconv.Atoi(arg[1])
|
||||
m.Assert(e)
|
||||
@ -2880,20 +2893,20 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
m.Assert(e)
|
||||
end = n
|
||||
}
|
||||
|
||||
success, failure := 0, 0
|
||||
for i := begin; i < end; i++ {
|
||||
key := fmt.Sprintf("%d", i)
|
||||
if c, ok := m.target.Commands[key]; ok {
|
||||
msg := m.Spawn(m.Target())
|
||||
msg.Cmd(key)
|
||||
msg := m.Spawn().Cmd(key)
|
||||
if m.Options("condition") {
|
||||
done := true
|
||||
condition := m.Meta["condition"]
|
||||
for j := 0; j < len(condition)-1; j += 2 {
|
||||
if !msg.Has(condition[j]) || msg.Append(condition[j]) != condition[j+1] {
|
||||
m.Echo("\033[31m%s %s %s\033[0m\n", key, " fail", c.Name)
|
||||
failure++
|
||||
m.Color(31, "%s %s %s\n", key, " fail", c.Name)
|
||||
done = false
|
||||
failure++
|
||||
}
|
||||
}
|
||||
if done {
|
||||
@ -2910,8 +2923,14 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
}
|
||||
}
|
||||
m.Echo("\033[32msuccess: %d\033[0m, \033[31mfailure: %d\033[0m, \033[33mtotal: %d\033[0m", success, failure, success+failure)
|
||||
m.Color(32, "success: %d, ", success)
|
||||
m.Color(31, "failure: %d, ", failure)
|
||||
m.Color(33, "total: %d", success+failure)
|
||||
return
|
||||
case "delete":
|
||||
if _, ok := m.target.Commands[arg[1]]; ok {
|
||||
delete(m.target.Commands, arg[1])
|
||||
}
|
||||
}
|
||||
|
||||
switch len(arg) {
|
||||
@ -2919,11 +2938,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case 1:
|
||||
switch {
|
||||
case m.Has("delete"):
|
||||
if _, ok := m.target.Commands[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
delete(m.target.Commands, arg[0])
|
||||
}
|
||||
}
|
||||
case m.Has("void"):
|
||||
if x, ok := m.target.Commands[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
@ -2987,120 +3001,101 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
// }}}
|
||||
}},
|
||||
"config": &Command{
|
||||
Name: "config [all] [[delete|void] key [value]|[name value help]]",
|
||||
Help: "删除、置空、查看、修改或添加配置",
|
||||
Form: map[string]int{"all": 0, "delete": 0, "void": 0},
|
||||
Name: "config [all|key [value]|key name value help|delete key]",
|
||||
Help: "查看、读写、添加配置变量",
|
||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
all := m.Has("all") // {{{
|
||||
|
||||
switch len(arg) {
|
||||
switch len(arg) { //{{{
|
||||
case 0:
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s configs:\n", m.target.Name)
|
||||
keys := []string{}
|
||||
for k, _ := range m.target.Configs {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
for k, x := range m.target.Configs {
|
||||
if m.Check(m.target, "configs", k) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
m.Echo("%s(%s): %s\n", k, m.Conf(k), m.target.Configs[k].Name)
|
||||
}
|
||||
m.Echo("%s(%s): %s\n", k, x.Value, x.Name)
|
||||
}
|
||||
}
|
||||
return all
|
||||
})
|
||||
return
|
||||
case 1:
|
||||
switch {
|
||||
case m.Has("delete"):
|
||||
if _, ok := m.target.Configs[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
delete(m.target.Configs, arg[0])
|
||||
if arg[0] == "all" {
|
||||
keys := []string{}
|
||||
values := map[string]*Config{}
|
||||
for s := m.target; s != nil; s = s.context {
|
||||
for k, v := range s.Configs {
|
||||
if _, ok := values[k]; !ok {
|
||||
keys = append(keys, k)
|
||||
values[k] = v
|
||||
}
|
||||
}
|
||||
case m.Has("void"):
|
||||
m.Conf(arg[0], "")
|
||||
}
|
||||
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
// if all {
|
||||
// m.Echo("%s config:\n", m.target.Name)
|
||||
// }
|
||||
if x, ok := m.target.Configs[arg[0]]; ok {
|
||||
if m.Check(m.target, "configs", arg[0]) {
|
||||
// if all {
|
||||
// m.Echo(" ")
|
||||
// }
|
||||
// m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
m.Echo("%s", x.Value)
|
||||
return false
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
m.Echo("%s(%s): %s\n", k, m.Conf(k), values[k].Name)
|
||||
}
|
||||
return
|
||||
}
|
||||
return true
|
||||
// return all
|
||||
})
|
||||
|
||||
case 2:
|
||||
if arg[0] == "delete" {
|
||||
if _, ok := m.target.Configs[arg[1]]; ok {
|
||||
delete(m.target.Configs, arg[1])
|
||||
}
|
||||
return
|
||||
}
|
||||
m.Conf(arg[0], arg[1])
|
||||
case 3:
|
||||
m.Conf(arg[0], arg[2])
|
||||
case 4:
|
||||
m.Conf(arg[0], arg[0], arg[2], arg[0])
|
||||
default:
|
||||
m.Conf(arg[0], arg[1:]...)
|
||||
}
|
||||
m.Echo("%s", m.Conf(arg[0]))
|
||||
// }}}
|
||||
}},
|
||||
"cache": &Command{
|
||||
Name: "cache [all] [[delete|void] key [value]|[name value help]]",
|
||||
Help: "删除、置空、查看、修改或添加缓存",
|
||||
Form: map[string]int{"all": 0, "delete": 0, "void": 0},
|
||||
Name: "cache [all|key [value]|key = value|key name value help|delete key]",
|
||||
Help: "查看、读写、赋值、新建、删除缓存变量",
|
||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
all := m.Has("all") // {{{
|
||||
|
||||
switch len(arg) {
|
||||
switch len(arg) { //{{{
|
||||
case 0:
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s configs:\n", m.target.Name)
|
||||
keys := []string{}
|
||||
for k, _ := range m.target.Caches {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
for k, x := range m.target.Caches {
|
||||
if m.Check(m.target, "caches", k) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
m.Echo("%s(%s): %s\n", k, m.Cap(k), m.target.Caches[k].Name)
|
||||
}
|
||||
m.Echo("%s(%s): %s\n", k, m.Cap(k), x.Name)
|
||||
}
|
||||
}
|
||||
return all
|
||||
})
|
||||
|
||||
return
|
||||
case 1:
|
||||
switch {
|
||||
case m.Has("delete"):
|
||||
if _, ok := m.target.Caches[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
delete(m.target.Caches, arg[0])
|
||||
if arg[0] == "all" {
|
||||
keys := []string{}
|
||||
values := map[string]*Cache{}
|
||||
for s := m.target; s != nil; s = s.context {
|
||||
for k, v := range s.Caches {
|
||||
if _, ok := values[k]; !ok {
|
||||
keys = append(keys, k)
|
||||
values[k] = v
|
||||
}
|
||||
}
|
||||
case m.Has("void"):
|
||||
m.Cap(arg[0], "")
|
||||
}
|
||||
|
||||
// if m.source == m.source.master {
|
||||
// m.source, m.target = m.target, m.source
|
||||
// }
|
||||
m.Echo("%s", m.Cap(arg[0]))
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
m.Echo("%s(%s): %s\n", k, m.Cap(k), values[k].Name)
|
||||
}
|
||||
return
|
||||
}
|
||||
case 2:
|
||||
// if m.source == m.source.master {
|
||||
// m.source, m.target = m.target, m.source
|
||||
// }
|
||||
if arg[0] == "delete" {
|
||||
if _, ok := m.target.Caches[arg[1]]; ok {
|
||||
delete(m.target.Caches, arg[1])
|
||||
}
|
||||
return
|
||||
}
|
||||
m.Cap(arg[0], arg[1])
|
||||
case 3:
|
||||
if m.source == m.source.master {
|
||||
m.source, m.target = m.target, m.source
|
||||
}
|
||||
m.Cap(arg[0], arg[2])
|
||||
case 4:
|
||||
m.Cap(arg[0], arg[0], arg[2], arg[0])
|
||||
default:
|
||||
m.Cap(arg[0], arg[1:]...)
|
||||
}
|
||||
m.Echo("%s", m.Cap(arg[0]))
|
||||
// }}}
|
||||
}},
|
||||
"right": &Command{
|
||||
|
Loading…
x
Reference in New Issue
Block a user