]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/*: Turn `\n` into `\r\n`
authorThassilo Schulze <thassilo@unikraft.io>
Fri, 28 Jun 2024 14:11:06 +0000 (16:11 +0200)
committerUnikraft Bot <monkey@unikraft.io>
Fri, 13 Sep 2024 20:51:57 +0000 (20:51 +0000)
See the TODOs in the diff

Signed-off-by: Thassilo Schulze <thassilo@unikraft.io>
Reviewed-by: Michalis Pappas <michalis@unikraft.io>
Reviewed-by: Simon Kuenzer <simon@unikraft.io>
Approved-by: Simon Kuenzer <simon@unikraft.io>
GitHub-Closes: #1479

lib/devfs/stdout.c
lib/nolibc/scanf.c
lib/nolibc/stdio.c
lib/posix-tty/serial.c
lib/syscall_shim/uk_prsyscall.c
lib/ukdebug/print.c

index 600a04dfaab90f6467a816f71fa0f97ec08ff8b2..091f45952d64fb0e0e07a2cae76579a5bdc66dfa 100644 (file)
 #ifdef CONFIG_LIBDEVFS_DEV_STDOUT
 #define DEV_STDOUT_NAME "stdout"
 
+/* TODO: Some consoles require both a newline and a carriage return to
+ * go to the start of the next line. This kind of behavior should be in
+ * a single place in posix-tty. We keep this workaround until we have feature
+ * in posix-tty that handles newline characters correctly.
+ */
+static inline __ssz _console_out(const char *buf, __sz len)
+{
+       const char *next_nl = NULL;
+       __sz l = len;
+       __sz off = 0;
+       __ssz rc = 0;
+
+       if (unlikely(!len))
+               return 0;
+       if (unlikely(!buf))
+               return -EINVAL;
+
+       while (l > 0) {
+               next_nl = memchr(buf, '\n', l);
+               if (next_nl) {
+                       off = next_nl - buf;
+                       if ((rc = uk_console_out(buf, off)) < 0)
+                               return rc;
+                       if ((rc = uk_console_out("\r\n", 2)) < 0)
+                               return rc;
+                       buf = next_nl + 1;
+                       l -= off + 1;
+               } else {
+                       if ((rc = uk_console_out(buf, l)) < 0)
+                               return rc;
+                       break;
+               }
+       }
+
+       return len;
+}
+
 static int __write_fn(void *dst __unused, void *src, size_t *cnt)
 {
-       int ret = uk_console_out(src, *cnt);
+       int ret = _console_out(src, *cnt);
 
        if (ret < 0)
                /* TODO: remove -1 when vfscore switches to negative
index 0ccc5db7f8fb20ced6bde2db1418373d377f8397..c6077d86e0b2227800a25c807ac8b523cda99289 100644 (file)
 
 #if CONFIG_LIBUKCONSOLE
 #include <uk/console.h>
+#include <string.h>
+
+/* TODO: Some consoles require both a newline and a carriage return to
+ * go to the start of the next line. This kind of behavior should be in
+ * a single place in posix-tty. We keep this workaround until we have feature
+ * in posix-tty that handles newline characters correctly.
+ */
+static inline __ssz _console_out(const char *buf, __sz len)
+{
+       const char *next_nl = NULL;
+       __sz l = len;
+       __sz off = 0;
+       __ssz rc = 0;
+
+       if (unlikely(!len))
+               return 0;
+       if (unlikely(!buf))
+               return -EINVAL;
+
+       while (l > 0) {
+               next_nl = memchr(buf, '\n', l);
+               if (next_nl) {
+                       off = next_nl - buf;
+                       if ((rc = uk_console_out(buf, off)) < 0)
+                               return rc;
+                       if ((rc = uk_console_out("\r\n", 2)) < 0)
+                               return rc;
+                       buf = next_nl + 1;
+                       l -= off + 1;
+               } else {
+                       if ((rc = uk_console_out(buf, l)) < 0)
+                               return rc;
+                       break;
+               }
+       }
+
+       return len;
+}
 #endif /* CONFIG_LIBUKCONSOLE */
 
 static int
@@ -45,14 +83,14 @@ uk_scanf(void *buffer __maybe_unused, size_t *cnt)
                        /* DELETE control character */
                        if (buf - 1 != buffer) {
                                /* If this is not the first byte */
-                               uk_console_out("\b \b", 3);
+                               _console_out("\b \b", 3);
                                buf -= 1;
                                if (bytes_total > 0)
                                        bytes_total--;
                        }
                        buf -= 1;
                } else {
-                       uk_console_out(buf - bytes_read, bytes_read);
+                       _console_out(buf - bytes_read, bytes_read);
                        bytes_total += bytes_read;
                }
 
