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