1 | #include <boost/regex.hpp>
|
---|
2 | #include <boost/filesystem.hpp>
|
---|
3 | #include <readline/readline.h>
|
---|
4 |
|
---|
5 | #include "tools.h"
|
---|
6 | #include "Time.h"
|
---|
7 | #include "StateMachineDim.h"
|
---|
8 | #include "MessageDim.h"
|
---|
9 | #include "Shell.h"
|
---|
10 | #include "ServiceList.h"
|
---|
11 | #include "Configuration.h"
|
---|
12 |
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | #include "RemoteControl.h"
|
---|
16 |
|
---|
17 | template <class T>
|
---|
18 | void RunShell(Configuration &conf)
|
---|
19 | {
|
---|
20 | // A normal kill will call its destructor! (Very nice feature ;) )
|
---|
21 | static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
---|
22 |
|
---|
23 | WindowLog &win = shell.GetStreamIn();
|
---|
24 | WindowLog &wout = shell.GetStreamOut();
|
---|
25 |
|
---|
26 | if (conf.Has("log"))
|
---|
27 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
28 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
29 |
|
---|
30 | shell.Run();
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | // ========================================================================
|
---|
35 | void SetupConfiguration(Configuration &conf)
|
---|
36 | {
|
---|
37 | const string n = conf.GetName()+".log";
|
---|
38 |
|
---|
39 | po::options_description config("Program options");
|
---|
40 | config.add_options()
|
---|
41 | ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
|
---|
42 | ("log,l", var<string>(n), "Write log-file")
|
---|
43 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
---|
44 | ;
|
---|
45 |
|
---|
46 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
47 |
|
---|
48 | conf.AddOptions(config);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /*
|
---|
52 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
53 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
54 | are used to match the usage synopsis in program output. An example from cp
|
---|
55 | (GNU coreutils) which contains both strings:
|
---|
56 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
57 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
58 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
59 | */
|
---|
60 | void PrintUsage()
|
---|
61 | {
|
---|
62 | cout << "\n"
|
---|
63 | "The console connects to all available Dim Servers and allows to "
|
---|
64 | "easily access all of their commands.\n"
|
---|
65 | "\n"
|
---|
66 | "Usage: test3 [-c type] [OPTIONS]\n"
|
---|
67 | " or: test3 [OPTIONS]\n"
|
---|
68 | "\n"
|
---|
69 | "Options:\n"
|
---|
70 | "The following describes the available commandline options. "
|
---|
71 | "For further details on how command line option are parsed "
|
---|
72 | "and in which order which configuration sources are accessed "
|
---|
73 | "please refer to the class reference of the Configuration class.";
|
---|
74 | cout << endl;
|
---|
75 |
|
---|
76 | }
|
---|
77 |
|
---|
78 | void PrintHelp()
|
---|
79 | {
|
---|
80 | cout <<
|
---|
81 | "The default is that the program is started without user intercation. "
|
---|
82 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
---|
83 | "option, a local shell can be initialized. With h or help a short "
|
---|
84 | "help message about the usuage can be brought to the screen."
|
---|
85 | << endl;
|
---|
86 |
|
---|
87 | /*
|
---|
88 | cout << "bla bla bla" << endl << endl;
|
---|
89 | cout << endl;
|
---|
90 | cout << "Environment:" << endl;
|
---|
91 | cout << "environment" << endl;
|
---|
92 | cout << endl;
|
---|
93 | cout << "Examples:" << endl;
|
---|
94 | cout << "test exam" << endl;
|
---|
95 | cout << endl;
|
---|
96 | cout << "Files:" << endl;
|
---|
97 | cout << "files" << endl;
|
---|
98 | cout << endl;
|
---|
99 | */
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*
|
---|
103 | The first line of the --version information is assumed to be in one
|
---|
104 | of the following formats:
|
---|
105 |
|
---|
106 | <version>
|
---|
107 | <program> <version>
|
---|
108 | {GNU,Free} <program> <version>
|
---|
109 | <program> ({GNU,Free} <package>) <version>
|
---|
110 | <program> - {GNU,Free} <package> <version>
|
---|
111 |
|
---|
112 | and separated from any copyright/author details by a blank line.
|
---|
113 |
|
---|
114 | Handle multi-line bug reporting sections of the form:
|
---|
115 |
|
---|
116 | Report <program> bugs to <addr>
|
---|
117 | GNU <package> home page: <url>
|
---|
118 | ...
|
---|
119 | */
|
---|
120 | void PrintVersion(const char *name)
|
---|
121 | {
|
---|
122 | string n = boost::filesystem::basename(name);
|
---|
123 | if (n.substr(0, 3)=="lt-")
|
---|
124 | n = n.substr(3);
|
---|
125 |
|
---|
126 | cout <<
|
---|
127 | n << " - FACT++ 1.0\n"
|
---|
128 | "\n"
|
---|
129 | "Written by Thomas Bretz <thomas.bretz@epfl.ch> et al.\n"
|
---|
130 | "\n"
|
---|
131 | "Report bugs to Thomas Bretz <thomas.bretz@epfl.ch>\n"
|
---|
132 | "FACT++ home page: http://www.xxx.com\n"
|
---|
133 | "\n"
|
---|
134 | "Copyright (C) 2011 by the FACT Collaboration.\n"
|
---|
135 | "This is free software; see the source for copying conditions.\n"
|
---|
136 | << endl;
|
---|
137 | }
|
---|
138 |
|
---|
139 | int main(int argc, char *argv[])
|
---|
140 | {
|
---|
141 | Configuration conf(argv[0]);
|
---|
142 | conf.SetPrintUsage(PrintUsage);
|
---|
143 | SetupConfiguration(conf);
|
---|
144 |
|
---|
145 | po::variables_map vm;
|
---|
146 | try
|
---|
147 | {
|
---|
148 | vm = conf.Parse(argc, argv);
|
---|
149 | }
|
---|
150 | catch (std::exception &e)
|
---|
151 | {
|
---|
152 | #if BOOST_VERSION > 104000
|
---|
153 | po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
|
---|
154 | if (MO)
|
---|
155 | cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
|
---|
156 | else
|
---|
157 | #endif
|
---|
158 | cout << "Error: " << e.what() << endl;
|
---|
159 | cout << endl;
|
---|
160 |
|
---|
161 | return -1;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (conf.HasPrint())
|
---|
165 | return -1;
|
---|
166 |
|
---|
167 | if (conf.HasVersion())
|
---|
168 | {
|
---|
169 | PrintVersion(argv[0]);
|
---|
170 | return -1;
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (conf.HasHelp())
|
---|
174 | {
|
---|
175 | PrintHelp();
|
---|
176 | return -1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | // To allow overwriting of DIM_DNS_NODE set 0 to 1
|
---|
180 | setenv("DIM_DNS_NODE", conf.Get<string>("dns").c_str(), 1);
|
---|
181 |
|
---|
182 | if (conf.Get<int>("console")==0)
|
---|
183 | RunShell<RemoteShell>(conf);
|
---|
184 | else
|
---|
185 | RunShell<RemoteConsole>(conf);
|
---|
186 |
|
---|
187 |
|
---|
188 | return 0;
|
---|
189 | }
|
---|