Added --verbose option to commands
[distributed.git] / src / client / distribute.cxx
blobe19e2e312583e38e5f7dcb33d8c465c13ef90cb3
1 //
2 // Copyright (C) 2008 Francesco Salvestrini
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <string>
24 #include <iostream>
26 #include "libs/misc/debug.h"
27 #include "libs/misc/environment.h"
28 #include "libs/conf/configuration.h"
30 #define PROGRAM_NAME "distribute"
32 void version(void)
34 std::cout
35 << PROGRAM_NAME << " (" << PACKAGE_NAME << ") " << PACKAGE_VERSION << std::endl
36 << std::endl
37 << "Copyright (C) 2008 Francesco Salvestrini" << std::endl
38 << std::endl
39 << "This is free software. You may redistribute copies of it under the terms of" << std::endl
40 << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>." << std::endl
41 << "There is NO WARRANTY, to the extent permitted by law." << std::endl;
45 #define DEFAULT_TIMEOUT 10
47 void help(void)
49 std::cout
50 << "Usage: " << PROGRAM_NAME << " [OPTION]... "<< std::endl
51 << std::endl
52 << "Options: " << std::endl
53 << " -t, --time-out=TIME time-out in secs (default " << DEFAULT_TIMEOUT << ")" << std::endl
54 << " -j, --job=FILE use FILE as job to distribute" << std::endl
55 << " -d, --debug enable debugging traces" << std::endl
56 << " -h, --help print this help, then exit" << std::endl
57 << " -V, --version print version number, then exit" << std::endl
58 << std::endl
59 << "Report bugs to <" << PACKAGE_BUGREPORT << ">" << std::endl;
62 void hint(const std::string & message)
64 BUG_ON(message.size() == 0);
66 std::cout
67 << message << std::endl
68 << "Try `" << PROGRAM_NAME << " -h' for more information." << std::endl;
71 int main(int argc, char * argv[])
73 TR_CONFIG_LVL(TR_LVL_DEFAULT);
74 TR_CONFIG_PFX(PROGRAM_NAME);
76 try {
77 std::string conffile = "";
79 int time_out = DEFAULT_TIMEOUT;
80 bool time_out_set = false;
81 std::string job = "";
83 int c;
84 //int digit_optind = 0;
85 while (1) {
86 //int this_option_optind = optind ? optind : 1;
87 int option_index = 0;
89 static struct option long_options[] = {
90 { "config", 1, 0, 'c' },
91 { "job", 1, 0, 'j' },
92 { "time-out", 1, 0, 't' },
93 { "debug", 0, 0, 'd' },
94 { "verbose", 0, 0, 'v' },
95 { "version", 0, 0, 'V' },
96 { "help", 0, 0, 'h' },
97 { 0, 0, 0, 0 }
100 c = getopt_long(argc, argv, "c:t:j:dvVh",
101 long_options, &option_index);
102 if (c == -1) {
103 break;
106 switch (c) {
107 case 'c':
108 conffile = optarg;
109 break;
110 case 't':
112 // XXX FIXME:
113 // The current solution is ugly.
114 // Add modifiers (s, m, h) to
115 // this option in order to use
116 // different time granularity.
118 time_out = atoi(optarg);
119 if (time_out < 0) {
120 hint("Wrong time-out value");
121 return 1;
123 time_out_set = true;
124 break;
125 case 'j':
126 job = optarg;
127 break;
128 case 'd':
129 TR_CONFIG_LVL(TR_LVL_DEBUG);
130 break;
131 case 'v':
132 TR_CONFIG_LVL(TR_LVL_VERBOSE);
133 break;
134 case 'V':
135 version();
136 return 0;
137 case 'h':
138 help();
139 return 0;
140 case '?':
141 hint("Unrecognized option");
142 return 1;
144 default:
145 BUG();
146 return 1;
150 // Options related checks
151 BUG_ON(time_out < 0);
153 // Build configuration file path
154 if (conffile.size() == 0) {
155 std::string homedir = Environment::get("HOME");
156 std::string confdir =
157 homedir +
158 std::string("/") +
159 std::string(".") +
160 std::string(PACKAGE_TARNAME);
161 conffile =
162 confdir +
163 std::string("/") +
164 std::string(PROGRAM_NAME);
165 } else {
166 TR_DBG("Configuration file overridden\n");
169 BUG_ON(conffile.size() == 0);
171 TR_DBG("Initial (configuration file) values:\n");
172 TR_DBG(" Time out: '%d'\n", time_out);
174 // Read configuration file (if available)
175 try {
176 TR_DBG("Reading configuration file from '%s'\n",
177 conffile.c_str());
179 Configuration::File config;
180 std::ifstream instream(conffile.c_str());
182 instream >> config;
184 int conf_timeout;
186 if (config.get<int>(conf_timeout, "time-out")) {
187 TR_DBG("Found 'max-mem' key, value '%d'\n",
188 conf_timeout);
190 // Check gathered configuration
191 if (time_out < 0) {
192 TR_ERR("Wrong time-out value in "
193 "configuration file");
194 return 1;
197 if (!time_out_set) {
198 TR_DBG("Updating 'time-out' key\n");
200 // Configuration value not specified
201 // in command line ...
202 time_out = conf_timeout;
205 } catch (std::exception & e) {
206 TR_ERR("%s\n", e.what());
207 } catch (...) {
208 BUG();
211 // Options related checks
212 BUG_ON(time_out < 0);
214 TR_DBG("Final (configuration file) values:\n");
215 TR_DBG(" Time out: '%d'\n", time_out);
217 // Dump (acquired and derived) infos
218 TR_DBG("Final values:\n");
219 TR_DBG(" Time out: '%d'\n", time_out);
220 TR_DBG(" Job: '%s'\n", job.c_str());
221 TR_DBG(" Configuration file: '%s'\n", conffile.c_str());
223 // Assert what is needed to be asserted
224 BUG_ON(time_out < 0);
226 // Do the supposed work
227 MISSING_CODE();
228 } catch (...) {
229 BUG();
232 return 0;