LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
kprintf.c
Go to the documentation of this file.
1
9#include <kernel/kprintf.h>
10#include <drivers/console.h>
11#include <drivers/serial.h>
12#include <arch/x86_64/io.h>
13#include <lib/kstring.h>
14#include <stdarg.h>
15#include <stdint.h>
16#include <stdbool.h>
17
18void kprintf(const char *fmt, ...) {
19 va_list ap;
20 va_start(ap, fmt);
21
22 kvprintf(fmt, ap);
23
24 va_end(ap);
25
26}
27
28void kvprintf(const char *fmt, va_list ap) {
29 unsigned int index = 0; // The index in the string
30 bool isDimmed = false; // Is the current text dimmed
31
32 for (index = 0; index < strlen(fmt); index++) { // Loop through the whole string
33
34 // Handle ANSI Codes
35 // More info at https://everything.explained.today/ANSI_escape_code/
36
37 if (fmt[index] == '\033') { // If the current char is an ansi escape code
38 index++;
39
40 if (fmt[index] == '[') {
41 // If its a valid ansi code...
42
43 index++; // Go to the next index, the start of the formats
44 unsigned int format_start_index = index;
45
46 // Types of ansi formats
47 #define ANSI_NONE 0
48 #define ANSI_TEXT_EFFECT 1
49
50 unsigned int format_type = ANSI_NONE;
51
52 while (index < strlen(fmt)) { // Loop through the whole string
53 switch (fmt[index]) { // Search for control sequence introducer
54 case 'm':
55 format_type = ANSI_TEXT_EFFECT;
56 break;
57 default:
58 break;
59 }
60
61 index++;
62
63 if (format_type != ANSI_NONE) break; // If a CSI has been found, stop the loop
64 }
65
66 index = format_start_index; // Go back to the start of the formatting
67
68 if (format_type != ANSI_NONE) {
69 if (format_type == ANSI_TEXT_EFFECT) { // If the CSI is an 'm', control the character style
70 index++;
71 ansi_text_effect_format:
72
73 //TODO: Add Bold (1), underline (4), and blink (5) effects
74
75 // Check the characters and apply their effects
76 switch (fmt[index - 1]) {
77 case '0':
78 // Return to normal
79 index--;
82 break;
83 case '3':
84 // Normal Text
85 switch (fmt[index]) {
86 case '0':
87 set_fg_color(COLOR_BLACK);
88 break;
89 case '1':
90 set_fg_color(COLOR_RED);
91 break;
92 case '2':
93 set_fg_color(COLOR_GREEN);
94 break;
95 case '3':
96 set_fg_color(COLOR_YELLOW);
97 break;
98 case '4':
99 set_fg_color(COLOR_BLUE);
100 break;
101 case '5':
102 set_fg_color(COLOR_MAGENTA);
103 break;
104 case '6':
105 set_fg_color(COLOR_CYAN);
106 break;
107 case '7':
108 set_fg_color(COLOR_WHITE);
109 break;
110 case '9':
112 break;
113 }
114 if (isDimmed) set_fg_color(sub_rgb(get_fg_color(), (struct rgb){80,80,80}));
115 break;
116 case '4':
117 // Normal BG
118 switch (fmt[index]) {
119 case '0':
120 set_bg_color(COLOR_BLACK);
121 break;
122 case '1':
123 set_bg_color(COLOR_RED);
124 break;
125 case '2':
126 set_bg_color(COLOR_GREEN);
127 break;
128 case '3':
129 set_bg_color(COLOR_YELLOW);
130 break;
131 case '4':
132 set_bg_color(COLOR_BLUE);
133 break;
134 case '5':
135 set_bg_color(COLOR_MAGENTA);
136 break;
137 case '6':
138 set_bg_color(COLOR_CYAN);
139 break;
140 case '7':
141 set_bg_color(COLOR_WHITE);
142 break;
143 case '9':
145 break;
146 }
147 break;
148 case '9':
149 // Bright Text
150 switch (fmt[index]) {
151 case '0':
152 set_fg_color(COLOR_BLACK_BRIGHT);
153 break;
154 case '1':
155 set_fg_color(COLOR_RED_BRIGHT);
156 break;
157 case '2':
158 set_fg_color(COLOR_GREEN_BRIGHT);
159 break;
160 case '3':
161 set_fg_color(COLOR_YELLOW_BRIGHT);
162 break;
163 case '4':
164 set_fg_color(COLOR_BLUE_BRIGHT);
165 break;
166 case '5':
167 set_fg_color(COLOR_MAGENTA_BRIGHT);
168 break;
169 case '6':
170 set_fg_color(COLOR_CYAN_BRIGHT);
171 break;
172 case '7':
173 set_fg_color(COLOR_WHITE_BRIGHT);
174 break;
175 case '9':
177 break;
178 }
179 if (isDimmed) set_fg_color(sub_rgb(get_fg_color(), (struct rgb){80,80,80}));
180 break;
181 case '1':
182 // Bright BG OR Bold
183 switch (fmt[index]) {
184 case '0':
185 // Bright BG
186 index++;
187 switch (fmt[index]) {
188 case '0':
189 set_bg_color(COLOR_BLACK_BRIGHT);
190 break;
191 case '1':
192 set_bg_color(COLOR_RED_BRIGHT);
193 break;
194 case '2':
195 set_bg_color(COLOR_GREEN_BRIGHT);
196 break;
197 case '3':
198 set_bg_color(COLOR_YELLOW_BRIGHT);
199 break;
200 case '4':
201 set_bg_color(COLOR_BLUE_BRIGHT);
202 break;
203 case '5':
204 set_bg_color(COLOR_MAGENTA_BRIGHT);
205 break;
206 case '6':
207 set_bg_color(COLOR_CYAN_BRIGHT);
208 break;
209 case '7':
210 set_bg_color(COLOR_WHITE_BRIGHT);
211 break;
212 case '9':
214 break;
215 }
216 break;
217 default:
218 // Bold
219 break;
220 }
221 break;
222 case '7':
223 // Reverse Color
224 index--;
225 struct rgb tmp_rgb = get_bg_color();
227 set_fg_color(tmp_rgb);
228 break;
229 case '8':
230 // Hide
231 index--;
233 break;
234 case '2':
235 // Dim or undo effect
236 switch (fmt[index]) {
237 case '2':
238 // Undo Dim
239 if (isDimmed) set_fg_color(add_rgb(get_fg_color(), (struct rgb){80,80,80}));
240 isDimmed = false;
241 break;
242 case '7':
243 struct rgb _tmp_rgb = get_bg_color();
245 set_fg_color(_tmp_rgb);
246 break;
247 case '8':
249 break;
250 default:
251 index--;
252 isDimmed = true;
253 set_fg_color(sub_rgb(get_fg_color(), (struct rgb){80,80,80}));
254 break;
255 }
256 break;
257
258 }
259 index++;
260
261 if (fmt[index] == ';') { // If there is a semicolon, continue checking for more effects
262 //Do this process again.
263 index++;
264 goto ansi_text_effect_format;
265 }
266
267 index++;
268 }
269 }
270
271 }
272 }
273
274 // If its not a format identifier, just continue printing
275 if (fmt[index] != '%') {
276 kputc(fmt[index]);
277 continue;
278 }
279
280
281 // Handle format strings
282 unsigned int starting_index = index; // The starting index of the format identifier
283
284 index++;
285
286 uint8_t l_count = 0; // Count of l's (for long and long long data types)
287 uint8_t len = 0; // The minimum length of the formatted variable
288
289 while (fmt[index] >= '0' && fmt[index] <= '9') { // if its a digit...
290 len = (len * 10) + (((uint8_t)fmt[index]) - '0'); // Move the digits over and add this one
291 index++;
292 }
293
294 while (l_count < 2) {
295 if (fmt[index] == 'l') { // If there is an l, add it to the l count
296 l_count++;
297 index++;
298 } else {
299 break;
300 }
301 }
302
303 // Handle the format cases
304 switch (fmt[index]) {
305 case 'c': // CHAR
306 kputc((char) va_arg(ap, int));
307 break;
308 case 's': // STRING
309 const char *str = va_arg(ap, const char*);
310 if (str == NULL)
311 str = "(null)";
312 for (unsigned int j = 0; j < strlen(str); j++) {
313 kputc(str[j]);
314 }
315 break;
316 case 'd': case 'i': // Signed Digits
317 switch (l_count) {
318 case 1:
319 print_signed(va_arg(ap, long), len);
320 break;
321 case 2:
322 print_signed(va_arg(ap, long long), len);
323 break;
324 default:
325 print_signed(va_arg(ap, int), len);
326 break;
327 }
328 break;
329 case 'u': // Unsigned Digits
330 switch (l_count) {
331 case 1:
332 print_unsigned(va_arg(ap, unsigned long), 10, false, len);
333 break;
334 case 2:
335 print_unsigned(va_arg(ap, unsigned long long), 10, false, len);
336 break;
337 default:
338 print_unsigned(va_arg(ap, unsigned int), 10, false, len);
339 break;
340 }
341 break;
342 case 'x': // Lowercase hex
343 switch (l_count) {
344 case 1:
345 print_unsigned(va_arg(ap, unsigned long), 16, false, len);
346 break;
347 case 2:
348 print_unsigned(va_arg(ap, unsigned long long), 16, false, len);
349 break;
350 default:
351 print_unsigned(va_arg(ap, unsigned int), 16, false, len);
352 break;
353 }
354 break;
355 case 'X': // Uppercase hex
356 switch (l_count) {
357 case 1:
358 print_unsigned(va_arg(ap, unsigned long), 16, true, len);
359 break;
360 case 2:
361 print_unsigned(va_arg(ap, unsigned long long), 16, true, len);
362 break;
363 default:
364 print_unsigned(va_arg(ap, unsigned int), 16, true, len);
365 break;
366 }
367 break;
368 case 'b': // Byte
369 switch (l_count) {
370 case 1:
371 print_unsigned(va_arg(ap, unsigned long), 2, true, len);
372 break;
373 case 2:
374 print_unsigned(va_arg(ap, unsigned long long), 2, true, len);
375 break;
376 default:
377 print_unsigned(va_arg(ap, unsigned int), 2, true, len);
378 break;
379 }
380 break;
381 case 'p': // Pointer
382 void *value = va_arg(ap, void *);
383 if (value != NULL) {
384 kputc('0');
385 kputc('x');
386 print_unsigned((uint64_t)value, 16, false, len);
387 } else {
388 char *str = "(null)";
389 for (unsigned int j = 0; j < strlen(str); j++) {
390 kputc(str[j]);
391 }
392 }
393 break;
394 case '%': // Just a %
395 kputc('%');
396 break;
397 default: // If its invalid, just print the whole sequence
398 for (unsigned int j = starting_index; j <= index; j++) {
399 kputc(fmt[j]);
400 }
401 break;
402
403 }
404 }
405
406 set_fg_color(get_default_fg_color()); // Reset the foreground and background colors
408}
409
410void kputc(char c) {
413}
414
415void print_unsigned(unsigned long long value, uint8_t base, bool uppercase, uint8_t _length) {
416 if (base > 36) return;
417
418 // The digits that will be printed
419 char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
420 if (uppercase)
421 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
422
423 unsigned long long next = value; // Holds the next value to calculate
424 uint64_t index = 0; // Holds how many characters have been calculated
425
426 // CALCULATE LENGTH
427 while (next != 0) { // Loop until the next value is 0, so the number is done
428 next /= base; // move the decimal point over by one to the left
429 index++; // continue, increase length counter
430 }
431
432 // If the value started as 0, set the length to 1
433 if (value == 0)
434 index = 1;
435
436
437 uint64_t length = _length > index ? _length : index;
438 char chars[length];
439
440 for (int i = 0; i < length; i++)
441 chars[i] = '0';
442
443 next = value; // Reset next value
444 index = 0; // Reset the index
445
446 while (next != 0) {
447 if (index >= length) // Check if the index passed the length
448 break; // Stop the loop if so
449
450 chars[index] = digits[next % base]; // Set the digit to the correct digit based on the remainder
451
452 next /= base; // move the decimal, using lossy division
453 index++;
454 }
455
456 if (value == 0) { // If the number started as one
457 chars[0] = '0'; // set the character in chars to 0
458 index = _length <= 0 ? 1 : _length; // set index to 1
459 } else index = length;
460
461 for (int i = index - 1; i >= 0; i--) { // For each char in chars backwards, print it
462 kputc(chars[i]);
463 }
464}
465
466void print_signed(long long value, uint8_t len) {
467 unsigned long long magnitude;
468
469 if (value < 0) { // If its a negative number
470 kputc('-'); // Print a - sign
471 magnitude = 0 - (unsigned long long)value; // Takes the absolute value of the number
472 } else {
473 magnitude = value;
474 }
475
476 // Prints the number (base 10)
477 print_unsigned(magnitude, 10, false, len);
478}
struct rgb add_rgb(struct rgb a, struct rgb b)
Adds two RGB color values.
Definition color.c:22
struct rgb sub_rgb(struct rgb a, struct rgb b)
Subtracts the second RGB color from the first.
Definition color.c:32
void set_bg_color(struct rgb color)
Sets the background colour.
Definition console.c:102
void console_putc(char c)
Arranges the cursor and prints characters.
Definition console.c:178
struct rgb get_default_bg_color(void)
Gets the default bg colour.
Definition console.c:108
bool get_console_status(void)
Returns the status of the console (initialized or uninitialized).
Definition console.c:23
struct rgb get_fg_color(void)
Gets the current fg colour.
Definition console.c:106
struct rgb get_bg_color(void)
Gets the current bg colour.
Definition console.c:105
struct rgb get_default_fg_color(void)
Gets the default fg colour.
Definition console.c:109
void set_fg_color(struct rgb color)
Sets the foreground colour.
Definition console.c:103
Console utilities and configuration.
Provides utilities for IO operations with the hardware.
void kputc(char c)
Sends characters to print in the console and serial.
Definition kprintf.c:410
void print_signed(long long value, uint8_t len)
Prepares a signed number to be printed through the unsigned print.
Definition kprintf.c:466
void kvprintf(const char *fmt, va_list ap)
Handles printing a formatted string.
Definition kprintf.c:28
void print_unsigned(unsigned long long value, uint8_t base, bool uppercase, uint8_t _length)
Prints unsigned integers of any base lower than base 36.
Definition kprintf.c:415
Tools for printing to the console and serial output.
void kprintf(const char *fmt,...) __attribute__((format(printf
Wrapper for a standard print.
Utilities for memory and string operations.
size_t strlen(const char *str)
Calculates the length of a string.
Definition kstring.c:92
bool get_serial_status(void)
Returns the status of the serial output (enabled or disabled).
Definition serial.c:16
void serial_putc(char c)
Prints a character to serial port.
Definition serial.c:63
Serial utilities and configuration.
RGB color values.
Definition color.h:15