aboutsummaryrefslogtreecommitdiff
path: root/kernel/test/kshell/io.c
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /kernel/test/kshell/io.c
Created student weenix repository
Diffstat (limited to 'kernel/test/kshell/io.c')
-rw-r--r--kernel/test/kshell/io.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/kernel/test/kshell/io.c b/kernel/test/kshell/io.c
new file mode 100644
index 0000000..65d816d
--- /dev/null
+++ b/kernel/test/kshell/io.c
@@ -0,0 +1,78 @@
+#include "test/kshell/io.h"
+#include "util/debug.h"
+
+#include "priv.h"
+
+#ifndef __VFS__
+
+#include "drivers/chardev.h"
+
+#endif
+
+#ifdef __VFS__
+
+#include "fs/vfs_syscall.h"
+
+#endif
+
+#include "util/printf.h"
+#include "util/string.h"
+
+/*
+ * If VFS is enabled, we can just use the syscalls.
+ *
+ * If VFS is not enabled, then we need to explicitly call the byte
+ * device.
+ */
+
+#ifdef __VFS__
+
+long kshell_write(kshell_t *ksh, const void *buf, size_t nbytes)
+{
+ long retval = do_write(ksh->ksh_out_fd, buf, nbytes);
+ KASSERT(retval < 0 || (size_t)retval == nbytes);
+ return retval;
+}
+
+long kshell_read(kshell_t *ksh, void *buf, size_t nbytes)
+{
+ return do_read(ksh->ksh_in_fd, buf, nbytes);
+}
+
+long kshell_write_all(kshell_t *ksh, void *buf, size_t nbytes)
+{
+ /* See comment in kshell_write */
+ return kshell_write(ksh, buf, nbytes);
+}
+
+#else
+
+long kshell_read(kshell_t *ksh, void *buf, size_t nbytes)
+{
+ return ksh->ksh_cd->cd_ops->read(ksh->ksh_cd, 0, buf, nbytes);
+}
+
+long kshell_write(kshell_t *ksh, const void *buf, size_t nbytes)
+{
+ return ksh->ksh_cd->cd_ops->write(ksh->ksh_cd, 0, buf, nbytes);
+}
+
+#endif
+
+void kprint(kshell_t *ksh, const char *fmt, va_list args)
+{
+ char buf[KSH_BUF_SIZE];
+ size_t count;
+
+ vsnprintf(buf, sizeof(buf), fmt, args);
+ count = strnlen(buf, sizeof(buf));
+ kshell_write(ksh, buf, count);
+}
+
+void kprintf(kshell_t *ksh, const char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ kprint(ksh, fmt, args);
+ va_end(args);
+}