rewrite comments into one, unified style

This commit is contained in:
maksw committed 2026-06-15 10:17:48 +02:00
1 parent 6260de0085
commit 467a58c9c6
163 files changed
+3560 -2556

No files matched your search

+48 -20
View File
@@ -1,3 +1,5 @@
// User application entry points for shell_native.
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
@@ -7,41 +9,46 @@
#define MAX_CMD 256
#define MAX_PROCS 64 // Maximum number of processes we will fetch at once
// ---------------------------------------------------------------------------
// Output helpers
// ---------------------------------------------------------------------------
// Output helpers.
static void print(const char* s) { SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) }; SysWriteConsole(&req); }
// Opens file.
static uint32_t open_file(const char* path, uint32_t* handleOut) {
SYS_PATH_HANDLE_REQUEST req = { SysSvFromCstr(path), handleOut };
return SysOpenFile(&req);
}
// Creates file.
static uint32_t create_file(const char* path, uint32_t* handleOut) {
SYS_PATH_HANDLE_REQUEST req = { SysSvFromCstr(path), handleOut };
return SysCreateFile(&req);
}
// Opens dir.
static uint32_t open_dir(const char* path, uint32_t* handleOut) {
SYS_PATH_HANDLE_REQUEST req = { SysSvFromCstr(path), handleOut };
return SysOpenDir(&req);
}
// Updates cwd.
static uint32_t set_cwd(const char* path) {
SYS_PATH_REQUEST req = { SysSvFromCstr(path) };
return SysSetCwd(&req);
}
// Releases file.
static uint32_t delete_file(const char* path) {
SYS_PATH_REQUEST req = { SysSvFromCstr(path) };
return SysDeleteFile(&req);
}
// Creates process.
static uint32_t create_process(const char* path, uint32_t* handleOut, const char* cmdline) {
SYS_CREATE_PROCESS_REQUEST req = { SysSvFromCstr(path), SysSvFromCstr(cmdline), handleOut };
return SysCreateProcess(&req);
}
// Prints u Int.
static void printUInt(uint32_t n) {
char buf[12]; int i = 0;
if (n == 0) { print("0"); return; }
@@ -49,11 +56,13 @@ static void printUInt(uint32_t n) {
while (i > 0) { char c[2] = { buf[--i], '\0' }; print(c); }
}
// Prints int.
static void printInt(int32_t n) {
if (n < 0) { print("-"); printUInt((uint32_t)-(n + 1) + 1); }
else { printUInt((uint32_t)n); }
}
// Prints u Int64.
static void printUInt64(uint64_t n) {
if (n == 0) { print("0"); return; }
char buf[20]; int i = 0;
@@ -61,34 +70,40 @@ static void printUInt64(uint64_t n) {
while (i > 0) { char c[2] = { buf[--i], '\0' }; print(c); }
}
// Implements the u64 Digits operation.
static uint32_t u64Digits(uint64_t n) {
uint32_t digits = 1;
while (n >= 10u) { n /= 10u; digits++; }
return digits;
}
// Prints spaces.
static void printSpaces(size_t count) {
while (count--) print(" ");
}
// Prints u Int Width.
static void printUIntWidth(uint32_t n, size_t width) {
uint32_t digits = u64Digits(n);
if (width > digits) printSpaces(width - digits);
printUInt(n);
}
// Prints u Int Zero Width.
static void printUIntZeroWidth(uint32_t n, size_t width) {
uint32_t digits = u64Digits(n);
while (width > digits) { print("0"); width--; }
printUInt(n);
}
// Prints u Int64 Width.
static void printUInt64Width(uint64_t n, size_t width) {
uint32_t digits = u64Digits(n);
if (width > digits) printSpaces(width - digits);
printUInt64(n);
}
// Prints hex32.
static void printHex32(uint32_t v) {
static const char hex[] = "0123456789abcdef";
char buf[9];
@@ -100,6 +115,7 @@ static void printHex32(uint32_t v) {
print(buf);
}
// Prints dir Time.
static void printDirTime(const SYS_SYSTEM_TIME* t) {
if (!t || !(t->Flags & SYS_TIME_FLAG_VALID)) {
print("---- -- -- --:-- ");
@@ -117,15 +133,14 @@ static void printDirTime(const SYS_SYSTEM_TIME* t) {
print(" ");
}
// Prints string Width.
static void printStringWidth(const char* s, size_t width) {
size_t len = s ? strlen(s) : 0;
if (s) print(s);
if (width > len) printSpaces(width - len);
}
// ---------------------------------------------------------------------------
// Line editor
// ---------------------------------------------------------------------------
// Line editor.
static void readLine(char* buf, int len) {
int pos = 0;
while (true) {
@@ -139,6 +154,7 @@ static void readLine(char* buf, int len) {
buf[pos] = '\0';
}
// Implements the trim operation.
static char* trim(char* s) {
while (*s == ' ') s++;
int len = (int)strlen(s);
@@ -147,9 +163,7 @@ static char* trim(char* s) {
return s;
}
// ---------------------------------------------------------------------------
// Built-in commands
// ---------------------------------------------------------------------------
// Built-in commands.
static void cmd_help() {
print("Available commands:\n"
" help - Show this list\n"
@@ -167,17 +181,20 @@ static void cmd_help() {
" exit - Close the shell\n");
}
// Prints size Ki B.
static void printSizeKiB(uint64_t bytes) {
printUInt64(bytes / 1024u);
print(" KiB");
}
// Prints pages Ki B.
static void printPagesKiB(uint64_t pages) {
printUInt64(pages * 4u);
print(" KiB");
}
// Prints cpu Flags.
static void printCpuFlags(uint32_t flags) {
if (flags & SYS_CPU_FLAG_BSP) print("bsp ");
if (flags & SYS_CPU_FLAG_ONLINE) print("online ");
@@ -187,6 +204,7 @@ static void printCpuFlags(uint32_t flags) {
if (flags & SYS_CPU_FLAG_PRESENT) print("present ");
}
// Implements the cmd stats operation.
static void cmd_stats(void) {
SYS_SCHEDULER_STATS sched;
SYS_PMM_STATS pmm;
@@ -263,6 +281,7 @@ static void cmd_stats(void) {
}
// Implements the cmd time operation.
static void cmd_time(void) {
SYS_SYSTEM_TIME t;
uint32_t status = SysGetSystemTime(&t);
@@ -290,6 +309,7 @@ static void cmd_time(void) {
print("\n");
}
// Prints display Caps.
static void printDisplayCaps(uint32_t caps) {
bool any = false;
if (caps & DISPLAY_CAP_FRAMEBUFFER) { print("framebuffer"); any = true; }
@@ -303,6 +323,7 @@ static void printDisplayCaps(uint32_t caps) {
if (!any) print("none");
}
// Prints display Stats.
static void printDisplayStats(const char* title, const DISPLAY_STATS* st) {
print(title); print(":\n");
print(" blit: "); printUInt64(st->BlitCount); print("\n");
@@ -318,6 +339,7 @@ static void printDisplayStats(const char* title, const DISPLAY_STATS* st) {
print(" hide="); printUInt64(st->CursorHideCount); print("\n");
}
// Implements the cmd display operation.
static void cmd_display(void) {
SYS_DISPLAY_INFO info;
uint32_t status = SysQueryDisplayInfo(&info);
@@ -338,6 +360,7 @@ static void cmd_display(void) {
printDisplayStats("Provider counters", &info.DeviceStats);
}
// Implements the cmd mem operation.
static void cmd_mem(void) {
SYS_MEMORY_STATUS st;
if (SysGetMemoryStatus(&st) != 0) {
@@ -355,6 +378,7 @@ static void cmd_mem(void) {
print("\n");
}
// Processes state Name.
static const char* processStateName(uint32_t state) {
switch (state) {
case SYS_PROCESS_STATE_RUNNING: return "run";
@@ -364,6 +388,7 @@ static const char* processStateName(uint32_t state) {
}
}
// Implements the cmd ps operation.
static void cmd_ps(void) {
SYS_PROCESS_INFO procs[MAX_PROCS];
size_t count = 0;
@@ -424,6 +449,7 @@ static void cmd_ps(void) {
}
}
// Implements the cmd write operation.
static void cmd_write(const char* args) {
char filename[MAX_CMD]; int i = 0;
while (args[i] != ' ' && args[i] != '\0' && i < MAX_CMD - 1) {
@@ -439,6 +465,7 @@ static void cmd_write(const char* args) {
print("File written successfully.\n");
}
// Implements the cmd dir operation.
static void cmd_dir(const char* path) {
uint32_t hDir;
const char* target = (path && *path != '\0') ? path : ".";
@@ -452,7 +479,7 @@ static void cmd_dir(const char* path) {
while (SysReadDir(hDir, &ke) == 0) {
printDirTime(&ke.LastWriteTime);
// Corrected Attribute Check: 0x10 is the Directory bit in FAT
// Corrected Attribute Check: 0x10 is the Directory bit in FAT.
if (ke.Attr & 0x10) {
print("<DIR> ");
} else {
@@ -461,7 +488,7 @@ static void cmd_dir(const char* path) {
print(ke.Name);
// Optional: print file size for non-directories
// Optional: print file size for non-directories.
if (!(ke.Attr & 0x10)) {
print(" (");
printUInt(ke.FileSize);
@@ -473,6 +500,7 @@ static void cmd_dir(const char* path) {
SysCloseHandle(hDir);
}
// Implements the cmd type operation.
static void cmd_type(const char* filename) {
uint32_t hFile;
if (open_file(filename, &hFile) != 0) { print("type: cannot open file\n"); return; }
@@ -484,6 +512,7 @@ static void cmd_type(const char* filename) {
}
// Implements the extract Program Path operation.
static bool extractProgramPath(const char* cmdline, char* out, size_t outSize) {
if (!cmdline || !out || outSize == 0) return false;
@@ -504,24 +533,22 @@ static bool extractProgramPath(const char* cmdline, char* out, size_t outSize) {
return pos != 0;
}
// ---------------------------------------------------------------------------
// Run one command
// ---------------------------------------------------------------------------
// Run one command.
static int runCommand(const char* cmd) {
if (!cmd || *cmd == '\0') return 0;
if (strcmp(cmd, "exit") == 0) return 1;
if (strcmp(cmd, "help") == 0) { cmd_help(); return 0; }
if (strcmp(cmd, "clear") == 0) { SysClearScreen(0x012456); return 0; }
if (strncmp(cmd, "crash", 5) == 0) {
// Check the sub-option trailing "crash " (offset 6)
// Check the sub-option trailing "crash " (offset 6).
if (strcmp(cmd + 6, "void") == 0) {
// Jumping into the void
// Jumping into the void.
void (*invalid_func)(void) = NULL;
invalid_func();
return 0;
}
else if (strcmp(cmd + 6, "noncanonical") == 0) {
// Non-canonical pointer dereference
// Non-canonical pointer dereference.
volatile int *ptr = (volatile int*)0x4242424242424242ULL;
*ptr = 1337;
return 0;
@@ -531,21 +558,21 @@ static int runCommand(const char* cmd) {
return 0;
}
else if (strcmp(cmd + 6, "nx") == 0) {
// NX / DEP Violation
// NX / DEP Violation.
unsigned char code[] = { 0xC3 };
void (*execute_stack)(void) = (void (*)(void))code;
execute_stack();
return 0;
}
else if (strcmp(cmd + 6, "divzero") == 0) {
// Division by zero
// Division by zero.
volatile int zero = 0;
volatile int result = 1 / zero;
(void)result;
return 0;
}
else {
// Default fallback if just "crash" or an unknown sub-option is passed
// Default fallback if just "crash" or an unknown sub-option is passed.
*(volatile int*)0 = 42;
return 0;
}
@@ -574,6 +601,7 @@ static int runCommand(const char* cmd) {
return 0;
}
// Runs the requested operation.
int main(void) {
const char* cmdline = SysGetPeb()->CommandLine.Data;
char lineBuf[MAX_CMD];