From 89c83809f881565fd0724e52c23379ef259137da Mon Sep 17 00:00:00 2001 From: Tyler McGurrin Date: Thu, 29 May 2025 08:00:00 -0400 Subject: [PATCH] we don't talk about this one... --- src/kernel/main.c | 2 +- src/kernel/shell/LICENSE | 21 ++++ src/kernel/shell/c_scan.c | 216 ++++++++++++++++++++++++++++++++++++++ src/kernel/shell/c_scan.h | 42 ++++++++ src/kernel/shell/shell.c | 26 +++++ src/kernel/shell/shell.h | 8 ++ 6 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 src/kernel/shell/LICENSE create mode 100644 src/kernel/shell/c_scan.c create mode 100644 src/kernel/shell/c_scan.h create mode 100644 src/kernel/shell/shell.c create mode 100644 src/kernel/shell/shell.h diff --git a/src/kernel/main.c b/src/kernel/main.c index 5df6231..01d9b31 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -60,7 +60,7 @@ void __attribute__((section(".entry"))) start(BootParams* bootParams) { // Debug Info for Memory :3 i REALLY need to make a like serial debug output thingy // printf("Boot Device: %x\n", bootParams->BootDevice); // printf("Memory Region Count: %x\n", bootParams->Memory.RegionCount); - // for (int i = 0; i < bootParams->Memory.RegionCount; i++) { + // for (int i = 0; i < bootParams->Memory.RegionCount; i++) {shell example // printf("Memory: start=0x%llx length=0x%llx type=0x%x\n", // bootParams->Memory.Regions[i].Begin, bootParams->Memory.Regions[i].Length, bootParams->Memory.Regions[i].Type); // } diff --git a/src/kernel/shell/LICENSE b/src/kernel/shell/LICENSE new file mode 100644 index 0000000..c2e8c4c --- /dev/null +++ b/src/kernel/shell/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Tyler McGurrin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/src/kernel/shell/c_scan.c b/src/kernel/shell/c_scan.c new file mode 100644 index 0000000..0bac656 --- /dev/null +++ b/src/kernel/shell/c_scan.c @@ -0,0 +1,216 @@ +/* + * mini-scanf - Minimal scanf() implementation for embedded projects. + * Copyright (c) 2023 Aleksej Muratov + */ + +#include "c_scan.h" + +// implementation of basic dependencies +// std +int c_isspace(const int c) +{ + switch (c) + { /* in the "C" locale: */ + case ' ': /* space */ + case '\f': /* form feed */ + case '\n': /* new-line */ + case '\r': /* carriage return */ + case '\t': /* horizontal tab */ + case '\v': /* vertical tab */ + return 1; + default: + return 0; + } +} + +// std +int c_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + else + return (0); +} + +// parodies the standard +#ifdef C_SSCANF + #define NEXTCHAR (PointBuf++) + #define CURCHAR (buff[PointBuf]) + int c_sscanf(const char* buff, char* format, ...) +#else + #define NEXTCHAR (charBuf=c_getch()) + #define CURCHAR (charBuf) + int c_scanf(char* format, ...) +#endif +{ + int count = 0; + + #ifdef C_SSCANF + int PointBuf = 0; + #else + char charBuf = c_getch(); + #endif + + int PointFt = 0; + + va_list ap; + va_start(ap, format); + while (format && format[PointFt]) // Read format + { + if (format[PointFt] == '%') + { + PointFt++; + // for %* + bool save = true; + if (format[PointFt] == '*') + { + save = false; + PointFt++; + } + // for %1234567890 + unsigned len = 0; + bool lenEn = false; + while (c_isdigit(format[PointFt])) + { + lenEn = true; + len *= 10; + len += (format[PointFt] - '0'); + PointFt++; + } + // for %[] + char stop[LENSCANS]; + unsigned stopN = 0; + if (format[PointFt] == '[') + { + while (format[PointFt] != ']') + { + if (format[PointFt] != '[') + { + stop[stopN] = format[PointFt]; + stopN++; + } + PointFt++; + } + } + // %? + switch (format[PointFt]) + { + case 'c': + while (c_isspace(CURCHAR)) // ignore isspace (std) + NEXTCHAR; // + if (save) + *(char*)va_arg(ap, char*) = CURCHAR; + NEXTCHAR; + //if (save) // ignore %* (std) + count++; + break; + case 'u': + case 'd': + int sign = 1; + while (!c_isdigit(CURCHAR)) + { + if (CURCHAR == '+' || CURCHAR == '-') + if (CURCHAR == '-') + //if(format[PointFt] != 'u') // ignore sign (no std) + sign = -1; + NEXTCHAR; + } + long value = 0; + while(c_isdigit(CURCHAR) && (lenEn != true || len > 0)) + { + value *= 10; + value += (int)(CURCHAR - '0'); + NEXTCHAR; + len--; + } + + if (save) + *(int*)va_arg(ap, int*) = value * sign; + //if (save) // ignore %* (std) + count++; + break; + case ']': + case 's': + char* t = save ? va_arg(ap, char*) : NULL; + + while (c_isspace(CURCHAR)) // ignor isspace (std) + NEXTCHAR; // + + while (true) + { + bool con = false; + if (stopN != 0) + { + bool invert = (stop[0] == '^'); + con = !invert; + for (unsigned i = (invert ? 1 : 0); i < stopN; i++) + if (stop[i] == CURCHAR) + { + con = invert; + break; + } + + if (con == true) + break; + } + + if (!c_isspace(CURCHAR) || (!con && stopN != 0) && (lenEn != true || len > 0)) + { + if (save) + *t = CURCHAR; + NEXTCHAR; + t++; + len--; + } + else + break; + } + // add \0 + { + if (save) + *t = '\0'; + t++; + } + //if (save) // ignore %* (std) + count++; + break; + } + #ifndef C_SSCANF + if(format[PointFt] != 'c' + && format[PointFt] != 'u' + && format[PointFt] != 'd') + c_getbackch(CURCHAR); + #endif + } + //else // drop char in buff (no std) + // NEXTCHAR; // + PointFt++; + } + va_end(ap); + return count; +} + +// custom +char backch = 0; +char c_getch() +{ + if(backch == 0) + return (char)getch(); + else + { + char tmp = backch; + backch = 0; + return tmp; + } +} + +// new +bool c_getbackch(char b) +{ + char tmp = backch; + backch = b; + if (tmp != 0) + return 1; + else + return 0; +} \ No newline at end of file diff --git a/src/kernel/shell/c_scan.h b/src/kernel/shell/c_scan.h new file mode 100644 index 0000000..482dfa7 --- /dev/null +++ b/src/kernel/shell/c_scan.h @@ -0,0 +1,42 @@ +/* + * mini-scanf - Minimal scanf() implementation for embedded projects. + * Copyright (c) 2023 Aleksej Muratov + */ + +#ifndef _C_SCAN_H_ +#define _C_SCAN_H_ + +// conf +// sscanf / scanf +// #define C_SSCANF +// %[..] +#define LENSCANS 10 + +#include +#include +#include + +#ifndef NULL +#define NULL 0 +#endif + +#ifndef EOF +#define EOF -1 +#endif + +int c_isspace(const int c); +int c_isdigit(int c); + +// scan +#ifdef C_SSCANF + int c_sscanf(const char* buff, char* format, ...); +#else + int c_scanf(char* format, ...); +#endif +char c_getch(); //custom +bool c_getbackch(char b); //new + +// HW +int getch(); + +#endif /* _C_SCAN_H_ */ \ No newline at end of file diff --git a/src/kernel/shell/shell.c b/src/kernel/shell/shell.c new file mode 100644 index 0000000..1e2ace2 --- /dev/null +++ b/src/kernel/shell/shell.c @@ -0,0 +1,26 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2025| +|Tyler McGurrin | +\*----------------*/ +#include "shell.h" + +#include +#include "c_scan.h" + +char* cursor = "=>"; + +/* +A simple Rescue shell +Because, Yes. +*/ + +void shell() +{ + printf("NANITE Rescue Shell"); + while(1 == 1) { + printf("%s", cursor); + c_scanf(""); + } + +} diff --git a/src/kernel/shell/shell.h b/src/kernel/shell/shell.h new file mode 100644 index 0000000..61dd66c --- /dev/null +++ b/src/kernel/shell/shell.h @@ -0,0 +1,8 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2025| +|Tyler McGurrin | +\*----------------*/ +#pragma once + +void shell(); \ No newline at end of file