fix: ajout d'une fonction sprintf, corrections diverses fonction utilisant printf

This commit is contained in:
Nicolas Hordé 2018-10-07 12:57:38 +02:00
parent c36e8afd62
commit 12f395461e
5 changed files with 109 additions and 49 deletions

View File

@ -1,6 +1,9 @@
/*******************************************************************************/
/* COS2000 - Compatible Operating System - LGPL v3 - Hordé Nicolas */
/* */
#include "stdarg.h"
typedef struct console {
u8 attrib;
s16 cursX;
@ -22,3 +25,4 @@ u8* itoa(u64 num, u8* str, u8 base, u64 dim, u8 achar);
u8* sitoa(u64 num, u8 * str, u64 dim);
u8* rtoadouble(double num, u8 * str, u8 precisioni , u8 precisionf);
u8* rtoasingle(float num, u8 * str, u8 precisioni , u8 precisionf);
u32 format(const u8 * string, va_list args, u32 (*fonction)(u8* src, u8** dest, u32 len), u8* dest);

View File

@ -28,7 +28,7 @@ for (tag = (struct multiboot_tag *) (addr + 8);
tag->type != MULTIBOOT_TAG_TYPE_END;
tag = (struct multiboot_tag *) ((u8 *) tag + ((tag->size + 7) & ~7)))
{
printf ("--- Tag % 4u, Taille % 4u\r\n", tag->type, tag->size);
printf ("--- Tag % hu, Taille % hu\r\n", tag->type, tag->size);
switch (tag->type)
{
case MULTIBOOT_TAG_TYPE_CMDLINE:
@ -51,7 +51,7 @@ for (tag = (struct multiboot_tag *) (addr + 8);
((u64)((struct multiboot_tag_basic_meminfo *) tag)->mem_upper)<<10);
break;
case MULTIBOOT_TAG_TYPE_BOOTDEV:
printf ("Peripherique de demarrage : %x,%u,%u\r\n\r\n",
printf ("Peripherique de demarrage : %X,%u,%u\r\n\r\n",
((struct multiboot_tag_bootdev *) tag)->biosdev,
((struct multiboot_tag_bootdev *) tag)->slice,
((struct multiboot_tag_bootdev *) tag)->part);
@ -62,7 +62,7 @@ for (tag = (struct multiboot_tag *) (addr + 8);
printf ("*** Plan de memoire ***\r\n");
for (mmap = ((struct multiboot_tag_mmap *) tag)->entries;(u8 *) mmap < (u8 *) tag + tag->size; mmap = (multiboot_memory_map_t *)
((unsigned long) mmap + ((struct multiboot_tag_mmap *) tag)->entry_size))
printf (" adresse: %lx,"" taille:%lx, type:%x\r\n",
printf (" adresse: %lX,"" taille:%lX, type:%X\r\n",
(u64) (mmap->addr),
(u64) (mmap->len),
(u32) (mmap->type));

View File

@ -224,28 +224,29 @@ int readidt()
u32 offset =
desc[index].offset0_15 + (desc[index].offset16_31 << 16);
u32 type = desc[index].type & 0x0F00;
u8 *type2;
u8 *typestr1, *typestr2;
if (i>=32 & i<=39)
type2="IRQ master";
typestr1="IRQ master";
else if (i>=96 & i<=103)
type2="IRQ slave ";
typestr1="IRQ slave ";
else if (i<19)
type2="EXCEPTION ";
typestr1="EXCEPTION ";
else
type2="INTERRUPT ";
printf("\r\%s % hu %hY:%Y - ", type2,i++, select, offset, type);
typestr1="INTERRUPT ";
if (type == INTGATE)
print("INTGATE");
typestr2="INTGATE";
else if (type == TRAPGATE)
print("TRAPGATE");
typestr2="TRAPGATE";
else if (type == TASKGATE)
print("TASKGATE");
typestr2="TASKGATE";
else if (type == CALLGATE)
print("CALLGATE");
typestr2="CALLGATE";
else if (type == LDTDES)
print("LDTDES");
typestr2="LDTDES";
else
print("inconnu");
printf("%s % hu %hY:%Y - %s\r\n", typestr1, i++, select, offset, typestr2);
if (i % 32 == 0) {
print("\r\n<Appuyez sur une touche>\r\n");
waitascii();

View File

@ -299,13 +299,54 @@ u32 print(u8 * string)
}
/*******************************************************************************/
/* affiche une chaine de caractère formaté a l'ecran */
/* Fonction d'affichage (pour printf) */
#define buffersize 1024
u32 printstr(u8* src, u8** dest, u32 len) {
for(u32 i=0;i<len;i++)
putchar(*(src++));
return len;
}
/*******************************************************************************/
/* Fonction d'enregistrement dans une variable (pour sprintf) */
u32 storestr(u8* src, u8** dest, u32 len) {
memcpy(src, *dest, len, 1);
*dest=*dest+len;
return len;
}
/*******************************************************************************/
/* affiche une chaine de caractère formaté a l'ecran */
u32 printf(const u8 * string, ...)
{
va_list args,argstemp;
va_list args;
va_start(args, string);
format(string, args, &printstr, NULL);
va_end(args);
}
/*******************************************************************************/
/* met une chaine de caractère formaté dans une variable */
u32 sprintf(u8 *variable, const u8 *string, ...)
{
va_list args;
va_start(args, string);
format(string, args, &storestr, variable);
va_end(args);
}
/*******************************************************************************/
/* affiche une chaine de caractère formaté a l'ecran */
#define maxsize 1024
u32 format(const u8 * string, va_list args, u32 (*fonction)(u8* src, u8** dest, u32 len), u8* dest)
{
u64 sizes[] = { 0xFF, 0xFFFF, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF };
u8 units[][4] = { "o\000\000", "kio", "mio", "gio", "tio", "pio" };
u8 strbase2[] = "0xb\000";
@ -314,17 +355,18 @@ u32 printf(const u8 * string, ...)
u8 hexadecimal[] = "*0x\000";
u8 achar, temp;
u8 asize, charadd, unit, precisioni, precisionf;
u8 buffer[buffersize];
u8 buffer[maxsize];
u8* bufferend;
u32 buffersize;
u8 *str = string;
u8 *strtemp;
u32 i = 0, counter = 0;
u64 num;
bool flag = false, intok = false, decok = false;
va_start(args, string);
for (achar = *str; achar != '\000'; i++, achar = *(str + i)) {
if (achar != '%' && !flag) {
putchar(achar);
fonction((str + i),&dest,1);
counter++;
asize = 2;
precisioni = 0;
@ -412,10 +454,11 @@ u32 printf(const u8 * string, ...)
break;
}
else if (asize==2)
rtoasingle((float)va_arg(args, double), &buffer, precisioni, precisionf);
bufferend=rtoasingle((float)va_arg(args, double), &buffer, precisioni, precisionf);
else
rtoadouble((double)va_arg(args, double), &buffer, precisioni, precisionf);
counter += print(&buffer);
bufferend=rtoadouble((double)va_arg(args, double), &buffer, precisioni, precisionf);
buffersize=bufferend-&buffer[0];
counter += fonction(&buffer,&dest,buffersize);
flag = false;
break;
case 'u':
@ -429,8 +472,9 @@ u32 printf(const u8 * string, ...)
num = (u64) va_arg(args, u64);
if (charadd == 0xFF)
charadd = '0';
itoa(num, &buffer, 10, sizes[asize], charadd);
counter += print(&buffer);
bufferend=itoa(num, &buffer, 10, sizes[asize], charadd);
buffersize=bufferend-&buffer[0];
counter += fonction(&buffer,&dest,buffersize);
flag = false;
break;
case 'o':
@ -444,14 +488,15 @@ u32 printf(const u8 * string, ...)
num = (u64) va_arg(args, u64);
if (charadd == 0xFF)
charadd = '0';
itoa(num, &buffer, 8, sizes[asize], charadd);
print(&strbase8);
counter += print(&buffer) + 1;
counter += fonction(&strbase8,&dest,1);
bufferend=itoa(num, &buffer, 8, sizes[asize], charadd);
buffersize=bufferend-&buffer[0];
counter += fonction(&buffer,&dest,buffersize);
flag = false;
break;
case 'c':
temp = (u8) va_arg(args, int);
putchar(temp);
temp = (u8) va_arg(args, u8);
fonction(&temp,string,1);
counter++;
flag = false;
break;
@ -472,9 +517,10 @@ u32 printf(const u8 * string, ...)
num=num>>10;
unit++;
}
sitoa(num, &buffer, sizes[asize]);
counter += print(&buffer);
print(units[unit]);
bufferend=sitoa(num, &buffer, sizes[asize]);
buffersize=bufferend-&buffer[0];
counter += fonction(&buffer,&dest,buffersize);
counter += fonction(units[unit],&dest,3);
flag = false;
break;
case 'd':
@ -489,22 +535,24 @@ u32 printf(const u8 * string, ...)
num = (u64) va_arg(args, u64);
if (charadd == 0xFF)
charadd = ' ';
sitoa(num, &buffer, sizes[asize]);
counter += print(&buffer);
bufferend=sitoa(num, &buffer, sizes[asize]);
buffersize=bufferend-&buffer[0];
counter += fonction(&buffer,&dest,buffersize);
flag = false;
break;
case 's':
strtemp = (u8 *) va_arg(args, u8 *);
counter += print(strtemp);
counter += fonction(strtemp,&dest,strlen(strtemp));
flag = false;
break;
case 'p':
num = (u32) va_arg(args, int);
if (charadd == 0xFF)
charadd = '0';
print(&hexadecimal);
itoa(num, buffer, 16, sizes[asize], '0');
counter += print(&buffer) + 2;
counter += fonction(&hexadecimal,&dest,3);
bufferend=itoa(num, &buffer, 16, sizes[asize], '0');
buffersize=bufferend-&buffer[0];
counter += fonction(&buffer,&dest,buffersize);
flag = false;
break;
case 'x':
@ -521,11 +569,12 @@ u32 printf(const u8 * string, ...)
num = (u64) va_arg(args, u64);
if (charadd == 0xFF)
charadd = '0';
itoa(num, &buffer, 16, sizes[asize], charadd);
if (achar == 'X') counter += fonction(&strbase16,&dest,2);
bufferend=itoa(num, &buffer, 16, sizes[asize], charadd);
buffersize=bufferend-&buffer[0];
if (achar == 'X'||achar == 'Y')
strtoupper(&buffer);
if (achar == 'X') print(&strbase16);
counter += print(&buffer) + 1;
counter += fonction(&buffer,&dest,buffersize);
flag = false;
break;
case 'b':
@ -539,9 +588,10 @@ u32 printf(const u8 * string, ...)
num = (u64) va_arg(args, u64);
if (charadd == 0xFF)
charadd = '0';
itoa(num, &buffer, 2, sizes[asize], charadd);
print(&strbase2);
counter += print(&buffer) + 1;
counter += fonction(&strbase2,&dest,2);
bufferend=itoa(num, &buffer, 2, sizes[asize], charadd);
buffersize=bufferend-&buffer[0];
counter += fonction(&buffer,&dest,buffersize);
flag = false;
break;
default:
@ -549,7 +599,7 @@ u32 printf(const u8 * string, ...)
}
}
}
va_end(args);
return counter;
}
@ -562,6 +612,9 @@ u8 *rtoadouble(double num, u8 * str, u8 precisioni , u8 precisionf) {
u8 i,j,integerpart,fracpart;
if (precisioni==0) precisioni=12;
if (precisionf==0) precisionf=8;
double round=0.5;
for (i=0;i<precisionf;i++) round/=10;
num+=round;
bool intok=false;
if (num<0) {
num=-num;

View File

@ -50,7 +50,7 @@ int main(u32 magic, u32 addr)
/* Efface l'ecran */
print("\033[2J\000");
printf(ansilogo);
print(ansilogo);
print("\033[37m\033[0m -Chargement noyaux");
ok();
@ -106,9 +106,11 @@ int main(u32 magic, u32 addr)
printf(" -Installation des appels systemes utilisateur");
initsyscall();
ok();
static u8 tester[]="ceci est un test";
static u8 strings[255];
sprintf(&strings," pour voir : %u %d %f %s",123456789,-2522,3.14f,tester);
print(&strings);
float test=-12101412121212121212.5555555555555555f;
printf(" -Test float & double :\r\n %e \r\n %f\r\n %1.2f\r\n %.2f\r\n %4.1f\r\n %10.5f\r\n %6f\r\n %8f",test,test,test,test,test,test,test,test);
retry:
shell();
}