59 #include "CommandStates.h" |
59 #include "CommandStates.h" |
60 #include "CommandUpload.h" |
60 #include "CommandUpload.h" |
61 #include "CommandVersion.h" |
61 #include "CommandVersion.h" |
62 #include "CommandXml.h" |
62 #include "CommandXml.h" |
63 |
63 |
|
64 #include "NumberListParser.h" |
|
65 #include "MasterDevice.h" |
|
66 |
64 /*****************************************************************************/ |
67 /*****************************************************************************/ |
65 |
68 |
66 typedef list<Command *> CommandList; |
69 typedef list<Command *> CommandList; |
67 CommandList commandList; |
70 CommandList commandList; |
68 |
|
69 MasterDevice masterDev; |
|
70 |
71 |
71 string binaryBaseName; |
72 string binaryBaseName; |
72 string commandName; |
73 string commandName; |
73 Command::StringVector commandArgs; |
74 Command::StringVector commandArgs; |
74 |
75 |
75 // option variables |
76 // option variables |
76 unsigned int masterIndex = 0; |
77 list<unsigned int> masterIndices; |
|
78 string masterIndexList = "-"; // all masters |
77 int slavePosition = -1; |
79 int slavePosition = -1; |
78 int slaveAlias = -1; |
80 int slaveAlias = -1; |
79 int domainIndex = -1; |
81 int domainIndex = -1; |
80 string dataTypeStr; |
82 string dataTypeStr; |
81 Command::Verbosity verbosity = Command::Normal; |
83 Command::Verbosity verbosity = Command::Normal; |
126 return str.str(); |
128 return str.str(); |
127 } |
129 } |
128 |
130 |
129 /*****************************************************************************/ |
131 /*****************************************************************************/ |
130 |
132 |
|
133 class MasterIndexParser: |
|
134 public NumberListParser |
|
135 { |
|
136 unsigned int getMax() |
|
137 { |
|
138 MasterDevice dev; |
|
139 dev.setIndex(0U); |
|
140 dev.open(MasterDevice::Read); |
|
141 return dev.getMasterCount() - 1; |
|
142 }; |
|
143 }; |
|
144 |
|
145 /*****************************************************************************/ |
|
146 |
131 void getOptions(int argc, char **argv) |
147 void getOptions(int argc, char **argv) |
132 { |
148 { |
133 int c, argCount; |
149 int c, argCount; |
134 stringstream str; |
150 stringstream str; |
135 |
151 |
151 do { |
167 do { |
152 c = getopt_long(argc, argv, "m:a:p:d:t:o:fqvh", longOptions, NULL); |
168 c = getopt_long(argc, argv, "m:a:p:d:t:o:fqvh", longOptions, NULL); |
153 |
169 |
154 switch (c) { |
170 switch (c) { |
155 case 'm': |
171 case 'm': |
156 str.clear(); |
172 masterIndexList = optarg; |
157 str.str(""); |
|
158 str << optarg; |
|
159 str >> resetiosflags(ios::basefield) // guess base from prefix |
|
160 >> masterIndex; |
|
161 if (str.fail() || masterIndex < 0) { |
|
162 cerr << "Invalid master number " << optarg << "!" << endl |
|
163 << endl << usage(); |
|
164 exit(1); |
|
165 } |
|
166 break; |
173 break; |
167 |
174 |
168 case 'a': |
175 case 'a': |
169 str.clear(); |
176 str.clear(); |
170 str.str(""); |
177 str.str(""); |
248 } else { |
255 } else { |
249 cerr << "Please specify a command!" << endl |
256 cerr << "Please specify a command!" << endl |
250 << endl << usage(); |
257 << endl << usage(); |
251 exit(1); |
258 exit(1); |
252 } |
259 } |
|
260 } |
|
261 |
|
262 try { |
|
263 MasterIndexParser p; |
|
264 masterIndices = p.parse(masterIndexList.c_str()); |
|
265 } catch (MasterDeviceException &e) { |
|
266 cerr << "Failed to obtain number of masters: " << e.what() << endl; |
|
267 exit(1); |
|
268 } catch (runtime_error &e) { |
|
269 cerr << "Invalid master argument " << masterIndexList |
|
270 << ": " << e.what() << endl |
|
271 << endl << usage(); |
|
272 exit(1); |
253 } |
273 } |
254 |
274 |
255 commandName = argv[optind]; |
275 commandName = argv[optind]; |
256 while (++optind < argc) |
276 while (++optind < argc) |
257 commandArgs.push_back(string(argv[optind])); |
277 commandArgs.push_back(string(argv[optind])); |
321 commandList.push_back(new CommandXml()); |
341 commandList.push_back(new CommandXml()); |
322 |
342 |
323 getOptions(argc, argv); |
343 getOptions(argc, argv); |
324 |
344 |
325 matchingCommands = getMatchingCommands(commandName); |
345 matchingCommands = getMatchingCommands(commandName); |
326 masterDev.setIndex(masterIndex); |
346 |
|
347 if (masterIndices.empty()) { |
|
348 cerr << "List of master indices may not be empty!" << endl |
|
349 << endl << usage(); |
|
350 exit(1); |
|
351 } |
327 |
352 |
328 if (matchingCommands.size()) { |
353 if (matchingCommands.size()) { |
329 if (matchingCommands.size() == 1) { |
354 if (matchingCommands.size() == 1) { |
330 cmd = matchingCommands.front(); |
355 cmd = matchingCommands.front(); |
331 if (!helpRequested) { |
356 if (!helpRequested) { |
332 try { |
357 try { |
|
358 cmd->setMasterIndices(masterIndices); |
333 cmd->setVerbosity(verbosity); |
359 cmd->setVerbosity(verbosity); |
334 cmd->setAlias(slaveAlias); |
360 cmd->setAlias(slaveAlias); |
335 cmd->setPosition(slavePosition); |
361 cmd->setPosition(slavePosition); |
336 cmd->setDomain(domainIndex); |
362 cmd->setDomain(domainIndex); |
337 cmd->setDataType(dataTypeStr); |
363 cmd->setDataType(dataTypeStr); |
338 cmd->setOutputFile(outputFile); |
364 cmd->setOutputFile(outputFile); |
339 cmd->setForce(force); |
365 cmd->setForce(force); |
340 cmd->execute(masterDev, commandArgs); |
366 cmd->execute(commandArgs); |
341 } catch (InvalidUsageException &e) { |
367 } catch (InvalidUsageException &e) { |
342 cerr << e.what() << endl << endl; |
368 cerr << e.what() << endl << endl; |
343 cerr << binaryBaseName << " " << cmd->helpString(); |
369 cerr << binaryBaseName << " " << cmd->helpString(); |
344 retval = 1; |
370 retval = 1; |
345 } catch (CommandException &e) { |
371 } catch (CommandException &e) { |