| 682 | } |
| 683 | |
| 684 | Input::Input() |
| 685 | { |
| 686 | if (!s_termios_initialized) |
| 687 | { |
| 688 | tcgetattr(0, &s_original_termios); |
| 689 | s_raw_termios = s_original_termios; |
| 690 | s_raw_termios.c_lflag &= ~(ECHO | ICANON); |
| 691 | atexit([] { tcsetattr(0, TCSANOW, &s_original_termios); }); |
| 692 | s_termios_initialized = true; |
| 693 | |
| 694 | m_backspace = s_original_termios.c_cc[VERASE]; |
| 695 | } |
| 696 | |
| 697 | char hostname_buffer[HOST_NAME_MAX]; |
| 698 | if (gethostname(hostname_buffer, sizeof(hostname_buffer)) == 0) { |
| 699 | MUST(m_hostname.append(hostname_buffer)); |
| 700 | } |
| 701 | |
| 702 | if (const char* home_env = getenv("HOME")) |
| 703 | { |
| 704 | MUST(s_history_path.append(home_env)); |
| 705 | MUST(s_history_path.append("/.shell_history")); |
| 706 | |
| 707 | atexit([] { |
| 708 | FILE* history_fp = fopen(s_history_path.data(), "w"); |
| 709 | if (history_fp == nullptr) |
| 710 | return; |
| 711 | |
| 712 | for (const auto& line : s_history) |
| 713 | { |
| 714 | fwrite(line.data(), line.size(), 1, history_fp); |
| 715 | fputc('\n', history_fp); |
| 716 | } |
| 717 | |
| 718 | fclose(history_fp); |
| 719 | }); |
| 720 | |
| 721 | FILE* history_fp = fopen(s_history_path.data(), "r"); |
| 722 | if (history_fp == nullptr) |
| 723 | goto history_initialized; |
| 724 | |
| 725 | char buffer[128]; |
| 726 | BAN::String current_line; |
| 727 | while (fgets(buffer, sizeof(buffer), history_fp)) |
| 728 | { |
| 729 | MUST(current_line.append(buffer)); |
| 730 | if (current_line.back() != '\n') |
| 731 | continue; |
| 732 | current_line.pop_back(); |
| 733 | |
| 734 | MUST(s_history.push_back(BAN::move(current_line))); |
| 735 | if (s_history.size() > s_history_max_size) |
| 736 | s_history.remove(0); |
| 737 | current_line.clear(); |
| 738 | } |
| 739 | |
| 740 | fclose(history_fp); |
| 741 |
nothing calls this directly
no test coverage detected