/*
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 __KLSH_ARGPARSE__
#define __KLSH_ARGPARSE__
#include <string>
#include <exec.h>
#include <input.h>
#include <variables.h>
class argument_parser {
public:
/* This class is to be initialised with a raw input string
* that we'll be working with.
*
* @param std::string cmd -- raw input from user.
* @param variables& variable_handler -- Our variable handler class reference
*/
argument_parser(std::string cmd, variables& variable_handler) : raw_input(cmd), variable_handler(variable_handler) { }
/* List of characters that affect the way commands are
* executed and how their input/output/.. are directed.
*
*/
static const char PIPE = '|';
static const char AND = '&';
static const char COMMENT = '#';
static const char EOL = ';';
static const char DIR_INTO = '>';
static const char DIR_FROM = '<';
static const char SPACE = ' ';
static const char DOLLAR = '$';
static const char EQUALS = '=';
/* Get next command from our user input
*
* @return command_handler::command if there's more commands to execute.
*/
command_handler::command get_next();
private:
std::string raw_input;
user_input_state state;
variables& variable_handler;
std::string extract_rest_of_argument() {
std::string name = "";
while (this->raw_input.length() > 0) {
char c = this->raw_input.at(0);
this->raw_input.erase(0, 1);
if (is_end_of_arg(c) || is_end_of_input(c)) {
break;
}
name += c;
}
return name;
}
bool is_variable(char c) {
return (c == DOLLAR);
}
bool is_variable_definition(char c) {
return (c == EQUALS);
}
bool is_modifier(char c) {
switch (c) {
case(PIPE):
case(AND):
case(DIR_INTO):
case(DIR_FROM):
return true;
default:
return false;
}
}
bool is_end_of_input(char c) {
return ((c == COMMENT) || (c == user_input::LF) || (c == EOL));
}
bool is_end_of_arg(char c) {
return (c == SPACE);
}
};
#endif