aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/test/kshell
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/include/test/kshell
Created student weenix repository
Diffstat (limited to 'kernel/include/test/kshell')
-rw-r--r--kernel/include/test/kshell/io.h61
-rw-r--r--kernel/include/test/kshell/kshell.h52
2 files changed, 113 insertions, 0 deletions
diff --git a/kernel/include/test/kshell/io.h b/kernel/include/test/kshell/io.h
new file mode 100644
index 0000000..72ac92a
--- /dev/null
+++ b/kernel/include/test/kshell/io.h
@@ -0,0 +1,61 @@
+#pragma once
+
+#include "test/kshell/kshell.h"
+
+/*
+ * When writing a kernel shell command, make sure to use the following
+ * I/O functions.
+ *
+ * Before VFS is not enabled, the kernel shell will use functions from
+ * chardev.h to get a pointer the the chardev_t struct for the TTY.
+ *
+ * When VFS is enabled, the kernel shell will use the functions from
+ * vfs_syscall.h to open and close the TTY and perform I/O operations
+ * on the TTY.
+ *
+ * If you use the functions below, this process will be completely
+ * transparent.
+ */
+
+/**
+ * Replacement for do_write.
+ *
+ * @param ksh the kshell to write to
+ * @param buf the buffer to write out to the kshell
+ * @param nbytes the maximum number of bytes to write
+ * @return number of bytes written on sucess and <0 on error
+ */
+long kshell_write(kshell_t *ksh, const void *buf, size_t nbytes);
+
+/**
+ * Replacement for do_read.
+ *
+ * @param ksh the kshell to read from
+ * @param buf the buffer to store data read from the kshell
+ * @param nbytes the maximum number of bytes to read
+ * @param number of bytes read on success and <0 on error
+ */
+long kshell_read(kshell_t *ksh, void *buf, size_t nbytes);
+
+/* Unless an error occurs, guarantees that all of buf will be
+ * written */
+/**
+ * Writes a specified number of bytes from a buffer to the
+ * kshell. Unlike kshell_write, this function guarantees it will write
+ * out the desired number of bytes.
+ *
+ * @param ksh the kshell to write to
+ * @param buf the buffer to write out to the kshell
+ * @param nbytes the number of bytes to write
+ * @return number of bytes written on success and <0 on error
+ */
+long kshell_write_all(kshell_t *ksh, void *buf, size_t nbytes);
+
+/* Replacement for printf */
+/**
+ * Write output to a kshell according to a format string.
+ *
+ * @param ksh the kshell to write to
+ * @param fmt the format string
+ */
+void kprintf(kshell_t *ksh, const char *fmt, ...);
diff --git a/kernel/include/test/kshell/kshell.h b/kernel/include/test/kshell/kshell.h
new file mode 100644
index 0000000..9baf4f5
--- /dev/null
+++ b/kernel/include/test/kshell/kshell.h
@@ -0,0 +1,52 @@
+#pragma once
+
+#include "types.h"
+
+typedef struct kshell kshell_t;
+
+typedef long (*kshell_cmd_func_t)(kshell_t *, size_t argc, char **argv);
+
+/**
+ * Process init function for a new kshell.
+ */
+void *kshell_proc_run(long tty, void *arg2);
+
+/**
+ * Adds a command to the global command table for kernel shells.
+ *
+ * Note: When writing commands for the kernel shell, you _MUST_ use
+ * the I/O functions from kshell_io.h instead of normal I/O
+ * functions. See comment in kshell_io.h for more information.
+ *
+ * @param name the name of the command. Typing this name into the
+ * shell will execute the command.
+ * @param command the command to add to the shell
+ * @param desc a description of the command. This is what will be
+ * printed by the command 'help <command>'
+ */
+void kshell_add_command(const char *name, kshell_cmd_func_t command,
+ const char *desc);
+
+/**
+ * Allocates and initializes a kshell.
+ *
+ * @param bd the byte device the kshell will read from and write to
+ * @return a kshell
+ */
+kshell_t *kshell_create(uint8_t ttyid);
+
+/**
+ * Destroys a kshell.
+ *
+ * @param ksh the kshell to destroy
+ */
+void kshell_destroy(kshell_t *ksh);
+
+/**
+ * Reads from the kshell's byte device and attempts to execute a
+ * command.
+ *
+ * @param ksh the kshell to execute commands with
+ * @return the number of bytes read
+ */
+long kshell_execute_next(kshell_t *ksh);