From 1f1e68751ca8f7a293b8cfb4245390517ed69351 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 16 Oct 2026 23:37:54 +0000 Subject: [PATCH] Local and Linux shells: the session listens before the shell starts (its first prompt was sometimes lost); ZMODEM: escapes in skipped data aren't a cancel Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs --- .../com/neodesktop/dmodem/terminal/Terminal.kt | 16 ++++++++++------ .../com/neodesktop/dmodem/transfer/ZModem.kt | 9 +++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/neodesktop/dmodem/terminal/Terminal.kt b/app/src/main/java/com/neodesktop/dmodem/terminal/Terminal.kt index 0b31bfa..c59d3aa 100644 --- a/app/src/main/java/com/neodesktop/dmodem/terminal/Terminal.kt +++ b/app/src/main/java/com/neodesktop/dmodem/terminal/Terminal.kt @@ -166,13 +166,15 @@ object Terminal { suspend fun openLocal(context: Context): String? { val (cols, rows) = synchronized(session.emulator) { session.emulator.cols to session.emulator.rows } val link = withContext(Dispatchers.IO) { localShell(context).link(cols, rows) } - if (!withContext(Dispatchers.IO) { link.start() }) { - return (link.state.value as? TerminalLink.State.Closed)?.reason ?: "the shell didn't start" - } val n = _sessions.value.count { it.kind == TerminalSession.Kind.LOCAL } + // (the session listening before the shell starts: its first prompt not lost) val s = TerminalSession(link, label = if (n == 0) "Shell" else "Shell ${n + 1}", kind = TerminalSession.Kind.LOCAL) { app.getExternalFilesDir("captures") } + if (!withContext(Dispatchers.IO) { link.start() }) { + s.close() + return (link.state.value as? TerminalLink.State.Closed)?.reason ?: "the shell didn't start" + } s.charset = TermCharset.UTF8 s.keys.backspace = BackspaceMode.DEL s.keys.enter = com.neodesktop.dmodem.terminal.EnterMode.CR @@ -192,17 +194,19 @@ object Terminal { } catch (e: java.io.IOException) { return e.message } - if (!withContext(Dispatchers.IO) { link.start() }) { - return (link.state.value as? TerminalLink.State.Closed)?.reason ?: "Linux didn't start" - } // (the tab: the distribution's name without its version, numbered when there are more) val short = sys.name.replace(Regex("\\s+[0-9(].*$"), "").removeSuffix(" Linux").ifEmpty { sys.name } val n = _sessions.value.count { it.linuxId == id } + // (listening before the shell starts: its first prompt not lost) val s = TerminalSession( link, label = if (n == 0) short else "$short ${n + 1}", kind = TerminalSession.Kind.LINUX, linuxId = id, ) { app.getExternalFilesDir("captures") } + if (!withContext(Dispatchers.IO) { link.start() }) { + s.close() + return (link.state.value as? TerminalLink.State.Closed)?.reason ?: "Linux didn't start" + } s.charset = TermCharset.UTF8 s.keys.backspace = BackspaceMode.DEL s.keys.enter = com.neodesktop.dmodem.terminal.EnterMode.CR diff --git a/app/src/main/java/com/neodesktop/dmodem/transfer/ZModem.kt b/app/src/main/java/com/neodesktop/dmodem/transfer/ZModem.kt index bf6b04f..de0ef59 100644 --- a/app/src/main/java/com/neodesktop/dmodem/transfer/ZModem.kt +++ b/app/src/main/java/com/neodesktop/dmodem/transfer/ZModem.kt @@ -166,8 +166,13 @@ class ZModem( continue } val kind = readByte() and 0x7f - // (the ZDLE before a header is also a CAN: one isn't a cancel) - if (kind == CAN) { cans = 2; continue } + // (the ZDLE before a header is also a CAN: one isn't a cancel; a cancel is + // CANs in a row, and an escape in skipped data, ZDLE then a letter, is none) + if (kind == CAN) { + if (++cans >= 5) throw TransferCancelled("cancelled by the other side") + continue + } + cans = 0 return when (kind) { 'A'.code -> readBinHeader(false) 'C'.code -> readBinHeader(true)