LineEditor.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "LineEditor.h"
  2. #include "GlobalState.h"
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <ctype.h>
  6. LineEditor::LineEditor()
  7. {
  8. }
  9. LineEditor::~LineEditor()
  10. {
  11. }
  12. void LineEditor::add_to_history(const String& line)
  13. {
  14. if ((m_history.size() + 1) > m_history_capacity)
  15. m_history.take_first();
  16. m_history.append(line);
  17. }
  18. void LineEditor::clear_line()
  19. {
  20. for (int i = 0; i < m_cursor; ++i)
  21. fputc(0x8, stdout);
  22. fputs("\033[K", stdout);
  23. fflush(stdout);
  24. m_buffer.clear();
  25. m_cursor = 0;
  26. }
  27. void LineEditor::append(const String& string)
  28. {
  29. m_buffer.append(string.characters(), string.length());
  30. fputs(string.characters(), stdout);
  31. fflush(stdout);
  32. m_cursor = m_buffer.size();
  33. }
  34. String LineEditor::get_line()
  35. {
  36. m_history_cursor = m_history.size();
  37. m_cursor = 0;
  38. for (;;) {
  39. char keybuf[16];
  40. ssize_t nread = read(0, keybuf, sizeof(keybuf));
  41. // FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
  42. if (nread == 0)
  43. exit(0);
  44. if (nread < 0) {
  45. if (errno == EINTR) {
  46. if (g.was_interrupted) {
  47. if (!m_buffer.is_empty())
  48. printf("^C");
  49. }
  50. g.was_interrupted = false;
  51. m_buffer.clear();
  52. putchar('\n');
  53. return String::empty();
  54. }
  55. perror("read failed");
  56. // FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
  57. exit(2);
  58. }
  59. for (ssize_t i = 0; i < nread; ++i) {
  60. char ch = keybuf[i];
  61. if (ch == 0)
  62. continue;
  63. switch (m_state) {
  64. case InputState::ExpectBracket:
  65. if (ch == '[') {
  66. m_state = InputState::ExpectFinal;
  67. continue;
  68. } else {
  69. m_state = InputState::Free;
  70. break;
  71. }
  72. case InputState::ExpectFinal:
  73. switch (ch) {
  74. case 'A': // up
  75. if (m_history_cursor > 0)
  76. --m_history_cursor;
  77. clear_line();
  78. if (m_history_cursor < m_history.size())
  79. append(m_history[m_history_cursor]);
  80. m_state = InputState::Free;
  81. continue;
  82. case 'B': // down
  83. if (m_history_cursor < m_history.size())
  84. ++m_history_cursor;
  85. clear_line();
  86. if (m_history_cursor < m_history.size())
  87. append(m_history[m_history_cursor]);
  88. m_state = InputState::Free;
  89. continue;
  90. case 'D': // left
  91. if (m_cursor > 0) {
  92. --m_cursor;
  93. fputs("\033[D", stdout);
  94. fflush(stdout);
  95. }
  96. m_state = InputState::Free;
  97. continue;
  98. case 'C': // right
  99. if (m_cursor < m_buffer.size()) {
  100. ++m_cursor;
  101. fputs("\033[C", stdout);
  102. fflush(stdout);
  103. }
  104. m_state = InputState::Free;
  105. continue;
  106. case 'H':
  107. if (m_cursor > 0) {
  108. fprintf(stdout, "\033[%dD", m_cursor);
  109. fflush(stdout);
  110. m_cursor = 0;
  111. }
  112. m_state = InputState::Free;
  113. continue;
  114. case 'F':
  115. if (m_cursor < m_buffer.size()) {
  116. fprintf(stdout, "\033[%dC", m_buffer.size() - m_cursor);
  117. fflush(stdout);
  118. m_cursor = m_buffer.size();
  119. }
  120. m_state = InputState::Free;
  121. continue;
  122. default:
  123. dbgprintf("Shell: Unhandled final: %b (%c)\n", ch, ch);
  124. m_state = InputState::Free;
  125. continue;
  126. }
  127. break;
  128. case InputState::Free:
  129. if (ch == 27) {
  130. m_state = InputState::ExpectBracket;
  131. continue;
  132. }
  133. break;
  134. }
  135. if (ch == '\t') {
  136. // FIXME: Implement tab-completion.
  137. continue;
  138. }
  139. if (ch == 8 || ch == g.termios.c_cc[VERASE]) {
  140. if (m_cursor == 0)
  141. continue;
  142. m_buffer.remove(m_cursor - 1);
  143. --m_cursor;
  144. putchar(8);
  145. fputs("\033[s", stdout);
  146. fputs("\033[K", stdout);
  147. for (int i = m_cursor; i < m_buffer.size(); ++i)
  148. fputc(m_buffer[i], stdout);
  149. fputs("\033[u", stdout);
  150. fflush(stdout);
  151. continue;
  152. }
  153. if (ch == g.termios.c_cc[VWERASE]) {
  154. bool has_seen_nonspace = false;
  155. while (!m_buffer.is_empty()) {
  156. if (isspace(m_buffer.last())) {
  157. if (has_seen_nonspace)
  158. break;
  159. } else {
  160. has_seen_nonspace = true;
  161. }
  162. putchar(0x8);
  163. m_buffer.take_last();
  164. }
  165. fflush(stdout);
  166. continue;
  167. }
  168. if (ch == g.termios.c_cc[VKILL]) {
  169. if (m_buffer.is_empty())
  170. continue;
  171. for (int i = 0; i < m_buffer.size(); ++i)
  172. putchar(0x8);
  173. m_buffer.clear();
  174. fflush(stdout);
  175. continue;
  176. }
  177. putchar(ch);
  178. fflush(stdout);
  179. if (ch == '\n') {
  180. auto string = String::copy(m_buffer);
  181. m_buffer.clear();
  182. return string;
  183. }
  184. if (m_cursor == m_buffer.size()) {
  185. m_buffer.append(ch);
  186. ++m_cursor;
  187. continue;
  188. }
  189. fputs("\033[s", stdout);
  190. fputs("\033[K", stdout);
  191. for (int i = m_cursor; i < m_buffer.size(); ++i)
  192. fputc(m_buffer[i], stdout);
  193. fputs("\033[u", stdout);
  194. fflush(stdout);
  195. m_buffer.insert(m_cursor, move(ch));
  196. ++m_cursor;
  197. }
  198. }
  199. }