shell.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 <string>
#include <termios.h>

#include <argparse.h>
#include <exec.h>
#include <input.h>
#include <history.h>
#include <variables.h>

float version_number = 0.1;

bool exit_requested = false;

struct termios attrib_backup;

void reset_input_mode(void) {
    tcsetattr(0, TCSANOW, &attrib_backup);
}
void termios_init(void) {
    struct termios term_attr;
    
    tcgetattr(0, &attrib_backup);
    atexit(reset_input_mode);
    tcgetattr(0, &term_attr);
    term_attr.c_lflag &= ~(ICANON|ECHO);
    term_attr.c_cc[VMIN] = 1;
    term_attr.c_cc[VTIME] = 0;
    tcsetattr(0, TCSAFLUSH, &term_attr);
}

int main() {
    termios_init();
    user_input in;
    history hist(".klsh_history");
    variables variable_handler;
    command_handler cmd_handler(variable_handler);
    command_handler::command cmd;
    variable_handler.set_variable("PATH", "/bin");
    variable_handler.set_variable("PS1", "klsh: ");

    while (exit_requested == false) {
        try {
            std::cout << variable_handler.get_variable("PS1");
            std::string raw = in.read();
            if (raw == "") {
                continue;
            }
            hist.log(raw);
            argument_parser parser(raw, variable_handler);

            int stat = 0;
            do {
                cmd = parser.get_next();
                if (cmd.argument_vector.size() == 0) {
                    break;
                }
                stat = cmd_handler.run(cmd);
            } while (cmd.argument_vector.size() > 0);
            variable_handler.set_variable("?", std::to_string(stat));

        } catch (std::runtime_error& err) {
            std::cerr << err.what() << std::endl;
            return 1;
        }
    }

    return 0;
}