Operating System
stdio.h
Go to the documentation of this file.
1 
6 #ifndef __STDIO_H_
7 #define __STDIO_H_
8 #include "graphic.h"
9 #include "utilities.h"
10 #include <stdarg.h>
11 #define DEFAULT_STYLE_DARK (G_DEFAULT | G_DARK)
12 #define DEFAULT_STYLE (G_DEFAULT)
13 
21 static inline int16_t _draw_char(char ch, int offset, uint8_t style) {
22  uint16_t written_data = ch | (style << 8);
23  __asm__ volatile (
24  "movb $1, %%ah\n"
25  "int $0x2B\n"
26  : /* no output */
27  : "c"(written_data), "D"(offset)
28  : "cc", "ax", "memory"
29  );
30  return 1;
31 
32 }
33 static inline int16_t putch_style(char ch, uint8_t style) {
34  uint16_t cursor = get_cursor();
35  uint8_t crow = cursor >> 8;
36  uint8_t ccol = cursor & 0b11111111;
37  if (ch == '\r') {
38  set_cursor(crow, 1);
39  } else if (ch == '\n') {
40  if (crow >= 23) {
42  } else {
43  set_cursor(crow + 1, ccol);
44  }
45  } else if (ch == '\b') {
46  set_cursor(crow, ccol-1);
47  }
48  else if (ch == '\t') {
49  set_cursor(crow, ccol + 4);
50  }
51  else {
52  _draw_char(ch, (crow * 80 + ccol)*2, style);
53  ccol++;
54  if (ccol == 79) {
55  crow++;
56  ccol = 1;
57  }
58  set_cursor(crow, ccol);
59  }
60  return 1;
61 }
62 static inline int16_t putch(char ch) {
63  return putch_style(ch, DEFAULT_STYLE);
64 }
65 
71 static inline int16_t padding(int16_t num) {
72  for (int16_t i = 0; i < num; ++i) {
73  putch(' ');
74  }
75  return num;
76 }
77 
82 static inline int16_t __uts_read_int(const char* s, int16_t* readNum) {
83  int16_t fac = 1;
84  int16_t cnt = 0;
85  if (*s == '-') {
86  fac = -1;
87  s++;
88  cnt++;
89  }
90  int16_t ret = 0;
91  while (*s && *s >= '0' && *s <= '9') {
92  ret = ret * 10 + (*s - '0');
93  s++;
94  cnt++;
95  }
96  *readNum = ret;
97  return cnt;
98 }
104 static inline int16_t draw_str(char const* str, int row, int col) {
105  int pos = (row * 80 + col) * 2;
106  int16_t index = 0;
107  while (*str != '\0') {
108  _draw_char(*str, pos, DEFAULT_STYLE_DARK);
109  str++;
110  index++;
111  pos += 2;
112  }
113  return index;
114 }
115 static inline int16_t newline() {
116  putch('\n');
117  putch('\r');
118  return 2;
119 }
120 static inline int16_t draw_str_style(char const* str, int row, int col, uint8_t style) {
121  int pos = (row * 80 + col) * 2;
122  int16_t index = 0;
123  while (*str != '\0') {
124  _draw_char(*str, pos, style);
125  str++;
126  index++;
127  pos += 2;
128  }
129  return index;
130 }
131 static inline int16_t draw_char_style(char ch, int row, int col, uint8_t style) {
132  int pos = (row * 80 + col) * 2;
133  return _draw_char(ch, pos, style);
134 }
137 static inline int16_t puts_style(char const* str, uint8_t style) {
138  int16_t index = 0;
139  while (*str) {
140  putch_style(*str, style);
141  str++;
142  index++;
143  }
144  return index;
145 }
146 static inline int16_t puts(char const* str) {
147  return puts_style(str, DEFAULT_STYLE);
148 }
149 static inline int16_t putln(char const* str) {
150  int16_t cnt = 0;
151  cnt += puts(str);
152  cnt += puts("\n\r");
153  return cnt;
154 }
159 static inline int16_t puti(int32_t num) {
160  char _buffer[30];
161  int index = 0;
162  if (num < 0) {
163  putch('-');
164  num = -num;
165  }
166  if (num == 0) {
167  putch('0');
168  return 0;
169  }
170  while (num) {
171  _buffer[index++] = num % 10 + '0';
172  num /= 10;
173  }
174  for (int i = index-1; i >= 0; --i) {
175  putch(_buffer[i]);
176  }
177  return index;
178 }
179 static inline int16_t putiln(int num) {
180  int16_t cnt = 0;
181  cnt += puti(num);
182  cnt += newline();
183  return cnt;
184 }
190 static inline int printf(const char* format, ...) {
191  va_list valist;
192  int16_t pcnt = 0;
193  int narg = 0;
194  int index = 0;
195  while (format[index]) {
196  if (format[index] == '%') narg++;
197  index++;
198  }
199 
200  va_start(valist, narg);
201 
202  index = 0;
203  while (format[index]) {
204  int16_t putSize = 0;
205  int16_t delta = 0;
206  int16_t digitLength = 0;
207  if (format[index] == '%') {
208  if ((format[index+1] >= '0' && format[index+1] <= '9') | format[index+1] == '-') {
209  // putln("get digit!");
210  digitLength = __uts_read_int(format + index + 1, &putSize);
211  // putiln(digitLength);
212  }
213  if (format[index + digitLength + 1] == 'd') {
214  int data = va_arg(valist, int);
215  delta = puti(data);
216  } else if (format[index+ digitLength + 1] == 'c') {
217  int c = va_arg(valist, int);
218  delta = putch(c);
219  } else if (format[index + digitLength + 1] == 's') {
220  char* str = va_arg(valist, char*);
221  delta = puts(str);
222  } else if (format[index + digitLength + 1] == '%'){
223  delta = putch('%');
224  }
225  if (delta < putSize) {
226  padding(putSize - delta);
227  }
228  pcnt += delta;
229  index += 2 + digitLength;
230  continue;
231  } else if (format[index] == '\n' || format[index] == '\r') {
232  pcnt += putch('\n');
233  pcnt += putch('\r');
234  } else {
235  pcnt += putch(format[index]);
236  }
237  index++;
238  }
239  va_end(valist);
240  return pcnt;
241 }
242 static inline int getch() {
243  return readkb();
244 }
245 static inline int16_t putn(const char* s, uint16_t size) {
246  for (int i = 0; i < size; ++i) {
247  if (!s[i]) {
248  return i;
249  }
250  putch(s[i]);
251  }
252  return size;
253 }
254 #endif
static int16_t putch_style(char ch, uint8_t style)
Definition: stdio.h:33
static int16_t __uts_read_int(const char *s, int16_t *readNum)
Definition: stdio.h:82
most importantly low level function used in kernel
static int16_t padding(int16_t num)
Definition: stdio.h:71
uint8_t style
Definition: timeout.c:16
static int16_t draw_str(char const *str, int row, int col)
Definition: stdio.h:104
int16_t row
Definition: kbhit.c:11
static int16_t draw_char_style(char ch, int row, int col, uint8_t style)
Definition: stdio.h:131
static int16_t newline()
Definition: stdio.h:115
static int16_t putch(char ch)
Definition: stdio.h:62
char ch
Definition: timeout.c:9
static int16_t puti(int32_t num)
Definition: stdio.h:159
static int16_t putn(const char *s, uint16_t size)
Definition: stdio.h:245
macro for graphic
static int16_t putiln(int num)
Definition: stdio.h:179
__asm__(".globl _start\)
static void scroll_up_one_line()
Definition: utilities.h:91
static uint16_t get_cursor()
Definition: utilities.h:45
static int16_t puts_style(char const *str, uint8_t style)
Definition: stdio.h:137
static void set_cursor(uint8_t row, uint8_t col)
Definition: utilities.h:26
#define DEFAULT_STYLE
Definition: stdio.h:12
static int printf(const char *format,...)
Definition: stdio.h:190
static int16_t putln(char const *str)
Definition: stdio.h:149
static int getch()
Definition: stdio.h:242
static int16_t draw_str_style(char const *str, int row, int col, uint8_t style)
Definition: stdio.h:120
#define DEFAULT_STYLE_DARK
Definition: stdio.h:11
static int16_t _draw_char(char ch, int offset, uint8_t style)
Definition: stdio.h:21
int readkb()
static int16_t puts(char const *str)
Definition: stdio.h:146