From 2ee0367f96881bf90107e56d103ef9e10054481d Mon Sep 17 00:00:00 2001 From: shylinux Date: Tue, 16 May 2023 09:22:26 +0800 Subject: [PATCH] opt some --- base/cli/readelf.go | 59 ++++---------------------------------- base/ssh/script.go | 8 ------ base/web/space.go | 20 +++++++------ core/chat/macos/desktop.js | 4 +-- core/code/go.go | 7 +++++ core/code/xterm.go | 4 +-- misc/xterm/iterm.go | 15 ++++++---- misc/xterm/xterm.go | 12 ++++---- 8 files changed, 44 insertions(+), 85 deletions(-) diff --git a/base/cli/readelf.go b/base/cli/readelf.go index 3625f567..4e5f98ed 100644 --- a/base/cli/readelf.go +++ b/base/cli/readelf.go @@ -2,7 +2,7 @@ package cli import ( "bytes" - "encoding/binary" + "debug/elf" "strings" ice "shylinux.com/x/icebergs" @@ -10,55 +10,6 @@ import ( kit "shylinux.com/x/toolkits" ) -type elf struct { - EI_CLASS int - EI_DATA int - EI_VERSION int - e_type uint16 - e_machine uint16 - e_version uint32 - e_entry uint64 - e_phoff uint64 - e_shoff uint64 - e_flags uint32 - e_ehsize uint16 - e_phentsize uint16 - e_phnum uint16 - e_shentsize uint16 - e_shnum uint16 - e_shstrndx uint16 -} - -func read2(buf []byte, offset int) (uint16, int) { - return binary.LittleEndian.Uint16(buf[offset : offset+2]), offset + 2 -} -func read4(buf []byte, offset int) (uint32, int) { - return binary.LittleEndian.Uint32(buf[offset : offset+4]), offset + 4 -} -func read8(buf []byte, offset int) (uint64, int) { - return binary.LittleEndian.Uint64(buf[offset : offset+8]), offset + 8 -} -func readelf(buf []byte) (elf elf) { - i := 16 - elf.EI_CLASS = int(buf[4]) - elf.EI_DATA = int(buf[5]) - elf.EI_VERSION = int(buf[6]) - elf.e_type, i = read2(buf, i) - elf.e_machine, i = read2(buf, i) - elf.e_version, i = read4(buf, i) - elf.e_entry, i = read8(buf, i) - elf.e_phoff, i = read8(buf, i) - elf.e_shoff, i = read8(buf, i) - elf.e_flags, i = read4(buf, i) - elf.e_ehsize, i = read2(buf, i) - elf.e_phentsize, i = read2(buf, i) - elf.e_phnum, i = read2(buf, i) - elf.e_shentsize, i = read2(buf, i) - elf.e_shnum, i = read2(buf, i) - elf.e_shstrndx, i = read2(buf, i) - return elf -} - func init() { Index.MergeCommands(ice.Commands{ "readelf": {Name: "readelf path=usr/publish/ice.linux.amd64 auto", Hand: func(m *ice.Message, arg ...string) { @@ -73,13 +24,15 @@ func init() { if m.Warn(e) { return } - if bytes.Equal(buf[:4], []byte{0x7f, 0x45, 0x4c, 0x46}) { - m.Echo("elf %#v", readelf(buf)) - } + kit.If(bytes.Equal(buf[:4], []byte{0x7f, 0x45, 0x4c, 0x46}), func() { + f, _ := elf.Open(arg[0]) + m.Echo("%v", kit.Formats(f)) + }) for i := 0; i < n; i++ { kit.If(i%16 == 0, func() { m.Push("addr", kit.Format("%04x", i)) }) m.Push(kit.Format("%02x", i%16), kit.Format("%02x", buf[i])) } + m.StatusTimeCount() } }}, }) diff --git a/base/ssh/script.go b/base/ssh/script.go index 93fb158b..d3c05d85 100644 --- a/base/ssh/script.go +++ b/base/ssh/script.go @@ -147,14 +147,6 @@ func (f *Frame) Begin(m *ice.Message, arg ...string) { func (f *Frame) Start(m *ice.Message, arg ...string) { m.Optionv(FRAME, f) switch f.source = kit.Select(STDIO, arg, 0); f.source { - case WEBIO: - wio := m.Optionv(WEBIO).(io.ReadWriter) - r, w, _ := os.Pipe() - go func() { io.Copy(w, wio) }() - f.pipe, f.stdin, f.stdout = w, r, wio - kit.If(f.target == nil, func() { f.target = m.Target() }) - m.Optionv(ice.MSG_OPTS, ice.MSG_USERNAME, ice.MSG_USERROLE) - f.scan(m, STDIO, "") case STDIO: r, w, _ := os.Pipe() go func() { io.Copy(w, os.Stdin) }() diff --git a/base/web/space.go b/base/web/space.go index fd4e2f61..e8ffccf4 100644 --- a/base/web/space.go +++ b/base/web/space.go @@ -87,20 +87,24 @@ func _space_handle(m *ice.Message, safe bool, name string, c *websocket.Conn) { } else { // 上行请求 msg.Option(ice.MSG_USERROLE, aaa.VOID) } - if msg.Option("_exec") == "go" { - m.Go(func() { _space_exec(msg, source, target, c) }, strings.Join(kit.Simple(SPACE, name, msg.Detailv()), lex.SP)) - } else { - _space_exec(msg, source, target, c) - } + m.Go(func() { _space_exec(msg, source, target, c) }, strings.Join(kit.Simple(SPACE, name, msg.Detailv()), lex.SP)) + // if msg.Option("_exec") == "go" { + // m.Go(func() { _space_exec(msg, source, target, c) }, strings.Join(kit.Simple(SPACE, name, msg.Detailv()), lex.SP)) + // } else { + // _space_exec(msg, source, target, c) + // } } else { + done := false m.Warn(!mdb.HashSelectDetail(m, next, func(value ice.Map) { switch c := value[mdb.TARGET].(type) { case (*websocket.Conn): // 转发报文 _space_echo(msg, source, target, c) case ice.Handler: // 接收响应 + done = true c(msg) } }), ice.ErrNotFound, next) + kit.If(done, func() { mdb.HashRemove(m, mdb.HASH, next) }) } } } @@ -133,12 +137,11 @@ func _space_echo(m *ice.Message, source, target []string, c *websocket.Conn) { m.Log(tcp.SEND, "%v->%v %v %v", source, target, m.Detailv(), m.FormatsMeta(nil)) } } -func _space_send(m *ice.Message, name string, arg ...string) { +func _space_send(m *ice.Message, name string, arg ...string) (h string) { wait, done := m.Wait(func(msg *ice.Message, arg ...string) { m.Cost(kit.Format("%v->[%v] %v %v", m.Optionv(ice.MSG_SOURCE), name, m.Detailv(), msg.FormatSize())).Copy(msg) }) - h := mdb.HashCreate(m.Spawn(), mdb.TYPE, tcp.SEND, mdb.NAME, kit.Keys(name, m.Target().ID()), mdb.TEXT, kit.Join(arg, lex.SP), kit.Dict(mdb.TARGET, done)) - defer mdb.HashRemove(m, mdb.HASH, h) + h = mdb.HashCreate(m.Spawn(), mdb.TYPE, tcp.SEND, mdb.NAME, kit.Keys(name, m.Target().ID()), mdb.TEXT, kit.Join(arg, lex.SP), kit.Dict(mdb.TARGET, done)) if target := kit.Split(name, nfs.PT, nfs.PT); mdb.HashSelectDetail(m, target[0], func(value ice.Map) { if c, ok := value[mdb.TARGET].(*websocket.Conn); !m.Warn(!ok, ice.ErrNotValid, mdb.TARGET) { kit.For(m.Optionv(ice.MSG_OPTS), func(k string) { m.Optionv(k, m.Optionv(k)) }) @@ -149,6 +152,7 @@ func _space_send(m *ice.Message, name string, arg ...string) { } else { m.Warn(kit.IndexOf([]string{ice.OPS, ice.DEV}, target[0]) == -1, ice.ErrNotFound, name) } + return } const ( diff --git a/core/chat/macos/desktop.js b/core/chat/macos/desktop.js index 4a954fb1..a0e92ff5 100644 --- a/core/chat/macos/desktop.js +++ b/core/chat/macos/desktop.js @@ -71,8 +71,8 @@ Volcanos(chat.ONIMPORT, { sub.onexport.record = function(sub, value, key, item) { can.onimport._window(can, item) } sub.onexport.marginTop = function() { return 25 } sub.onexport.actionHeight = function(sub) { return can.page.ClassList.has(can, sub._target, html.OUTPUT)? 0: html.ACTION_HEIGHT+20 }, - sub.onmotion.resize(can, sub._target, function(height, width) { sub.onimport.size(sub, height, width) }, 25) - sub.onimport.size(sub, item.height, item.width, true), can.onmotion.move(can, sub._target, {"z-index": 10, top: item.top, left: item.left}) + can.onmotion.move(can, sub._target, {"z-index": 10, top: item.top, left: item.left}), sub.onmotion.resize(can, sub._target, function(height, width) { sub.onimport.size(sub, height, width) }, 25) + sub.onimport.size(sub, item.height, item.width, true) sub._target.onclick = function(event) { can.page.Select(can, sub._target.parentNode, html.FIELDSET, function(target) { can.page.style(can, target, "z-index", target == sub._target? "10": "9") }) } }, can.ui.desktop) }, diff --git a/core/code/go.go b/core/code/go.go index 0ce04c96..5c44913a 100644 --- a/core/code/go.go +++ b/core/code/go.go @@ -99,6 +99,13 @@ const GO = "go" func init() { Index.MergeCommands(ice.Commands{ + "godoc": {Name: "godoc key auto", Hand: func(m *ice.Message, arg ...string) { + if len(arg) == 0 { + m.Cmdy(cli.SYSTEM, "go", "list", "std") + } else { + m.Cmdy(cli.SYSTEM, "go", "doc", arg) + } + }}, GO: {Name: "go path auto", Help: "后端编程", Actions: ice.MergeActions(ice.Actions{ mdb.RENDER: {Hand: func(m *ice.Message, arg ...string) { if arg[1] == "main.go" { diff --git a/core/code/xterm.go b/core/code/xterm.go index b6d93a90..805dac68 100644 --- a/core/code/xterm.go +++ b/core/code/xterm.go @@ -40,7 +40,7 @@ func _xterm_get(m *ice.Message, h string) xterm.XTerm { if cmd := text[0]; text[0] != "" { m.Go(func() { m.Sleep30ms() - term.Write(cmd + lex.NL) + term.Writeln(cmd) }) } text = text[1:] @@ -113,7 +113,7 @@ func init() { if b, e := base64.StdEncoding.DecodeString(strings.Join(arg, "")); !m.Warn(e) { // m.Debug("what ---%o--- ---[%v]---", b, string(b)) m.Debug("what ---%o---", b) - _xterm_get(m, "").Write(string(b)) + _xterm_get(m, "").Write(b) } }}, web.OUTPUT: {Help: "全屏", Hand: func(m *ice.Message, arg ...string) { diff --git a/misc/xterm/iterm.go b/misc/xterm/iterm.go index 74f6c4f7..176bec42 100644 --- a/misc/xterm/iterm.go +++ b/misc/xterm/iterm.go @@ -35,25 +35,28 @@ type iterm struct { *idata } -func newiterm(m *ice.Message) (XTerm, error) { +func NewITerm(m *ice.Message) (XTerm, error) { r, w, e := os.Pipe() return &iterm{m: m, r: r, w: w, idata: &idata{cmds: kit.Simple( kit.SortedKey(ice.Info.Index), m.Cmd(ctx.COMMAND).Appendv(ctx.INDEX), m.Cmd(nfs.DIR, "/bin", mdb.NAME).Appendv(mdb.NAME), + strings.Split(m.Cmdx(nfs.CAT, kit.HomePath(".bash_history")), lex.NL), )}}, e } func (s iterm) Setsize(rows, cols string) error { s.w.Write([]byte(s.prompt())) return nil } -func (s iterm) Writeln(data string, arg ...ice.Any) { s.Write(kit.Format(data, arg...) + lex.NL) } -func (s iterm) Write(data string) (int, error) { +func (s iterm) Writeln(str string, arg ...ice.Any) { + s.Write([]byte(kit.Format(str, arg...) + lex.NL)) +} +func (s iterm) Write(buf []byte) (int, error) { if s.pipe != nil { - return s.pipe.Write([]byte(data)) + return s.pipe.Write(buf) } res, ctrl := "", "" - for _, c := range data { + for _, c := range string(buf) { switch c := string(c); c { case SOH: // Ctrl+A res += s.repeat(s.arg) @@ -176,7 +179,7 @@ func (s iterm) Write(data string) (int, error) { } } s.w.Write([]byte(res)) - return len(data), nil + return len(buf), nil } func (s iterm) Read(buf []byte) (int, error) { return s.r.Read(buf) diff --git a/misc/xterm/xterm.go b/misc/xterm/xterm.go index 3b26c267..1aa3f112 100644 --- a/misc/xterm/xterm.go +++ b/misc/xterm/xterm.go @@ -21,7 +21,7 @@ type Winsize struct { type XTerm interface { Setsize(rows, cols string) error Writeln(data string, arg ...ice.Any) - Write(data string) (int, error) + Write(buf []byte) (int, error) Read(buf []byte) (int, error) Close() error } @@ -33,14 +33,14 @@ type xterm struct { func (s xterm) Setsize(rows, cols string) error { return Setsize(s.File, &Winsize{Rows: uint16(kit.Int(rows)), Cols: uint16(kit.Int(cols))}) } -func (s xterm) Writeln(data string, arg ...ice.Any) { s.Write(kit.Format(data, arg...) + lex.NL) } -func (s xterm) Write(data string) (int, error) { return s.File.Write([]byte(data)) } -func (s xterm) Read(buf []byte) (int, error) { return s.File.Read(buf) } -func (s xterm) Close() error { return s.Cmd.Process.Kill() } +func (s xterm) Writeln(str string, arg ...ice.Any) { s.Write([]byte(kit.Format(str, arg...) + lex.NL)) } +func (s xterm) Write(buf []byte) (int, error) { return s.File.Write(buf) } +func (s xterm) Read(buf []byte) (int, error) { return s.File.Read(buf) } +func (s xterm) Close() error { return s.Cmd.Process.Kill() } func Command(m *ice.Message, dir string, cli string, arg ...string) (XTerm, error) { if path.Base(cli) == "ish" { - return newiterm(m) + return NewITerm(m) } cmd := exec.Command(cli, arg...) cmd.Dir = nfs.MkdirAll(m, kit.Path(dir))