terminal.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch. */
  2. #include "io.h"
  3. #include "rtc.h"
  4. #include "terminal.h"
  5. #include "timer.h"
  6. extern void program_hello_main(void);
  7. #define DATETIME_MS 1000
  8. #define LINE_MAX 64
  9. static unsigned int last_datetime_tick;
  10. static char datetime_buf[IO_DATETIME_LEN + 1];
  11. static int cursor_visible;
  12. static unsigned int last_cursor_tick;
  13. static void redraw_datetime(void)
  14. {
  15. rtc_read_datetime(datetime_buf);
  16. {
  17. unsigned int i;
  18. for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
  19. io_vga_write(IO_DATETIME_POS + i,
  20. (unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
  21. }
  22. }
  23. static void update_datetime(void)
  24. {
  25. unsigned int t = get_ticks_ms();
  26. if (t - last_datetime_tick >= DATETIME_MS) {
  27. last_datetime_tick = t;
  28. redraw_datetime();
  29. }
  30. }
  31. static void update_cursor_blink(void)
  32. {
  33. unsigned int t = get_ticks_ms();
  34. if (t - last_cursor_tick >= CURSOR_MS) {
  35. last_cursor_tick = t;
  36. cursor_visible = !cursor_visible;
  37. }
  38. if (cursor_visible)
  39. io_cursor_show_block();
  40. else
  41. io_cursor_hide_block();
  42. }
  43. static const char banner[] __attribute__((section(".text"))) = "Nebs OS |o|";
  44. #define BANNER_LEN 12
  45. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  46. static const char prompt_first[] __attribute__((section(".text"))) = "> ";
  47. /* Draw row 0: banner left, datetime right. Call when terminal is active. */
  48. static void draw_header_row(void)
  49. {
  50. unsigned int i;
  51. for (i = 0; i < BANNER_LEN && banner[i]; i++)
  52. io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
  53. for (; i < IO_DATETIME_POS; i++)
  54. io_vga_write(i, (unsigned short)(0x0A << 8));
  55. redraw_datetime();
  56. }
  57. static int is_cmd_hello(const char *line, unsigned int line_len)
  58. {
  59. unsigned int i = 0;
  60. while (i < line_len && line[i] == ' ')
  61. i++;
  62. if (i + 5 > line_len)
  63. return 0;
  64. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  65. return 0;
  66. i += 5;
  67. while (i < line_len && line[i] == ' ')
  68. i++;
  69. return (i == line_len);
  70. }
  71. void terminal_run(void)
  72. {
  73. static char line[LINE_MAX];
  74. static unsigned int line_len;
  75. last_cursor_tick = get_ticks_ms();
  76. last_datetime_tick = get_ticks_ms();
  77. cursor_visible = 1;
  78. line_len = 0;
  79. line[0] = '\0';
  80. io_drain_keyboard();
  81. draw_header_row();
  82. io_putstr(prompt_first);
  83. for (;;) {
  84. update_datetime();
  85. update_cursor_blink();
  86. {
  87. char c = io_poll_key();
  88. if (c) {
  89. if (c == '\b') {
  90. if (line_len > 0) {
  91. line_len--;
  92. line[line_len] = '\0';
  93. io_putchar(c);
  94. }
  95. } else if (c == '\n') {
  96. io_putchar(c);
  97. if (is_cmd_hello(line, line_len)) {
  98. program_hello_main();
  99. io_drain_keyboard();
  100. draw_header_row();
  101. }
  102. line_len = 0;
  103. line[0] = '\0';
  104. io_putstr(prompt);
  105. if (io_did_scroll())
  106. redraw_datetime();
  107. } else {
  108. {
  109. unsigned char u = (unsigned char)c;
  110. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  111. line[line_len++] = c;
  112. line[line_len] = '\0';
  113. }
  114. }
  115. io_putchar(c);
  116. }
  117. } else {
  118. __asm__ volatile ("hlt");
  119. }
  120. }
  121. }
  122. }