mirror of
https://shylinux.com/x/ContextOS
synced 2025-04-26 01:04:06 +08:00
mac add nfs.scan 把cli文件读写移到nfs, 添加了消息树动态变量读写
This commit is contained in:
parent
5fe88cc55a
commit
9bddeea4cc
@ -1,3 +1,2 @@
|
||||
~cli
|
||||
~aaa login root root
|
||||
var a = 4 + 3 * 5 - 2
|
||||
|
@ -34,16 +34,7 @@ type CLI struct { // {{{
|
||||
|
||||
// }}}
|
||||
|
||||
func (cli *CLI) print(str string, arg ...interface{}) bool { // {{{
|
||||
if cli.out != nil {
|
||||
fmt.Fprintf(cli.out, str, arg...)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) check(arg []string) bool {
|
||||
func (cli *CLI) check(arg []string) bool { // {{{
|
||||
if len(arg) < 0 {
|
||||
return false
|
||||
}
|
||||
@ -56,6 +47,7 @@ func (cli *CLI) check(arg []string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// }}}
|
||||
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{}
|
||||
@ -74,7 +66,7 @@ func (cli *CLI) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
cli.Caches["target"] = &ctx.Cache{Name: "操作目标", Value: cli.Name, Help: "命令操作的目标"}
|
||||
cli.Caches["result"] = &ctx.Cache{Name: "执行结果", Value: "", Help: "前一条命令的执行结果"}
|
||||
cli.Caches["back"] = &ctx.Cache{Name: "前一条指令", Value: "", Help: "前一条指令"}
|
||||
@ -108,7 +100,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
// }}}
|
||||
}}
|
||||
cli.Configs["yac"] = &ctx.Config{Name: "词法解析器", Value: "", Help: "命令行词法解析器", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||
if len(arg) > 0 && len(arg[0]) > 0 {
|
||||
if len(arg) > 0 && len(arg[0]) > 0 { // {{{
|
||||
cli, ok := m.Target().Server.(*CLI)
|
||||
m.Assert(ok, "模块类型错误")
|
||||
|
||||
@ -125,10 +117,10 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
m.Spawn(yac.Target()).Cmd("train", "tran", "tran", "mul{", "@", "$", "}", "opt{", "[a-zA-Z0-9]+", "}")
|
||||
m.Spawn(yac.Target()).Cmd("train", "word", "word", "mul{", "~", "!", "tran", "\"[^\"]*\"", "'[^']*'", "[a-zA-Z0-9_/.]+", "}")
|
||||
|
||||
m.Spawn(yac.Target()).Cmd("train", "op1", "op1", "opt{", "mul{", "-z", "-n", "}", "}", "word")
|
||||
m.Spawn(yac.Target()).Cmd("train", "op2", "op2", "op1", "rep{", "mul{", "+", "-", "*", "/", "}", "op1", "}")
|
||||
m.Spawn(yac.Target()).Cmd("train", "op1", "op1", "(", "op2", ")")
|
||||
|
||||
// m.Spawn(yac.Target()).Cmd("train", "op1", "op1", "opt{", "mul{", "-z", "-n", "}", "}", "word")
|
||||
// m.Spawn(yac.Target()).Cmd("train", "op2", "op2", "op1", "rep{", "mul{", "+", "-", "*", "/", "}", "op1", "}")
|
||||
// m.Spawn(yac.Target()).Cmd("train", "op1", "op1", "(", "op2", ")")
|
||||
//
|
||||
m.Spawn(yac.Target()).Cmd("train", "stm", "var", "var", "key", "opt{", "=", "op2", "}")
|
||||
|
||||
m.Spawn(yac.Target()).Cmd("train", "cmd", "cmd", "rep{", "word", "}")
|
||||
@ -140,7 +132,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
return arg[0]
|
||||
}
|
||||
return x.Value
|
||||
|
||||
// }}}
|
||||
}}
|
||||
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 { // {{{
|
||||
@ -199,6 +191,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
return cli
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
cli.Caches["#"] = &ctx.Cache{Name: "参数个数", Value: fmt.Sprintf("%d", len(arg)), Help: "参数个数"}
|
||||
for i, v := range arg {
|
||||
@ -212,61 +205,71 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
m.Caps("else", true)
|
||||
}
|
||||
m.Caps("exit", false)
|
||||
|
||||
if m.Has("stdio") {
|
||||
cli.Caches["init.shy"] = &ctx.Cache{Name: "启动脚本", Value: "etc/init.shy", Help: "模块启动时自动运行的脚本"}
|
||||
cli.Caches["level"] = &ctx.Cache{Name: "模块嵌套层数", Value: "0", Help: "模块嵌套层数"}
|
||||
if len(arg) > 0 {
|
||||
m.Cap("init.shy", arg[0])
|
||||
if true {
|
||||
if m.Has("stdio") {
|
||||
cli.Caches["init.shy"] = &ctx.Cache{Name: "启动脚本", Value: "etc/init.shy", Help: "模块启动时自动运行的脚本"}
|
||||
if len(arg) > 0 {
|
||||
m.Cap("init.shy", arg[0])
|
||||
}
|
||||
go func() {
|
||||
m.Find("nfs").Cmd("scan", m.Cap("init.shy"))
|
||||
m.Find("nfs").Cmd("scan", "stdio")
|
||||
}()
|
||||
}
|
||||
m.Find("nfs").Cmd("scan", m.Cap("init.shy"))
|
||||
|
||||
m.Cap("next", fmt.Sprintf("source %s\n", m.Cap("init.shy")))
|
||||
cli.bio = bufio.NewReader(os.Stdin)
|
||||
cli.out = os.Stdout
|
||||
m.Conf("yac", "yac")
|
||||
m.Cap("stream", "stdout")
|
||||
} else if stream, ok := m.Data["file"]; ok {
|
||||
if bio, ok := stream.(*bufio.Reader); ok {
|
||||
cli.bio = bio
|
||||
m.Cap("stream", "bufio")
|
||||
} else {
|
||||
cli.bio = bufio.NewReader(stream.(io.ReadWriteCloser))
|
||||
m.Cap("stream", "file")
|
||||
} else {
|
||||
if m.Has("stdio") {
|
||||
cli.Caches["init.shy"] = &ctx.Cache{Name: "启动脚本", Value: "etc/init.shy", Help: "模块启动时自动运行的脚本"}
|
||||
cli.Caches["level"] = &ctx.Cache{Name: "模块嵌套层数", Value: "0", Help: "模块嵌套层数"}
|
||||
if len(arg) > 0 {
|
||||
m.Cap("init.shy", arg[0])
|
||||
}
|
||||
m.Cap("next", fmt.Sprintf("source %s\n", m.Cap("init.shy")))
|
||||
cli.bio = bufio.NewReader(os.Stdin)
|
||||
cli.out = os.Stdout
|
||||
m.Conf("yac", "yac")
|
||||
m.Cap("stream", "stdout")
|
||||
} else if stream, ok := m.Data["file"]; ok {
|
||||
if bio, ok := stream.(*bufio.Reader); ok {
|
||||
cli.bio = bio
|
||||
m.Cap("stream", "bufio")
|
||||
} else {
|
||||
cli.bio = bufio.NewReader(stream.(io.ReadWriteCloser))
|
||||
m.Cap("stream", "file")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m.Capi("nline", 0, len(cli.lines))
|
||||
m.Caps("pos", m.Has("for"))
|
||||
m.Capi("nline", 0, len(cli.lines))
|
||||
m.Caps("pos", m.Has("for"))
|
||||
|
||||
m.Log("info", nil, "%p %s pos:%s nline:%s %d", cli.bio, m.Cap("stream"), m.Cap("pos"), m.Cap("nline"), len(cli.lines))
|
||||
m.Log("info", nil, "%p %s pos:%s nline:%s %d", cli.bio, m.Cap("stream"), m.Cap("pos"), m.Cap("nline"), len(cli.lines))
|
||||
|
||||
go m.AssertOne(m, true, func(m *ctx.Message) {
|
||||
for !m.Caps("exit") {
|
||||
line := m.Cap("next")
|
||||
if m.Cap("next", ""); line == "" {
|
||||
if cli.bio == nil {
|
||||
line = cli.lines[m.Capi("pos", 1)-1]
|
||||
} else {
|
||||
cli.print(m.Conf("PS1"))
|
||||
if l, e := cli.bio.ReadString('\n'); m.Assert(e) {
|
||||
line = l
|
||||
go m.AssertOne(m, true, func(m *ctx.Message) {
|
||||
for !m.Caps("exit") {
|
||||
line := m.Cap("next")
|
||||
if m.Cap("next", ""); line == "" {
|
||||
if cli.bio == nil {
|
||||
line = cli.lines[m.Capi("pos", 1)-1]
|
||||
} else {
|
||||
// cli.print(m.Conf("PS1"))
|
||||
if l, e := cli.bio.ReadString('\n'); m.Assert(e) {
|
||||
line = l
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if line == "\n" && cli.out != nil {
|
||||
line = m.Cap("back") + "\n"
|
||||
m.Cap("back", "")
|
||||
}
|
||||
if line == "\n" && cli.out != nil {
|
||||
line = m.Cap("back") + "\n"
|
||||
m.Cap("back", "")
|
||||
}
|
||||
|
||||
yac := m.Spawn(cli.yac.Target())
|
||||
yac.Cmd("parse", "line", "void", line)
|
||||
}
|
||||
}, func(m *ctx.Message) {
|
||||
m.Caps("exit", true)
|
||||
m.Spawn(cli.Context).Set("detail", "end").Post(cli.Context)
|
||||
})
|
||||
yac := m.Spawn(cli.yac.Target())
|
||||
yac.Cmd("parse", "line", "void", line)
|
||||
}
|
||||
}, func(m *ctx.Message) {
|
||||
m.Caps("exit", true)
|
||||
m.Spawn(cli.Context).Set("detail", "end").Post(cli.Context)
|
||||
})
|
||||
}
|
||||
|
||||
m.Deal(func(msg *ctx.Message, arg ...string) bool {
|
||||
return !cli.Has("skip") || !m.Caps("skip") || Index.Has(msg.Get("detail"), "command")
|
||||
@ -359,7 +362,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
Configs: map[string]*ctx.Config{},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"express": &ctx.Command{Name: "express exp", Help: "表达式运算", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
result := "false"
|
||||
result := "false" // {{{
|
||||
switch len(arg) {
|
||||
case 0:
|
||||
result = ""
|
||||
@ -469,9 +472,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
}
|
||||
}
|
||||
m.Echo(result)
|
||||
// }}}
|
||||
}},
|
||||
"op1": &ctx.Command{Name: "op1 word", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if _, ok := m.Target().Server.(*CLI); m.Assert(ok) {
|
||||
if _, ok := m.Target().Server.(*CLI); m.Assert(ok) { // {{{
|
||||
if len(arg) == 1 {
|
||||
m.Echo(arg[0])
|
||||
return
|
||||
@ -492,9 +496,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
}
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
}},
|
||||
"op2": &ctx.Command{Name: "op2 word", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if cli, ok := m.Target().Server.(*CLI); m.Assert(ok) {
|
||||
if cli, ok := m.Target().Server.(*CLI); m.Assert(ok) { // {{{
|
||||
pre := map[string]int{"+": 1, "-": 1, "*": 2, "/": 2}
|
||||
num := []string{arg[0]}
|
||||
op := []string{}
|
||||
@ -516,6 +521,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
|
||||
m.Echo("%s", num[0])
|
||||
}
|
||||
// }}}
|
||||
}},
|
||||
"tran": &ctx.Command{Name: "tran word", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if _, ok := m.Target().Server.(*CLI); m.Assert(ok) { // {{{
|
||||
@ -555,13 +561,33 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
}
|
||||
} // }}}
|
||||
}},
|
||||
"line": &ctx.Command{Name: "line word", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
"parse": &ctx.Command{Name: "parse word", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if cli, ok := m.Target().Server.(*CLI); m.Assert(ok) { // {{{
|
||||
|
||||
msg := m.Spawn(cli.target)
|
||||
if a, ok := cli.alias[arg[0]]; ok {
|
||||
msg.Set("detail", a...)
|
||||
msg.Meta["detail"] = append(msg.Meta["detail"], arg[1:]...)
|
||||
} else {
|
||||
msg.Set("detail", arg...)
|
||||
}
|
||||
|
||||
msg.Post(cli.Context)
|
||||
if m.Hand = false; msg.Hand {
|
||||
m.Hand = true
|
||||
m.Meta["result"] = msg.Meta["result"]
|
||||
}
|
||||
} // }}}
|
||||
}},
|
||||
"lines": &ctx.Command{Name: "line word", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if cli, ok := m.Target().Server.(*CLI); m.Assert(ok) { // {{{
|
||||
arg = arg[:len(arg)-1]
|
||||
|
||||
m.Hand = false
|
||||
result := strings.TrimRight(strings.Join(arg, ""), "\n")
|
||||
if m.Cap("result", result); len(result) > 0 {
|
||||
cli.print(result + "\n")
|
||||
m.Echo(result + "\n")
|
||||
// cli.print(result + "\n")
|
||||
}
|
||||
|
||||
if m.Cap("back", ""); cli.bio != nil {
|
||||
|
@ -19,6 +19,10 @@ import ( // {{{
|
||||
|
||||
// }}}
|
||||
|
||||
func right(str string) bool {
|
||||
return str != "" && str != "0" && str != "false"
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
Name string
|
||||
Value string
|
||||
@ -499,7 +503,7 @@ func (m *Message) Log(action string, ctx *Context, str string, arg ...interface{
|
||||
case "begin", "start", "close":
|
||||
color = 36
|
||||
case "debug":
|
||||
if m.root.Conf("debug") != "on" {
|
||||
if !m.Confs("debug") {
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -919,8 +923,7 @@ func (m *Message) Geti(key string) int { // {{{
|
||||
|
||||
// }}}
|
||||
func (m *Message) Gets(key string) bool { // {{{
|
||||
b := m.Get(key)
|
||||
return b != "" && b != "0" && b != "false"
|
||||
return right(m.Get(key))
|
||||
}
|
||||
|
||||
// }}}
|
||||
@ -930,6 +933,96 @@ func (m *Message) Echo(str string, arg ...interface{}) *Message { // {{{
|
||||
|
||||
// }}}
|
||||
|
||||
func (m *Message) Option(key string, arg ...string) string { // {{{
|
||||
if len(arg) > 0 {
|
||||
m.Log("fuck", nil, "option set %s %v", key, arg)
|
||||
m.Set("option", append([]string{key}, arg...)...)
|
||||
}
|
||||
|
||||
for msg := m; msg != nil; msg = msg.message {
|
||||
msg.Log("fuck", nil, "option")
|
||||
if m.Has(key) {
|
||||
return m.Get(key)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Optioni(key string, arg ...int) int { // {{{
|
||||
if len(arg) > 0 {
|
||||
meta := []string{}
|
||||
for _, v := range arg {
|
||||
meta = append(meta, fmt.Sprintf("%d", v))
|
||||
}
|
||||
m.Option(key, meta...)
|
||||
}
|
||||
|
||||
i, e := strconv.Atoi(m.Option(key))
|
||||
m.Assert(e)
|
||||
return i
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Options(key string, arg ...bool) bool { // {{{
|
||||
if len(arg) > 0 {
|
||||
meta := []string{}
|
||||
for _, v := range arg {
|
||||
meta = append(meta, fmt.Sprintf("%t", v))
|
||||
}
|
||||
m.Option(key, meta...)
|
||||
}
|
||||
|
||||
return right(m.Option(key))
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
func (m *Message) Append(key string, arg ...string) string { // {{{
|
||||
if len(arg) > 0 {
|
||||
m.Log("fuck", nil, "append set %s %v", key, arg)
|
||||
m.Set("append", append([]string{key}, arg...)...)
|
||||
}
|
||||
|
||||
for msg := m; msg != nil; msg = msg.message {
|
||||
msg.Log("fuck", nil, "append")
|
||||
if m.Has(key) {
|
||||
return m.Get(key)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Appendi(key string, arg ...int) int { // {{{
|
||||
if len(arg) > 0 {
|
||||
meta := []string{}
|
||||
for _, v := range arg {
|
||||
meta = append(meta, fmt.Sprintf("%d", v))
|
||||
}
|
||||
m.Append(key, meta...)
|
||||
}
|
||||
|
||||
i, e := strconv.Atoi(m.Append(key))
|
||||
m.Assert(e)
|
||||
return i
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Appends(key string, arg ...bool) bool { // {{{
|
||||
if len(arg) > 0 {
|
||||
meta := []string{}
|
||||
for _, v := range arg {
|
||||
meta = append(meta, fmt.Sprintf("%t", v))
|
||||
}
|
||||
m.Append(key, meta...)
|
||||
}
|
||||
|
||||
return right(m.Append(key))
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
func (m *Message) Exec(key string, arg ...string) string { // {{{
|
||||
|
||||
for _, c := range []*Context{m.target, m.target.master, m.target.Owner, m.source, m.source.master, m.source.Owner} {
|
||||
@ -1271,7 +1364,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
"ContextSessionSize": &Config{Name: "会话队列长度", Value: "10", Help: "每个模块可以启动其它模块的数量"},
|
||||
"MessageQueueSize": &Config{Name: "消息队列长度", Value: "10", Help: "每个模块接收消息的队列长度"},
|
||||
|
||||
"debug": &Config{Name: "调试模式(off/on)", Value: "off", Help: "是否打印错误信息,off:不打印,on:打印)"},
|
||||
"debug": &Config{Name: "调试模式(true/false)", Value: "false", Help: "是否打印错误信息,off:不打印,on:打印)"},
|
||||
"cert": &Config{Name: "证书文件", Value: "etc/cert.pem", Help: "证书文件"},
|
||||
"key": &Config{Name: "私钥文件", Value: "etc/key.pem", Help: "私钥文件"},
|
||||
},
|
||||
@ -1401,6 +1494,30 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
|
||||
// }}}
|
||||
}},
|
||||
"option": &Command{Name: "option key val...", Help: "查看消息", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
if len(arg) > 0 { // {{{
|
||||
m.Option(arg[0], arg[1:]...)
|
||||
} else {
|
||||
for msg := m; msg != nil; msg = msg.message {
|
||||
m.Echo("%d %s:%s->%s %v\n", msg.code, msg.time.Format("15:03:04"), msg.source.Name, msg.target.Name, msg.Meta["detail"])
|
||||
for _, k := range msg.Meta["option"] {
|
||||
m.Echo("%s: %v\n", k, msg.Meta[k])
|
||||
}
|
||||
}
|
||||
} // }}}
|
||||
}},
|
||||
"append": &Command{Name: "append key val...", Help: "查看消息", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
if len(arg) > 0 { // {{{
|
||||
m.Append(arg[0], arg[1:]...)
|
||||
} else {
|
||||
for msg := m; msg != nil; msg = msg.message {
|
||||
m.Echo("%d %s:%s->%s %v\n", msg.code, msg.time.Format("15:03:04"), msg.source.Name, msg.target.Name, msg.Meta["result"])
|
||||
for _, k := range msg.Meta["append"] {
|
||||
m.Echo("%s: %v\n", k, msg.Meta[k])
|
||||
}
|
||||
}
|
||||
} // }}}
|
||||
}},
|
||||
"context": &Command{Name: "context back|[[home] [find|search] name] [info|list|show|spawn|start|switch|close][args]", Help: "查找并操作模块,\n查找起点root:根模块、back:父模块、home:本模块,\n查找方法find:路径匹配、search:模糊匹配,\n查找对象name:支持点分和正则,\n操作类型show:显示信息、switch:切换为当前、start:启动模块、spawn:分裂子模块,args:启动参数",
|
||||
Formats: map[string]int{"back": 0, "home": 0, "find": 1, "search": 1, "info": 1, "list": 0, "show": 0, "close": 0, "switch": 0, "start": 0, "spawn": 0},
|
||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
|
@ -339,7 +339,7 @@ var Index = &ctx.Context{Name: "lex", Help: "词法中心",
|
||||
}
|
||||
|
||||
lex.seed = append(lex.seed, &Seed{page, hash, string(arg[0])})
|
||||
lex.Log("info", nil, "%d %d %d %v", page, hash, lex.Capi("nseed", 1), arg[0])
|
||||
lex.Log("debug", nil, "%d %d %d %v", page, hash, lex.Capi("nseed", 1), arg[0])
|
||||
lex.Cap("stream", fmt.Sprintf("%s,%s,%s", lex.Cap("nseed"), lex.Cap("npage"), lex.Cap("nhash")))
|
||||
|
||||
m.Echo("%d", lex.train(page, hash, []byte(arg[0])))
|
||||
|
@ -3,6 +3,7 @@ package nfs
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/skip2/go-qrcode"
|
||||
"io"
|
||||
@ -12,28 +13,35 @@ import (
|
||||
)
|
||||
|
||||
type NFS struct {
|
||||
file *os.File
|
||||
in *os.File
|
||||
out *os.File
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||
file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||
m.Assert(e)
|
||||
info, e := os.Stat(arg[0])
|
||||
m.Assert(e)
|
||||
func (nfs *NFS) print(str string, arg ...interface{}) bool {
|
||||
if nfs.out == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Fprintf(nfs.out, str, arg...)
|
||||
return true
|
||||
}
|
||||
|
||||
func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||
c.Caches = map[string]*ctx.Cache{
|
||||
"name": &ctx.Cache{Name: "name", Value: info.Name(), Help: "文件名"},
|
||||
"mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件权限"},
|
||||
"time": &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), Help: "创建时间"},
|
||||
"size": &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件大小"},
|
||||
"pos": &ctx.Cache{Name: "pos", Value: "0", Help: "读写位置"},
|
||||
"pos": &ctx.Cache{Name: "读写位置", Value: "0", Help: "读写位置"},
|
||||
}
|
||||
c.Configs = map[string]*ctx.Config{}
|
||||
|
||||
if info, e := os.Stat(arg[1]); e == nil {
|
||||
c.Caches["name"] = &ctx.Cache{Name: "name", Value: info.Name(), Help: "文件名"}
|
||||
c.Caches["mode"] = &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件权限"}
|
||||
c.Caches["size"] = &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件大小"}
|
||||
c.Caches["time"] = &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), Help: "创建时间"}
|
||||
}
|
||||
|
||||
s := new(NFS)
|
||||
s.Context = c
|
||||
s.file = file
|
||||
return s
|
||||
|
||||
}
|
||||
@ -46,18 +54,49 @@ func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
}
|
||||
|
||||
func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool {
|
||||
m.Log("info", nil, "%d open %s", Pulse.Capi("nfile"), m.Cap("name"))
|
||||
m.Cap("stream", m.Cap("name"))
|
||||
return false
|
||||
if out, ok := m.Data["out"]; ok {
|
||||
nfs.out = out.(*os.File)
|
||||
}
|
||||
if in, ok := m.Data["in"]; ok {
|
||||
nfs.in = in.(*os.File)
|
||||
}
|
||||
|
||||
m.Log("info", nil, "%d %v", Pulse.Capi("nfile"), arg)
|
||||
if m.Cap("stream", arg[1]); arg[0] == "open" {
|
||||
return false
|
||||
}
|
||||
|
||||
cli := m.Reply()
|
||||
cli.Conf("yac", "yac")
|
||||
yac := m.Find(cli.Conf("yac"))
|
||||
bio := bufio.NewScanner(nfs.in)
|
||||
nfs.print("%s", cli.Conf("PS1"))
|
||||
|
||||
for bio.Scan() {
|
||||
line := m
|
||||
if yac != nil {
|
||||
// line = cli.Spawn(yac.Target())
|
||||
line = m.Spawn(yac.Target())
|
||||
} else {
|
||||
line = m.Reply()
|
||||
}
|
||||
line.Cmd(append([]string{"parse", "line", "void"}, strings.Split(bio.Text()+" \n", " ")...)...)
|
||||
|
||||
if result := strings.TrimRight(strings.Join(line.Meta["result"], ""), "\n"); len(result) > 0 {
|
||||
nfs.print("%s", result+"\n")
|
||||
}
|
||||
nfs.print("%s", cli.Conf("PS1"))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool {
|
||||
switch nfs.Context {
|
||||
case m.Target():
|
||||
if nfs.file != nil {
|
||||
if nfs.in != nil {
|
||||
m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name"))
|
||||
nfs.file.Close()
|
||||
nfs.file = nil
|
||||
nfs.in.Close()
|
||||
nfs.in = nil
|
||||
}
|
||||
case m.Source():
|
||||
}
|
||||
@ -73,8 +112,21 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||
"size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"},
|
||||
},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"scan": &ctx.Command{Name: "scan file", Help: "扫描文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if arg[0] == "stdio" {
|
||||
m.Put("option", "in", os.Stdin).Put("option", "out", os.Stdout)
|
||||
m.Start("stdio", "扫描文件", m.Meta["detail"]...)
|
||||
} else if f, e := os.Open(arg[0]); m.Assert(e) {
|
||||
m.Put("option", "in", f)
|
||||
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "扫描文件", m.Meta["detail"]...)
|
||||
}
|
||||
m.Echo(m.Target().Name)
|
||||
}},
|
||||
"open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件", arg...)
|
||||
if f, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm); m.Assert(e) {
|
||||
m.Put("option", "in", f).Put("option", "out", f)
|
||||
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件", m.Meta["detail"]...)
|
||||
}
|
||||
m.Echo(m.Target().Name)
|
||||
}},
|
||||
"read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
@ -92,7 +144,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||
}
|
||||
|
||||
buf := make([]byte, n)
|
||||
if n, e = nfs.file.ReadAt(buf, int64(m.Capi("pos"))); e != io.EOF {
|
||||
if n, e = nfs.in.ReadAt(buf, int64(m.Capi("pos"))); e != io.EOF {
|
||||
m.Assert(e)
|
||||
}
|
||||
m.Echo(string(buf))
|
||||
@ -108,11 +160,11 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||
}
|
||||
|
||||
if len(arg[0]) == 0 {
|
||||
m.Assert(nfs.file.Truncate(int64(m.Capi("pos"))))
|
||||
m.Assert(nfs.in.Truncate(int64(m.Capi("pos"))))
|
||||
m.Cap("size", m.Cap("pos"))
|
||||
m.Cap("pos", "0")
|
||||
} else {
|
||||
n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos")))
|
||||
n, e := nfs.in.WriteAt([]byte(arg[0]), int64(m.Capi("pos")))
|
||||
if m.Assert(e) && m.Capi("pos", n) > m.Capi("size") {
|
||||
m.Cap("size", m.Cap("pos"))
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ func (yac *YAC) train(page, hash int, word []string) ([]*State, int) { // {{{
|
||||
x.star = s
|
||||
sn[x.next] = true
|
||||
point = append(point, x)
|
||||
yac.Pulse.Log("info", nil, "END: %v", x)
|
||||
yac.Pulse.Log("debug", nil, "END: %v", x)
|
||||
}
|
||||
case "mul{":
|
||||
mul, n = true, 1
|
||||
@ -117,7 +117,7 @@ func (yac *YAC) train(page, hash int, word []string) ([]*State, int) { // {{{
|
||||
}
|
||||
|
||||
state := yac.mat[s][c]
|
||||
yac.Pulse.Log("info", nil, "GET(%d, %d): %v", s, c, state)
|
||||
yac.Pulse.Log("debug", nil, "GET(%d, %d): %v", s, c, state)
|
||||
if state == nil {
|
||||
state = &State{}
|
||||
yac.Pulse.Capi("nnode", 1)
|
||||
@ -141,7 +141,7 @@ func (yac *YAC) train(page, hash int, word []string) ([]*State, int) { // {{{
|
||||
}
|
||||
yac.mat[s][c] = state
|
||||
|
||||
yac.Pulse.Log("info", nil, "SET(%d, %d): %v", s, c, state)
|
||||
yac.Pulse.Log("debug", nil, "SET(%d, %d): %v", s, c, state)
|
||||
ends = append(ends, state)
|
||||
point = append(point, state)
|
||||
if s > begin {
|
||||
@ -175,7 +175,7 @@ func (yac *YAC) train(page, hash int, word []string) ([]*State, int) { // {{{
|
||||
}
|
||||
}
|
||||
if void {
|
||||
yac.Pulse.Log("info", nil, "DEL: %d %d", yac.Pulse.Capi("nline"), n)
|
||||
yac.Pulse.Log("debug", nil, "DEL: %d %d", yac.Pulse.Capi("nline"), n)
|
||||
yac.Pulse.Capi("nline", 0, n)
|
||||
yac.mat = yac.mat[:n]
|
||||
}
|
||||
@ -184,14 +184,14 @@ func (yac *YAC) train(page, hash int, word []string) ([]*State, int) { // {{{
|
||||
for _, n := range ss {
|
||||
for _, s := range point {
|
||||
if s.next == n {
|
||||
yac.Pulse.Log("info", nil, "GET: %v", s)
|
||||
yac.Pulse.Log("debug", nil, "GET: %v", s)
|
||||
if s.next >= len(yac.mat) {
|
||||
s.next = 0
|
||||
}
|
||||
if hash > 0 {
|
||||
s.hash = hash
|
||||
}
|
||||
yac.Pulse.Log("info", nil, "SET: %v", s)
|
||||
yac.Pulse.Log("debug", nil, "SET: %v", s)
|
||||
}
|
||||
}
|
||||
|
||||
@ -204,7 +204,7 @@ func (yac *YAC) train(page, hash int, word []string) ([]*State, int) { // {{{
|
||||
func (yac *YAC) parse(m *ctx.Message, page, void int, line string) ([]string, string) { // {{{
|
||||
|
||||
level := m.Capi("level", 1)
|
||||
m.Log("info", nil, "%s\\%d %s(%d):", m.Cap("label")[0:level], level, yac.word[page], page)
|
||||
m.Log("debug", nil, "%s\\%d %s(%d):", m.Cap("label")[0:level], level, yac.word[page], page)
|
||||
|
||||
hash, word := 0, []string{}
|
||||
for star, s := 0, page; s != 0 && len(line) > 0; {
|
||||
@ -215,7 +215,6 @@ func (yac *YAC) parse(m *ctx.Message, page, void int, line string) ([]string, st
|
||||
|
||||
lex = yac.lex.Spawn(yac.lex.Target())
|
||||
lex.Cmd("parse", line, fmt.Sprintf("yac%d", s))
|
||||
line = lex.Meta["result"][2]
|
||||
|
||||
c := byte(lex.Geti("result"))
|
||||
state := yac.mat[s][c]
|
||||
@ -226,9 +225,9 @@ func (yac *YAC) parse(m *ctx.Message, page, void int, line string) ([]string, st
|
||||
if i >= m.Capi("nlang") || x == nil {
|
||||
continue
|
||||
}
|
||||
m.Log("info", nil, "%s|%d try(%d,%d): %v", m.Cap("label")[0:level], level, s, i, x)
|
||||
m.Log("debug", nil, "%s|%d try(%d,%d): %v", m.Cap("label")[0:level], level, s, i, x)
|
||||
if w, l := yac.parse(m, i, void, line); l != line {
|
||||
m.Log("info", nil, "%s|%d end(%d,%d): %v", m.Cap("label")[0:level], level, s, i, x)
|
||||
m.Log("debug", nil, "%s|%d end(%d,%d): %v", m.Cap("label")[0:level], level, s, i, x)
|
||||
word = append(word, w...)
|
||||
state = x
|
||||
line = l
|
||||
@ -236,8 +235,9 @@ func (yac *YAC) parse(m *ctx.Message, page, void int, line string) ([]string, st
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m.Log("info", nil, "%s|%d get(%d,%d): %v \033[31m(%s)\033[0m", m.Cap("label")[0:level], level, s, c, state, lex.Meta["result"][1])
|
||||
m.Log("debug", nil, "%s|%d get(%d,%d): %v \033[31m(%s)\033[0m", m.Cap("label")[0:level], level, s, c, state, lex.Meta["result"][1])
|
||||
word = append(word, lex.Meta["result"][1])
|
||||
line = lex.Meta["result"][2]
|
||||
}
|
||||
|
||||
if state == nil {
|
||||
@ -254,14 +254,14 @@ func (yac *YAC) parse(m *ctx.Message, page, void int, line string) ([]string, st
|
||||
if hash == 0 {
|
||||
word = word[:0]
|
||||
} else {
|
||||
msg := m.Spawn(m.Source()).Add("detail", yac.hand[hash], word...)
|
||||
msg := m.Spawn(yac.Message.Source()).Add("detail", yac.hand[hash], word...)
|
||||
if msg.Cmd(); msg.Hand {
|
||||
m.Log("info", nil, "%s>%d set(%d): \033[31m%v\033[0m->\033[32m%v\033[0m", m.Cap("label")[0:level], level, hash, word, msg.Meta["result"])
|
||||
m.Log("debug", nil, "%s>%d set(%d): \033[31m%v\033[0m->\033[32m%v\033[0m", m.Cap("label")[0:level], level, hash, word, msg.Meta["result"])
|
||||
word = msg.Meta["result"]
|
||||
}
|
||||
}
|
||||
|
||||
m.Log("info", nil, "%s/%d %s(%d):", m.Cap("label")[0:level], level, yac.hand[hash], hash)
|
||||
m.Log("debug", nil, "%s/%d %s(%d):", m.Cap("label")[0:level], level, yac.hand[hash], hash)
|
||||
level = m.Capi("level", -1)
|
||||
return word, line
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user