forked from betelgeuse/libbash
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Aparicio99/master
Read builtin
- Loading branch information
Showing
19 changed files
with
640 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Please use git log for copyright holder and year information | ||
This file is part of libbash. | ||
libbash is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
libbash is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libbash. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/// | ||
/// \file read_builtin.cpp | ||
/// \brief class that implements the read builtin | ||
/// | ||
|
||
#include "builtins/read_builtin.h" | ||
|
||
#include <string.h> | ||
#include <boost/algorithm/string.hpp> | ||
|
||
#include "core/interpreter.h" | ||
#include "builtins/builtin_exceptions.h" | ||
|
||
void read_builtin::process(const std::vector<std::string>& args, const std::string& input) | ||
{ | ||
std::vector<std::string> split_input; | ||
boost::split(split_input, input, boost::is_any_of(" ")); | ||
|
||
auto vars = args.begin(); | ||
for(auto words = split_input.begin(); vars != args.end() && words != split_input.end(); ++vars, ++words) | ||
{ | ||
if(vars != args.end() - 1) | ||
{ | ||
_walker.set_value(*vars, *words); | ||
} | ||
else | ||
{ | ||
std::string rest; | ||
for(; words != split_input.end() - 1; ++words) | ||
rest += *words + " "; | ||
rest += *words; | ||
_walker.set_value(*vars, rest); | ||
} | ||
} | ||
|
||
for(; vars != args.end(); ++vars) | ||
_walker.set_value(*vars, ""); | ||
} | ||
|
||
int read_builtin::exec(const std::vector<std::string>& bash_args) | ||
{ | ||
int return_value = 0; | ||
std::string input; | ||
std::stringstream formated_input; | ||
|
||
getline(this->input_buffer(), input); | ||
|
||
if(this->input_buffer().eof()) | ||
return_value = 1; | ||
|
||
if(input.size() < 1) | ||
return return_value; | ||
|
||
while(input[input.length()-1] == '\\') { | ||
input.erase(input.end()-1); | ||
std::string input_line; | ||
getline(this->input_buffer(), input_line); | ||
|
||
if(this->input_buffer().eof()) | ||
return_value = 1; | ||
|
||
if(input.size() < 1) | ||
return return_value; | ||
|
||
input += input_line; | ||
} | ||
|
||
cppbash_builtin::transform_escapes(input, formated_input, false); | ||
|
||
if(bash_args.empty()) | ||
process({"REPLY"}, formated_input.str()); | ||
else | ||
process(bash_args, formated_input.str()); | ||
|
||
return return_value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Please use git log for copyright holder and year information | ||
This file is part of libbash. | ||
libbash is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
libbash is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libbash. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/// | ||
/// \file read_builtin.h | ||
/// \brief class that implements the read builtin | ||
/// | ||
|
||
#ifndef LIBBASH_BUILTINS_READ_BUILTIN_H_ | ||
#define LIBBASH_BUILTINS_READ_BUILTIN_H_ | ||
|
||
#include "../cppbash_builtin.h" | ||
|
||
/// | ||
/// \class read_builtin | ||
/// \brief the read builtin for bash | ||
/// | ||
class read_builtin: public virtual cppbash_builtin | ||
{ | ||
public: | ||
BUILTIN_CONSTRUCTOR(read) | ||
|
||
/// | ||
/// \brief runs the read builtin on the supplied arguments | ||
/// \param bash_args the arguments to the read builtin | ||
/// \return exit status of read | ||
/// | ||
virtual int exec(const std::vector<std::string>& bash_args); | ||
|
||
private: | ||
/// | ||
/// \brief assigns words from input to the variables | ||
/// \param args the variables to be used | ||
/// \param input the input to be assigned to the variables | ||
/// | ||
virtual void process(const std::vector<std::string>& bash_args, const std::string& input); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Please use git log for copyright holder and year information | ||
This file is part of libbash. | ||
libbash is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
libbash is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libbash. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/// | ||
/// \file set_builtin.cpp | ||
/// \brief class that implements the set builtin | ||
/// | ||
#include "builtins/set_builtin.h" | ||
|
||
#include "core/interpreter.h" | ||
#include "exceptions.h" | ||
|
||
int set_builtin::exec(const std::vector<std::string>& bash_args) | ||
{ | ||
if(bash_args.empty()) | ||
{ | ||
throw libbash::unsupported_exception("set: variables printing are not supported"); | ||
return 1; | ||
} | ||
|
||
if(bash_args[0][0] != '-' && bash_args[0][0] != '+') | ||
{ | ||
throw libbash::illegal_argument_exception("set: invalid option"); | ||
return 1; | ||
} | ||
|
||
switch(bash_args[0][1]) | ||
{ | ||
case '-': | ||
if(bash_args[0][0] != '-') { | ||
throw libbash::unsupported_exception("set: invalid option"); | ||
return 1; | ||
} | ||
else | ||
{ | ||
_walker.define_positional_arguments(bash_args.begin() + 1, bash_args.end()); | ||
return 0; | ||
} | ||
case 'a': | ||
case 'b': | ||
case 'e': | ||
case 'f': | ||
case 'h': | ||
case 'k': | ||
case 'm': | ||
case 'n': | ||
case 'p': | ||
case 't': | ||
case 'u': | ||
_walker.set_option('u', bash_args[0][0] == '-'); | ||
return 0; | ||
case 'v': | ||
case 'x': | ||
case 'B': | ||
case 'C': | ||
case 'E': | ||
case 'H': | ||
case 'P': | ||
case 'T': | ||
throw libbash::unsupported_exception("set " + bash_args[0] + " is not supported yet"); | ||
return 1; | ||
default: | ||
throw libbash::illegal_argument_exception("set: unrecognized option: " + bash_args[0]); | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Please use git log for copyright holder and year information | ||
This file is part of libbash. | ||
libbash is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 2 of the License, or | ||
(at your option) any later version. | ||
libbash is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libbash. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/// | ||
/// \file set_builtin.h | ||
/// \brief class that implements the set builtin | ||
/// | ||
|
||
#ifndef LIBBASH_BUILTINS_SET_BUILTIN_H_ | ||
#define LIBBASH_BUILTINS_SET_BUILTIN_H_ | ||
|
||
#include "cppbash_builtin.h" | ||
|
||
/// | ||
/// \class set_builtin | ||
/// \brief the set builtin for bash | ||
/// | ||
class set_builtin: public virtual cppbash_builtin | ||
{ | ||
public: | ||
BUILTIN_CONSTRUCTOR(set) | ||
|
||
/// | ||
/// \brief runs the set builtin on the supplied arguments | ||
/// \param bash_args the arguments to the set builtin | ||
/// \return exit status of set | ||
/// | ||
virtual int exec(const std::vector<std::string>& bash_args); | ||
}; | ||
|
||
#endif |
Oops, something went wrong.