My Project
Chargement...
Recherche...
Aucune correspondance
printk.c
Aller à la documentation de ce fichier.
1
7#include <manux/kmalloc-zs.h>
8#include <manux/scheduler.h>
9#include <manux/printk.h>
10
11#ifdef MANUX_JOURNAL
12# include <manux/journal.h>
13#else
14# include <manux/console.h>
15#endif
16#include <manux/appelsysteme.h>
17#include <manux/memoire.h> /* NULL */
18
19#include <manux/debug.h>
20
21#include <manux/arith64.h>
22//#include "../i386/arith64.c" // WARNING, pourquoi dois-je faire ça ?
23
24#define chiffre "0123456789abcdef"
25
39static
40int vsnprintk(char * str, const size_t l, char * format, va_list argList)
41{
42 size_t indice = 0;
43 // long long int n; // valeur associée à un %[l]d
44 long int n; // valeur associée à un %[l]d
45 char nombre[10]; // chaîne du nombre
46 char * s; // valeur associée à un %s
47 char c; // Affichage d'un caractère
48 int in; // indice pour les boucles internes
49 int nbChiffres; // pour les %[n]d
50 int base; // de l'affichage entier
51 int prefixe; // 0 int, 1 long int, 2 long long int
52
53 while ((*format) && (indice < l-1)) {
54 switch (*format) {
55 case '%' :
56 format++;
57 // Lecture de la taille
58 nbChiffres = 0;
59 while ((*format <= '9') && (*format >= '0')) {
60 nbChiffres = nbChiffres * 10 + *format - '0';
61 format++;
62 }
63
64 // Pas forcément bien là, mais j'ai pas mieux dans l'immédiat
65 prefixe = 0;
66 if (*format == 'l') {
67 format++;
68 if (*format == 'l') {
69 format++;
70 prefixe = 2;
71 } else {
72 prefixe = 1;
73 }
74 }
75 switch (*format) {
76 case 'o' :
77 base = 8;
78 goto affent;
79 case 'x' :
80 base = 16;
81 goto affent;
82 case 'd' :
83 base = 10;
84affent : switch (prefixe) {
85 case 2 :
86 n = 0; //;va_arg(argList, long long int); WARNING
87 break;
88 case 1 :
89 n = va_arg(argList, long int);
90 break;
91 default :
92 n = va_arg(argList, int);
93 break;
94 }
95 if (n < 0) {
96 n = -n;
97 str[indice++] = '-';
98 }
99 in = 0;
100 do {
101 nombre[in++] = chiffre[n%base];
102 n = n/base; // n est défini comme long long,
103 } while (n != 0);
104 while (nbChiffres > in) {
105 nbChiffres--;
106 str[indice++] = ' ';
107 }
108 do {
109 str[indice++] = nombre[--in];
110 } while (in);
111 break;
112 case 's' :
113 s = va_arg(argList, char *);
114 in = 0;
115 while (s[in]) {
116 str[indice++] = s[in++];
117 }
118 break;
119 case 'c' :
120 c = (char)va_arg(argList, int);
121 in = 0;
122 str[indice++] = c;
123 break;
124 default :
125 break;
126 }
127 break;
128 default :
129 str[indice++] = *format;
130 break;
131 }
132 format++;
133 }
134
135 assert(indice < l);
136
137 str[(indice < l)?indice:(l-1)] = 0;
138
139 return indice;
140}
141
148int snprintk(char * str, size_t l, char * format, ...)
149{
150 va_list argList;
151 int result;
152
153 va_start(argList, format);
154 result = vsnprintk(str, l, format, argList);
155 va_end(argList);
156
157 return result;
158}
159
166void printk(char * format, ...)
167{
168 va_list argList;
169#ifdef MANUX_KMALLOC_NON // WARNING : Ca ne marche pas (kmalloc pas initialisé)
170 char * chaine = kmalloc(MAX_PRINTK_LENGTH);
171#else
172 char chaine[MAX_PRINTK_LENGTH]; // WARNING, il faut une gestion dynamique
173 // attention aux risques de telescopage avec la pile !
174#endif // MANUX_KMALLOC
175 [[maybe_unused]] int result;
176
177 va_start(argList, format);
178 result = vsnprintk(chaine, MAX_PRINTK_LENGTH, format, argList);
179 va_end(argList);
180
181 // On affiche
182#ifdef MANUX_JOURNAL
183 journaliser(chaine);
184#else
185 consoleNoyauAfficher(chaine);
186#endif
187
188#ifdef MANUX_KMALLOC_NON
189 kfree(chaine);
190#endif
191}
192
200#ifdef MANUX_CONSOLES_VIRTUELLES
201void printkc(char * format, ...)
202{
203 va_list argList;
204#ifdef MANUX_KMALLOC_NON // WARNING : Ca ne marche pas (kmalloc pas initialisé)
205 char * chaine = kmalloc(MAX_PRINTK_LENGTH);
206#else
207 char chaine[MAX_PRINTK_LENGTH]; // WARNING, il faut une gestion dynamique
208 // attention aux risques de telescopage avec la pile !
209#endif // MANUX_KMALLOC
210 [[maybe_unused]] int result;
211
212 va_start(argList, format);
213 result = vsnprintk(chaine, MAX_PRINTK_LENGTH, format, argList);
214 va_end(argList);
215
216 // On affiche
217 consoleAfficher(tacheEnCours->console, chaine);
218
219#ifdef MANUX_KMALLOC_NON
220 kfree(chaine);
221#endif
222}
223#endif
Définition des fonctions de base d'accés à la console.
void consoleAfficher(Console *cons, char *msg)
Affichage d'un message sur une console.
Definition console.c:427
void consoleNoyauAfficher(char *msg)
Affichage d'un message sur la console noyau.
Definition console.c:437
#define str(tr)
Definition i386.h:19
Définition des outils de journalisation des messages du noyau.
void journaliser(char *message)
Journalisation d'un message.
Definition journal.c:179
Définition des fonctions de base d'entrée-sortie du noyau.
#define MAX_PRINTK_LENGTH
Definition printk.h:21
#define printkc
Une version de printk qui écrit dans la console de la tâche en cours.
Definition printk.h:61
#define va_arg(ap, type)
Definition stdarg.h:7
#define va_end(ap)
Definition stdarg.h:8
#define va_start(ap, last)
Definition stdarg.h:6
__builtin_va_list va_list
Definition stdarg.h:4
Une implatation simple de kmalloc fondé sur les zones siamoises (buddy areas) (C) Emmanuel Chaput 202...
void * kmalloc(size_t n)
Allocation d'une zone de n octets.
Definition kmalloc-zs.c:139
void kfree(void *p)
Libération d'une zone mémoire allouée.
Definition kmalloc-zs.c:265
uint16_t c
#define assert(cond)
Definition debug.h:126
void printk(char *format,...)
La fonction d'affichage à utiliser dans le noyau.
Definition printk.c:166
#define chiffre
Definition printk.c:24
int snprintk(char *str, size_t l, char *format,...)
: Écriture formattée dans une chaîne de caractères
Definition printk.c:148
Tache * tacheEnCours
: L'identification de la tâche en cours
Definition scheduler.c:91