| 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 chatclient is a simple Dim based chatclient.\n"
|
|---|
| 55 | "\n"
|
|---|
| 56 | "The chatclient is always started with user intercation. "
|
|---|
| 57 | "Just enter a message. It will be broadcasted through the chatserv, "
|
|---|
| 58 | "which need to be running."
|
|---|
| 59 | "\n"
|
|---|
| 60 | "Usage: chatclient [-c type] [OPTIONS]\n"
|
|---|
| 61 | " or: chatclient [OPTIONS]\n";
|
|---|
| 62 | cout << endl;
|
|---|
| 63 |
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | void PrintHelp()
|
|---|
| 67 | {
|
|---|
| 68 | /* Additional help text which is printed after the configuration
|
|---|
| 69 | options goes here */
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | int main(int argc, const char *argv[])
|
|---|
| 73 | {
|
|---|
| 74 | Configuration conf(argv[0]);
|
|---|
| 75 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 76 | SetupConfiguration(conf);
|
|---|
| 77 |
|
|---|
| 78 | po::variables_map vm;
|
|---|
| 79 | try
|
|---|
| 80 | {
|
|---|
| 81 | vm = conf.Parse(argc, argv);
|
|---|
| 82 | }
|
|---|
| 83 | #if BOOST_VERSION > 104000
|
|---|
| 84 | catch (po::multiple_occurrences &e)
|
|---|
| 85 | {
|
|---|
| 86 | cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
|
|---|
| 87 | return -1;
|
|---|
| 88 | }
|
|---|
| 89 | #endif
|
|---|
| 90 | catch (exception& e)
|
|---|
| 91 | {
|
|---|
| 92 | cerr << "Program options invalid due to: " << e.what() << endl;
|
|---|
| 93 | return -1;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if (conf.HasVersion() || conf.HasPrint())
|
|---|
| 97 | return -1;
|
|---|
| 98 |
|
|---|
| 99 | if (conf.HasHelp())
|
|---|
| 100 | {
|
|---|
| 101 | PrintHelp();
|
|---|
| 102 | return -1;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | // To allow overwriting of DIM_DNS_NODE set 0 to 1
|
|---|
| 106 | setenv("DIM_DNS_NODE", conf.Get<string>("dns").c_str(), 1);
|
|---|
| 107 |
|
|---|
| 108 | if (conf.Get<int>("console")==0)
|
|---|
| 109 | RunShell<ChatShell>(conf);
|
|---|
| 110 | else
|
|---|
| 111 | RunShell<ChatConsole>(conf);
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | return 0;
|
|---|
| 115 | }
|
|---|