- TTY_CLEAR
-
- int state = 0;
- int g[2] = { 0, 0 };
- vga_attribute current_theme = tty_theme_color;
- while (1) {
- size_t ptr = pos % buf_size;
- if (pos == limit) {
- break;
- }
- char chr = data[pos];
- if (state == 0 && chr == '\x033') {
- state = 1;
- } else if (state == 1 && chr == '[') {
- state = 2;
- } else if (state > 1) {
- if ('0' <= chr && chr <= '9') {
- g[state - 2] = (chr - '0') + g[state - 2] * 10;
- } else if (chr == ';' && state == 2) {
- state = 3;
- } else {
- if (g[0] == 39 && g[1] == 49) {
- current_theme = tty_theme_color;
- } else {
- current_theme = (g[1] << 4 | g[0]) << 8;
- }
- g[0] = 0;
- g[1] = 0;
- state = 0;
- }
- } else {
- state = 0;
- switch (chr) {
- case '\t':
- x += 4;
- break;
- case '\n':
- y++;
- // fall through
- case '\r':
- x = 0;
- break;
- case '\x08':
- x = x ? x - 1 : 0;
- *(tty_vga_buffer + x + y * TTY_WIDTH) =
- (current_theme | 0x20);
- break;
- default:
- *(tty_vga_buffer + x + y * TTY_WIDTH) =
- (current_theme | chr);
- (x)++;
- break;
- }
-
- if (x >= TTY_WIDTH) {
- x = 0;