common_setup.cc 5.85 KB
Newer Older
1
// -*- coding: utf-8 -*-
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
2
// Copyright (C) 2012-2020 Laboratoire de Recherche et Développement
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
3
// de l'Epita (LRDE).
4
5
6
7
8
//
// 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
9
// the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
// (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
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20

#include "common_setup.hh"
21
22
#include "common_aoutput.hh"

23
#include "argp.h"
24
#include "closeout.h"
25
#include <cstdlib>
26
#include <unistd.h>
27
#include <iostream>
28
29
#include <signal.h>
#include <sys/wait.h>
30
#include <spot/misc/tmpfile.hh>
31
32
33
34
35

static void
display_version(FILE *stream, struct argp_state*)
{
  fputs(program_name, stream);
36
  fputs(" (" PACKAGE_NAME ") " PACKAGE_VERSION "\n\
37
\n\
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
38
Copyright (C) 2020  Laboratoire de Recherche et Développement de l'Epita.\n\
39
40
41
42
License GPLv3+: \
GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
This is free software: you are free to change and redistribute it.\n\
There is NO WARRANTY, to the extent permitted by law.\n", stream);
43
}
44

45
46
47
48
49
50
// This is called on normal exit (i.e., when leaving
// main or calling exit(), even via error()).
static void atexit_cleanup()
{
  spot::cleanup_tmpfiles();
}
51

52
#ifdef HAVE_SIGACTION
53
54
// This is called on abnormal exit, i.e. when the process is killed by
// some signal.
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
static void sig_handler(int sig)
{
  spot::cleanup_tmpfiles();
  // Send the signal again, this time to the default handler, so that
  // we return a meaningful error code.
  raise(sig);
}

static void setup_sig_handler()
{
  struct sigaction sa;
  sa.sa_handler = sig_handler;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_RESETHAND;
  // Catch termination signals, so we can cleanup temporary files.
70
71
72
73
74
75
  sigaction(SIGALRM, &sa, nullptr);
  sigaction(SIGHUP, &sa, nullptr);
  sigaction(SIGINT, &sa, nullptr);
  sigaction(SIGPIPE, &sa, nullptr);
  sigaction(SIGQUIT, &sa, nullptr);
  sigaction(SIGTERM, &sa, nullptr);
76
77
78
79
80
}
#else
# define setup_sig_handler() while (0);
#endif

81
82
83
84
85
86
87
88

static void bad_alloc_handler()
{
  std::set_new_handler(nullptr);
  std::cerr << "not enough memory\n";
  abort();
}

89
90
91
void
setup(char** argv)
{
92
93
  argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";

94
95
96
  // Simplify the program name, because argp() uses it to report
  // errors and display help text.
  set_program_name(argv[0]);
97

98
  argv[0] = const_cast<char*>(program_name);
99
100

  argp_program_version_hook = display_version;
101
102

  argp_err_exit_status = 2;
103

104
105
106
  if (getenv("SPOT_OOM_ABORT"))
    std::set_new_handler(bad_alloc_handler);

107
  std::ios_base::sync_with_stdio(false);
108
109
110
111
112
  // Do not flush std::cout every time we read from std::cin, unless
  // we are reading from a terminal.  Note that we do flush regularly
  // in check_cout().
  if (!isatty(STDIN_FILENO))
    std::cin.tie(nullptr);
113

114
  setup_default_output_format();
115
  setup_sig_handler();
116
  atexit(atexit_cleanup);
117
118
119
120
121
122
123
}


// argp's default behavior of offering -? for --help is just too silly.
// I mean, come on, why not also add -* to Darwinise more shell users?
// We disable this option as well as -V (because --version don't need
// a short version).
124
125
126
127
128
enum {
  OPT_HELP = 1,
  OPT_VERSION,
};

129
130
static const argp_option options[] =
  {
131
132
    { "version", OPT_VERSION, nullptr, 0, "print program version", -1 },
    { "help", OPT_HELP, nullptr, 0, "print this help", -1 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
133
134
135
    // We support --usage as a synonym for --help because argp's
    // hardcoded error message for unknown options mentions it.
    { "usage", OPT_HELP, nullptr, OPTION_HIDDEN, nullptr, -1 },
136
    { nullptr, 0, nullptr, 0, nullptr, 0 }
137
138
  };

139
140
static const argp_option options_hidden[] =
  {
141
142
143
    { "version", OPT_VERSION, nullptr, OPTION_HIDDEN,
      "print program version", -1 },
    { "help", OPT_HELP, nullptr, OPTION_HIDDEN, "print this help", -1 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
144
145
146
    // We support --usage as a synonym for --help because argp's
    // hardcoded error message for unknown options mentions it.
    { "usage", OPT_HELP, nullptr, OPTION_HIDDEN, nullptr, -1 },
147
    { nullptr, 0, nullptr, 0, nullptr, 0 }
148
149
  };

150
151
152
static int
parse_opt_misc(int key, char*, struct argp_state* state)
{
153
154
  // Called from C code, so should not raise any exception.
  BEGIN_EXCEPTION_PROTECT;
155
156
157
158
  // This switch is alphabetically-ordered.
  switch (key)
    {
    case OPT_HELP:
159
160
161
162
163
164
165
      argp_state_help(state, state->out_stream,
                      // Do not let argp exit: we want to diagnose a
                      // failure to print --help by closing stdout
                      // properly.
                      ARGP_HELP_STD_HELP & ~ARGP_HELP_EXIT_OK);
      close_stdout();
      exit(0);
166
      break;
167
168
    case OPT_VERSION:
      display_version(state->out_stream, state);
169
      close_stdout();
170
171
      exit(0);
      break;
172
173
174
    default:
      return ARGP_ERR_UNKNOWN;
    }
175
  END_EXCEPTION_PROTECT;
176
  return 0;
177
}
178
179


180
const struct argp misc_argp = { options, parse_opt_misc,
181
                                nullptr, nullptr, nullptr, nullptr, nullptr };
182
183

const struct argp misc_argp_hidden = { options_hidden, parse_opt_misc,
184
185
                                       nullptr, nullptr, nullptr,
                                       nullptr, nullptr };
186

187
188
189
190
191
192
193
194
195
196
197
198
[[noreturn]] void handle_any_exception()
{
  try
    {
      throw;
    }
  catch (const std::exception& e)
    {
      error(2, 0, "%s", e.what());
    }
  SPOT_UNREACHABLE();
}
199
200
201
202
203
204
205
206

int protected_main(char** progname, std::function<int()> mainfun)
{
  try
    {
      setup(progname);
      return mainfun();
    }
207
  catch (...)
208
    {
209
      handle_any_exception();
210
211
212
213
    }
  SPOT_UNREACHABLE();
  return 2;
}