diff options
| author | Jake Mannens <jakem_5@hotmail.com> | 2020-03-18 14:01:12 +1100 |
|---|---|---|
| committer | Jake Mannens <jakem_5@hotmail.com> | 2020-03-18 14:01:12 +1100 |
| commit | 4d6fe1c317f0a541922f4cf945365fd31e608e10 (patch) | |
| tree | b78f721d50924094d4926ee90131a98ba12eb95e | |
| parent | eda36531e8daedd045fb02c9c590cdf4dddd957f (diff) | |
Added decimal and octal conversion specifiers ('%d' and '%o') to
vsprintf().
Coverted all printk()/printf() functions to capatalised hex for
readability.
Tidied up some code.
| -rw-r--r-- | kernel/asm.s | 7 | ||||
| -rw-r--r-- | kernel/fd.c | 6 | ||||
| -rw-r--r-- | kernel/fs/buffer.c | 8 | ||||
| -rw-r--r-- | kernel/fs/mount.c | 14 | ||||
| -rw-r--r-- | kernel/hd.c | 2 | ||||
| -rw-r--r-- | kernel/sched.c | 2 | ||||
| -rw-r--r-- | kernel/timer.s | 2 | ||||
| -rw-r--r-- | kernel/traps.s | 4 | ||||
| -rw-r--r-- | kernel/tty.c | 16 | ||||
| -rw-r--r-- | lib/vsprintf.c | 90 | ||||
| -rw-r--r-- | usrbin/main.c | 2 |
11 files changed, 69 insertions, 84 deletions
diff --git a/kernel/asm.s b/kernel/asm.s index 655dcfc..4e1e376 100644 --- a/kernel/asm.s +++ b/kernel/asm.s @@ -15,11 +15,7 @@ switch_to: ; abort if we're switching to the current task mov ebx, [ctask] cmp ebx, [ebp+12] - jne .skip - pop ebx - pop ebp - ret -.skip: + je .end ; update ctask and ctaskn mov ebx, [ebp+12] mov [ctask], ebx @@ -31,6 +27,7 @@ switch_to: shl ebx, 3 mov [.tmp+4], bx jmp far [.tmp] +.end: pop ebx pop ebp ret diff --git a/kernel/fd.c b/kernel/fd.c index f6563ea..d5e713a 100644 --- a/kernel/fd.c +++ b/kernel/fd.c @@ -194,7 +194,7 @@ static int sense_intr(struct sense_result *sr) { if(in < sizeof(struct sense_result)) return -1; - printk("[fd] Sense command success (Drive: %c:, CYL: 0x%02x, ST0: 0x%02x)\n", 'A' + (sr->st0 & 3), sr->cyl, sr->st0); + printk("[fd] Sense command success (Drive: %c:, CYL: 0x%02X, ST0: 0x%02X)\n", 'A' + (sr->st0 & 3), sr->cyl, sr->st0); return 0; } @@ -283,7 +283,7 @@ int fd_read_sect(int sect, struct req *r) { read_queue = r; } - printk("[fd] Reading sector 0x%01x (C: 0x%01x, H: 0x%01x, S: 0x%01x)\n", sect, cmd[2], cmd[3], cmd[4]); + printk("[fd] Reading sector 0x%01X (C: 0x%01X, H: 0x%01X, S: 0x%01X)\n", sect, cmd[2], cmd[3], cmd[4]); cli(); write_data(cmd, sizeof(cmd)); @@ -296,7 +296,7 @@ int fd_read_sect(int sect, struct req *r) { in = read_data(&res, sizeof(res)); if(in == sizeof(res) && !(res.st0 & 0xC0)) { - printk("[fd] Successfully read a sector (ST0: 0x%02x, N: 0x%02x)!\n", res.st0, res.n); + printk("[fd] Successfully read a sector (ST0: 0x%02X, N: 0x%02X)!\n", res.st0, res.n); printk(" DATA: %s\n", buffer); return 0; } diff --git a/kernel/fs/buffer.c b/kernel/fs/buffer.c index 99c3161..b106afc 100644 --- a/kernel/fs/buffer.c +++ b/kernel/fs/buffer.c @@ -38,10 +38,10 @@ struct buffer *buffer_get_block(uint16_t device, uint16_t block) { /* if the buffer exists but isn't populated. wait on it */ if(!b->b_present) { sleep_on(&b->b_wait); - printk("[buf] Waiting for buffer %04x:%04x to be populated...\n", b->b_device, b->b_block); + printk("[buf] Waiting for buffer %04X:%04X to be populated...\n", b->b_device, b->b_block); } sti(); - printk("[buf] Fetched block %04x:%04x from cache\n", b->b_device, b->b_block); + printk("[buf] Fetched block %04X:%04X from cache\n", b->b_device, b->b_block); return b->b_device ? b : NULL; next: p = &b->b_prev; @@ -68,7 +68,7 @@ next: b->b_present = 0; block_read(b); wake_up(&b->b_wait); - printk("[buf] Called driver to read block %04x:%04x\n", b->b_device, b->b_block); + printk("[buf] Called driver to read block %04X:%04X\n", b->b_device, b->b_block); return b->b_device ? b : NULL; } @@ -98,5 +98,5 @@ void buffer_init(void) { lru = (struct buffer*) BSTART; mru = last; - printk("[buf] Buffers initialized (0x%x buffers in pool)\n", c); + printk("[buf] Buffers initialized (%d buffers [%dKiB] in pool)\n", c, c * BLOCK_SIZE / 1024); } diff --git a/kernel/fs/mount.c b/kernel/fs/mount.c index e4d3636..401d2e8 100644 --- a/kernel/fs/mount.c +++ b/kernel/fs/mount.c @@ -24,13 +24,13 @@ void mount_root(void) { } printk("[fs] Found valid super-block for /\n"); - printk(" 0x%01x inodes\n", s->s_ninodes); - printk(" 0x%01x zones\n", s->s_nzones); - printk(" 0x%01x inode block bitmaps\n", s->s_imap_blocks); - printk(" 0x%01x zone block bitmaps\n", s->s_zmap_blocks); - printk(" First data zone: 0x%01x\n", s->s_firstdatazone); - printk(" Log zone size: 0x%01x\n", s->s_log_zone_size); - printk(" Max file size: 0x%01x\n", s->s_max_size); + printk(" 0x%01X inodes\n", s->s_ninodes); + printk(" 0x%01X zones\n", s->s_nzones); + printk(" 0x%01X inode block bitmaps\n", s->s_imap_blocks); + printk(" 0x%01X zone block bitmaps\n", s->s_zmap_blocks); + printk(" First data zone: 0x%01X\n", s->s_firstdatazone); + printk(" Log zone size: 0x%01X\n", s->s_log_zone_size); + printk(" Max file size: 0x%01X\n", s->s_max_size); /* copy the super block into the table */ memcpy(sblocks, s, ((unsigned) &s->s_imap - (unsigned) s)); diff --git a/kernel/hd.c b/kernel/hd.c index 83108dd..1540294 100644 --- a/kernel/hd.c +++ b/kernel/hd.c @@ -179,6 +179,6 @@ int hd_init(void) { return -1; } - printk("[hd] Primary bus master drive initialized! (status: 0x%02x, nsects: 0x%01x)\n", s, nblocks); + printk("[hd] Primary bus master drive initialized! (status: 0x%02X, nsects: 0x%01X)\n", s, nblocks); return 0; } diff --git a/kernel/sched.c b/kernel/sched.c index ff2d036..cced987 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -310,7 +310,7 @@ void sighandler_default(uint32_t sig) { kill_proc(ctask); break; default: - printk("[kernel] Handling signal 0x%02x for PID 0x%04x\n", sig, ctask->pid); + printk("[kernel] Handling signal 0x%02X for PID 0x%04X\n", sig, ctask->pid); break; } } diff --git a/kernel/timer.s b/kernel/timer.s index e6da428..7d54c38 100644 --- a/kernel/timer.s +++ b/kernel/timer.s @@ -26,7 +26,7 @@ tick_handler: inc dword [ticks] mov al, 0x20 out 0x20, al - ; check that the user isn't executing in kernel space + ; check that the task isn't executing in kernel space mov eax, [esp+38] test eax, 0x03 jz .end diff --git a/kernel/traps.s b/kernel/traps.s index 408a650..ca083dd 100644 --- a/kernel/traps.s +++ b/kernel/traps.s @@ -199,7 +199,7 @@ exc_gprot: RESTORE_ERR .fmt: - db "General Protection Fault (0x%08x)", 10, 0 + db "General Protection Fault (0x%08X)", 10, 0 exc_pagef: SAVE_ERR @@ -242,7 +242,7 @@ exc_pagef: RESTORE_ERR .fmt: - db "Page Fault (illegal memory access: 0x%08x:0x%02x)", 10, 0 + db "Page Fault (illegal memory access: 0x%08X:0x%02X)", 10, 0 exc_mathf: SAVE diff --git a/kernel/tty.c b/kernel/tty.c index a17429f..e7e55f4 100644 --- a/kernel/tty.c +++ b/kernel/tty.c @@ -79,12 +79,8 @@ ssize_t tty_read(unsigned tty, void *buf, size_t len) { for(i = 0; i < len; i++) { /* check if the buffer's empty and if so, go to sleep */ - if(sleep_while_empty(q) < 0) { - if(i == 0) - return -EINTR; - - return i; - } + if(sleep_while_empty(q) < 0) + return i == 0 ? -EINTR : i; *b++ = q->buf[q->pread]; q->pread = (q->pread + 1) % TTY_BUF_SIZE; @@ -109,12 +105,8 @@ ssize_t tty_write(unsigned tty, void *buf, size_t len) { for(i = 0; i < len; i++) { /* check if the buffer's full and if so, go to sleep */ - if(sleep_while_full(q, ttys[tty]) < 0) { - if(i == 0) - return -EINTR; - - return i; - } + if(sleep_while_full(q, ttys[tty]) < 0) + return i == 0 ? -EINTR : i; q->buf[q->pwrite] = *b++; q->pwrite = (q->pwrite + 1) % TTY_BUF_SIZE; diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 4188308..d2b4d48 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1,14 +1,16 @@ #include <stdarg.h> -#include <stdbool.h> #include <stdint.h> +#include <sys/types.h> enum TYPE { TYPE_INT = 1, TYPE_UINT = 2, - TYPE_HEX = 3, - TYPE_CHAR = 4, - TYPE_STRING = 5, - TYPE_PERCENT = 6 + TYPE_DEC = 3, + TYPE_OCT = 4, + TYPE_HEX = 5, + TYPE_CHAR = 6, + TYPE_STRING = 7, + TYPE_PERCENT = 8 }; enum FLAGS { @@ -17,60 +19,40 @@ enum FLAGS { FL_UPPER = 4 /* uppercase */ }; -static void hex(char **str, char flags, int fwidth, int size, int x) { - bool seen; - int i, y, len; +static void num(char **str, char flags, int base, int fwidth, int x) { + int i, y, len = 0; char c; - char *prefl = "0x"; - char *prefu = "0X"; + char *s = NULL; /* print the prefix, if the alt flag is set */ - if(flags & FL_ALT) { - if(flags & FL_UPPER) { - while(*prefu) - *(*str)++ = *prefu++; - } else { - while(*prefl) - *(*str)++ = *prefl++; - } - } + if(x && (flags & FL_ALT) && base == 8) + s = "0"; + if(x && (flags & FL_ALT) && base == 16) + s = (flags & FL_UPPER) ? "0X" : "0x"; + while(s && *s) + *(*str)++ = *s++; /* calculate the number of digits */ - len = 0; y = x; - seen = false; - for(i = 0; i < sizeof(y) << 1; i++) { - c = ((unsigned int) y >> 28); - if(c) - seen = true; - if(c || seen) - len++; - y = y << 4; + while(y) { + y /= base; + len++; } /* print the padding characters (if any) */ - c = ' '; - if(flags & FL_ZERO) - c = '0'; for(i = 0; i < fwidth - len; i++) - *(*str)++ = c; + *((*str)++) = (flags & FL_ZERO) ? '0' : ' '; - /* actually print the digits */ - seen = false; - for(i = 0; i < sizeof(x) << 1; i++) { - c = ((unsigned int) x >> 28) + '0'; - if(c > '9') { - c += 0x27; - if(flags & FL_UPPER) - c -= 0x20; - } + *str += len; + s = *str; + while(len--) { + c = (x % base) + '0'; + if(c > '9') + c += (flags & FL_UPPER) ? 7 : 39; - if(c != '0') - seen = true; - if(c != '0' || seen) - *(*str)++ = c; + *(--s) = c; - x = x << 4; + x /= base; } } @@ -103,6 +85,12 @@ int vsprintf(char *str, char *fmt, va_list ap) { case 'c': type = TYPE_CHAR; goto done; + case 'd': + type = TYPE_DEC; + goto done; + case 'o': + type = TYPE_OCT; + goto done; case 's': type = TYPE_STRING; goto done; @@ -144,9 +132,17 @@ done: case TYPE_CHAR: *str++ = va_arg(ap, char); break; + case TYPE_DEC: + x = va_arg(ap, int); + num(&str, flags, 10, fwidth, x); + break; + case TYPE_OCT: + x = va_arg(ap, int); + num(&str, flags, 8, fwidth, x); + break; case TYPE_HEX: x = va_arg(ap, int); - hex(&str, flags, fwidth, 4, x); + num(&str, flags, 16, fwidth, x); break; case TYPE_PERCENT: *str++ = '%'; diff --git a/usrbin/main.c b/usrbin/main.c index f05cb84..4706ceb 100644 --- a/usrbin/main.c +++ b/usrbin/main.c @@ -47,7 +47,7 @@ void pid2(void) { sleep(1); if(time() == 2) kill(1, 1); - printf("0x%04x:0x%08x: 0x%08x, 0x%08x\n", getpid(), (uint32_t) getpdir(), time(), x++); + printf("0x%04X:0x%08X: 0x%08X, 0x%08X\n", getpid(), (uint32_t) getpdir(), time(), x++); if(x > 3) kill(getpid(), SIGKILL); |
