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