aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-06-02 19:27:04 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-06-03 21:31:00 +0800
commit5454166103257b3f19b829dbc2c824ad67acf344 (patch)
tree20204509efde1d1bb880d37139188e1615eac009 /utils
parentCore: fix header ordering (diff)
downloadlibbash-5454166103257b3f19b829dbc2c824ad67acf344.tar.gz
libbash-5454166103257b3f19b829dbc2c824ad67acf344.tar.bz2
libbash-5454166103257b3f19b829dbc2c824ad67acf344.zip
Core: make error handling POSIX compliant
For a non-interactive shell, an error condition encountered by a special built-in or other type of utility shall cause the shell to write a diagnostic message to standard error and exit.
Diffstat (limited to 'utils')
-rw-r--r--utils/ast_printer.cpp66
-rw-r--r--utils/variable_printer.cpp10
2 files changed, 41 insertions, 35 deletions
diff --git a/utils/ast_printer.cpp b/utils/ast_printer.cpp
index b7dcfc1..e55df78 100644
--- a/utils/ast_printer.cpp
+++ b/utils/ast_printer.cpp
@@ -34,6 +34,7 @@
#include <boost/fusion/include/adapt_struct.hpp>
#include "core/bash_ast.h"
+#include "core/interpreter_exception.h"
#include "libbashParser.h"
namespace po = boost::program_options;
@@ -48,19 +49,17 @@ BOOST_FUSION_ADAPT_STRUCT(
(ANTLR3_INT32, first)
)
-static int print_ast(std::istream& input, bool silent, bool dot)
+static void print_ast(std::istream& input, bool silent, bool dot)
{
bash_ast ast(input);
if(silent)
- return ast.get_error_count();
+ return;
if(dot)
std::cout << ast.get_dot_graph() << std::endl;
else
std::cout << ast.get_string_tree() << std::endl;
-
- return ast.get_error_count();
}
static inline std::string token_mapper(std::unordered_map<ANTLR3_INT32, std::string> token_map,
@@ -85,58 +84,49 @@ static bool build_token_map(std::unordered_map<ANTLR3_INT32, std::string>& token
return qi::parse(first, last, line % qi::eol >> qi::eol, token_map) && first == last;
}
-static int print_token(std::istream& input,
+static void print_token(std::istream& input,
const std::string& token_path,
bool silent)
{
if(silent)
- return 0;
+ return;
bash_ast ast(input);
std::unordered_map<ANTLR3_INT32, std::string> token_map;
if(build_token_map(token_map, token_path))
- {
std::cout << ast.get_tokens(std::bind(&token_mapper,
token_map,
std::placeholders::_1))
<< std::endl;
- return ast.get_error_count();
- }
else
- {
std::cerr << "Building token map failed" << std::endl;
- return ast.get_error_count() + 1;
- }
}
-static int print_files(const std::vector<std::string>& files,
+static void print_files(const std::vector<std::string>& files,
bool print_name,
- std::function<int(std::istream&)> printer)
+ std::function<void(std::istream&)> printer)
{
- int result = 0;
for(auto iter = files.begin(); iter != files.end(); ++iter)
{
if(print_name)
std::cout << "Interpreting " << *iter << std::endl;
std::ifstream input(iter->c_str());
- result += printer(input);
+ printer(input);
}
-
- return result;
}
-static inline int print_expression(const std::string& expr,
- std::function<int(std::istream&)> printer)
+static inline void print_expression(const std::string& expr,
+ std::function<void(std::istream&)> printer)
{
std::istringstream input(expr);
- return printer(input);
+ printer(input);
}
-static inline int print_cin(std::function<int(std::istream&)> printer)
+static inline void print_cin(std::function<void(std::istream&)> printer)
{
- return printer(std::cin);
+ printer(std::cin);
}
int main(int argc, char** argv)
@@ -168,7 +158,7 @@ int main(int argc, char** argv)
return EXIT_FAILURE;
}
- std::function<int(std::istream&)> printer;
+ std::function<void(std::istream&)> printer;
if(vm.count("token"))
printer = std::bind(&print_token,
std::placeholders::_1,
@@ -180,15 +170,23 @@ int main(int argc, char** argv)
vm.count("silent"),
vm.count("dot"));
- int result;
- if(vm.count("files"))
- result = print_files(vm["files"].as<std::vector<std::string>>(),
- vm.count("name"),
- printer);
- else if(vm.count("expr"))
- result = print_expression(vm["expr"].as<std::string>(), printer);
- else
- result = print_cin(printer);
+ try
+ {
+ if(vm.count("files"))
+ print_files(vm["files"].as<std::vector<std::string>>(),
+ vm.count("name"),
+ printer);
+ else if(vm.count("expr"))
+ print_expression(vm["expr"].as<std::string>(), printer);
+ else
+ print_cin(printer);
+ }
+ catch(interpreter_exception& e)
+ {
+ if(!vm.count("silent"))
+ std::cerr << e.what() << std::endl;
+ return EXIT_FAILURE;
+ }
- return result;
+ return EXIT_SUCCESS;
}
diff --git a/utils/variable_printer.cpp b/utils/variable_printer.cpp
index 001e75e..252ee9b 100644
--- a/utils/variable_printer.cpp
+++ b/utils/variable_printer.cpp
@@ -46,7 +46,15 @@ int main(int argc, char** argv)
std::unordered_map<std::string, std::vector<std::string>> variables;
std::vector<std::string> functions;
- libbash::interpret(argv[1], variables, functions);
+ try
+ {
+ libbash::interpret(argv[1], variables, functions);
+ }
+ catch(interpreter_exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ return EXIT_FAILURE;
+ }
std::map<std::string, std::vector<std::string>> sorted(variables.begin(), variables.end());
// Currently we don't need internal variables