Frobby  0.9.5
main.cpp
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see http://www.gnu.org/licenses/.
16 */
17 #include "stdinc.h"
18 #include "main.h"
19 
20 #include "Action.h"
21 #include "DebugAllocator.h"
22 #include "error.h"
23 #include "display.h"
24 
25 #include <ctime>
26 #include <cstdlib>
27 #include <sys/types.h>
28 #include <unistd.h>
29 
34 int frobbyMain(int argc, const char** argv) {
35  string prefix;
36  if (argc > 1) {
37  prefix = argv[1];
38  --argc;
39  ++argv;
40  } else
41  prefix = "help";
42 
43  const auto_ptr<Action> action(Action::createActionWithPrefix(prefix));
44  action->parseCommandLine(argc - 1, argv + 1);
45  action->perform();
46 
47  return ExitCodeSuccess;
48 }
49 
54  fputs("INTERNAL ERROR: Something caused terminate() to be called. "
55  "This should never happen.\nPlease contact the Frobby developers.\n",
56  stderr);
57  fflush(stderr);
58  ASSERT(false);
59  abort();
60 }
61 
66  fputs("INTERNAL ERROR: Something caused unexpected() to be called. "
67  "This should never happen.\nPlease contact the Frobby developers.\n",
68  stderr);
69  fflush(stderr);
70  ASSERT(false);
71  abort();
72 }
73 
78 int main(int argc, const char** argv) {
79  try {
80  set_terminate(frobbyTerminate);
81  set_unexpected(frobbyUnexpected);
82 
83  srand((unsigned int)time(0) +
84 #ifdef __GNUC__ // Only GCC defines this macro.
85  (unsigned int)getpid() +
86 #endif
87  (unsigned int)clock());
88 
89 #ifdef PROFILE
90  fputs("This is a PROFILE build of Frobby. It is therefore SLOW.\n",
91  stderr);
92 #endif
93 #ifdef DEBUG
94  fputs("This is a DEBUG build of Frobby. It is therefore SLOW.\n",
95  stderr);
96 #endif
97 
98 #ifdef DEBUG
99  return DebugAllocator::getSingleton().runDebugMain(argc, argv);
100 #else
101  return frobbyMain(argc, argv);
102 #endif
103  } catch (const bad_alloc&) {
104  displayError("Ran out of memory.");
105  return ExitCodeOutOfMemory;
106  } catch (const InternalFrobbyException& e) {
107  displayException(e);
108  return ExitCodeInternalError;
109  } catch (const FrobbyException& e) {
110  displayException(e);
111  return ExitCodeError;
112  } catch (...) {
113  try {
114  throw;
115  } catch (const exception& e) {
116  try {
117  displayError(e.what());
118  } catch (...) {
119  }
120  } catch (...) {
121  }
122  return ExitCodeUnknownError;
123  }
124 }
static auto_ptr< Action > createActionWithPrefix(const string &prefix)
Definition: Action.cpp:109
This is the base of the Frobby exception hierarchy for exceptions that can occur due to expected erro...
Definition: error.h:27
This exception signals that a bug in Frobby has been detected.
Definition: error.h:33
void displayError(const string &msg)
Display msg to standard error in a way that indicates that it is an error.
Definition: display.cpp:139
void displayException(const std::exception &exception)
Display the message of exception.
Definition: display.cpp:147
This file contains functions for printing strings to standard error.
int main(int argc, const char **argv)
This function is the entry point for Frobby as a console program.
Definition: main.cpp:78
void frobbyTerminate()
A replacement for the default C++ built-in terminate() function.
Definition: main.cpp:53
void frobbyUnexpected()
A replacement for the default C++ built-in unexpected() function.
Definition: main.cpp:65
int frobbyMain(int argc, const char **argv)
This function runs the Frobby console interface.
Definition: main.cpp:34
static const int ExitCodeInternalError
Definition: main.h:40
static const int ExitCodeUnknownError
Definition: main.h:46
static const int ExitCodeOutOfMemory
Definition: main.h:43
static const int ExitCodeSuccess
Definition: main.h:33
static const int ExitCodeError
Definition: main.h:36
#define ASSERT(X)
Definition: stdinc.h:86