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