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