input.cc

back


/*
 BSD 3-Clause License
 
 Copyright (c) 2024, k4m1 <me@k4m1.net>
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
 
 3. Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <iostream>
#include <stdexcept>
#include <string>

#include <stdlib.h>

#include <input.h>

bool user_input_state::update_from_keypress(char key) {
    if (this->escaped == true) {
        this->escaped = false;
        return true;
    }

    switch (key) {
    case (user_input::BACKSLASH):
        this->escaped = true;
        break;
    case (user_input::LF):
        this->toggle_state(this->DONE);
        break;
    case (user_input::DQUOTE):
        this->toggle_state(this->DQUOTED);
        break;
    case (user_input::SQUOTE):
        this->toggle_state(this->SQUOTED);
        break;
    case (user_input::BACKQUOTE):
        this->toggle_state(this->BACKQUOTED);
        break;
    default:
        return false;
    }
    return true;
}

void user_input::push_character(char c) {
    if (this->offset < 0) {
        this->input_string += c;
    } else {
        this->input_string.insert(this->offset, &c);
    }
}

void user_input::handle_backspace() {
    if (this->input_string.length() > 0) {
        this->state.update_from_keypress(this->input_string.back());
        this->input_string.pop_back();
        clear_character_from_screen();
    }
}

unsigned short read_esc_cmd() {
    char esc[2];

    for (int i = 0; i < 2; i++) {
        if (!std::cin.get(esc[i])) {
            throw std::runtime_error("Lost stdin");
        }
    }
    return ((esc[0] << 8) | esc[1]);
}

void user_input::handle_esc_cmd() {
    unsigned short cmd = read_esc_cmd();

}

void user_input::handle_control_character(char c) {
    switch (c) {
    case (user_input::NUL):
        /* Ignore for now */
        break;
    case (user_input::BEL):
        std::cout << "\nBEEP BOOP" << std::endl;
        break;
    case (user_input::HT):
        this->offset = -1;
        break;
    case (user_input::BS):
        handle_backspace();
        break;
    case (user_input::VT):
    case (user_input::FF):
    case (user_input::LF):
        this->state.update_from_keypress(c);
        std::cout << c << std::flush;
        break;
    case (user_input::CR):
        this->offset = 0;
        break;
    case (user_input::SI):
    case (user_input::SO):
        /* Ignore for now */
        break;
    case (user_input::CAN):
    case (user_input::SUB):
        /* Ignore, escape sequence handler deals
         * with these when appropriate.
         */
        break;
    case (user_input::ESC):
        // Handle escape sequences eventually
        //
        // ok let's get to it
        //
        handle_esc_cmd();
        break;
    case (user_input::DEL):
        handle_backspace();
        break;
    }
}

/* Read a new command from user.
 *
 * @return std::string user input
 */
std::string user_input::read() {
    this->input_string = "";
    this->state.reset();
    char key;

    do {
        if (!std::cin.get(key)) {
            throw std::runtime_error("Lost stdin");
        }
        if (key_is_command(key)) {
            handle_control_character(key);
            continue;
        }
        this->state.update_from_keypress(key);
        push_character(key);
        std::cout << key << std::flush;
    } while (this->state.is_done() == false);

    return this->input_string;
}