From 7bfe06b30b682071509e86d6a06752767f010651 Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Wed, 26 Jul 2017 12:10:36 +0200 Subject: [PATCH] ltlcross: extract the color handling code * bin/common_color.cc, bin/common_color.hh: New files, with code extracted from ltlcross.cc. * bin/Makefile.am: Add them. * bin/ltlcross.cc: Simplify. --- bin/Makefile.am | 2 + bin/common_color.cc | 98 +++++++++++++++++++++++++++++++++++++++++++++ bin/common_color.hh | 31 ++++++++++++++ bin/ltlcross.cc | 83 ++++++-------------------------------- 4 files changed, 143 insertions(+), 71 deletions(-) create mode 100644 bin/common_color.cc create mode 100644 bin/common_color.hh diff --git a/bin/Makefile.am b/bin/Makefile.am index 6a8afe0da..09f84f932 100644 --- a/bin/Makefile.am +++ b/bin/Makefile.am @@ -32,6 +32,8 @@ noinst_LIBRARIES = libcommon.a libcommon_a_SOURCES = \ common_aoutput.cc \ common_aoutput.hh \ + common_color.cc \ + common_color.hh \ common_conv.hh \ common_conv.cc \ common_cout.hh \ diff --git a/bin/common_color.cc b/bin/common_color.cc new file mode 100644 index 000000000..b25f9cb0e --- /dev/null +++ b/bin/common_color.cc @@ -0,0 +1,98 @@ +// -*- coding: utf-8 -*- +// Copyright (C) 2017 Laboratoire de Recherche et Développement de +// l'Epita (LRDE). +// +// This file is part of Spot, a model checking library. +// +// Spot 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 3 of the License, or +// (at your option) any later version. +// +// Spot 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 this program. If not, see . + +#include "common_sys.hh" +#include "common_color.hh" + +#include +#include + +#include "argmatch.h" + +static char const *const color_args[] = +{ + "always", "yes", "force", + "never", "no", "none", + "auto", "tty", "if-tty", nullptr +}; +static color_type const color_types[] = +{ + color_always, color_always, color_always, + color_never, color_never, color_never, + color_if_tty, color_if_tty, color_if_tty +}; +ARGMATCH_VERIFY(color_args, color_types); + +color_type color_opt = color_if_tty; +const char* bright_red = "\033[1;31m"; +const char* bold = "\033[1m"; +const char* bold_std = "\033[0;1m"; +const char* reset_color = "\033[m"; + + +void setup_color() +{ + if (color_opt == color_if_tty) + color_opt = isatty(STDERR_FILENO) ? color_always : color_never; + + if (color_opt == color_never) + { + bright_red = ""; + bold = ""; + bold_std = ""; + reset_color = ""; + } +} + +enum { + OPT_COLOR = 256, +}; + +static const argp_option options_color[] = + { + { "color", OPT_COLOR, "WHEN", OPTION_ARG_OPTIONAL, + "colorize output; WHEN can be 'never', 'always' (the default if " + "--color is used without argument), or " + "'auto' (the default if --color is not used)", -2 }, + { nullptr, 0, nullptr, 0, nullptr, 0 } + }; + + +static int +parse_opt_color(int key, char* arg, struct argp_state*) +{ + // This switch is alphabetically-ordered. + switch (key) + { + case OPT_COLOR: + { + if (arg) + color_opt = XARGMATCH("--color", arg, color_args, color_types); + else + color_opt = color_always; + break; + } + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +const struct argp color_argp = { options_color, parse_opt_color, + nullptr, nullptr, nullptr, nullptr, nullptr }; diff --git a/bin/common_color.hh b/bin/common_color.hh new file mode 100644 index 000000000..657d9647d --- /dev/null +++ b/bin/common_color.hh @@ -0,0 +1,31 @@ +// -*- coding: utf-8 -*- +// Copyright (C) 2017 Laboratoire de Recherche et Développement de +// l'Epita (LRDE). +// +// This file is part of Spot, a model checking library. +// +// Spot 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 3 of the License, or +// (at your option) any later version. +// +// Spot 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 this program. If not, see . + +#pragma once + +enum color_type { color_never, color_always, color_if_tty }; + +extern color_type color_opt; +extern const char* bright_red; +extern const char* bold; +extern const char* bold_std; +extern const char* reset_color; + +extern const struct argp color_argp; +void setup_color(); diff --git a/bin/ltlcross.cc b/bin/ltlcross.cc index d8408b533..a01473700 100644 --- a/bin/ltlcross.cc +++ b/bin/ltlcross.cc @@ -40,6 +40,7 @@ #include "common_finput.hh" #include "common_hoaread.hh" #include "common_aoutput.hh" +#include "common_color.hh" #include #include #include @@ -83,7 +84,6 @@ enum { OPT_AMBIGUOUS = 256, OPT_AUTOMATA, OPT_BOGUS, - OPT_COLOR, OPT_CSV, OPT_DENSITY, OPT_DUPS, @@ -154,10 +154,6 @@ static const argp_option options[] = { "unambiguous", 0, nullptr, OPTION_ALIAS, nullptr, 0 }, /**************************************************/ { nullptr, 0, nullptr, 0, "Miscellaneous options:", -2 }, - { "color", OPT_COLOR, "WHEN", OPTION_ARG_OPTIONAL, - "colorize output; WHEN can be 'never', 'always' (the default if " - "--color is used without argument), or " - "'auto' (the default if --color is not used)", 0 }, { "grind", OPT_GRIND, "[>>]FILENAME", 0, "for each formula for which a problem was detected, write a simpler " \ "formula that fails on the same test in FILENAME", 0 }, @@ -177,31 +173,10 @@ const struct argp_child children[] = { &trans_argp, 0, nullptr, 0 }, { &hoaread_argp, 0, "Parsing of automata:", 4 }, { &misc_argp, 0, nullptr, -2 }, + { &color_argp, 0, nullptr, 0 }, { nullptr, 0, nullptr, 0 } }; -enum color_type { color_never, color_always, color_if_tty }; - -static char const *const color_args[] = -{ - "always", "yes", "force", - "never", "no", "none", - "auto", "tty", "if-tty", nullptr -}; -static color_type const color_types[] = -{ - color_always, color_always, color_always, - color_never, color_never, color_never, - color_if_tty, color_if_tty, color_if_tty -}; -ARGMATCH_VERIFY(color_args, color_types); - -static color_type color_opt = color_if_tty; -static const char* bright_red = "\033[1;31m"; -static const char* bold = "\033[1m"; -static const char* bold_std = "\033[0;1m"; -static const char* reset_color = "\033[m"; - static unsigned states = 200; static float density = 0.1; static const char* json_output = nullptr; @@ -233,16 +208,14 @@ static std::ostream& global_error() { global_error_flag = true; - if (color_opt) - std::cerr << bright_red; + std::cerr << bright_red; return std::cerr; } static std::ostream& example() { - if (color_opt) - std::cerr << bold_std; + std::cerr << bold_std; return std::cerr; } @@ -250,8 +223,7 @@ example() static void end_error() { - if (color_opt) - std::cerr << reset_color; + std::cerr << reset_color; } @@ -462,14 +434,6 @@ parse_opt(int key, char* arg, struct argp_state*) bogus_output_filename = arg; break; } - case OPT_COLOR: - { - if (arg) - color_opt = XARGMATCH("--color", arg, color_args, color_types); - else - color_opt = color_always; - break; - } case OPT_CSV: want_stats = true; csv_output = arg ? arg : "-"; @@ -912,14 +876,8 @@ namespace unsigned mutation_max; while (res) { - std::cerr << "Trying to find a bogus mutation of "; - if (color_opt) - std::cerr << bold; - std::cerr << bogus; - if (color_opt) - std::cerr << reset_color; - std::cerr << "...\n"; - + std::cerr << "Trying to find a bogus mutation of " << bold + << bogus << reset_color << "...\n"; mutations = mutate(f); mutation_count = 1; mutation_max = mutations.size(); @@ -944,19 +902,9 @@ namespace bogus_output->ostream() << bogus << std::endl; } } - std::cerr << "Smallest bogus mutation found for "; - if (color_opt) - std::cerr << bold; - std::cerr << input; - if (color_opt) - std::cerr << reset_color; - std::cerr << " is "; - if (color_opt) - std::cerr << bold; - std::cerr << bogus; - if (color_opt) - std::cerr << reset_color; - std::cerr << ".\n\n"; + std::cerr << "Smallest bogus mutation found for " + << bold << input << reset_color << " is " + << bold << bogus << reset_color << ".\n\n"; grind_output->ostream() << bogus << std::endl; } return 0; @@ -1017,11 +965,7 @@ namespace std::cerr << linenum << ':'; if (filename || linenum) std::cerr << ' '; - if (color_opt) - std::cerr << bold; - std::cerr << fstr << '\n'; - if (color_opt) - std::cerr << reset_color; + std::cerr << bold << fstr << reset_color << '\n'; // Make sure we do not translate the same formula twice. if (!allow_dups) @@ -1037,7 +981,6 @@ namespace } } - int problems = 0; // These store the result of the translation of the positive and @@ -1556,9 +1499,7 @@ main(int argc, char** argv) error(2, 0, "No translator to run? Run '%s --help' for usage.", program_name); - if (color_opt == color_if_tty) - color_opt = isatty(STDERR_FILENO) ? color_always : color_never; - + setup_color(); setup_sig_handler(); processor p;