built_in.h

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.
 */

#ifndef __COMMAND_HANDLER__
#define __COMMAND_HANDLER__

#include <functional>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>

#include <variables.h>

extern bool exit_requested;

class built_in {
    public:
        /* Execute built-in command given to us by user.
         *
         * @param command_handler::command &cmd -- Command to execute
         * @param variables& var_handler -- variable handler reference
         * @return int return status
         */
        static int run(std::vector<std::string> argv, variables& var_handler) {
            std::string name = argv[0];
            func_t func = built_in::built_in_list.at(name);
            if (func) {
                return func(argv, var_handler);
            }
            std::cerr << "BUG: " << __FUNCTION__ << " called without valid built-in command" << std::endl;
            return 127;
        }

        /* Check if given command exists in built-in command list.
         *
         * @param std::string name -- name to check against
         * @return bool true if built-in with this name exists
         */
        static bool is_built_in(std::string name) {
            return (built_in::built_in_list.count(name) > 0);
        }

    private:
        static int exit(std::vector<std::string>__attribute__((unused)), variables&__attribute__((unused)));
        static int version(std::vector<std::string>__attribute((unused)), variables&__attribute__((unused)));
        static int unset(std::vector<std::string>, variables& variable_handler);
        static int history(std::vector<std::string>, variables&__attribute__((unused)));

        using func_t = std::function<int(std::vector<std::string>, variables&)>;

        static std::unordered_map<std::string, func_t> built_in_list;

};


#endif // __COMMAND_HANDLER__