1 | #include <boost/regex.hpp>
|
---|
2 | #include <boost/filesystem.hpp>
|
---|
3 |
|
---|
4 | #include "Dim.h"
|
---|
5 | #include "Shell.h"
|
---|
6 | #include "Configuration.h"
|
---|
7 | #include "RemoteControl.h"
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | template <class T>
|
---|
12 | void RunShell(Configuration &conf)
|
---|
13 | {
|
---|
14 | // A normal kill will call its destructor! (Very nice feature ;) )
|
---|
15 | static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
---|
16 |
|
---|
17 | WindowLog &win = shell.GetStreamIn();
|
---|
18 | WindowLog &wout = shell.GetStreamOut();
|
---|
19 |
|
---|
20 | if (conf.Has("log"))
|
---|
21 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
22 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
23 |
|
---|
24 | if (conf.Has("exec"))
|
---|
25 | {
|
---|
26 | const vector<string> v = conf.Get<vector<string>>("exec");
|
---|
27 | for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
|
---|
28 | shell.Execute(*it);
|
---|
29 | }
|
---|
30 |
|
---|
31 | shell.Run();
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | // ========================================================================
|
---|
36 | void SetupConfiguration(Configuration &conf)
|
---|
37 | {
|
---|
38 | const string n = conf.GetName()+".log";
|
---|
39 |
|
---|
40 | po::options_description config("Program options");
|
---|
41 | config.add_options()
|
---|
42 | ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
|
---|
43 | ("log,l", var<string>(n), "Write log-file")
|
---|
44 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
---|
45 | ("exec,e", vars<string>(), "Execute one or more scrips at startup")
|
---|
46 | ;
|
---|
47 |
|
---|
48 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
49 |
|
---|
50 | conf.AddOptions(config);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /*
|
---|
54 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
55 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
56 | are used to match the usage synopsis in program output. An example from cp
|
---|
57 | (GNU coreutils) which contains both strings:
|
---|
58 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
59 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
60 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
61 | */
|
---|
62 | void PrintUsage()
|
---|
63 | {
|
---|
64 | cout << "\n"
|
---|
65 | "The console connects to all available Dim Servers and allows to "
|
---|
66 | "easily access all of their commands.\n"
|
---|
67 | "\n"
|
---|
68 | "Usage: dimctrl [-c type] [OPTIONS]\n"
|
---|
69 | " or: dimctrl [OPTIONS]\n";
|
---|
70 | cout << endl;
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | void PrintHelp()
|
---|
75 | {
|
---|
76 | /* Additional help text which is printed after the configuration
|
---|
77 | options goes here */
|
---|
78 | }
|
---|
79 |
|
---|
80 | int main(int argc, const char *argv[])
|
---|
81 | {
|
---|
82 | Configuration conf(argv[0]);
|
---|
83 | conf.SetPrintUsage(PrintUsage);
|
---|
84 | SetupConfiguration(conf);
|
---|
85 |
|
---|
86 | po::variables_map vm;
|
---|
87 | try
|
---|
88 | {
|
---|
89 | vm = conf.Parse(argc, argv);
|
---|
90 | }
|
---|
91 | #if BOOST_VERSION > 104000
|
---|
92 | catch (po::multiple_occurrences &e)
|
---|
93 | {
|
---|
94 | cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
|
---|
95 | return -1;
|
---|
96 | }
|
---|
97 | #endif
|
---|
98 | catch (exception& e)
|
---|
99 | {
|
---|
100 | cerr << "Program options invalid due to: " << e.what() << endl;
|
---|
101 | return -1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (conf.HasVersion() || conf.HasPrint())
|
---|
105 | return -1;
|
---|
106 |
|
---|
107 | if (conf.HasHelp())
|
---|
108 | {
|
---|
109 | PrintHelp();
|
---|
110 | return -1;
|
---|
111 | }
|
---|
112 |
|
---|
113 | Dim::Setup(conf.Get<string>("dns"));
|
---|
114 |
|
---|
115 | if (conf.Get<int>("console")==0)
|
---|
116 | RunShell<RemoteShell>(conf);
|
---|
117 | else
|
---|
118 | RunShell<RemoteConsole>(conf);
|
---|
119 |
|
---|
120 |
|
---|
121 | return 0;
|
---|
122 | }
|
---|