From 21e163b776f36771dacdec58b5b012663cb1d2c0 Mon Sep 17 00:00:00 2001 From: Yi-Yo Chiang Date: Thu, 16 May 2024 18:31:41 +0800 Subject: [PATCH] microcom: Don't crash if failed to open paste file * When running the "paste" command and failed to open the paste file (such as file not found or permission error), don't crash the entire microcom program. Instead print the error message and give the user a chance to fix the problem. * If "paste" command is cancelled by "ESC" or an empty file name, clear the hanging "Filename:" prompt before returning to the main loop. --- toys/net/microcom.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/toys/net/microcom.c b/toys/net/microcom.c index 1ee9204e..49ccde0f 100644 --- a/toys/net/microcom.c +++ b/toys/net/microcom.c @@ -54,7 +54,8 @@ static void handle_esc(void) while (1) { xprintf("\r\e[2K\e[1mFilename: \e[0m%s", toybuf); if (read(0, &input, 1) <= 0 || input == CTRL('[')) { - return; + len = 0; + break; } if (input == '\r') break; if (input == 0x7f && len > 0) toybuf[--len] = 0; @@ -62,10 +63,16 @@ static void handle_esc(void) else if (input >= ' ' && input <= 0x7f && len < sizeof(toybuf)) toybuf[len++] = input; } + xputsn("\r\e[2K"); toybuf[len] = 0; if (!len) return; + if ((fd = xopen(toybuf, O_RDONLY | WARN_ONLY)) < 0) { + // xopen() warning message ends with a LF without CR, so manually print a + // CR here to move the cursor back to the front. + fputc('\r', stderr); + return; + } filename = xstrdup(toybuf); - fd = xopen(filename, O_RDONLY); size = fdlength(fd); // The alternative would be to just feed this fd into the usual loop, // so we're reading back these characters if they're being echoed, but -- 2.39.2