1 | #include <boost/regex.hpp>
|
---|
2 | #include <readline/readline.h>
|
---|
3 |
|
---|
4 | #include "tools.h"
|
---|
5 | #include "Time.h"
|
---|
6 | #include "StateMachineDim.h"
|
---|
7 | #include "MessageDim.h"
|
---|
8 | #include "Shell.h"
|
---|
9 | #include "ServiceList.h"
|
---|
10 | #include "Configuration.h"
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | #include "RemoteControl.h"
|
---|
15 |
|
---|
16 | template <class T>
|
---|
17 | void RunShell(Configuration &conf)
|
---|
18 | {
|
---|
19 | // A normal kill will call its destructor! (Very nice feature ;) )
|
---|
20 | static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
---|
21 |
|
---|
22 | WindowLog &win = shell.GetStreamIn();
|
---|
23 | WindowLog &wout = shell.GetStreamOut();
|
---|
24 |
|
---|
25 | if (conf.Has("log"))
|
---|
26 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
27 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
28 |
|
---|
29 | shell.Run();
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | // ========================================================================
|
---|
34 | void SetupConfiguration(Configuration &conf)
|
---|
35 | {
|
---|
36 | const string n = conf.GetName()+".log";
|
---|
37 |
|
---|
38 | po::options_description config("Program options");
|
---|
39 | config.add_options()
|
---|
40 | ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
|
---|
41 | ("log,l", var<string>(n), "Write log-file")
|
---|
42 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
---|
43 | ;
|
---|
44 |
|
---|
45 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
46 |
|
---|
47 | conf.AddOptions(config);
|
---|
48 | }
|
---|
49 |
|
---|
50 | int main(int argc, char *argv[])
|
---|
51 | {
|
---|
52 | cout << "Starting " << argv[0] << "..." << endl;
|
---|
53 |
|
---|
54 | Configuration conf(argv[0]);
|
---|
55 | SetupConfiguration(conf);
|
---|
56 |
|
---|
57 | po::variables_map vm;
|
---|
58 | try
|
---|
59 | {
|
---|
60 | vm = conf.Parse(argc, argv);
|
---|
61 | }
|
---|
62 | catch (std::exception &e)
|
---|
63 | {
|
---|
64 | #if BOOST_VERSION > 104000
|
---|
65 | po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
|
---|
66 | if (MO)
|
---|
67 | cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
|
---|
68 | else
|
---|
69 | #endif
|
---|
70 | cout << "Error: " << e.what() << endl;
|
---|
71 | cout << endl;
|
---|
72 |
|
---|
73 | return -1;
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (conf.HasHelp() || conf.HasPrint())
|
---|
77 | return -1;
|
---|
78 |
|
---|
79 | // To allow overwriting of DIM_DNS_NODE set 0 to 1
|
---|
80 | setenv("DIM_DNS_NODE", conf.Get<string>("dns").c_str(), 1);
|
---|
81 |
|
---|
82 | if (conf.Get<int>("console")==0)
|
---|
83 | RunShell<RemoteShell>(conf);
|
---|
84 | else
|
---|
85 | RunShell<RemoteConsole>(conf);
|
---|
86 |
|
---|
87 |
|
---|
88 | return 0;
|
---|
89 | }
|
---|