diff options
author | Freya Murphy <freya@freyacat.org> | 2025-04-10 23:21:40 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-04-10 23:23:00 -0400 |
commit | c5cbf5d747c0d8249c162e075863ee55a804b6db (patch) | |
tree | ace128dfee9221e2e6efdb0a05914684278d6dfd /kernel | |
parent | fix random ampersand? how did i do that (diff) | |
download | comus-c5cbf5d747c0d8249c162e075863ee55a804b6db.tar.gz comus-c5cbf5d747c0d8249c162e075863ee55a804b6db.tar.bz2 comus-c5cbf5d747c0d8249c162e075863ee55a804b6db.zip |
make lib betterer
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/include/lib/kio.h | 17 | ||||
-rw-r--r-- | kernel/include/lib/klib.h | 9 | ||||
-rw-r--r-- | kernel/lib/kprintf.c | 70 | ||||
-rw-r--r-- | kernel/lib/panic.c | 3 |
4 files changed, 66 insertions, 33 deletions
diff --git a/kernel/include/lib/kio.h b/kernel/include/lib/kio.h index 652a85b..ef22f95 100644 --- a/kernel/include/lib/kio.h +++ b/kernel/include/lib/kio.h @@ -31,8 +31,9 @@ void kputs(const char *s); * * @param format - the format string * @param ... - variable args for the format + * @returns number of bytes written */ -__attribute__((format(printf, 1, 2))) void kprintf(const char *format, ...); +__attribute__((format(printf, 1, 2))) int kprintf(const char *format, ...); /** * prints out a formatted string to a buffer @@ -42,7 +43,7 @@ __attribute__((format(printf, 1, 2))) void kprintf(const char *format, ...); * @param ... - variable args for the format * @returns number of bytes written */ -__attribute__((format(printf, 2, 3))) size_t ksprintf(char *restrict s, +__attribute__((format(printf, 2, 3))) int ksprintf(char *restrict s, const char *format, ...); /** @@ -53,8 +54,9 @@ __attribute__((format(printf, 2, 3))) size_t ksprintf(char *restrict s, * @param format - the format string * @param ... - variable args for the format * @returns number of bytes written + * @returns number of bytes that would of been written (past maxlen) */ -__attribute__((format(printf, 3, 4))) size_t ksnprintf(char *restrict s, +__attribute__((format(printf, 3, 4))) int ksnprintf(char *restrict s, size_t maxlen, const char *format, ...); @@ -63,8 +65,9 @@ __attribute__((format(printf, 3, 4))) size_t ksnprintf(char *restrict s, * * @param format - the format string * @param args - variable arg list for the format + * @returns number of bytes written */ -void kvprintf(const char *format, va_list args); +int kvprintf(const char *format, va_list args); /** * prints out a formatted string to a buffer @@ -74,7 +77,7 @@ void kvprintf(const char *format, va_list args); * @param args - variable arg list for the format * @returns number of bytes written */ -size_t kvsprintf(char *restrict s, const char *format, va_list args); +int kvsprintf(char *restrict s, const char *format, va_list args); /** * prints out a formatted string to a buffer with a given max length @@ -83,9 +86,9 @@ size_t kvsprintf(char *restrict s, const char *format, va_list args); * @param maxlen - the max len of the buffer * @param format - the format string * @param args - variable arg list for the format - * @returns number of bytes written + * @returns number of bytes that would of been written (past maxlen) */ -size_t kvsnprintf(char *restrict s, size_t maxlen, const char *format, +int kvsnprintf(char *restrict s, size_t maxlen, const char *format, va_list args); #endif /* kio.h */ diff --git a/kernel/include/lib/klib.h b/kernel/include/lib/klib.h index 3afe925..cb1de01 100644 --- a/kernel/include/lib/klib.h +++ b/kernel/include/lib/klib.h @@ -185,13 +185,20 @@ char *btoa(size_t bytes, char *buf); */ unsigned int bound(unsigned int min, unsigned int value, unsigned int max); +#define __PANIC_STR(x) __PANIC_STR2(x) +#define __PANIC_STR2(x) #x + +#define panic(...) __panic(__PANIC_STR(__LINE__), __FILE__, __VA_ARGS__) +#define assert(val, ...) do { if (!(val)) { panic(__VA_ARGS__); } } while (0) + /** * Abort the kernel with a given message. * * @param format - the format string * @param ... - variable args for the format */ -__attribute__((noreturn)) void panic(const char *format, ...); +__attribute__((noreturn, format(printf, 3, 4))) +void __panic(const char *line, const char *file, const char *format, ...); /** * Fill dst with a stack trace consisting of return addresses in order diff --git a/kernel/lib/kprintf.c b/kernel/lib/kprintf.c index 552b923..aabefc4 100644 --- a/kernel/lib/kprintf.c +++ b/kernel/lib/kprintf.c @@ -71,6 +71,7 @@ typedef struct { /* output */ size_t written_len; + size_t possible_written_len; bool sprintf; char *sprintf_buf; @@ -80,6 +81,8 @@ typedef struct { static void printf_putc(context_t *ctx, char c) { + ctx->possible_written_len++; + // bounds check if (ctx->has_max_len) if (ctx->written_len >= ctx->max_len) @@ -348,9 +351,11 @@ static void handle_string_specifier(context_t *ctx, options_t *opts, data_t data) { char *str = data.str; - int str_len = 0; + if (str == NULL) + str = "(null)"; // get length of string + int str_len = 0; if (opts->precision_set) str_len = opts->precision; else @@ -486,6 +491,13 @@ static void do_printf(context_t *ctx, va_list args) case 's': handle_string_specifier(ctx, &opts, data); break; + // very terrible why in the love of FUCKING GOD would you do this + // but its in printf so im adding it for you fucks + case 'n': { + size_t *bad = va_arg(args, size_t *); + *bad = ctx->written_len; + break; + } // unknown default: // print from % to current @@ -494,73 +506,83 @@ static void do_printf(context_t *ctx, va_list args) break; } } + + // add \0 on sprintf + if (ctx->sprintf) { + int len, plen; + len = ctx->written_len; + plen = ctx->possible_written_len; + printf_putc(ctx, '\0'); + ctx->written_len = len; + ctx->possible_written_len = plen; + } } -void kprintf(const char *format, ...) +int kprintf(const char *format, ...) { va_list args; + int len; + va_start(args, format); - kvprintf(format, args); + len = kvprintf(format, args); va_end(args); + return len; } -size_t ksprintf(char *restrict s, const char *format, ...) +int ksprintf(char *restrict s, const char *format, ...) { va_list args; - size_t amt; + int len; + va_start(args, format); - amt = kvsprintf(s, format, args); + len = kvsprintf(s, format, args); va_end(args); - return amt; + return len; } -size_t snprintf(char *restrict s, size_t maxlen, const char *format, ...) +int snprintf(char *restrict s, size_t maxlen, const char *format, ...) { va_list args; - size_t amt; + int len; + va_start(args, format); - amt = kvsnprintf(s, maxlen, format, args); + len = kvsnprintf(s, maxlen, format, args); va_end(args); - return amt; + return len; } -void kvprintf(const char *format, va_list args) +int kvprintf(const char *format, va_list args) { - // create context context_t ctx = { 0 }; ctx.format = format; - // print + do_printf(&ctx, args); + return ctx.written_len; } -size_t kvsprintf(char *restrict s, const char *format, va_list args) +int kvsprintf(char *restrict s, const char *format, va_list args) { - // create context context_t ctx = { 0 }; ctx.format = format; - // sprintf buffer ctx.sprintf_buf = s; ctx.sprintf = 1; - // print + do_printf(&ctx, args); return ctx.written_len; } -size_t kvsnprintf(char *restrict s, size_t maxlen, const char *format, +int kvsnprintf(char *restrict s, size_t maxlen, const char *format, va_list args) { - // create context context_t ctx = { 0 }; ctx.format = format; - // sprintf buffer ctx.sprintf_buf = s; ctx.sprintf = 1; - // sprintf max_len ctx.has_max_len = 1; ctx.max_len = maxlen; - // print + do_printf(&ctx, args); - return ctx.written_len; + return ctx.possible_written_len; } void kputc(char c) diff --git a/kernel/lib/panic.c b/kernel/lib/panic.c index d3db908..486f4b5 100644 --- a/kernel/lib/panic.c +++ b/kernel/lib/panic.c @@ -3,12 +3,13 @@ #include <stdarg.h> #include <comus/asm.h> -__attribute__((noreturn)) void panic(const char *format, ...) +__attribute__((noreturn)) void __panic(const char *line, const char *file, const char *format, ...) { cli(); va_list list; va_start(list, format); kprintf("\n\n!!! PANIC !!!\n"); + kprintf("In file %s at line %s:\n", file, line); kvprintf(format, list); kprintf("\n\n"); log_backtrace(); |