klibc: put printf inside (instead of vga.h)

This commit is contained in:
Mathieu Maret 2019-05-17 09:35:23 +02:00
parent de3c5e5dc2
commit ca22696b29
12 changed files with 155 additions and 152 deletions

View File

@ -2,10 +2,10 @@
//#define DEBUG
#include "alloc.h"
#include "errno.h"
#include "klibc.h"
#include "list.h"
#include "math.h"
#include "mem.h"
#include "vga.h"
#define IS_SELF_CONTAINED(desc) ((vaddr_t)((desc)->page) == (vaddr_t)(desc))
// Slab will contains object from sizeof(void *) to PAGE_SIZE/2 by pow2

View File

@ -1,6 +1,6 @@
#pragma once
#include "stack.h"
#include "vga.h"
#include "klibc.h"
#define assert(p) do { \
if (!(p)) { \

View File

@ -1,8 +1,10 @@
#include "klibc.h"
#include "vga.h"
int memcmp(const void* aptr, const void* bptr, size_t size) {
const unsigned char* a = (const unsigned char*) aptr;
const unsigned char* b = (const unsigned char*) bptr;
int memcmp(const void *aptr, const void *bptr, size_t size)
{
const unsigned char *a = (const unsigned char *)aptr;
const unsigned char *b = (const unsigned char *)bptr;
for (size_t i = 0; i < size; i++) {
if (a[i] < b[i])
return -1;
@ -30,37 +32,34 @@ void *memset(void *src, int c, size_t n)
return src;
}
char * itoa( int value, char * str, int base )
char *itoa(int value, char *str, int base)
{
char * rc;
char * ptr;
char * low;
char *rc;
char *ptr;
char *low;
// Check for supported base.
if ( base < 2 || base > 36 )
{
if (base < 2 || base > 36) {
*str = '\0';
return str;
}
rc = ptr = str;
// Set '-' for negative decimals.
if ( value < 0 && base == 10 )
{
if (value < 0 && base == 10) {
*ptr++ = '-';
}
// Remember where the numbers start.
low = ptr;
// The actual conversion.
do
{
do {
// Modulo is negative for negative value. This trick makes abs() unnecessary.
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"
[35 + value % base];
value /= base;
} while ( value );
} while (value);
// Terminating the string.
*ptr-- = '\0';
// Invert the numbers.
while ( low < ptr )
{
while (low < ptr) {
char tmp = *low;
*low++ = *ptr;
*ptr-- = tmp;
@ -99,3 +98,89 @@ int strcmp(char s1[], char s2[])
}
return s1[i] - s2[i];
}
void printf(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
}
void puts(const char *str)
{
while (*str) {
putc(*(str++));
}
}
void putc(const char str){
VGAputc(str);
}
void printInt(int integer)
{
char num[sizeof(int) * 3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3)
// = 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
int i = 0, k = 0;
if (integer < 0) {
putc('-');
}
if (integer == 0) {
num[i++] = 0;
}
while (integer != 0) {
int digit = integer % 10;
num[i++] = (digit > 0) ? digit : -digit;
integer = integer / 10;
}
for (k = i - 1; k >= 0; k--) {
putc(num[k] + '0');
}
}
void vprintf(const char *format, va_list ap)
{
int i = 0;
while (format[i] != '\0') {
switch (format[i]) {
case '%':
switch (format[i + 1]) {
case 'i':
case 'd': {
int d = va_arg(ap, int);
printInt(d);
break;
}
case 'x': {
char val[sizeof(int) * 2];
int d = va_arg(ap, int);
itoa(d, val, 16);
puts(val);
break;
}
case 'c': {
int c = va_arg(ap, int);
putc((char)c);
break;
}
case 's': {
char *str = va_arg(ap, char *);
if (!str)
str = "[NULL STR]";
puts(str);
break;
}
case '%':
putc('%');
break;
}
i++;
break;
default:
putc(format[i]);
}
i++;
}
}

View File

@ -16,3 +16,30 @@ char *itoa(int value, char *str, int base);
void reverse(char s[]);
int strlen(char s[]);
int strcmp(char s1[], char s2[]);
void puts(const char *str);
void putc(const char str);
void printInt(int integer);
void vprintf(const char *format, va_list ap);
void printf(const char *format, ...);
/*
* Dummy printk for disabled debugging statements to use whilst maintaining
* gcc's format checking.
*/
#define no_printf(fmt, ...) \
({ \
if (0) \
printf(fmt, ##__VA_ARGS__); \
0; \
})
#ifdef DEBUG
#define pr_devel(fmt, ...) \
printf(pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_devel(fmt, ...) \
no_printf(pr_fmt(fmt), ##__VA_ARGS__)
#endif
#define pr_info(fmt, ...) \
printf(pr_fmt(fmt), ##__VA_ARGS__)

View File

@ -1,7 +1,7 @@
#include "mem.h"
#include "list.h"
#include "types.h"
#include "vga.h"
#include "klibc.h"
static struct mem_desc *page_desc = (struct mem_desc *)&__ld_kernel_end;
static struct mem_desc *free_page;

View File

@ -3,7 +3,6 @@
#include "klibc.h"
#include "mem.h"
#include "stdarg.h"
#include "vga.h"
// In a Vaddr, 10 first bit (MSB) are the index in the Page Directory. A Page Directory Entry
// point to a Page Table. The 10 next bits are then an index in this Page Table. A Page Table

View File

@ -1,6 +1,6 @@
#include "stack.h"
#include "types.h"
#include "vga.h"
#include "klibc.h"
extern vaddr_t _stack_bottom;
extern vaddr_t _stack_top;

View File

@ -1,6 +1,6 @@
#include "keyboard.h"
#include "klibc.h"
#include "io.h"
#include "vga.h"
const char *scancode[128] = {
/* 0 */ 0,

View File

@ -1,7 +1,6 @@
#include "serial.h"
#include "io.h"
#include "irq.h"
#include "vga.h"
#define PORT 0x3f8 /* COM1 */
#define SERIAL_MAX_SPEED 115200

View File

@ -34,28 +34,6 @@ void clearScreenLine(uint bgColor, uint line)
}
}
void printInt(int integer)
{
char num[sizeof(int) *
3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3) =
// 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
int i = 0, k = 0;
if (integer < 0) {
printChar('-');
}
if (integer == 0) {
num[i++] = 0;
}
while (integer != 0) {
int digit = integer % 10;
num[i++] = (digit > 0) ? digit : -digit;
integer = integer / 10;
}
for (k = i - 1; k >= 0; k--) {
printChar(num[k] + '0');
}
}
void printIntDetails(int integer, uint color, uint bgColor, int startX, int startY)
{
char num[sizeof(int) *
@ -100,68 +78,9 @@ void vgaScrollUp(void)
}
}
void vprintf(const char *format, va_list ap)
{
int i = 0;
while (format[i] != '\0') {
switch (format[i]) {
case '%':
switch (format[i + 1]) {
case 'i':
case 'd': {
int d = va_arg(ap, int);
printInt(d);
break;
}
case 'x': {
char val[sizeof(int) * 2];
int d = va_arg(ap, int);
itoa(d, val, 16);
printString(val);
break;
}
case 'c': {
int c = va_arg(ap, int);
printChar((char)c);
break;
}
case 's': {
char *str = va_arg(ap, char *);
if (!str)
str = "[NULL STR]";
printString(str);
break;
}
case '%':
printChar('%');
break;
}
i++;
break;
default:
printChar(format[i]);
}
i++;
}
}
void printf(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
}
void printString(const char *str)
{
while (*str) {
printChar(*(str++));
}
}
void printChar(const char str)
void VGAputc(const char str)
{
if (str == '\n') {
line++;

View File

@ -21,40 +21,15 @@
#define pr_fmt(fmt) fmt
#endif
void vprintf(const char *format, va_list ap);
void printf(const char *format, ...);
int VGASetup(uint bgColor, uint color);
void VGAputc(const char str);
void clearScreen(uint bgColor);
void clearScreenLine(uint bgColor, uint line);
void printInt(int integer);
void printIntDetails(int integer, uint color, uint bgColor, int startX, int startY);
void printCharDetails(char str, uint color, uint bgColor, int startX, int startY);
void printStringDetails(const char *str, uint color, uint bgColor, int startX, int startY);
void printString(const char *str);
void printChar(const char str);
void vgaScrollUp(void);
void cursorEnable(uint8_t cursor_start, uint8_t cursor_end);
void cursorDisable(void);
void cursorMove(int x, int y);
/*
* Dummy printk for disabled debugging statements to use whilst maintaining
* gcc's format checking.
*/
#define no_printf(fmt, ...) \
({ \
if (0) \
printf(fmt, ##__VA_ARGS__); \
0; \
})
#ifdef DEBUG
#define pr_devel(fmt, ...) \
printf(pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_devel(fmt, ...) \
no_printf(pr_fmt(fmt), ##__VA_ARGS__)
#endif
#define pr_info(fmt, ...) \
printf(pr_fmt(fmt), ##__VA_ARGS__)

View File

@ -7,7 +7,6 @@
#include "paging.h"
#include "serial.h"
#include "stack.h"
#include "vga.h"
void testPhymem(void)
{