index 8bf69c369677a5c2ec8159e195d776a6ab1e641d..ab1f89169663761d465aeff316e2a2df2d2490ad 100644 (file)
 
 #include <uk/essentials.h>
 #include <uk/arch/lcpu.h>
+
 #if CONFIG_LIBUKCONSOLE
 #include <uk/console.h>
+
+/* TODO: Some consoles require both a newline and a carriage return to
+ * go to the start of the next line. This kind of behavior should be in
+ * a single place in posix-tty. We keep this workaround until we have feature
+ * in posix-tty that handles newline characters correctly.
+ */
+static inline __ssz _console_out(const char *buf, __sz len)
+{
+       const char *next_nl = NULL;
+       __sz l = len;
+       __sz off = 0;
+       __ssz rc = 0;
+
+       if (unlikely(!len))
+               return 0;
+       if (unlikely(!buf))
+               return -EINVAL;
+
+       while (l > 0) {
+               next_nl = memchr(buf, '\n', l);
+               if (next_nl) {
+                       off = next_nl - buf;
+                       if ((rc = uk_console_out(buf, off)) < 0)
+                               return rc;
+                       if ((rc = uk_console_out("\r\n", 2)) < 0)
+                               return rc;
+                       buf = next_nl + 1;
+                       l -= off + 1;
+               } else {
+                       if ((rc = uk_console_out(buf, l)) < 0)
+                               return rc;
+                       break;
+               }
+       }
+
+       return len;
+}
 #endif /* CONFIG_LIBUKCONSOLE */
 
 /* 64 bits + 0-Byte at end */
