Invent a value for argv[0] if not present
[libs/core.git] / source / core / application.cpp
1 #include <cstring>
2 #include <typeinfo>
3 #include <signal.h>
4 #include <msp/debug/demangle.h>
5 #include <msp/debug/errorreporter.h>
6 #include <msp/fs/dir.h>
7 #include <msp/fs/path.h>
8 #include <msp/fs/utils.h>
9 #include <msp/io/print.h>
10 #include "application.h"
11 #include "getopt.h"
12
13 using namespace std;
14
15 namespace Msp {
16
17 Application *Application::app_ = 0;
18 Application::Starter *Application::starter_ = 0;
19 const char *Application::argv0_ = 0;
20 string Application::name_;
21 void *Application::data_ = 0;
22
23 Application::Application(const string &n):
24         exit_code(0)
25 {
26         if(app_)
27                 throw logic_error("instance already exists");
28
29         if(!n.empty())
30                 name_ = n;
31         else
32                 name_ = FS::basename(argv0_);
33 }
34
35 int Application::run(int argc, char **argv, void *data, void (*created_callback)(void *))
36 {
37         if(!starter_)
38         {
39                 IO::cerr.write("Application::run called with no RegisteredApplication class!\n");
40                 return 126;
41         }
42
43         set_startup_info(argv[0], data);
44
45         try
46         {
47                 try
48                 {
49                         app_ = starter_->create_app(argc, argv);
50                 }
51                 catch(const usage_error &e)
52                 {
53                         IO::print(IO::cerr, "%s\n%s\n", e.what(), e.help());
54                         return 1;
55                 }
56
57                 if(created_callback)
58                         created_callback(data);
59
60                 int result = app_->main();
61                 Application *a = app_;
62                 app_ = 0;
63                 delete a;
64                 return result;
65         }
66         catch(const exception &e)
67         {
68                 bool handled = false;
69                 if(const Debug::ErrorReporter *er = Debug::ErrorReporter::get_current())
70                         handled = er->report_uncaught_exception(e);
71
72                 if(!handled)
73                 {
74                         IO::print(IO::cerr, "An uncaught exception occurred.\n");
75                         IO::print(IO::cerr, "  type:   %s\n", Debug::demangle(typeid(e).name()));
76                         IO::print(IO::cerr, "  what(): %s\n", e.what());
77                 }
78
79                 delete app_;
80                 app_ = 0;
81
82                 return 124;
83         }
84 }
85
86 void Application::set_startup_info(const char *argv0, void *data)
87 {
88         if(argv0_)
89                 throw logic_error("startup info already set");
90
91         static FS::Path exe;
92
93         if(!argv0 || !*argv0)
94         {
95 #ifdef _WIN32
96                 argv0 = "application.exe";
97 #else
98                 argv0 = "./application";
99 #endif
100         }
101
102         bool has_slash = strchr(argv0, FS::DIRSEP);
103         if(!has_slash)
104                 exe = FS::path_lookup(argv0);
105         if(exe.empty())
106                 exe = FS::realpath(argv0);
107
108         argv0_ = exe.c_str();
109         data_ = data;
110 }
111
112 int Application::main()
113 {
114         done = false;
115         while(!done)
116                 tick();
117
118         return exit_code;
119 }
120
121 void Application::catch_signal(int s)
122 {
123         signal(s, &sighandler_);
124 }
125
126 void Application::exit(int c)
127 {
128         done = true;
129         exit_code = c;
130 }
131
132 void Application::sighandler_(int s)
133 {
134         app_->sighandler(s);
135 }
136
137
138 Application::Starter::Starter()
139 {
140         if(starter_)
141                 throw logic_error("Can't create more than one Starter instance");
142
143         starter_ = this;
144 }
145
146 } // namespace Msp