From ea2085d8d32497f7f6d403e9e0b745443ad1f43d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Apar=C3=ADcio?= Date: Thu, 5 Apr 2012 02:19:28 +0100 Subject: [PATCH] Builtin: implement read builtin --- Makefile.am | 2 + src/builtins/read_builtin.cpp | 83 +++++++++++++++++++++++++++++++++++++++++ src/builtins/read_builtin.h | 46 ++++++++++++++++++++++ src/cppbash_builtin.cpp | 2 + 4 files changed, 133 insertions(+), 0 deletions(-) create mode 100644 src/builtins/read_builtin.cpp create mode 100644 src/builtins/read_builtin.h diff --git a/Makefile.am b/Makefile.am index cbc9f3b..c923675 100644 --- a/Makefile.am +++ b/Makefile.am @@ -236,6 +236,8 @@ 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/builtin_exceptions.h \ $(GENERATED_PARSER_C) \ $(GENERATED_PARSER_H) \ diff --git a/src/builtins/read_builtin.cpp b/src/builtins/read_builtin.cpp new file mode 100644 index 0000000..7d656d1 --- /dev/null +++ b/src/builtins/read_builtin.cpp @@ -0,0 +1,83 @@ +/* + 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 . +*/ +/// +/// \file read_builtin.cpp +/// \brief class that implements the read builtin +/// + +#include "builtins/read_builtin.h" + +#include + +#include "core/interpreter.h" +#include "builtins/builtin_exceptions.h" + +int read_builtin::exec(const std::vector& bash_args) +{ + std::string input; + std::stringstream formated_input; + + getline(this->input_buffer(), input); + + while(input[input.length()-1] == '\\') { + input.erase(input.end()-1); + std::string input_line; + getline(this->input_buffer(), input_line); + input += input_line; + } + + cppbash_builtin::transform_escapes(input, formated_input, false); + + if(bash_args.empty()) + { + _walker.set_value("REPLY", formated_input.str()); + return 0; + } + + char *cstr, *token; + cstr = new char[formated_input.str().size() + 1]; + strcpy(cstr, formated_input.str().c_str()); + + for(auto i = bash_args.begin(); i != bash_args.end(); i++) + { + if(i != bash_args.end() - 1) + { + if(i == bash_args.begin()) + token = strtok(cstr, " "); + else + token = strtok(NULL, " "); + + if(token == NULL) + return 0; + } + else + { + if(i == bash_args.begin()) + token = cstr; + else + token += strlen(token) + 1; + } + + _walker.set_value(*i, token); + } + + free(cstr); + + return 0; +} diff --git a/src/builtins/read_builtin.h b/src/builtins/read_builtin.h new file mode 100644 index 0000000..5e67f6d --- /dev/null +++ b/src/builtins/read_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 . +*/ +/// +/// \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& bash_args); +}; + +#endif diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp index 452fe64..c93b94a 100644 --- a/src/cppbash_builtin.cpp +++ b/src/cppbash_builtin.cpp @@ -44,6 +44,7 @@ #include "builtins/shopt_builtin.h" #include "builtins/source_builtin.h" #include "builtins/unset_builtin.h" +#include "builtins/read_builtin.h" namespace qi = boost::spirit::qi; namespace karma = boost::spirit::karma; @@ -73,6 +74,7 @@ cppbash_builtin::builtins_type& cppbash_builtin::builtins() { {"printf", boost::factory()}, {"let", boost::factory()}, {"unset", boost::factory()}, + {"read", boost::factory()}, }); return *p; } -- 1.7.3.4