@@ -451,7 +489,7 @@ int vfprintf(FILE *fp __maybe_unused, const char *fmt __maybe_unused,
         */
        if (fp == stdout || fp == stderr)
 #if CONFIG_LIBUKCONSOLE
-               ret = uk_console_out(buf, ret);
+               ret = _console_out(buf, ret);
 #else /* !CONFIG_LIBUKCONSOLE */
                ret = strlen(buf);
 #endif /* !CONFIG_LIBUKCONSOLE */
@@ -503,7 +541,7 @@ int fputc(int _c __maybe_unused, FILE *fp __maybe_unused)
        unsigned char c = _c;
 
        if (fp == stdout || fp == stderr)
-               ret = uk_console_out((char *)&c, 1);
+               ret = _console_out((char *)&c, 1);
 
        if (ret == 1)
                return _c;
@@ -530,11 +568,11 @@ fputs_internal(const char *restrict s __maybe_unused,
        len = strlen(s);
 
        if (stream == stdout || stream == stderr)
-               ret = uk_console_out(s, len);
+               ret = _console_out(s, len);
        else
                return EOF;
 
-       /* If uk_console_out wasn't able to write all characters, assume
+       /* If _console_out wasn't able to write all characters, assume
         * that an error happened and there is no point in retrying.
         */
        if ((size_t)ret != len)
index 97d2f7f4e68fbad0854fe29920b04c070d16952c..3c0dfac1397e845f4fbda91e15cdcbd85ee231a9 100644 (file)
@@ -50,6 +50,42 @@ static const char SERIAL_TERMIOS_CONTROL_CHARS[KNCCS] = {
        [VWERASE] = 027,
 };
 
+/* TODO: Some consoles require both a newline and a carriage return to
+ * go to the start of the next line. This kind of behavior should be in
+ * a single place in posix-tty. We keep this workaround until we have feature
+ * in posix-tty that handles newline characters correctly.
+ */
+static inline __ssz _console_out(const char *buf, __sz len)
+{
+       const char *next_nl = NULL;
+       __sz l = len;
+       __sz off = 0;
+       __ssz rc = 0;
+
+       if (unlikely(!len))
+               return 0;
+       if (unlikely(!buf))
+               return -EINVAL;
+
+       while (l > 0) {
+               next_nl = memchr(buf, '\n', l);
+               if (next_nl) {
+                       off = next_nl - buf;
+                       if ((rc = uk_console_out(buf, off)) < 0)
+                               return rc;
+                       if ((rc = uk_console_out("\r\n", 2)) < 0)
+                               return rc;
+                       buf = next_nl + 1;
+                       l -= off + 1;
+               } else {
+                       if ((rc = uk_console_out(buf, l)) < 0)
+                               return rc;
+                       break;
+               }
+       }
+
+       return len;
+}
 
 static ssize_t serial_read(const struct uk_file *f,
                           const struct iovec *iov, int iovcnt,
@@ -86,7 +122,7 @@ static ssize_t serial_read(const struct uk_file *f,
                        *last = '\n';
 
                /* Echo the input to the console (NOT stdout!) */
-               uk_console_out(buf, bytes_read);
+               _console_out(buf, bytes_read);
 
                if (*last == '\n')
                        break;
@@ -118,7 +154,7 @@ static ssize_t serial_write(const struct uk_file *f __maybe_unused,
                if (unlikely(!buf && len))
                        return  -EFAULT;
 
-               bytes_written = uk_console_out(buf, len);
+               bytes_written = _console_out(buf, len);
                if (unlikely(bytes_written < 0))
                        return bytes_written;
 
index 8dbe1d46df46f7e52860d86acd0c9326c7348a34..f98ae94c978a8d3717db3d756a00e7789942185a 100644 (file)
@@ -1277,11 +1277,18 @@ static void pr_syscall(struct uk_streambuf *sb, int fmtf,
                break;
        }
 
+       /* TODO: Some consoles require both a newline and a carriage return to
+        * go to the start of the next line. There should be a dedicated TTY
+        * layer to take care of this. Once that layer exists, stop printing
+        * a CRLF sequence if `UK_PRSYSCALL_FMTF_NEWLINE` is set and print a
+        * single newline character instead.
+        */
+
        /* Try to fix the line ending if content got truncated */
        if (uk_streambuf_istruncated(sb)) {
                /* reserve an extra byte for the newline ending if configured */
                __sz needed_len = 3
-                                + ((fmtf & UK_PRSYSCALL_FMTF_NEWLINE) ? 1 : 0);
+                                + ((fmtf & UK_PRSYSCALL_FMTF_NEWLINE) ? 2 : 0);
                if (uk_streambuf_buflen(sb) > needed_len + 1) {
                        sb->seek = uk_streambuf_seek(sb) - needed_len;
                        uk_streambuf_strcpy(sb, "...");
@@ -1289,7 +1296,7 @@ static void pr_syscall(struct uk_streambuf *sb, int fmtf,
        }
 
        if (fmtf & UK_PRSYSCALL_FMTF_NEWLINE)
-               uk_streambuf_strcpy(sb, "\n");
+               uk_streambuf_strcpy(sb, "\r\n");
 }
 
 int uk_snprsyscall(char *buf, __sz maxlen, int fmtf, long syscall_num,
index 9df40ce294fc548b9ae654592927e72d0eb84ef4..48659c182154ff4624e1f9e923adbb152f905dc7 100644 (file)
@@ -51,6 +51,7 @@
 
 #if CONFIG_LIBUKCONSOLE
 #include <uk/console.h>
+#include <errno.h>
 #endif /* CONFIG_LIBUKCONSOLE */
 
 #if CONFIG_LIBUKDEBUG_ANSI_COLOR
@@ -105,11 +106,50 @@ struct _vprint_console {
        int prevlvl;
 };
 
+#if CONFIG_LIBUKCONSOLE
+/* TODO: Some consoles require both a newline and a carriage return to
+ * go to the start of the next line. This kind of behavior should be in
+ * a single place in posix-tty. We keep this workaround until we have feature
+ * in posix-tty that handles newline characters correctly.
+ */
+static inline __ssz _console_out(const char *buf, __sz len)
+{
+       const char *next_nl = NULL;
+       __sz l = len;
+       __sz off = 0;
+       __ssz rc = 0;
+
+       if (unlikely(!len))
+               return 0;
+       if (unlikely(!buf))
+               return -EINVAL;
+
+       while (l > 0) {
+               next_nl = memchr(buf, '\n', l);
+               if (next_nl) {
+                       off = next_nl - buf;
+                       if ((rc = uk_console_out(buf, off)) < 0)
+                               return rc;
+                       if ((rc = uk_console_out("\r\n", 2)) < 0)
+                               return rc;
+                       buf = next_nl + 1;
+                       l -= off + 1;
+               } else {
+                       if ((rc = uk_console_out(buf, l)) < 0)
+                               return rc;
+                       break;
+               }
+       }
+
+       return len;
+}
+#endif /* CONFIG_LIBUKCONSOLE */
+
 /* Console state for kernel output */
 #if CONFIG_LIBUKDEBUG_REDIR_PRINTD || CONFIG_LIBUKDEBUG_PRINTK
 static struct _vprint_console kern  = {
 #if CONFIG_LIBUKCONSOLE
-       .cout = uk_console_out,
+       .cout = _console_out,
 #else
        .cout = NULL,
 #endif /* CONFIG_LIBUKCONSOLE */
@@ -122,7 +162,7 @@ static struct _vprint_console kern  = {
 #if !CONFIG_LIBUKDEBUG_REDIR_PRINTD
 static struct _vprint_console debug = {
 #if CONFIG_LIBUKCONSOLE
-       .cout = uk_console_out,
+       .cout = _console_out,
 #else
        .cout = NULL,
 #endif /* CONFIG_LIBUKCONSOLE */