Skip to content

Commit

Permalink
Merge pull request #1 from Aparicio99/master
Browse files Browse the repository at this point in the history
Read builtin
  • Loading branch information
qiaomuf committed Jul 8, 2012
2 parents d5775bf + 8164155 commit f6fa493
Show file tree
Hide file tree
Showing 19 changed files with 640 additions and 71 deletions.
7 changes: 7 additions & 0 deletions Makefile.am
Expand Up @@ -105,6 +105,9 @@ cppunittests_SOURCES = test/run_tests.cpp \
src/builtins/tests/shift_tests.cpp \
src/builtins/tests/shopt_tests.cpp \
src/builtins/tests/return_tests.cpp \
src/builtins/tests/read_tests.cpp \
src/builtins/tests/set_tests.cpp \
src/builtins/tests/unset_tests.cpp \
src/builtins/tests/printf_tests.cpp \
test/test.h \
test/test.cpp \
Expand Down Expand Up @@ -236,6 +239,10 @@ libbash_la_SOURCES = include/common.h \
src/builtins/inherit_builtin.cpp \
src/builtins/unset_builtin.h \
src/builtins/unset_builtin.cpp \
src/builtins/read_builtin.h \
src/builtins/read_builtin.cpp \
src/builtins/set_builtin.h \
src/builtins/set_builtin.cpp \
src/builtins/builtin_exceptions.h \
$(GENERATED_PARSER_C) \
$(GENERATED_PARSER_H) \
Expand Down
9 changes: 5 additions & 4 deletions bashast/libbashWalker.g
Expand Up @@ -649,7 +649,7 @@ execute_command[std::string& name, std::vector<std::string>& libbash_args]
bool redirection = false;
}
@init {
if(name != "local")
if(name != "local" && name != "set")
current_scope.reset(new interpreter::local_scope(*walker));
}
:var_def[true]* (redirect[out, err, in]{ redirection = true; })* {
Expand Down Expand Up @@ -736,18 +736,19 @@ argument[std::vector<std::string>& args, bool split]
}
};
logic_command_list
logic_command
@declarations {
bool logic_and;
}
:command
|^((LOGICAND { logic_and = true; } | LOGICOR { logic_and = false; }) command {
: ^((LOGICAND { logic_and = true; } | LOGICOR { logic_and = false; }) logic_command_list {
if(logic_and ? !walker->get_status() : walker->get_status())
command(ctx);
else
seek_to_next_tree(ctx);
});
logic_command_list: command | logic_command;
command_list: ^(LIST logic_command_list+);
compound_command
Expand Down
3 changes: 3 additions & 0 deletions scripts/command_execution.bash
Expand Up @@ -17,6 +17,9 @@ false && echo "wrong"
false || echo "right"
true ||
echo "wrong"
echo right1 && echo right2 && false && echo wrong
false || echo right3 || echo wrong
true && false || echo right4 && echo right5
echo "end"
: ${DEFAULTED:="yes"}
FOO="abc" echo "command environment"
Expand Down
15 changes: 15 additions & 0 deletions scripts/function_def.bash
Expand Up @@ -76,6 +76,21 @@ func_positional_args() {
func_positional_args 1 2 3
IFS=" \t\n"

nested_func_override_positional_args() {
echo $@
set -- 40 50 60
echo $@
}
func_override_positional_args() {
echo $@
nested_func_override_positional_args 4 5 6
set -- 10 20 30
echo $@
}
set -- foo bar
func_override_positional_args 1 2 3
echo $@

if true; then
function_in_compound_statement() {
echo "function_in_compound_statement"
Expand Down
94 changes: 94 additions & 0 deletions src/builtins/read_builtin.cpp
@@ -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;
}
54 changes: 54 additions & 0 deletions src/builtins/read_builtin.h
@@ -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
81 changes: 81 additions & 0 deletions src/builtins/set_builtin.cpp
@@ -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;
}
}
46 changes: 46 additions & 0 deletions src/builtins/set_builtin.h
@@ -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

0 comments on commit f6fa493

Please sign in to comment.