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 | |
---|
30 | /** |
---|
31 | * constructs and registers a new Command |
---|
32 | * @param commandName the name of the Command |
---|
33 | * @param className the name of the class to apply this command to |
---|
34 | * @param paramCount the count of parameters this command takes |
---|
35 | */ |
---|
36 | ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, const Executor& executor) |
---|
37 | { |
---|
38 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
39 | PRINTF(5)("create shellcommand %s %s\n", commandName, className); |
---|
40 | this->setName(commandName); |
---|
41 | this->executor = executor.clone(); |
---|
42 | this->executor->setName(commandName); |
---|
43 | this->alias = NULL; |
---|
44 | |
---|
45 | // this->classID = classID; |
---|
46 | this->shellClass = ShellCommandClass::getCommandClass(className); |
---|
47 | if (this->shellClass != NULL) |
---|
48 | this->shellClass->commandList.push_back(this); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * deconstructs a ShellCommand |
---|
53 | */ |
---|
54 | ShellCommand::~ShellCommand() |
---|
55 | { |
---|
56 | if (this->alias != NULL) |
---|
57 | delete this->alias; |
---|
58 | delete this->executor; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * registers a new ShellCommand |
---|
63 | */ |
---|
64 | ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, const Executor& executor) |
---|
65 | { |
---|
66 | if (ShellCommand::isRegistered(commandName, className)) |
---|
67 | return NULL; |
---|
68 | else |
---|
69 | return new ShellCommand(commandName, className, executor); |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * unregister an existing commandName |
---|
75 | * @param className the name of the Class the command belongs to. |
---|
76 | * @param commandName the name of the command itself |
---|
77 | */ |
---|
78 | void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className) |
---|
79 | { |
---|
80 | /// FIXME |
---|
81 | /* if (ShellCommandClass::commandClassList == NULL) |
---|
82 | ShellCommandClass::initCommandClassList(); |
---|
83 | |
---|
84 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
85 | |
---|
86 | if (checkClass != NULL) |
---|
87 | { |
---|
88 | std::list<ShellCommand*>::iterator elem; |
---|
89 | for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++) |
---|
90 | { |
---|
91 | if (!strcmp(commandName, (*elem)->getName())) |
---|
92 | { |
---|
93 | delete (*elem); |
---|
94 | checkClass->commandList.remove(*elem); |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | if (checkClass->commandList->size() == 0) |
---|
100 | { |
---|
101 | ShellCommandClass::commandClassList->remove(checkClass); |
---|
102 | delete checkClass; |
---|
103 | } |
---|
104 | }*/ |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * checks if a command has already been registered. |
---|
109 | * @param commandName the name of the Command |
---|
110 | * @param className the name of the Class the command should apply to. |
---|
111 | * @returns true, if the command is registered/false otherwise |
---|
112 | * |
---|
113 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
114 | * This is checked in the registerCommand-function. |
---|
115 | */ |
---|
116 | bool ShellCommand::isRegistered(const std::string& commandName, const std::string& className) |
---|
117 | { |
---|
118 | if (ShellCommandClass::commandClassList == NULL) |
---|
119 | { |
---|
120 | ShellCommandClass::initCommandClassList(); |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
124 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
125 | if (checkClass != NULL) |
---|
126 | { |
---|
127 | std::vector<ShellCommand*>::const_iterator elem; |
---|
128 | for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++) |
---|
129 | { |
---|
130 | if (commandName == (*elem)->getName()) |
---|
131 | { |
---|
132 | PRINTF(2)("Command '%s::%s' already registered\n", className.c_str(), commandName.c_str()); |
---|
133 | return true; |
---|
134 | } |
---|
135 | } |
---|
136 | return false; |
---|
137 | } |
---|
138 | else |
---|
139 | return false; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | * executes commands |
---|
145 | * @param executionString the string containing the following input |
---|
146 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
147 | * @return true on success, false otherwise. |
---|
148 | */ |
---|
149 | bool ShellCommand::execute(const std::string& executionString) |
---|
150 | { |
---|
151 | if (ShellCommandClass::commandClassList == NULL) |
---|
152 | return false; |
---|
153 | |
---|
154 | long classID = CL_NULL; //< the classID retrieved from the Class. |
---|
155 | ShellCommandClass* commandClass = NULL; //< the command class this command applies to. |
---|
156 | const std::list<BaseObject*>* objectList = NULL; //< the list of Objects stored in classID |
---|
157 | BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on |
---|
158 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
159 | // long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
160 | SubString inputSplits(executionString, SubString::WhiteSpacesWithComma); |
---|
161 | |
---|
162 | |
---|
163 | // if we do not have any input return |
---|
164 | if (inputSplits.empty()) |
---|
165 | return false; |
---|
166 | |
---|
167 | // if we only have one input (!MUST BE AN ALIAS) |
---|
168 | if (inputSplits.size() >= 1) |
---|
169 | { |
---|
170 | // CHECK FOR ALIAS |
---|
171 | std::vector<ShellCommandAlias*>::iterator alias; |
---|
172 | for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++ ) |
---|
173 | { |
---|
174 | if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL && |
---|
175 | (*alias)->getCommand()->shellClass != NULL ) |
---|
176 | { |
---|
177 | objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName()); |
---|
178 | if (objectList != NULL) |
---|
179 | { |
---|
180 | (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join()); |
---|
181 | return true; |
---|
182 | } |
---|
183 | /// TODO CHECK FOR STATIC functions. |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | // looking for a Matching Class |
---|
188 | if (likely(ShellCommandClass::commandClassList != NULL)) |
---|
189 | { |
---|
190 | std::list<ShellCommandClass*>::iterator commandClassIT; |
---|
191 | for (commandClassIT = ShellCommandClass::commandClassList->begin(); commandClassIT != ShellCommandClass::commandClassList->end(); commandClassIT++) |
---|
192 | { |
---|
193 | if ((*commandClassIT)->getName() && inputSplits[0] == (*commandClassIT)->getName()) |
---|
194 | { |
---|
195 | //elemCL->getName(); |
---|
196 | classID = ClassList::StringToID((*commandClassIT)->getName()); |
---|
197 | commandClass = (*commandClassIT); |
---|
198 | objectList = ClassList::getList((ClassID)classID); |
---|
199 | break; |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | // Second Agument. (either Object, or Function) |
---|
205 | if (commandClass != NULL && inputSplits.size() >= 2) |
---|
206 | { |
---|
207 | int fktPos = 1; // The position of the Function (either at pos 1, or 2) |
---|
208 | // If we have an ObjectList. |
---|
209 | if (objectList != NULL) |
---|
210 | { |
---|
211 | // Checking for a Match in the Objects of classID (else take the first) |
---|
212 | std::list<BaseObject*>::const_iterator object; |
---|
213 | for (object = objectList->begin(); object != objectList->end(); object++) |
---|
214 | { |
---|
215 | if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName()) |
---|
216 | { |
---|
217 | objectPointer = (*object); |
---|
218 | fktPos = 2; |
---|
219 | break; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | // if we did not find an Object with matching name, take the first. |
---|
224 | if (objectPointer == NULL) |
---|
225 | objectPointer = objectList->front(); |
---|
226 | } |
---|
227 | |
---|
228 | // match a function. |
---|
229 | if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3))) |
---|
230 | { |
---|
231 | std::vector<ShellCommand*>::iterator cmdIT; |
---|
232 | for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++) |
---|
233 | { |
---|
234 | if (inputSplits[fktPos] == (*cmdIT)->getName()) |
---|
235 | { |
---|
236 | if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective) |
---|
237 | return false; |
---|
238 | else |
---|
239 | { |
---|
240 | (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK |
---|
241 | return true; |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|
248 | return false; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * lets a command be described |
---|
253 | * @param description the description of the Given command |
---|
254 | */ |
---|
255 | ShellCommand* ShellCommand::describe(const std::string& description) |
---|
256 | { |
---|
257 | if (this == NULL) |
---|
258 | return NULL; |
---|
259 | else |
---|
260 | { |
---|
261 | this->description = description; |
---|
262 | return this; |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | /** |
---|
267 | * @brief adds an Alias to this Command |
---|
268 | * @param alias the name of the Alias to set |
---|
269 | * @returns itself |
---|
270 | */ |
---|
271 | ShellCommand* ShellCommand::setAlias(const std::string& alias) |
---|
272 | { |
---|
273 | if (this == NULL) |
---|
274 | return NULL; |
---|
275 | |
---|
276 | if (this->alias != NULL) |
---|
277 | { |
---|
278 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); |
---|
279 | } |
---|
280 | else |
---|
281 | { |
---|
282 | ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); |
---|
283 | ShellCommandAlias::aliasList.push_back(aliasCMD); |
---|
284 | this->alias = aliasCMD; |
---|
285 | } |
---|
286 | return this; |
---|
287 | } |
---|
288 | |
---|
289 | /** |
---|
290 | * @brief set the default values of the executor |
---|
291 | * @param value0 the first default value |
---|
292 | * @param value1 the second default value |
---|
293 | * @param value2 the third default value |
---|
294 | * @param value3 the fourth default value |
---|
295 | * @param value4 the fifth default value |
---|
296 | */ |
---|
297 | ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1, |
---|
298 | const MultiType& value2, const MultiType& value3, |
---|
299 | const MultiType& value4) |
---|
300 | { |
---|
301 | if (this == NULL || this->executor == NULL) |
---|
302 | return NULL; |
---|
303 | |
---|
304 | this->executor->defaultValues(value0, value1, value2, value3, value4); |
---|
305 | |
---|
306 | return this; |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * prints out nice information about the Shells Commands |
---|
311 | */ |
---|
312 | void ShellCommand::debug() |
---|
313 | { |
---|
314 | if (ShellCommandClass::commandClassList == NULL) |
---|
315 | { |
---|
316 | PRINT(0)("No Command registered.\n"); |
---|
317 | return; |
---|
318 | } |
---|
319 | |
---|
320 | std::list<ShellCommandClass*>::iterator classIT; |
---|
321 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
322 | { |
---|
323 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
324 | |
---|
325 | std::vector<ShellCommand*>::iterator cmdIT; |
---|
326 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
327 | { |
---|
328 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
329 | /// FIXME |
---|
330 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
331 | printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
332 | if (!(*cmdIT)->description.empty()) |
---|
333 | printf("- %s", (*cmdIT)->description.c_str()); |
---|
334 | printf("\n"); |
---|
335 | |
---|
336 | } |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * converts a Parameter to a String |
---|
342 | * @param parameter the Parameter we have. |
---|
343 | * @returns the Name of the Parameter at Hand |
---|
344 | */ |
---|
345 | const char* ShellCommand::paramToString(long parameter) |
---|
346 | { |
---|
347 | return MultiType::MultiTypeToString((MT_Type)parameter); |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command) |
---|
352 | { |
---|
353 | this->aliasName = aliasName; |
---|
354 | this->command = command; |
---|
355 | ShellCommandAlias::aliasList.push_back(this); |
---|
356 | }; |
---|
357 | |
---|
358 | ShellCommandAlias::~ShellCommandAlias() |
---|
359 | { |
---|
360 | std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this); |
---|
361 | if (delA != aliasList.end()) |
---|
362 | ShellCommandAlias::aliasList.push_back(this); |
---|
363 | |
---|
364 | } |
---|
365 | |
---|
366 | std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList; |
---|
367 | /** |
---|
368 | * @brief collects the Aliases registered to the ShellCommands |
---|
369 | * @param stringList a List to paste the Aliases into. |
---|
370 | * @returns true on success, false otherwise |
---|
371 | */ |
---|
372 | bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList) |
---|
373 | { |
---|
374 | std::vector<ShellCommandAlias*>::iterator alias; |
---|
375 | for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++) |
---|
376 | stringList.push_back((*alias)->getName()); |
---|
377 | return true; |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | } |
---|