Frobby  0.9.0
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 <unistd.h>
28 
33 int frobbyMain(int argc, const char** argv) {
34  string prefix;
35  if (argc > 1) {
36  prefix = argv[1];
37  --argc;
38  ++argv;
39  } else
40  prefix = "help";
41 
42  const auto_ptr<Action> action(Action::createActionWithPrefix(prefix));
43  action->parseCommandLine(argc - 1, argv + 1);
44  action->perform();
45 
46  return ExitCodeSuccess;
47 }
48 
53  fputs("INTERNAL ERROR: Something caused terminate() to be called. "
54  "This should never happen.\nPlease contact the Frobby developers.\n",
55  stderr);
56  fflush(stderr);
57  ASSERT(false);
58  abort();
59 }
60 
65  fputs("INTERNAL ERROR: Something caused unexpected() to be called. "
66  "This should never happen.\nPlease contact the Frobby developers.\n",
67  stderr);
68  fflush(stderr);
69  ASSERT(false);
70  abort();
71 }
72 
77 int main(int argc, const char** argv) {
78  try {
79  set_terminate(frobbyTerminate);
80  set_unexpected(frobbyUnexpected);
81 
82  srand((unsigned int)time(0) +
83 #ifdef __GNUC__ // Only GCC defines this macro.
84  (unsigned int)getpid() +
85 #endif
86  (unsigned int)clock());
87 
88 #ifdef PROFILE
89  fputs("This is a PROFILE build of Frobby. It is therefore SLOW.\n",
90  stderr);
91 #endif
92 #ifdef DEBUG
93  fputs("This is a DEBUG build of Frobby. It is therefore SLOW.\n",
94  stderr);
95 #endif
96 
97 #ifdef DEBUG
98  return DebugAllocator::getSingleton().runDebugMain(argc, argv);
99 #else
100  return frobbyMain(argc, argv);
101 #endif
102  } catch (const bad_alloc&) {
103  displayError("Ran out of memory.");
104  return ExitCodeOutOfMemory;
105  } catch (const InternalFrobbyException& e) {
106  displayException(e);
107  return ExitCodeInternalError;
108  } catch (const FrobbyException& e) {
109  displayException(e);
110  return ExitCodeError;
111  } catch (...) {
112  try {
113  throw;
114  } catch (const exception& e) {
115  try {
116  displayError(e.what());
117  } catch (...) {
118  }
119  } catch (...) {
120  }
121  return ExitCodeUnknownError;
122  }
123 }
DebugAllocator.h
InternalFrobbyException
This exception signals that a bug in Frobby has been detected.
Definition: error.h:33
stdinc.h
ExitCodeUnknownError
static const int ExitCodeUnknownError
Definition: main.h:46
ExitCodeSuccess
static const int ExitCodeSuccess
Definition: main.h:33
frobbyMain
int frobbyMain(int argc, const char **argv)
This function runs the Frobby console interface.
Definition: main.cpp:33
frobbyUnexpected
void frobbyUnexpected()
A replacement for the default C++ built-in unexpected() function.
Definition: main.cpp:64
frobbyTerminate
void frobbyTerminate()
A replacement for the default C++ built-in terminate() function.
Definition: main.cpp:52
Action.h
error.h
displayException
void displayException(const std::exception &exception)
Display the message of exception.
Definition: display.cpp:147
display.h
FrobbyException
This is the base of the Frobby exception hierarchy for exceptions that can occur due to expected erro...
Definition: error.h:27
main.h
Action::createActionWithPrefix
static auto_ptr< Action > createActionWithPrefix(const string &prefix)
Definition: Action.cpp:109
displayError
void displayError(const string &msg)
Display msg to standard error in a way that indicates that it is an error.
Definition: display.cpp:139
ASSERT
#define ASSERT(X)
Definition: stdinc.h:85
ExitCodeError
static const int ExitCodeError
Definition: main.h:36
ExitCodeInternalError
static const int ExitCodeInternalError
Definition: main.h:40
ExitCodeOutOfMemory
static const int ExitCodeOutOfMemory
Definition: main.h:43
main
int main(int argc, const char **argv)
This function is the entry point for Frobby as a console program.
Definition: main.cpp:77