Skip to content

Commit

Permalink
common(stdio): implement %p and %#x printf formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmeh committed Jun 29, 2024
1 parent 5bd10a3 commit 2da4fe0
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 55 deletions.
5 changes: 2 additions & 3 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ Checks: >
-readability-braces-around-statements,
-readability-identifier-length,
-readability-magic-numbers,
-readability-inconsistent-declaration-parameter-name
-readability-inconsistent-declaration-parameter-name,
-readability-function-cognitive-complexity,
CheckOptions:
- key: bugprone-narrowing-conversions.WarnOnIntegerNarrowingConversion
value: false
- key: readability-function-cognitive-complexity.Threshold
value: 100
91 changes: 48 additions & 43 deletions common/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,44 @@ int vsnprintf(char* buffer, size_t size, const char* format, va_list args) {
return 0;

size_t idx = 0;

#define PUT(c) \
do { \
buffer[idx++] = (c); \
if (idx >= size) \
goto too_long; \
} while (0)

char ch;
while ((ch = *format++) != 0) {
if (ch != '%') {
buffer[idx++] = ch;
if (idx >= size)
goto too_long;
PUT(ch);
continue;
}

ch = *format++;
if (ch == '%') {
buffer[idx++] = '%';
if (idx >= size)
goto too_long;
PUT('%');
continue;
}

bool alternative_form = false;
bool left_justify = false;
bool pad0 = false;
size_t pad_len = 0;

if (ch == '-') {
left_justify = true;
ch = *format++;
}
if (ch == '0') {
pad0 = true;
ch = *format++;
for (;;) {
if (!alternative_form && ch == '#') {
alternative_form = true;
ch = *format++;
} else if (!left_justify && ch == '-') {
left_justify = true;
ch = *format++;
} else if (!pad0 && ch == '0') {
pad0 = true;
ch = *format++;
} else {
break;
}
}
while ('0' <= ch && ch <= '9') {
pad_len = pad_len * 10 + ch - '0';
Expand All @@ -95,36 +105,36 @@ int vsnprintf(char* buffer, size_t size, const char* format, va_list args) {

switch (ch) {
case 'c':
buffer[idx++] = (char)va_arg(args, int);
if (idx >= size)
goto too_long;
PUT((char)va_arg(args, int));
break;
case 'p':
case 'x':
if (alternative_form || ch == 'p') {
PUT('0');
if (pad_len > 0)
--pad_len;
PUT('x');
if (pad_len > 0)
--pad_len;
}
// falls through
case 'd':
case 'i':
case 'u':
case 'x': {
case 'u': {
char num_buf[20];
itoa(va_arg(args, int), num_buf, ch == 'x' ? 16 : 10);
int radix = (ch == 'x' || ch == 'p') ? 16 : 10;
itoa(va_arg(args, int), num_buf, radix);

size_t len = strlen(num_buf);
if (!left_justify && pad_len > len) {
for (size_t i = 0; i < pad_len - len; ++i) {
buffer[idx++] = pad0 ? '0' : ' ';
if (idx >= size)
goto too_long;
}
}
for (size_t i = 0; i < len; ++i) {
buffer[idx++] = num_buf[i];
if (idx >= size)
goto too_long;
for (size_t i = 0; i < pad_len - len; ++i)
PUT(pad0 ? '0' : ' ');
}
for (size_t i = 0; i < len; ++i)
PUT(num_buf[i]);
if (left_justify && pad_len > len) {
for (size_t i = 0; i < pad_len - len; ++i) {
buffer[idx++] = ' ';
if (idx >= size)
goto too_long;
}
for (size_t i = 0; i < pad_len - len; ++i)
PUT(' ');
}

break;
Expand All @@ -133,17 +143,12 @@ int vsnprintf(char* buffer, size_t size, const char* format, va_list args) {
const char* str = va_arg(args, const char*);
if (!str)
str = "(null)";
while (*str) {
buffer[idx++] = *str++;
if (idx >= size)
goto too_long;
}
while (*str)
PUT(*str++);
break;
}
default:
buffer[idx++] = '?';
if (idx >= size)
goto too_long;
PUT('?');
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/drivers/graphics/bochs.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct fb* bochs_fb_init(void) {
if (!phys_addr)
return NULL;

kprintf("bochs_fb: found framebuffer at P0x%x\n", phys_addr);
kprintf("bochs_fb: found framebuffer at P%#x\n", phys_addr);
configure(640, 480, 32);

static struct fb fb = {.get_info = bochs_fb_get_info,
Expand Down
2 changes: 1 addition & 1 deletion kernel/drivers/graphics/multiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct fb* multiboot_fb_init(const multiboot_info_t* mb_info) {
info.height = mb_info->framebuffer_height;
info.pitch = mb_info->framebuffer_pitch;
info.bpp = mb_info->framebuffer_bpp;
kprintf("multiboot_fb: found framebuffer at P0x%x\n", phys_addr);
kprintf("multiboot_fb: found framebuffer at P%#x\n", phys_addr);

static struct fb fb = {.get_info = multiboot_fb_get_info,
.set_info = multiboot_fb_set_info,
Expand Down
2 changes: 1 addition & 1 deletion kernel/fs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void vfs_init(void) {
}

void vfs_populate_root_fs(const multiboot_module_t* initrd_mod) {
kprintf("vfs: populating root fs with initrd at P0x%x - P0x%x\n",
kprintf("vfs: populating root fs with initrd at P%#x - P%#x\n",
initrd_mod->mod_start, initrd_mod->mod_end);
initrd_populate_root_fs(initrd_mod->mod_start,
initrd_mod->mod_end - initrd_mod->mod_start);
Expand Down
4 changes: 2 additions & 2 deletions kernel/idt.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ static void handle_exception14(registers* regs) {
uint32_t write = regs->err_code & 0x2;
uint32_t user = regs->err_code & 0x4;

kprintf("Page fault (%s%s%s) at 0x%x\n",
kprintf("Page fault (%s%s%s) at %p\n",
present ? "page-protection " : "non-present ",
write ? "write " : "read ", user ? "user-mode" : "kernel-mode",
read_cr2());
(void*)read_cr2());
crash(regs, SIGSEGV);
}

Expand Down
4 changes: 2 additions & 2 deletions kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ noreturn void start(uint32_t mb_magic, uintptr_t mb_info_phys_addr) {
sti();

kprintf("version: %s\n"
"kernel end: V0x%x\n",
utsname()->version, (uintptr_t)kernel_end);
"kernel end: V%p\n",
utsname()->version, kernel_end);
ASSERT(mb_magic == MULTIBOOT_BOOTLOADER_MAGIC);

const multiboot_info_t* mb_info =
Expand Down
2 changes: 1 addition & 1 deletion kernel/memory/page.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void page_init(const multiboot_info_t* mb_info) {
uintptr_t lower_bound;
uintptr_t upper_bound;
get_available_physical_addr_bounds(mb_info, &lower_bound, &upper_bound);
kprintf("page: available physical memory address space P0x%x - P0x%x\n",
kprintf("page: available physical memory address space P%#x - P%#x\n",
lower_bound, upper_bound);

bitmap_init(mb_info, lower_bound, upper_bound);
Expand Down
2 changes: 1 addition & 1 deletion kernel/memory/page_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void page_directory_switch(struct page_directory* to) {
}

void page_table_init(void) {
kprintf("page_table: kernel page directory is at P0x%x\n",
kprintf("page_table: kernel page directory is at P%#x\n",
(uintptr_t)kernel_page_directory_start);

// Populate page directory entries for kernel space so that all processes
Expand Down

0 comments on commit 2da4fe0

Please sign in to comment.