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 | const vector<string> v1 = conf.Vec<string>("cmd");
|
---|
21 | for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
|
---|
22 | shell.ProcessLine(*it);
|
---|
23 |
|
---|
24 | const vector<string> v2 = conf.Vec<string>("exec");
|
---|
25 | for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
|
---|
26 | shell.Execute(*it);
|
---|
27 |
|
---|
28 | if (conf.Get<bool>("quit"))
|
---|
29 | shell.Stop();
|
---|
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>(0), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
---|
45 | ("cmd", vars<string>(), "Execute one or more commands at startup")
|
---|
46 | ("exec,e", vars<string>(), "Execute one or more scrips at startup")
|
---|
47 | ("quit", po_switch(), "Quit after startup");
|
---|
48 | ;
|
---|
49 |
|
---|
50 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
51 |
|
---|
52 | conf.AddOptions(config);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /*
|
---|
56 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
57 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
58 | are used to match the usage synopsis in program output. An example from cp
|
---|
59 | (GNU coreutils) which contains both strings:
|
---|
60 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
61 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
62 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
63 | */
|
---|
64 | void PrintUsage()
|
---|
65 | {
|
---|
66 | cout << "\n"
|
---|
67 | "The console connects to all available Dim Servers and allows to "
|
---|
68 | "easily access all of their commands.\n"
|
---|
69 | "\n"
|
---|
70 | "Usage: dimctrl [-c type] [OPTIONS]\n"
|
---|
71 | " or: dimctrl [OPTIONS]\n";
|
---|
72 | cout << endl;
|
---|
73 |
|
---|
74 | }
|
---|
75 |
|
---|
76 | void PrintHelp()
|
---|
77 | {
|
---|
78 | /* Additional help text which is printed after the configuration
|
---|
79 | options goes here */
|
---|
80 | }
|
---|
81 |
|
---|
82 | int main(int argc, const char *argv[])
|
---|
83 | {
|
---|
84 | Configuration conf(argv[0]);
|
---|
85 | conf.SetPrintUsage(PrintUsage);
|
---|
86 | SetupConfiguration(conf);
|
---|
87 |
|
---|
88 | po::variables_map vm;
|
---|
89 | try
|
---|
90 | {
|
---|
91 | vm = conf.Parse(argc, argv);
|
---|
92 | }
|
---|
93 | #if BOOST_VERSION > 104000
|
---|
94 | catch (po::multiple_occurrences &e)
|
---|
95 | {
|
---|
96 | cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
|
---|
97 | return -1;
|
---|
98 | }
|
---|
99 | #endif
|
---|
100 | catch (exception& e)
|
---|
101 | {
|
---|
102 | cerr << "Program options invalid due to: " << e.what() << endl;
|
---|
103 | return -1;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (conf.HasVersion() || conf.HasPrint())
|
---|
107 | return -1;
|
---|
108 |
|
---|
109 | if (conf.HasHelp())
|
---|
110 | {
|
---|
111 | PrintHelp();
|
---|
112 | return -1;
|
---|
113 | }
|
---|
114 |
|
---|
115 | Dim::Setup(conf.Get<string>("dns"));
|
---|
116 |
|
---|
117 | if (conf.Get<int>("console")==0)
|
---|
118 | //Main<RemoteShell, DummyService>(conf);
|
---|
119 | RunShell<RemoteShell>(conf);
|
---|
120 | else
|
---|
121 | //Main<RemoteConsole, DummyService>(conf);
|
---|
122 | RunShell<RemoteConsole>(conf);
|
---|
123 |
|
---|
124 |
|
---|
125 | return 0;
|
---|
126 | }
|
---|