1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL |
---|
17 | |
---|
18 | #include "shell_command.h" |
---|
19 | #include "shell_command_class.h" |
---|
20 | |
---|
21 | #include "compiler.h" |
---|
22 | #include "debug.h" |
---|
23 | #include "class_list.h" |
---|
24 | |
---|
25 | #include "key_names.h" |
---|
26 | |
---|
27 | namespace OrxShell |
---|
28 | { |
---|
29 | SHELL_COMMAND_STATIC(debug, ShellCommand, ShellCommand::debug); |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * @brief constructs and registers a new Command |
---|
34 | * @param commandName the name of the Command |
---|
35 | * @param className the name of the class to apply this command to |
---|
36 | * @param paramCount the count of parameters this command takes |
---|
37 | */ |
---|
38 | ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, Executor* executor) |
---|
39 | { |
---|
40 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
41 | PRINTF(4)("create shellcommand '%s' for class '%s'\n", commandName.c_str(), className.c_str()); |
---|
42 | this->setName(commandName); |
---|
43 | |
---|
44 | // copy the executor: |
---|
45 | this->executor = executor; |
---|
46 | this->executor->setName(commandName); |
---|
47 | |
---|
48 | for (unsigned int i = 0; i < this->executor->getParamCount(); i++) |
---|
49 | this->completors.push_back(new CompletorDefault(&this->executor->getDefaultValue(i))); |
---|
50 | this->alias = NULL; |
---|
51 | |
---|
52 | // this->classID = classID; |
---|
53 | this->shellClass = ShellCommandClass::acquireCommandClass(className); |
---|
54 | assert (this->shellClass != NULL); |
---|
55 | this->shellClass->registerCommand(this); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * @brief deconstructs a ShellCommand |
---|
60 | */ |
---|
61 | ShellCommand::~ShellCommand() |
---|
62 | { |
---|
63 | this->shellClass->unregisterCommand(this); |
---|
64 | if (this->alias != NULL) |
---|
65 | delete this->alias; |
---|
66 | while (!this->completors.empty()) |
---|
67 | { |
---|
68 | delete this->completors.back(); |
---|
69 | this->completors.pop_back(); |
---|
70 | } |
---|
71 | delete this->executor; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * @brief registers a new ShellCommand |
---|
76 | */ |
---|
77 | ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, Executor* executor) |
---|
78 | { |
---|
79 | if (ShellCommand::exists(commandName, className)) |
---|
80 | { |
---|
81 | delete executor; |
---|
82 | return NULL; |
---|
83 | } |
---|
84 | else |
---|
85 | return new ShellCommand(commandName, className, executor); |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * @brief unregister an existing commandName |
---|
91 | * @param className the name of the Class the command belongs to. |
---|
92 | * @param commandName the name of the command itself |
---|
93 | */ |
---|
94 | void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className) |
---|
95 | { |
---|
96 | |
---|
97 | ShellCommandClass* cmdClass = ShellCommandClass::acquireCommandClass(className); |
---|
98 | if (cmdClass != NULL) |
---|
99 | { |
---|
100 | std::vector<ShellCommand*>::iterator cmd; |
---|
101 | for (cmd = cmdClass->commandList.begin(); cmd < cmdClass->commandList.end(); cmd++) |
---|
102 | if (commandName == (*cmd)->getName()) |
---|
103 | { |
---|
104 | delete (*cmd); |
---|
105 | break; |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * @brief gets a command if it has already been registered. |
---|
112 | * @param commandName the name of the Command |
---|
113 | * @param cmdClass the CommandClass of the Class the command is in. |
---|
114 | * @returns The Registered Command, or NULL if it does not exist. |
---|
115 | */ |
---|
116 | const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const ShellCommandClass* cmdClass) |
---|
117 | { |
---|
118 | assert(cmdClass != NULL); |
---|
119 | |
---|
120 | std::vector<ShellCommand*>::const_iterator elem; |
---|
121 | for (elem = cmdClass->commandList.begin(); elem != cmdClass->commandList.end(); elem++) |
---|
122 | if (commandName == (*elem)->getName()) |
---|
123 | return (*elem); |
---|
124 | return NULL; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | /** |
---|
129 | * @brief gets a command if it has already been registered. |
---|
130 | * @param commandName the name of the Command |
---|
131 | * @param className the name of the Class the command is in. |
---|
132 | * @returns The Registered Command, or NULL if it does not exist. |
---|
133 | */ |
---|
134 | const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className) |
---|
135 | { |
---|
136 | const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className); |
---|
137 | if (likely(checkClass != NULL)) |
---|
138 | return ShellCommand::getCommand(commandName, checkClass); |
---|
139 | return NULL; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * @brief takes out an InputLine, searching for a Command. |
---|
144 | * @see const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings) |
---|
145 | * @param inputLine: the Input to analyse. |
---|
146 | * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section. |
---|
147 | * @returns: The ShellCommand if found. |
---|
148 | */ |
---|
149 | const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList) |
---|
150 | { |
---|
151 | ShellCommand::getCommandFromInput(SubString(inputLine, SubString::WhiteSpaces), paramBegin); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * @brief takes out an InputLine, searching for a Command. |
---|
156 | * @param strings: the Input to analyse. |
---|
157 | * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section. |
---|
158 | * @returns: The ShellCommand if found. |
---|
159 | */ |
---|
160 | const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList) |
---|
161 | { |
---|
162 | // no input, no Command. |
---|
163 | if (strings.size() == 0) |
---|
164 | { |
---|
165 | paramBegin = 0; |
---|
166 | return NULL; |
---|
167 | } |
---|
168 | |
---|
169 | // CHECK FOR ALIAS |
---|
170 | std::vector<ShellCommandAlias*>::const_iterator alias; |
---|
171 | for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ ) |
---|
172 | { |
---|
173 | if (strings[0] == (*alias)->getName()) |
---|
174 | { |
---|
175 | assert ((*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL); |
---|
176 | // Search for Objects. |
---|
177 | if (strings.size() == 1) |
---|
178 | { |
---|
179 | if (fillObjectList("", (*alias)->getCommand(), boList)) |
---|
180 | ; |
---|
181 | } |
---|
182 | else |
---|
183 | { |
---|
184 | if (!fillObjectList(strings[1], (*alias)->getCommand(), boList)) |
---|
185 | fillObjectList("", (*alias)->getCommand(), boList); |
---|
186 | } |
---|
187 | return (*alias)->getCommand(); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | // CHECK FOR COMMAND_CLASS |
---|
192 | const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]); |
---|
193 | if (cmdClass != NULL) |
---|
194 | { |
---|
195 | const ShellCommand* retCmd; |
---|
196 | // Function/Command right after Class |
---|
197 | if (strings.size() >= 1) |
---|
198 | { |
---|
199 | // Search for Objects. |
---|
200 | retCmd = ShellCommand::getCommand(strings[1], cmdClass); |
---|
201 | if (retCmd != NULL) |
---|
202 | { |
---|
203 | paramBegin = 2; |
---|
204 | fillObjectList("", retCmd, boList); |
---|
205 | return retCmd; |
---|
206 | } |
---|
207 | } |
---|
208 | // Function/Command after Class and 'Object' |
---|
209 | if (retCmd == NULL && strings.size() >= 2) |
---|
210 | { |
---|
211 | retCmd = ShellCommand::getCommand(strings[2], cmdClass); |
---|
212 | if (retCmd != NULL) |
---|
213 | { |
---|
214 | paramBegin = 3; |
---|
215 | fillObjectList(strings[1], retCmd, boList); |
---|
216 | return retCmd; |
---|
217 | } |
---|
218 | } |
---|
219 | if (retCmd != NULL) // check for the paramBegin. |
---|
220 | return retCmd; |
---|
221 | } |
---|
222 | // Nothing usefull found at all. |
---|
223 | paramBegin = 0; |
---|
224 | return NULL; |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * @brief fills the ObjectList boList with Objects that can be reffered to by cmd. |
---|
229 | * @param objectDescriptor: the ObjectName (beginning, full name or empty) to fill the List with |
---|
230 | * @param cmd: The Command to complete Objects for. |
---|
231 | * @param boList: The List of BaseObject's that will be filled with found entries. |
---|
232 | * @returns: true if more than one Entry was fond, else (false , or if boList is NULL). |
---|
233 | */ |
---|
234 | bool ShellCommand::fillObjectList(const std::string& objectDescriptor, const ShellCommand* cmd, std::vector<BaseObject*>* boList) |
---|
235 | { |
---|
236 | assert (cmd != NULL && cmd->shellClass != NULL); |
---|
237 | if(boList == NULL) |
---|
238 | return false; |
---|
239 | |
---|
240 | const std::list<BaseObject*>* objectList = ClassList::getList(cmd->shellClass->getName()); |
---|
241 | if (objectList != NULL) |
---|
242 | { |
---|
243 | std::list<BaseObject*>::const_iterator bo; |
---|
244 | |
---|
245 | // No Description given (only for speedup) |
---|
246 | if (objectDescriptor.empty()) |
---|
247 | { |
---|
248 | for (bo = objectList->begin(); bo != objectList->end(); bo++) |
---|
249 | boList->push_back(*bo); |
---|
250 | } |
---|
251 | // some description |
---|
252 | else |
---|
253 | { |
---|
254 | for (bo = objectList->begin(); bo != objectList->end(); bo++) |
---|
255 | if ((*bo)->getName() != NULL && !nocaseCmp(objectDescriptor, (*bo)->getName(), objectDescriptor.size())) |
---|
256 | boList->push_back(*bo); |
---|
257 | } |
---|
258 | } |
---|
259 | return !boList->empty(); |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * @brief checks if a command has already been registered. |
---|
264 | * @param commandName the name of the Command |
---|
265 | * @param className the name of the Class the command should apply to. |
---|
266 | * @returns true, if the command is registered/false otherwise |
---|
267 | * |
---|
268 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
269 | * This is checked in the registerCommand-function. |
---|
270 | */ |
---|
271 | bool ShellCommand::exists(const std::string& commandName, const std::string& className) |
---|
272 | { |
---|
273 | return (ShellCommand::getCommand(commandName, className) != NULL); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | /** |
---|
278 | * @brief executes commands |
---|
279 | * @param executionString the string containing the following input |
---|
280 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
281 | * @return true on success, false otherwise. |
---|
282 | */ |
---|
283 | bool ShellCommand::execute(const std::string& executionString) |
---|
284 | { |
---|
285 | SubString inputSplits(executionString, SubString::WhiteSpacesWithComma); |
---|
286 | |
---|
287 | // if we do not have any input return |
---|
288 | if (inputSplits.empty()) |
---|
289 | return false; |
---|
290 | |
---|
291 | unsigned int paramBegin; |
---|
292 | const ShellCommand* sc; |
---|
293 | std::vector<BaseObject*> boList; |
---|
294 | sc = getCommandFromInput(inputSplits, paramBegin, &boList); |
---|
295 | if (sc != NULL) |
---|
296 | { |
---|
297 | PRINT(0)("Command %s on ", sc->getName()); |
---|
298 | for(std::vector<BaseObject*>::const_iterator bo = boList.begin(); bo != boList.end(); ++bo) |
---|
299 | { |
---|
300 | PRINT(0)("%s::%s\n", (*bo)->getClassName(), (*bo)->getName()); |
---|
301 | (*sc->executor)((*bo), inputSplits.getSubSet(paramBegin).join()); |
---|
302 | } |
---|
303 | return true; |
---|
304 | } |
---|
305 | return false; |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | /** |
---|
310 | * @brief lets a command be described |
---|
311 | * @param description the description of the Given command |
---|
312 | */ |
---|
313 | ShellCommand* ShellCommand::describe(const std::string& description) |
---|
314 | { |
---|
315 | if (this == NULL) |
---|
316 | return NULL; |
---|
317 | this->description = description; |
---|
318 | return this; |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | * @brief adds an Alias to this Command |
---|
323 | * @param alias the name of the Alias to set |
---|
324 | * @returns itself |
---|
325 | */ |
---|
326 | ShellCommand* ShellCommand::setAlias(const std::string& alias) |
---|
327 | { |
---|
328 | if (this == NULL) |
---|
329 | return NULL; |
---|
330 | |
---|
331 | if (this->alias != NULL) |
---|
332 | { |
---|
333 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); |
---|
338 | this->alias = aliasCMD; |
---|
339 | } |
---|
340 | return this; |
---|
341 | } |
---|
342 | |
---|
343 | /** |
---|
344 | * @brief set the default values of the executor |
---|
345 | * @param value0 the first default value |
---|
346 | * @param value1 the second default value |
---|
347 | * @param value2 the third default value |
---|
348 | * @param value3 the fourth default value |
---|
349 | * @param value4 the fifth default value |
---|
350 | */ |
---|
351 | ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1, |
---|
352 | const MultiType& value2, const MultiType& value3, |
---|
353 | const MultiType& value4) |
---|
354 | { |
---|
355 | if (this == NULL || this->executor == NULL) |
---|
356 | return NULL; |
---|
357 | |
---|
358 | this->executor->defaultValues(value0, value1, value2, value3, value4); |
---|
359 | |
---|
360 | return this; |
---|
361 | } |
---|
362 | |
---|
363 | ShellCommand* ShellCommand::completionPlugin(unsigned int parameter, const CompletorPlugin& completorPlugin) |
---|
364 | { |
---|
365 | if (this == NULL || this->executor == NULL) |
---|
366 | return NULL; |
---|
367 | |
---|
368 | if (parameter >= this->executor->getParamCount()) |
---|
369 | { |
---|
370 | PRINTF(1)("Parameter %d not inside of valid ParameterCount %d of Command %s::%s\n", |
---|
371 | parameter, this->executor->getParamCount(), this->getName(), this->shellClass->getName()); |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | delete this->completors[parameter]; |
---|
376 | this->completors[parameter] = completorPlugin.clone(); |
---|
377 | } |
---|
378 | return this; |
---|
379 | } |
---|
380 | |
---|
381 | |
---|
382 | /** |
---|
383 | * @brief prints out nice information about the Shells Commands |
---|
384 | */ |
---|
385 | void ShellCommand::debug() |
---|
386 | { |
---|
387 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
388 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
389 | { |
---|
390 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
391 | |
---|
392 | std::vector<ShellCommand*>::iterator cmdIT; |
---|
393 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
394 | { |
---|
395 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
396 | /// FIXME |
---|
397 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
398 | printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
399 | if (!(*cmdIT)->description.empty()) |
---|
400 | printf("- %s", (*cmdIT)->description.c_str()); |
---|
401 | printf("\n"); |
---|
402 | |
---|
403 | } |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | /** |
---|
408 | * @brief converts a Parameter to a String |
---|
409 | * @param parameter the Parameter we have. |
---|
410 | * @returns the Name of the Parameter at Hand |
---|
411 | */ |
---|
412 | const std::string& ShellCommand::paramToString(long parameter) |
---|
413 | { |
---|
414 | return MultiType::MultiTypeToString((MT_Type)parameter); |
---|
415 | } |
---|
416 | |
---|
417 | |
---|
418 | |
---|
419 | /////////// |
---|
420 | // ALIAS // |
---|
421 | /////////// |
---|
422 | |
---|
423 | /** |
---|
424 | * @param aliasName the name of the Alias |
---|
425 | * @param command the Command, to associate this alias with |
---|
426 | */ |
---|
427 | ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command) |
---|
428 | { |
---|
429 | this->aliasName = aliasName; |
---|
430 | this->command = command; |
---|
431 | ShellCommandAlias::aliasList.push_back(this); |
---|
432 | }; |
---|
433 | |
---|
434 | ShellCommandAlias::~ShellCommandAlias() |
---|
435 | { |
---|
436 | std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this); |
---|
437 | if (delA != aliasList.end()) |
---|
438 | ShellCommandAlias::aliasList.push_back(this); |
---|
439 | |
---|
440 | } |
---|
441 | |
---|
442 | std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList; |
---|
443 | /** |
---|
444 | * @brief collects the Aliases registered to the ShellCommands |
---|
445 | * @param stringList a List to paste the Aliases into. |
---|
446 | * @returns true on success, false otherwise |
---|
447 | */ |
---|
448 | |
---|
449 | bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList) |
---|
450 | { |
---|
451 | std::vector<ShellCommandAlias*>::iterator alias; |
---|
452 | for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++) |
---|
453 | stringList.push_back((*alias)->getName()); |
---|
454 | return true; |
---|
455 | } |
---|
456 | |
---|
457 | |
---|
458 | } |
---|