kprintf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2011-2018 K. Lange
  5. *
  6. * Kernel printf implementation
  7. *
  8. * Simple, painfully lacking, implementation of printf(),
  9. * for the kernel of all things.
  10. */
  11. #include <kernel/system.h>
  12. #include <kernel/process.h>
  13. #include <kernel/fs.h>
  14. #include <va_list.h>
  15. /*
  16. * Integer to string
  17. */
  18. static void print_dec(unsigned int value, unsigned int width, char * buf, int * ptr ) {
  19. unsigned int n_width = 1;
  20. unsigned int i = 9;
  21. while (value > i && i < UINT32_MAX) {
  22. n_width += 1;
  23. i *= 10;
  24. i += 9;
  25. }
  26. int printed = 0;
  27. while (n_width + printed < width) {
  28. buf[*ptr] = '0';
  29. *ptr += 1;
  30. printed += 1;
  31. }
  32. i = n_width;
  33. while (i > 0) {
  34. unsigned int n = value / 10;
  35. int r = value % 10;
  36. buf[*ptr + i - 1] = r + '0';
  37. i--;
  38. value = n;
  39. }
  40. *ptr += n_width;
  41. }
  42. /*
  43. * Hexadecimal to string
  44. */
  45. static void print_hex(unsigned int value, unsigned int width, char * buf, int * ptr) {
  46. int i = width;
  47. if (i == 0) i = 8;
  48. unsigned int n_width = 1;
  49. unsigned int j = 0x0F;
  50. while (value > j && j < UINT32_MAX) {
  51. n_width += 1;
  52. j *= 0x10;
  53. j += 0x0F;
  54. }
  55. while (i > (int)n_width) {
  56. buf[*ptr] = '0';
  57. *ptr += 1;
  58. i--;
  59. }
  60. i = (int)n_width;
  61. while (i-- > 0) {
  62. buf[*ptr] = "0123456789abcdef"[(value>>(i*4))&0xF];
  63. *ptr += + 1;
  64. }
  65. }
  66. /*
  67. * vasprintf()
  68. */
  69. size_t vasprintf(char * buf, const char * fmt, va_list args) {
  70. int i = 0;
  71. char * s;
  72. char * b = buf;
  73. for (const char *f = fmt; *f; f++) {
  74. if (*f != '%') {
  75. *b++ = *f;
  76. continue;
  77. }
  78. ++f;
  79. unsigned int arg_width = 0;
  80. while (*f >= '0' && *f <= '9') {
  81. arg_width *= 10;
  82. arg_width += *f - '0';
  83. ++f;
  84. }
  85. /* fmt[i] == '%' */
  86. switch (*f) {
  87. case 's': /* String pointer -> String */
  88. s = (char *)va_arg(args, char *);
  89. if (s == NULL) {
  90. s = "(null)";
  91. }
  92. while (*s) {
  93. *b++ = *s++;
  94. }
  95. break;
  96. case 'c': /* Single character */
  97. *b++ = (char)va_arg(args, int);
  98. break;
  99. case 'x': /* Hexadecimal number */
  100. i = b - buf;
  101. print_hex((unsigned long)va_arg(args, unsigned long), arg_width, buf, &i);
  102. b = buf + i;
  103. break;
  104. case 'd': /* Decimal number */
  105. i = b - buf;
  106. print_dec((unsigned long)va_arg(args, unsigned long), arg_width, buf, &i);
  107. b = buf + i;
  108. break;
  109. case '%': /* Escape */
  110. *b++ = '%';
  111. break;
  112. default: /* Nothing at all, just dump it */
  113. *b++ = *f;
  114. break;
  115. }
  116. }
  117. /* Ensure the buffer ends in a null */
  118. *b = '\0';
  119. return b - buf;
  120. }
  121. static unsigned short * textmemptr = (unsigned short *)0xB8000;
  122. static void placech(unsigned char c, int x, int y, int attr) {
  123. unsigned short *where;
  124. unsigned att = attr << 8;
  125. where = textmemptr + (y * 80 + x);
  126. *where = c | att;
  127. }
  128. int fprintf(fs_node_t * device, char *fmt, ...) {
  129. va_list args;
  130. va_start(args, fmt);
  131. char buffer[1024];
  132. vasprintf(buffer, fmt, args);
  133. va_end(args);
  134. return write_fs(device, 0, strlen(buffer), (uint8_t *)buffer);
  135. }
  136. int sprintf(char * buf, const char *fmt, ...) {
  137. va_list args;
  138. va_start(args, fmt);
  139. int out = vasprintf(buf, fmt, args);
  140. va_end(args);
  141. return out;
  142. }