WKern
Loading...
Searching...
No Matches
shell.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include <KShell/filecmds.h>
20#include <KShell/shell.h>
21#include <fileio/fileio.h>
22#include <global.h>
23#include <io/kio.h>
24#include <mem/kmem.h>
25#include <net/virtnet.h>
26#include <utils/util.h>
27#include <wex/stdwex.h>
28
37typedef void (*CommandFunc)(const char *argv[], int argc);
38
42typedef struct {
43 const char *name;
46
48void CmdCmp(const char *argv[], int argc) { Kshcmp(); }
50void CmdHelp(const char *argv[], int argc) { Help(); }
54void CmdTst(const char *argv[], int argc) { Runwex(Execr()); }
55
59void CmdClear(const char *argv[], int argc) { Kcfp(); }
63void CmdLs(const char *argv[], int argc) { Fat16Ls(); }
64
68void CmdUser(const char *argv[], int argc) { Kprintf("\nUsername: %s", name); }
72void CmdRegex(const char *argv[], int argc) { Regexc(); }
76void CmdRecvPack(const char *argv[], int argc) {
77 unsigned char Key = 0;
78 while (!(Key = Kgetkey()) && Key == 'a') {
79 NetStq(iob);
80 }
81}
82
85void CmdShutdown(const char *argv[], int argc) {
86#ifdef VMQEMU
87#include <qemu/shutdown.h>
89#endif
90}
91
98void CmdSetName(const char *argv[], int argc) {
99 if (argc < 2) {
100 Kprintf("Not enough args\nUsage: setname [New Name]");
101 return;
102 }
103 name = argv[1];
104}
105
110 {"cmp", CmdCmp},
111 {"help", CmdHelp},
112 {"tst", CmdTst},
113 {"clear", CmdClear},
114 {"ls", CmdLs},
115 {"read", Readf},
116 {"user", CmdUser},
117 {"mKfile", Mkf},
118 {"rm", Rm},
119 {"write", Writef},
120 {"regex", CmdRegex},
121 {"recvpacK", CmdRecvPack},
122 {"shutdown", CmdShutdown},
123 {"setname", CmdSetName},
124};
125
129#define NUM_COMMANDS (sizeof(commands) / sizeof(CommandEntry))
130
144void Sh() {
145 while (1) {
146 char *cmd = (char *)Kmalloc(128, 8);
147 if (!cmd) {
148 Kprintf("Malloc Failed");
149 Kfree(cmd);
150 continue;
151 }
152
153 Kprintf("\nSH> ");
154 Kgetstr(cmd, 127);
155 Kflush();
156
157 char *argv[8];
158 int argc = Split(cmd, argv, 8);
159
160 if (argc == 0) {
161 Kfree(cmd);
162 continue;
163 }
164
165 int found = 0;
166 for (int i = 0; i < NUM_COMMANDS; i++) {
167 if (Kstrcmp(argv[0], commands[i].name) == 0) {
168 commands[i].func((const char **)argv, argc);
169 found = 1;
170 break;
171 }
172 }
173
174 if (!found) {
175 Kprintf("\nUnKnown Command - Try `help`\n");
176 }
177
178 Kfree(cmd);
179 }
180}
void * Kmalloc(size_t size, size_t align)
Allocate memory.
Definition alloc.c:48
void Kfree(void *ptr)
Free allocated memory.
Definition alloc.c:75
void Readf(const char *argv[], int argc)
Read and display contents of a file from the FAT16 filesystem.
Definition filecmds.c:108
void Rm(const char *argv[], int argc)
Delete a file from the FAT16 filesystem.
Definition filecmds.c:31
void Writef(const char *argv[], int argc)
Write data to a file in the FAT16 filesystem from user input.
Definition filecmds.c:68
void Mkf(const char *argv[], int argc)
Create a new empty file on the FAT16 filesystem.
Definition filecmds.c:48
Definitions and declarations for FAT16 filesystem and ATA I/O.
void Fat16Ls(void)
Lists files in the FAT16 root directory.
Definition ls.c:37
u32 iob
Definition main.c:36
char * name
Definition main.c:31
char Kgetkey()
Gets the next character from the keyboard buffer (blocking).
Definition keyin.c:169
void Kgetstr(char *str, int length)
Reads a line of input from the keyboard (blocking).
Definition keyin.c:186
void Kflush()
Flushes the keyboard controller input buffer.
Definition keyin.c:219
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
void Kcfp()
Clears the screen by filling the video memory with spaces.
Definition printer.c:28
void Regexc()
Interactive regex command.
Definition regexcmd.c:28
CommandEntry commands[]
Array of all available shell commands.
Definition shell.c:109
void CmdRecvPack(const char *argv[], int argc)
Receives network packets from VirtNet (on keypress).
Definition shell.c:76
void CmdHelp(const char *argv[], int argc)
Displays built-in help information.
Definition shell.c:50
void CmdShutdown(const char *argv[], int argc)
Sends shutdown signal when running under QEMU.
Definition shell.c:85
void CmdUser(const char *argv[], int argc)
Prints the current username.
Definition shell.c:68
void Sh()
The main interactive WKern shell loop.
Definition shell.c:144
void CmdLs(const char *argv[], int argc)
Lists FAT16 filesystem contents.
Definition shell.c:63
void CmdCmp(const char *argv[], int argc)
Compares two strings using the internal shell comparison function.
Definition shell.c:48
void CmdClear(const char *argv[], int argc)
Clears the shell screen.
Definition shell.c:59
void(* CommandFunc)(const char *argv[], int argc)
Command function pointer type.
Definition shell.c:37
void CmdTst(const char *argv[], int argc)
Executes a test WEX executable.
Definition shell.c:54
void CmdSetName(const char *argv[], int argc)
Sets the username used in the shell.
Definition shell.c:98
#define NUM_COMMANDS
Number of commands available in the shell.
Definition shell.c:129
void CmdRegex(const char *argv[], int argc)
Runs the regex test utility.
Definition shell.c:72
void Help()
Prints Shell Help.
Definition shellhelp.c:23
void QemuShutdown()
void Runwex(unsigned char *wexexecu)
Automatically checks and executes an executable.
Definition wexent.c:73
unsigned char * Execr()
Definition testexec.c:34
int Split(char *input, char **argv, int max_args)
Split into ARGC.
Definition strings.c:146
void Kshcmp()
Interactive string comparison.
Definition strings.c:38
int Kstrcmp(const char *a, const char *b)
Compare Strings - Similar to standard libc strcmp.
Definition strings.c:27
Represents an individual shell command.
Definition shell.c:42
const char * name
Definition shell.c:43
CommandFunc func
Definition shell.c:44
void NetStq(u32 iob)
Processes received packets from the VirtIO queue.
Definition virtio.c:138