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_ |
---|
17 | |
---|
18 | #include "shell_command.h" |
---|
19 | #include "shell_command_class.h" |
---|
20 | |
---|
21 | #include "list.h" |
---|
22 | #include "debug.h" |
---|
23 | #include "class_list.h" |
---|
24 | |
---|
25 | #include "key_names.h" |
---|
26 | #include <stdarg.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <string.h> |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | /** |
---|
33 | * 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 char* commandName, const char* className, const Executor& executor) |
---|
39 | { |
---|
40 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
41 | this->setName(commandName); |
---|
42 | this->description = NULL; |
---|
43 | this->alias = NULL; |
---|
44 | this->executor = executor.clone(); |
---|
45 | |
---|
46 | // this->classID = classID; |
---|
47 | this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID); |
---|
48 | if (this->shellClass != NULL) |
---|
49 | this->shellClass->commandList->add(this); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * deconstructs a ShellCommand |
---|
54 | */ |
---|
55 | ShellCommand::~ShellCommand() |
---|
56 | { |
---|
57 | if (this->alias != NULL && ShellCommandClass::aliasList != NULL) |
---|
58 | { |
---|
59 | ShellCommandClass::aliasList->remove(this->alias); |
---|
60 | delete this->alias; |
---|
61 | } |
---|
62 | delete this->executor; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * registers a new ShellCommand |
---|
67 | */ |
---|
68 | ShellCommand* ShellCommand::registerCommand(const char* commandName, const char* className, const Executor& executor) |
---|
69 | { |
---|
70 | if (ShellCommand::isRegistered(commandName, className, executor)) |
---|
71 | return NULL; |
---|
72 | else |
---|
73 | return new ShellCommand(commandName, className, executor); |
---|
74 | |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * unregister an existing commandName |
---|
82 | * @param className the name of the Class the command belongs to. |
---|
83 | * @param commandName the name of the command itself |
---|
84 | */ |
---|
85 | void ShellCommand::unregisterCommand(const char* commandName, const char* className) |
---|
86 | { |
---|
87 | if (ShellCommandClass::commandClassList == NULL) |
---|
88 | ShellCommandClass::initCommandClassList(); |
---|
89 | |
---|
90 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
91 | |
---|
92 | if (checkClass != NULL) |
---|
93 | { |
---|
94 | tIterator<ShellCommand>* iterator = checkClass->commandList->getIterator(); |
---|
95 | ShellCommand* elem = iterator->firstElement(); |
---|
96 | while(elem != NULL) |
---|
97 | { |
---|
98 | if (!strcmp(commandName, elem->getName())) |
---|
99 | { |
---|
100 | checkClass->commandList->remove(elem); |
---|
101 | delete elem; |
---|
102 | break; |
---|
103 | } |
---|
104 | elem = iterator->nextElement(); |
---|
105 | } |
---|
106 | delete iterator; |
---|
107 | |
---|
108 | if (checkClass->commandList->getSize() == 0) |
---|
109 | { |
---|
110 | ShellCommandClass::commandClassList->remove(checkClass); |
---|
111 | delete checkClass; |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * checks if a command has already been registered. |
---|
118 | * @param commandName the name of the Command |
---|
119 | * @param className the name of the Class the command should apply to. |
---|
120 | * @param paramCount how many arguments the Command takes |
---|
121 | * @returns true, if the command is registered/false otherwise |
---|
122 | * |
---|
123 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
124 | * This is checked in the registerCommand-function. |
---|
125 | */ |
---|
126 | bool ShellCommand::isRegistered(const char* commandName, const char* className, const Executor& executor) |
---|
127 | { |
---|
128 | if (ShellCommandClass::commandClassList == NULL) |
---|
129 | { |
---|
130 | ShellCommandClass::initCommandClassList(); |
---|
131 | return false; |
---|
132 | } |
---|
133 | |
---|
134 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
135 | if (checkClass != NULL) |
---|
136 | { |
---|
137 | tIterator<ShellCommand>* iterator = checkClass->commandList->getIterator(); |
---|
138 | ShellCommand* elem = iterator->firstElement(); |
---|
139 | while(elem != NULL) |
---|
140 | { |
---|
141 | if (!strcmp(commandName, elem->getName())) |
---|
142 | { |
---|
143 | PRINTF(2)("Command already registered\n"); |
---|
144 | delete iterator; |
---|
145 | return true; |
---|
146 | } |
---|
147 | elem = iterator->nextElement(); |
---|
148 | } |
---|
149 | delete iterator; |
---|
150 | return false; |
---|
151 | } |
---|
152 | else |
---|
153 | return false; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /** |
---|
158 | * executes commands |
---|
159 | * @param executionString the string containing the following input |
---|
160 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
161 | * @return true on success, false otherwise. |
---|
162 | */ |
---|
163 | bool ShellCommand::execute(const char* executionString) |
---|
164 | { |
---|
165 | if (ShellCommandClass::commandClassList == NULL) |
---|
166 | return false; |
---|
167 | |
---|
168 | long classID = CL_NULL; //< the classID retrieved from the Class. |
---|
169 | ShellCommandClass* commandClass = NULL; //< the command class this command applies to. |
---|
170 | tList<BaseObject>* objectList = NULL; //< the list of Objects stored in classID |
---|
171 | BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on |
---|
172 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
173 | unsigned int fktPos = 1; //< the position of the function (needed for finding it) |
---|
174 | // long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
175 | SubString inputSplits(executionString, true); |
---|
176 | |
---|
177 | if (inputSplits.getCount() == 0) |
---|
178 | return false; |
---|
179 | if (inputSplits.getCount() >= 1) |
---|
180 | { |
---|
181 | // CHECK FOR ALIAS |
---|
182 | if (ShellCommandClass::aliasList != NULL) |
---|
183 | { |
---|
184 | tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator(); |
---|
185 | ShellCommandAlias* elemAL = itAL->firstElement(); |
---|
186 | while(elemAL != NULL) |
---|
187 | { |
---|
188 | if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL && |
---|
189 | elemAL->getCommand()->shellClass != NULL ) |
---|
190 | { |
---|
191 | objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName()); |
---|
192 | if (objectList != NULL) |
---|
193 | { |
---|
194 | if (inputSplits.getCount() > 1) |
---|
195 | elemAL->getCommand()->executor->execute(objectList->firstElement(), executionString+inputSplits.getOffset(1)); |
---|
196 | else |
---|
197 | elemAL->getCommand()->executor->execute(objectList->firstElement(), ""); |
---|
198 | delete itAL; |
---|
199 | return true; |
---|
200 | } |
---|
201 | } |
---|
202 | elemAL = itAL->nextElement(); |
---|
203 | } |
---|
204 | delete itAL; |
---|
205 | } |
---|
206 | // looking for a Matching Class |
---|
207 | if (likely(ShellCommandClass::commandClassList != NULL)) |
---|
208 | { |
---|
209 | tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator(); |
---|
210 | ShellCommandClass* elemCL = itCL->firstElement(); |
---|
211 | while(elemCL != NULL) |
---|
212 | { |
---|
213 | if (elemCL->getName() && !strcasecmp(inputSplits.getString(0), elemCL->getName())) |
---|
214 | { |
---|
215 | //elemCL->getName(); |
---|
216 | classID = ClassList::StringToID(elemCL->getName()); |
---|
217 | commandClass = elemCL; |
---|
218 | objectList = ClassList::getList(classID); |
---|
219 | break; |
---|
220 | } |
---|
221 | elemCL = itCL->nextElement(); |
---|
222 | } |
---|
223 | delete itCL; |
---|
224 | } |
---|
225 | |
---|
226 | if (commandClass != NULL && inputSplits.getCount() >= 2) |
---|
227 | { |
---|
228 | if (objectList != NULL) |
---|
229 | { |
---|
230 | // Checking for a Match in the Objects of classID (else take the first) |
---|
231 | tIterator<BaseObject>* itBO = objectList->getIterator(); |
---|
232 | BaseObject* enumBO = itBO->firstElement(); |
---|
233 | while(enumBO) |
---|
234 | { |
---|
235 | if (enumBO->getName() != NULL && !strcasecmp(enumBO->getName(), inputSplits.getString(1))) |
---|
236 | { |
---|
237 | objectPointer = enumBO; |
---|
238 | fktPos = 2; |
---|
239 | break; |
---|
240 | } |
---|
241 | enumBO = itBO->nextElement(); |
---|
242 | } |
---|
243 | delete itBO; |
---|
244 | |
---|
245 | // |
---|
246 | if (objectPointer == NULL) |
---|
247 | objectPointer = objectList->firstElement(); |
---|
248 | } |
---|
249 | // match a function. |
---|
250 | if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3))) |
---|
251 | { |
---|
252 | tIterator<ShellCommand>* itCMD = commandClass->commandList->getIterator(); |
---|
253 | ShellCommand* enumCMD = itCMD->firstElement(); |
---|
254 | while (enumCMD != NULL) |
---|
255 | { |
---|
256 | if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos))) |
---|
257 | { |
---|
258 | if (objectPointer == NULL && enumCMD->executor->getType() == Executor_Objective) |
---|
259 | { |
---|
260 | delete itCMD; |
---|
261 | return false; |
---|
262 | } |
---|
263 | if (inputSplits.getCount() > fktPos+1) |
---|
264 | enumCMD->executor->execute(objectPointer, executionString+inputSplits.getOffset(fktPos +1)); |
---|
265 | else |
---|
266 | enumCMD->executor->execute(objectPointer, ""); |
---|
267 | delete itCMD; |
---|
268 | return true; |
---|
269 | } |
---|
270 | |
---|
271 | enumCMD = itCMD->nextElement(); |
---|
272 | } |
---|
273 | delete itCMD; |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | /** |
---|
280 | * lets a command be described |
---|
281 | * @param description the description of the Given command |
---|
282 | */ |
---|
283 | ShellCommand* ShellCommand::describe(const char* description) |
---|
284 | { |
---|
285 | if (this == NULL) |
---|
286 | return NULL; |
---|
287 | else |
---|
288 | { |
---|
289 | this->description = description; |
---|
290 | return this; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * adds an Alias to this Command |
---|
296 | * @param alias the name of the Alias to set |
---|
297 | * @returns itself |
---|
298 | */ |
---|
299 | ShellCommand* ShellCommand::setAlias(const char* alias) |
---|
300 | { |
---|
301 | if (this == NULL) |
---|
302 | return NULL; |
---|
303 | |
---|
304 | if (this->alias != NULL) |
---|
305 | { |
---|
306 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); |
---|
307 | } |
---|
308 | else |
---|
309 | { |
---|
310 | if (ShellCommandClass::aliasList == NULL) |
---|
311 | ShellCommandClass::aliasList = new tList<ShellCommandAlias>; |
---|
312 | |
---|
313 | ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); |
---|
314 | ShellCommandClass::aliasList->add(aliasCMD); |
---|
315 | this->alias = aliasCMD; |
---|
316 | } |
---|
317 | return this; |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * sets default Values of the Commands |
---|
322 | * @param count how many default Values to set. |
---|
323 | * @param ... the default Values in order. They will be cast to the right type |
---|
324 | * @returns itself |
---|
325 | * |
---|
326 | * Be aware, that when you use this Function, you !!MUST!! match the input as |
---|
327 | * count, [EXACTLY THE SAME AS IF YOU WOULD CALL THE FUNCTION UP TO count ARGUMENTS] |
---|
328 | */ |
---|
329 | ShellCommand* ShellCommand::defaultValues(unsigned int count, ...) |
---|
330 | { |
---|
331 | if (this == NULL) |
---|
332 | return NULL; |
---|
333 | |
---|
334 | va_list values; |
---|
335 | va_start(values, count); |
---|
336 | |
---|
337 | this->executor->defaultValues(count, values); |
---|
338 | |
---|
339 | return this; |
---|
340 | } |
---|
341 | |
---|
342 | /** |
---|
343 | * prints out nice information about the Shells Commands |
---|
344 | */ |
---|
345 | void ShellCommand::debug() |
---|
346 | { |
---|
347 | if (ShellCommandClass::commandClassList == NULL) |
---|
348 | { |
---|
349 | PRINT(0)("No Command registered.\n"); |
---|
350 | return; |
---|
351 | } |
---|
352 | |
---|
353 | tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator(); |
---|
354 | ShellCommandClass* elemCL = iteratorCL->firstElement(); |
---|
355 | while(elemCL != NULL) |
---|
356 | { |
---|
357 | PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); |
---|
358 | tIterator<ShellCommand>* iterator = elemCL->commandList->getIterator(); |
---|
359 | const ShellCommand* elem = iterator->firstElement(); |
---|
360 | while(elem != NULL) |
---|
361 | { |
---|
362 | PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->executor->getParamCount()); |
---|
363 | /// FIXME |
---|
364 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
365 | printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
366 | if (elem->description != NULL) |
---|
367 | printf("- %s", elem->description); |
---|
368 | printf("\n"); |
---|
369 | |
---|
370 | elem = iterator->nextElement(); |
---|
371 | } |
---|
372 | delete iterator; |
---|
373 | elemCL = iteratorCL->nextElement(); |
---|
374 | } |
---|
375 | delete iteratorCL; |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | * converts a Parameter to a String |
---|
380 | * @param parameter the Parameter we have. |
---|
381 | * @returns the Name of the Parameter at Hand |
---|
382 | */ |
---|
383 | const char* ShellCommand::paramToString(long parameter) |
---|
384 | { |
---|
385 | return MultiType::MultiTypeToString((MT_Type)parameter); |
---|
386 | // FIXME |
---|
387 | /* switch (parameter) |
---|
388 | { |
---|
389 | case ParameterBool: |
---|
390 | return "BOOL"; |
---|
391 | break; |
---|
392 | case ParameterChar: |
---|
393 | return "CHAR"; |
---|
394 | break; |
---|
395 | case ParameterString: |
---|
396 | return "STRING"; |
---|
397 | break; |
---|
398 | case ParameterInt: |
---|
399 | return "INT"; |
---|
400 | break; |
---|
401 | case ParameterUInt: |
---|
402 | return "UINT"; |
---|
403 | break; |
---|
404 | case ParameterFloat: |
---|
405 | return "FLOAT"; |
---|
406 | break; |
---|
407 | case ParameterLong: |
---|
408 | return "LONG"; |
---|
409 | break; |
---|
410 | default: |
---|
411 | return "NULL"; |
---|
412 | break; |
---|
413 | }*/ |
---|
414 | } |
---|