exec.h

back


/*
 BSD 3-Clause License
 
 Copyright (c) 2025, 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.
 */
#ifndef __KLSH_EXEC__
#define __KLSH_EXEC__

#include <fstream>
#include <string>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

#include <built_in.h>
#include <variables.h>

class command_handler {
    public:
        typedef struct {
            std::vector<std::string> argument_vector;
        } command;
        
        /* Run a command given to us by user. We'll handle
         * resolving command type, and then call appropriate
         * exec_... function.
         *
         * @param command_handler::command cmd -- Command to run
         * @return int exit status of command
         */
         int run(command& cmd);

         command_handler(variables& variable_handler) : variable_handler(variable_handler) { }
    private:
        variables& variable_handler;

        enum command_type {
            BUILT_IN,
            PATH_GIVEN,
            PATH_TO_BE_RESOLVED
        };

        /* Check if our program is in current directory of $PATH
         *
         * @param std::string& name -- Absolute path to program
         * @return bool true if program exists
         */
        static inline bool program_is_in_directory(std::string &name) {
            std::ifstream f(name, std::ios::binary);
            return f.is_open();
        }

        /* Resolve path for a given program
         *
         * @param std::string prog -- Program to find
         * @return std::string path -- Path to program if one is found, "" otherwise
         */
        std::string get_path_to_program(std::string& prog) {
            std::string path = variable_handler.get_variable("PATH");
            if (path == "") {
                return "";
            }
            std::string entry;
            std::stringstream stream(path);
            while (std::getline(stream, entry, ':')) {
                std::string full_path = entry + "/" + prog;
                if (program_is_in_directory(full_path)) {
                    return full_path;
                }
            }
            return "";
        }

        /* Figure out if given command is 
         * built-in, relative or absolute path to program
         * to execute, or if we should find it from $PATH
         *
         * @param std::string cmd_string -- Command string to check
         * @return enum command_type
         */
        enum command_type get_command_type(std::string& cmd_string) {
            if (cmd_string.rfind("/", 0) == 0) {
                // This is an absolute path to program
                //
                return PATH_GIVEN;
            }
            if (cmd_string.rfind(".", 0) == 0) {
                // This is a relative path to program
                //
                return PATH_GIVEN;
            }
            if (built_in::is_built_in(cmd_string)) {
                return BUILT_IN;
            }
            return PATH_TO_BE_RESOLVED;
        }

        /* Execute command given at cmd structure
         * using exec family of functions.
         *
         * @param std::string& prog -- Absolute path to program to execute
         * @param command& cmd -- Command to execute
         * @return int exit_status
         */
        int exec(std::string& prog, command& cmd);

};

#endif // __KLSH_EXEC__