Commit 645bb556 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz

bin: factor exception-handling code

* bin/common_setup.cc, bin/common_setup.hh: Define a protected_main()
function that deal with exceptions.
* bin/autcross.cc, bin/autfilt.cc, bin/dstar2tgba.cc, bin/genaut.cc,
bin/genltl.cc, bin/ltl2tgba.cc, bin/ltl2tgta.cc, bin/ltlcross.cc,
bin/ltldo.cc, bin/ltlfilt.cc, bin/ltlgrind.cc, bin/ltlsynt.cc,
bin/randaut.cc, bin/randltl.cc: Use it for all tools.
parent fc0ed01a
Pipeline #1730 passed with stages
in 225 minutes and 24 seconds
// -*- coding: utf-8 -*-
// Copyright (C) 2017 Laboratoire de Recherche et Développement de
// Copyright (C) 2017, 2018 Laboratoire de Recherche et Développement de
// l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
......@@ -822,97 +822,97 @@ print_stats_csv(const char* filename)
int
main(int argc, char** argv)
{
setup(argv);
return protected_main(argv, [&]() -> unsigned {
const argp ap = { options, parse_opt, "[COMMANDFMT...]",
argp_program_doc, children, nullptr, nullptr };
const argp ap = { options, parse_opt, "[COMMANDFMT...]",
argp_program_doc, children, nullptr, nullptr };
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
check_no_automaton();
check_no_automaton();
auto s = tools.size();
if (s == 0)
error(2, 0, "No tool to run? Run '%s --help' for usage.",
program_name);
auto s = tools.size();
if (s == 0)
error(2, 0, "No tool to run? Run '%s --help' for usage.",
program_name);
if (s == 1 && !opt_language_preserved && !no_checks)
error(2, 0, "Since --language-preserved is not used, you need "
"at least two tools to compare.");
if (s == 1 && !opt_language_preserved && !no_checks)
error(2, 0, "Since --language-preserved is not used, you need "
"at least two tools to compare.");
setup_color();
setup_sig_handler();
setup_color();
setup_sig_handler();
autcross_processor p;
if (p.run())
return 2;
autcross_processor p;
if (p.run())
return 2;
if (round_num == 0)
{
error(2, 0, "no automaton to translate");
}
else
{
if (global_error_flag)
if (round_num == 0)
{
std::ostream& err = global_error();
if (bogus_output)
err << ("error: some error was detected during the above runs.\n"
" Check file ")
<< bogus_output_filename
<< " for problematic automata.";
else
err << ("error: some error was detected during the above runs,\n"
" please search for 'error:' messages in the above"
" trace.");
err << std::endl;
end_error();
}
else if (timeout_count == 0 && ignored_exec_fail == 0)
{
std::cerr << "No problem detected." << std::endl;
error(2, 0, "no automaton to translate");
}
else
{
std::cerr << "No major problem detected." << std::endl;
}
unsigned additional_errors = 0U;
additional_errors += timeout_count > 0;
additional_errors += ignored_exec_fail > 0;
if (additional_errors)
{
std::cerr << (global_error_flag ? "Additionally, " : "However, ");
if (timeout_count)
if (global_error_flag)
{
if (additional_errors > 1)
std::cerr << "\n - ";
if (timeout_count == 1)
std::cerr << "1 timeout occurred";
std::ostream& err = global_error();
if (bogus_output)
err << ("error: some error was detected during the above "
"runs.\n Check file ")
<< bogus_output_filename
<< " for problematic automata.";
else
std::cerr << timeout_count << " timeouts occurred";
err << ("error: some error was detected during the above "
"runs,\n please search for 'error:' messages"
" in the above trace.");
err << std::endl;
end_error();
}
else if (timeout_count == 0 && ignored_exec_fail == 0)
{
std::cerr << "No problem detected." << std::endl;
}
else
{
std::cerr << "No major problem detected." << std::endl;
}
if (ignored_exec_fail)
unsigned additional_errors = 0U;
additional_errors += timeout_count > 0;
additional_errors += ignored_exec_fail > 0;
if (additional_errors)
{
if (additional_errors > 1)
std::cerr << "\n - ";
if (ignored_exec_fail == 1)
std::cerr << "1 non-zero exit status was ignored";
else
std::cerr << ignored_exec_fail
<< " non-zero exit statuses were ignored";
std::cerr << (global_error_flag ? "Additionally, " : "However, ");
if (timeout_count)
{
if (additional_errors > 1)
std::cerr << "\n - ";
if (timeout_count == 1)
std::cerr << "1 timeout occurred";
else
std::cerr << timeout_count << " timeouts occurred";
}
if (ignored_exec_fail)
{
if (additional_errors > 1)
std::cerr << "\n - ";
if (ignored_exec_fail == 1)
std::cerr << "1 non-zero exit status was ignored";
else
std::cerr << ignored_exec_fail
<< " non-zero exit statuses were ignored";
}
if (additional_errors == 1)
std::cerr << '.';
std::cerr << std::endl;
}
if (additional_errors == 1)
std::cerr << '.';
std::cerr << std::endl;
}
}
if (csv_output)
print_stats_csv(csv_output);
if (csv_output)
print_stats_csv(csv_output);
return global_error_flag;
return global_error_flag;
});
}
......@@ -1496,13 +1496,10 @@ namespace
int
main(int argc, char** argv)
{
setup(argv);
return protected_main(argv, [&] {
const argp ap = { options, parse_opt, "[FILENAME[/COL]...]",
argp_program_doc, children, nullptr, nullptr };
const argp ap = { options, parse_opt, "[FILENAME[/COL]...]",
argp_program_doc, children, nullptr, nullptr };
try
{
// This will ensure that all objects stored in this struct are
// destroyed before global variables.
opt_t o;
......@@ -1544,19 +1541,11 @@ main(int argc, char** argv)
// Diagnose unused -x options
extra_options.report_unused_options();
}
catch (const std::runtime_error& e)
{
error(2, 0, "%s", e.what());
}
catch (const std::invalid_argument& e)
{
error(2, 0, "%s", e.what());
}
if (automaton_format == Count)
std::cout << match_count << std::endl;
if (automaton_format == Count)
std::cout << match_count << std::endl;
check_cout();
return !match_count;
check_cout();
return match_count ? 0 : 1;
});
}
......@@ -169,3 +169,23 @@ const struct argp misc_argp = { options, parse_opt_misc,
const struct argp misc_argp_hidden = { options_hidden, parse_opt_misc,
nullptr, nullptr, nullptr,
nullptr, nullptr };
int protected_main(char** progname, std::function<int()> mainfun)
{
try
{
setup(progname);
return mainfun();
}
catch (const std::runtime_error& e)
{
error(2, 0, "%s", e.what());
}
catch (const std::invalid_argument& e)
{
error(2, 0, "%s", e.what());
}
SPOT_UNREACHABLE();
return 2;
}
// -*- coding: utf-8 -*-
// Copyright (C) 2012, 2013 Laboratoire de Recherche et Développement de
// l'Epita (LRDE).
// Copyright (C) 2012, 2013, 2018 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
......@@ -21,8 +21,13 @@
#include "common_sys.hh"
#include "progname.h"
#include <functional>
void setup(char** progname);
extern const struct argp misc_argp;
extern const struct argp misc_argp_hidden;
// Call setup(progname) then Run mainfun() and handle exceptions.
int protected_main(char** progname, std::function<int()> mainfun);
// -*- coding: utf-8 -*-
// Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
// Copyright (C) 2013-2018 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
......@@ -136,33 +136,26 @@ namespace
int
main(int argc, char** argv)
{
setup(argv);
return protected_main(argv, [&] {
const argp ap = { options, parse_opt, "[FILENAME[/COL]...]",
argp_program_doc, children, nullptr, nullptr };
const argp ap = { options, parse_opt, "[FILENAME[/COL]...]",
argp_program_doc, children, nullptr, nullptr };
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
check_no_automaton();
check_no_automaton();
spot::postprocessor post(&extra_options);
post.set_pref(pref | comp | sbacc | colored);
post.set_type(type);
post.set_level(level);
spot::postprocessor post(&extra_options);
post.set_pref(pref | comp | sbacc | colored);
post.set_type(type);
post.set_level(level);
try
{
dstar_processor processor(post);
if (processor.run())
return 2;
// Diagnose unused -x options
extra_options.report_unused_options();
}
catch (const std::runtime_error& e)
{
error(2, 0, "%s", e.what());
}
return 0;
return 0;
});
}
// -*- coding: utf-8 -*-
// Copyright (C) 2017 Laboratoire de Recherche et Développement de
// l'Epita (LRDE).
// Copyright (C) 2017, 2018 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
......@@ -134,29 +134,22 @@ run_jobs()
int
main(int argc, char** argv)
{
strcpy(F_doc, "the name of the pattern");
strcpy(L_doc, "the argument of the pattern");
setup(argv);
return protected_main(argv, [&] {
strcpy(F_doc, "the name of the pattern");
strcpy(L_doc, "the argument of the pattern");
const argp ap = { options, parse_opt, nullptr, argp_program_doc,
children, nullptr, nullptr };
const argp ap = { options, parse_opt, nullptr, argp_program_doc,
children, nullptr, nullptr };
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (jobs.empty())
error(1, 0, "Nothing to do. Try '%s --help' for more information.",
program_name);
if (jobs.empty())
error(1, 0, "Nothing to do. Try '%s --help' for more information.",
program_name);
try
{
run_jobs();
}
catch (const std::runtime_error& e)
{
error(2, 0, "%s", e.what());
}
flush_cout();
return 0;
flush_cout();
return 0;
});
}
......@@ -286,27 +286,19 @@ run_jobs()
int
main(int argc, char** argv)
{
setup(argv);
return protected_main(argv, [&] {
const argp ap = { options, parse_opt, nullptr, argp_program_doc,
children, nullptr, nullptr };
const argp ap = { options, parse_opt, nullptr, argp_program_doc,
children, nullptr, nullptr };
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (jobs.empty())
error(1, 0, "Nothing to do. Try '%s --help' for more information.",
program_name);
if (jobs.empty())
error(1, 0, "Nothing to do. Try '%s --help' for more information.",
program_name);
try
{
run_jobs();
}
catch (const std::runtime_error& e)
{
error(2, 0, "%s", e.what());
}
flush_cout();
return 0;
flush_cout();
return 0;
});
}
......@@ -158,23 +158,20 @@ namespace
int
main(int argc, char** argv)
{
// By default we name automata using the formula.
opt_name = "%f";
return protected_main(argv, [&] {
// By default we name automata using the formula.
opt_name = "%f";
setup(argv);
const argp ap = { options, parse_opt, "[FORMULA...]",
argp_program_doc, children, nullptr, nullptr };
const argp ap = { options, parse_opt, "[FORMULA...]",
argp_program_doc, children, nullptr, nullptr };
simplification_level = 3;
simplification_level = 3;
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
check_no_formula();
check_no_formula();
try
{
spot::translator trans(&extra_options);
trans.set_type(type);
trans.set_pref(pref | comp | sbacc | unambig | colored);
......@@ -186,11 +183,6 @@ main(int argc, char** argv)
// Diagnose unused -x options
extra_options.report_unused_options();
}
catch (const std::runtime_error& e)
{
error(2, 0, "%s", e.what());
}
return 0;
return 0;
});
}
// -*- coding: utf-8 -*-
// Copyright (C) 2012-2017 Laboratoire de Recherche et Développement
// Copyright (C) 2012-2018 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
......@@ -224,20 +224,17 @@ namespace
int
main(int argc, char** argv)
{
setup(argv);
return protected_main(argv, [&] {
const argp ap = { options, parse_opt, "[FORMULA...]",
argp_program_doc, children, nullptr, nullptr };
const argp ap = { options, parse_opt, "[FORMULA...]",
argp_program_doc, children, nullptr, nullptr };
simplification_level = 3;
simplification_level = 3;
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
check_no_formula();
check_no_formula();
try
{
spot::translator trans(&extra_options);
trans.set_pref(pref | comp | sbacc);
trans.set_type(type);
......@@ -248,11 +245,6 @@ main(int argc, char** argv)
return 2;
// Diagnose unused -x options
extra_options.report_unused_options();
}
catch (const std::runtime_error& e)
{
error(2, 0, "%s", e.what());
}
return 0;
return 0;
});
}
......@@ -1591,108 +1591,109 @@ print_stats_json(const char* filename)
int
main(int argc, char** argv)
{
setup(argv);
return protected_main(argv, [&]() -> unsigned {
const argp ap = { options, parse_opt, "[COMMANDFMT...]",
argp_program_doc, children, nullptr, nullptr };
const argp ap = { options, parse_opt, "[COMMANDFMT...]",
argp_program_doc, children, nullptr, nullptr };
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
exit(err);
check_no_formula();
check_no_formula();
if (tools.empty())
error(2, 0, "No translator to run? Run '%s --help' for usage.",
program_name);
if (tools.empty())
error(2, 0, "No translator to run? Run '%s --help' for usage.",
program_name);
setup_color();
setup_sig_handler();
setup_color();
setup_sig_handler();
processor p;
if (p.run())
return 2;
processor p;
if (p.run())
return 2;
if (formulas.empty())
{
error(2, 0, "no formula to translate");
}
else
{
if (global_error_flag)
{
std::ostream& err = global_error();
if (bogus_output)
err << ("error: some error was detected during the above runs.\n"
" Check file ")
<< bogus_output_filename
<< " for problematic formulas.";
else
err << ("error: some error was detected during the above runs,\n"
" please search for 'error:' messages in the above"
" trace.");
err << std::endl;
end_error();
}
else if (timeout_count == 0 && ignored_exec_fail == 0 && oom_count == 0)
if (formulas.empty())
{
std::cerr << "No problem detected." << std::endl;
error(2, 0, "no formula to translate");
}
else
{
std::cerr << "No major problem detected." << std::endl;
}
unsigned additional_errors = 0U;
additional_errors += timeout_count > 0;
additional_errors += ignored_exec_fail > 0;
additional_errors += oom_count > 0;
if (additional_errors)
{
std::cerr << (global_error_flag ? "Additionally, " : "However, ");
if (timeout_count)
if (global_error_flag)
{
if (additional_errors > 1)
std::cerr << "\n - ";
if (timeout_count == 1)
std::cerr << "1 timeout occurred";
std::ostream& err = global_error();
if (bogus_output)
err << ("error: some error was detected during the above "
"runs.\n Check file ")
<< bogus_output_filename
<< " for problematic formulas.";
else
std::cerr << timeout_count << " timeouts occurred";
err << ("error: some error was detected during the above "
"runs,\n please search for 'error:' messages"
" in the above trace.");
err << std::endl;
end_error();
}
if (oom_count)
else if (timeout_count == 0
&& ignored_exec_fail == 0 && oom_count == 0)
{