From ff000f5c7fb0dff3642d7e1cc9bd92e1b2d66aaa Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 22:34:31 +0000 Subject: [PATCH] IND$FILE member names, device size fallback, generate errors - ParseIndFile keeps a "(" within a name, so 'USER.DATA(MEM)' and USER.DATA(MEM) keep their member, and RECFM(V)-style options stay whole; a CMS "(" (alone, at a word's start, or "fm(ASCII") still begins the options. - makeDeviceInfo: a client that does not answer the Query gets its model's size (43x80 for a -4), not always 24x80; IBM-DYNAMIC with replies but no Usable Area is 24x80, not 0x0. - ReadRecord returns a read error inside a subnegotiation rather than reading on. - codepage/generate's read returns its errors, naming the line, instead of panicking. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr --- graphics_test.go | 48 +++++++++++++++++++ indfile.go | 53 +++++++++++++++------ indfile_test.go | 11 ++++- internal/codepage/generate/generate.go | 4 +- internal/codepage/generate/generate_test.go | 24 ++++++++++ telnet.go | 15 +++++- 6 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 internal/codepage/generate/generate_test.go diff --git a/graphics_test.go b/graphics_test.go index 2a12ebe..eea95f5 100644 --- a/graphics_test.go +++ b/graphics_test.go @@ -458,3 +458,51 @@ func TestLoadPSPlane(t *testing.T) { t.Error("plane 3 taken") } } + +// TestDeviceInfoSize: with no Usable Area reply the size is the model's +// (a -4 is 43x80), else 24x80, never 0x0 (IBM-DYNAMIC answering without +// one). +func TestDeviceInfoSize(t *testing.T) { + for _, c := range []struct { + termtype string + reply []byte // after the Query, IAC EOR added + rows, cols int + }{ + {"IBM-3278-4-E", []byte{0x7d}, 43, 80}, // Enter, not query replies + {"IBM-DYNAMIC", []byte{0x88, 0, 5, 0x81, 0x80, 0x80}, 24, 80}, // a Summary alone + } { + srv, cli := net.Pipe() + done := make(chan DevInfo, 1) + go func() { + d, err := makeDeviceInfo(srv, c.termtype) + if err != nil { + t.Error(err) + } + done <- d + }() + go func() { + buf := make([]byte, 64) + cli.Read(buf) + cli.Read(buf) + cli.Write(append(c.reply, 0xff, 0xef)) + for { + if _, err := cli.Read(buf); err != nil { + return + } + } + }() + select { + case d := <-done: + if d == nil { + break + } + if r, k := d.AltDimensions(); r != c.rows || k != c.cols { + t.Errorf("%s: %dx%d, want %dx%d", c.termtype, r, k, c.rows, c.cols) + } + case <-time.After(5 * time.Second): + t.Fatalf("%s: no answer", c.termtype) + } + srv.Close() + cli.Close() + } +} diff --git a/indfile.go b/indfile.go index 399338e..30225b8 100644 --- a/indfile.go +++ b/indfile.go @@ -40,9 +40,10 @@ type IndFile struct { } // ParseIndFile parses "IND$FILE GET|PUT name [(] options" (TSO puts the -// options after the name; VM/CMS after a "("), case aside. +// options after the name; VM/CMS after a "("), case aside. A "(" within +// a name (a TSO member, 'USER.DATA(MEM)') stays in it. func ParseIndFile(s string) (*IndFile, bool) { - f := strings.Fields(strings.ReplaceAll(s, "(", " ( ")) + f := strings.Fields(s) if len(f) < 3 || !strings.EqualFold(f[0], "IND$FILE") { return nil, false } @@ -54,31 +55,49 @@ func ParseIndFile(s string) (*IndFile, bool) { default: return nil, false } + var name, opts []string i := 2 - var name []string - for ; i < len(f) && f[i] != "("; i++ { - u := strings.ToUpper(f[i]) - if len(name) > 0 && isIndOption(u) { + for ; i < len(f); i++ { + tok := f[i] + if strings.HasPrefix(tok, "(") { + opts = append(opts, tok[1:]) // CMS: the options from here + i++ + break + } + if len(name) > 0 && isIndOption(strings.ToUpper(tok)) { break // TSO: options after the name } - name = append(name, f[i]) + // CMS written close up, "fm(ASCII": the "(" begins options when + // one follows it (a TSO member, "DATA(MEM)", does not) + if j := strings.IndexByte(tok, '('); j > 0 && !strings.HasPrefix(tok, "'") && + isIndOption(strings.ToUpper(strings.TrimSuffix(tok[j+1:], ")"))) { + name = append(name, tok[:j]) + opts = append(opts, tok[j+1:]) + i++ + break + } + name = append(name, tok) } if len(name) == 0 { return nil, false } ind.Name = strings.Join(name, " ") - for ; i < len(f); i++ { - if f[i] == "(" || f[i] == ")" { + for _, o := range append(opts, f[i:]...) { + // "(ASCII", "CRLF)": the CMS parentheses off; "RECFM(V)" kept whole + u := strings.ToUpper(strings.TrimPrefix(o, "(")) + if !strings.Contains(u, "(") { + u = strings.TrimSuffix(u, ")") + } + if u == "" { continue } - u := strings.ToUpper(f[i]) ind.Options = append(ind.Options, u) - switch { - case u == "ASCII": + switch u { + case "ASCII": ind.ASCII = true - case u == "CRLF": + case "CRLF": ind.CRLF = true - case u == "APPEND": + case "APPEND": ind.Append = true } } @@ -185,7 +204,11 @@ func ReadRecord(conn net.Conn, timeout time.Duration) ([]byte, error) { return nil, err } if x == 0xff { - if y, _ := r.ReadByte(); y == 0xf0 { + y, err := r.ReadByte() + if err != nil { + return nil, err + } + if y == 0xf0 { break } } diff --git a/indfile_test.go b/indfile_test.go index 89c5833..27c6a0f 100644 --- a/indfile_test.go +++ b/indfile_test.go @@ -6,6 +6,7 @@ package go3270 import ( "net" + "strings" "testing" "time" ) @@ -23,7 +24,10 @@ func TestParseIndFile(t *testing.T) { {"IND$FILE PUT ft1 text (ASCII CRLF", true, false, "ft1 text", true, true}, {"ind$file get readme.txt", true, true, "readme.txt", false, false}, {"IND$FILE GET my file.zip (", true, true, "my file.zip", false, false}, - {"IND$FILE GET 'USER.DATA(MEM)' ASCII", true, true, "'USER.DATA", true, false}, + {"IND$FILE GET 'USER.DATA(MEM)' ASCII", true, true, "'USER.DATA(MEM)'", true, false}, + {"IND$FILE PUT USER.DATA(MEM) ASCII CRLF", true, false, "USER.DATA(MEM)", true, true}, + {"IND$FILE PUT 'X.LIB(A)' RECFM(V) LRECL(80)", true, false, "'X.LIB(A)'", false, false}, + {"IND$FILE PUT ft1 text a(ASCII CRLF", true, false, "ft1 text a", true, true}, {"IND$FILE DELETE x", false, false, "", false, false}, {"IND$FILE PUT", false, false, "", false, false}, {"1", false, false, "", false, false}, @@ -37,10 +41,13 @@ func TestParseIndFile(t *testing.T) { if !ok { continue } - if f.Get != c.get || (c.name != "'USER.DATA" && f.Name != c.name) || f.ASCII != c.ascii || f.CRLF != c.crlf { + if f.Get != c.get || f.Name != c.name || f.ASCII != c.ascii || f.CRLF != c.crlf { t.Errorf("%q: %+v", c.in, f) } } + if f, _ := ParseIndFile("IND$FILE PUT 'X.LIB(A)' RECFM(V) LRECL(80) (APPEND)"); strings.Join(f.Options, " ") != "RECFM(V) LRECL(80) APPEND" || !f.Append { + t.Errorf("options %q", f.Options) + } } // TestDFTBufferSize: the smaller of the DDM reply's LIMIN and LIMOUT; 0 without diff --git a/internal/codepage/generate/generate.go b/internal/codepage/generate/generate.go index 81c914a..44dbd09 100644 --- a/internal/codepage/generate/generate.go +++ b/internal/codepage/generate/generate.go @@ -167,7 +167,7 @@ func read(input string) (map[int]int, error) { codepoint, ebcdic, err := parseUcmLine(line) if err != nil { - panic(err) + return nil, fmt.Errorf("%s: %q: %w", input, line, err) } if _, ok := u2e[codepoint]; ok { @@ -178,7 +178,7 @@ func read(input string) (map[int]int, error) { } if err := s.Err(); err != nil { - panic(err) + return nil, fmt.Errorf("%s: %w", input, err) } return u2e, nil diff --git a/internal/codepage/generate/generate_test.go b/internal/codepage/generate/generate_test.go new file mode 100644 index 0000000..cb1c136 --- /dev/null +++ b/internal/codepage/generate/generate_test.go @@ -0,0 +1,24 @@ +package main + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// TestReadBadLine: a CHARMAP line read cannot is an error naming it, not a +// panic; a good file's mappings are read, the non-roundtrip ones left out. +func TestReadBadLine(t *testing.T) { + dir := t.TempDir() + good := filepath.Join(dir, "good.ucm") + os.WriteFile(good, []byte("CHARMAP\n \\xC1 |0\n \\x41 |1\nEND CHARMAP\n"), 0644) + if m, err := read(good); err != nil || len(m) != 1 || m[0x41] != 0xc1 { + t.Errorf("good: %v %v", m, err) + } + bad := filepath.Join(dir, "bad.ucm") + os.WriteFile(bad, []byte("CHARMAP\nnonsense\nEND CHARMAP\n"), 0644) + if _, err := read(bad); err == nil || !strings.Contains(err.Error(), "nonsense") { + t.Errorf("bad: %v", err) + } +} diff --git a/telnet.go b/telnet.go index 1170d38..14ec25e 100644 --- a/telnet.go +++ b/telnet.go @@ -384,6 +384,14 @@ func makeDeviceInfo(conn net.Conn, termtype string) (DevInfo, error) { return nil, err } + // the size when no Usable Area says: the model's, else 24x80 + size := func() (int, int) { + if rows == 0 || cols == 0 { + return 24, 80 + } + return rows, cols + } + // We'll use a timeout in case the client doesn't support/reply to our // structured field query. var aid [1]byte @@ -394,7 +402,8 @@ func makeDeviceInfo(conn net.Conn, termtype string) (DevInfo, error) { // Timeout. In this case, we'll assume it's because the client didn't // reply to our query command. In that case, we'll return whatever // we're already assuming. - return &deviceInfo{rows: 24, cols: 80, termtype: termtype}, nil + rows, cols = size() + return &deviceInfo{rows: rows, cols: cols, termtype: termtype}, nil } else if err != nil { return nil, err } @@ -404,7 +413,8 @@ func makeDeviceInfo(conn net.Conn, termtype string) (DevInfo, error) { // structured field queries? If we're not getting the kind of reply // we're expecting, we'll return whatever we're already assuming. flushConnection(conn, 50*time.Millisecond) - return &deviceInfo{rows: 24, cols: 80, termtype: termtype}, nil + rows, cols = size() + return &deviceInfo{rows: rows, cols: cols, termtype: termtype}, nil } // There are an arbitrary number of query reply structured fields. We @@ -487,6 +497,7 @@ func makeDeviceInfo(conn net.Conn, termtype string) (DevInfo, error) { codepage = nil } + rows, cols = size() // IBM-DYNAMIC with no Usable Area too return &deviceInfo{rows: rows, cols: cols, termtype: termtype, codepage: codepage, hasGE: hasGE, replies: replies, cellW: cellW, cellH: cellH, symbols: symbols}, nil