Skip to content

Commit 96bdba9

Browse files
authored
plan9/srv9p/example/netshell: clean up a few misc things (#126)
1 parent 3835d56 commit 96bdba9

File tree

1 file changed

+18
-14
lines changed
  • plan9/srv9p/example/netshell

1 file changed

+18
-14
lines changed

plan9/srv9p/example/netshell/main.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"os/exec"
2626
"runtime"
27+
"strconv"
2728
"strings"
2829
"sync"
2930
"syscall"
@@ -51,7 +52,7 @@ func init() {
5152

5253
func main() {
5354
log.SetFlags(0)
54-
log.SetPrefix("consfs: ")
55+
log.SetPrefix("netshell: ")
5556

5657
flag.Usage = usage
5758
flag.Parse()
@@ -183,7 +184,8 @@ func (r rendez) Sleep(ctx context.Context) {
183184
}
184185
}
185186

186-
// Wakeup wakes up one call to r.Sleep.
187+
// Wakeup wakes up one call to r.Sleep or returns immediately if
188+
// there are no calls sleeping.
187189
func (r rendez) Wakeup() {
188190
select {
189191
default:
@@ -196,7 +198,8 @@ const (
196198
maxBuf = 8192 // max read/write buffer
197199
)
198200

199-
// A console
201+
// A console implements a simple terminal-like device reading from
202+
// r and writing to w.
200203
type console struct {
201204
r io.Reader // reader connected to network
202205
w io.Writer // writer connected to network
@@ -218,8 +221,8 @@ type console struct {
218221
// newConsole creates a new console reading from r and writing to w.
219222
func newConsole(r io.Reader, w io.Writer) *console {
220223
c := &console{
221-
r: os.Stdin,
222-
w: os.Stdout,
224+
r: r,
225+
w: w,
223226
notefd: -1,
224227
readEmpty: newRendez(),
225228
readFull: newRendez(),
@@ -236,31 +239,30 @@ func newConsole(r io.Reader, w io.Writer) *console {
236239
// consctlClose handles a close of /dev/consctl; it turns raw mode off.
237240
func (c *console) consctlClose() {
238241
c.mu.Lock()
242+
defer c.mu.Unlock()
239243
c.raw = false
240-
c.mu.Unlock()
241244
}
242245

243246
// consctlWrite handles writes to /dev/consctl.
244247
func (c *console) consctlWrite(data []byte) (int, error) {
245248
c.mu.Lock()
246249
defer c.mu.Unlock()
247250

248-
s := string(data)
251+
s := strings.TrimSpace(string(data))
249252
switch s {
250-
case "rawon", "rawon\n": // turn raw mode on
253+
case "rawon": // turn raw mode on
251254
c.raw = true
252255
c.read = append(c.read, c.line...)
253256
c.line = nil
254257
return len(data), nil
255258

256-
case "rawoff", "rawoff\n": // turn raw mode off
259+
case "rawoff": // turn raw mode off
257260
c.raw = false
258261
return len(data), nil
259262
}
260263

261-
if strings.HasPrefix(s, "notepg") { // notepg n means send interrupt notes to /proc/n/notepg
262-
s = strings.TrimSpace(strings.TrimPrefix(s, "notepg"))
263-
if s == "" {
264+
if s, ok := strings.CutPrefix(s, "notepg"); ok { // notepg n means send interrupt notes to /proc/n/notepg
265+
if _, err := strconv.ParseUint(s, 10, 64); err != nil {
264266
return 0, fmt.Errorf("bad notepg")
265267
}
266268
fd, err := syscall.Open("/proc/"+s+"/notepg", syscall.O_WRONLY)
@@ -280,7 +282,9 @@ func (c *console) readNet(ctx context.Context) {
280282
buf := make([]byte, 4096)
281283
for context.Cause(ctx) == nil {
282284
n, err := c.r.Read(buf)
283-
c.consType(ctx, buf[:n], err)
285+
if n > 0 {
286+
c.consType(ctx, buf[:n])
287+
}
284288
if err != nil {
285289
return
286290
}
@@ -297,7 +301,7 @@ const (
297301
)
298302

299303
// consType processes typed characters, adding them to the console.
300-
func (c *console) consType(ctx context.Context, data []byte, err error) {
304+
func (c *console) consType(ctx context.Context, data []byte) {
301305
c.mu.Lock()
302306
defer c.mu.Unlock()
303307

0 commit comments

Comments
 (0)