terminal.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Terminal-in-a-window: per-instance state and the prompt/echo loop. */
  2. #ifndef TERMINAL_H
  3. #define TERMINAL_H
  4. #include "sched.h"
  5. #include "screen.h"
  6. #include "vfs.h"
  7. #define CURSOR_MS 500
  8. #define LINE_MAX 64
  9. #define HISTORY_SIZE 32
  10. #define KEY_BUF_SIZE 16
  11. struct term {
  12. struct text_screen screen;
  13. char history[HISTORY_SIZE][LINE_MAX];
  14. unsigned int history_count;
  15. unsigned int history_next;
  16. char line[LINE_MAX];
  17. unsigned int line_len;
  18. unsigned int line_pos;
  19. unsigned int line_start;
  20. unsigned int history_browse;
  21. int fg_pid; /* foreground child pid, or -1 when none */
  22. char cwd[VFS_PATH_MAX]; /* absolute current working directory */
  23. char path[VFS_PATH_MAX]; /* command search path (colon separated) */
  24. /* Per-terminal key queue: the kworker pushes, the terminal pops. */
  25. int keys[KEY_BUF_SIZE];
  26. unsigned int key_head;
  27. unsigned int key_tail;
  28. struct wait_queue key_wq;
  29. };
  30. /* Entry point for a terminal process (arg = struct term *). */
  31. void term_entry(void *arg);
  32. void term_run(struct term *t);
  33. /* Ring-3 terminal: run a privileged kernel command as a kernel process with
  34. * output on `tty` (the command table and handlers stay in terminal.c). */
  35. int term_kcmd_spawn(const char *name, const char *args, unsigned int tty);
  36. /* Single-producer (WM) / single-consumer (terminal) key queue. */
  37. static inline void term_key_push(struct term *t, int k)
  38. {
  39. unsigned int next = (t->key_head + 1) % KEY_BUF_SIZE;
  40. if (next != t->key_tail) {
  41. t->keys[t->key_head] = k;
  42. t->key_head = next;
  43. }
  44. }
  45. static inline int term_key_pop(struct term *t)
  46. {
  47. int k;
  48. if (t->key_tail == t->key_head)
  49. return 0;
  50. k = t->keys[t->key_tail];
  51. t->key_tail = (t->key_tail + 1) % KEY_BUF_SIZE;
  52. return k;
  53. }
  54. static inline void term_key_wake(struct term *t)
  55. {
  56. wait_wake_one(&t->key_wq);
  57. }
  58. #endif