1 //-----------------------------------------------------------------------------
2 // Commands
3 //-----------------------------------------------------------------------------
4 
5 #ifndef __COMMANDS_H__
6 #define __COMMANDS_H__
7 
8 typedef void (*xcommand_t) (int argc, char *argv[]);
9 
10 /**
11  * Command structure.
12  */
13 typedef struct cmd_function_s
14 {
15   struct cmd_function_s *next;      /**< Pointer to next command */
16   char          *name;      /**< Command name */
17   xcommand_t        function;   /**< Pointer to corresponding function */
18   char *          description;  /**< Function description */
19 } cmd_function_t;
20 
21 typedef char commline[COMMAND_LINELENGTH];
22 
23 /**
24  * Commands class
25  * The commands are used in the console to do some modifications on the
26  * configuration of the engine. A command is typically a structure containing
27  * a name, an attached function and an eventual description.
28  * @see cmd_function_t
29  */
30 class Commands
31 {
32     int cmd_argc;
33     char** cmd_argv;
34 
35     char last_command[COMMAND_LINELENGTH];
36 
37     cmd_function_t *cmd_functions;    /**< Pointer on the first function of the list */
38 
39     commline *CommandsHistory;      /**< commands history
40                        *   The allocation is made block by block, so the console has more allocated lines than filled lines^. */
41     int NbrAllocatedHistLines;      /**< number of allocated lines for history */
42     int NbrCommHistLines;       /**< number of history lines */
43 
44   public:
45     bool AddToConsole;          /**< defines if commands must be added to console */
46     bool AddToHistory;          /**< defines if commands must be added to history */
47 
48     Commands(void);
49     ~Commands(void);
50 
51     /**
52      * Add new command.
53      * Add a new command to commands list. The function stops if new
54      * command already exists.
55      * @param cmd_name the new command name
56      * @param function the command associated function
57      * @param description_message a description message for the command
58      */
59     void AddCommand(char *cmd_name, xcommand_t function, const char* description_message = NULL);
60 
61     /**
62      * Set a new description for an existing command.
63      * @param cmd_name the command name
64      * @param description a null ended string containing command description
65      */
66     void SetDescription(char *cmd_name, const char *description);
67 
68     /**
69      * Remove an existing command of the list.
70      * Commands can be removed from commands list. Remove it by entering
71      * the command name. If the command cannot be found, the function has
72      * no effect.
73      * @param cmd_name the command name
74      */
75     void RemoveCommand(char *cmd_name);
76 
77     /**
78      * Check for existing command.
79      * You can check if a command is already defined in commands list.
80      * @param cmd_name the command name
81      * @return a boolean value that has 1 if the command was found, else 0
82      */
83     bool Exists(char *cmd_name);
84 
85     /**
86      * Console command autocomplete.
87      * The function helps you to find the name of an existing command. The
88      * function works with global console (gConsole variable).
89      * @param partial the partial command to complete
90      * @param display_solutions displays the list of solutions if more than
91      *        one solution
92      * @param auto_complete_command complete current console command with
93      *        common begin of every potential solution
94      */
95     void CompleteCommand(const char *partial, bool display_solutions = 1, bool auto_complete_command = 1);
96 
97     /**
98      * Give the first solution for auto-complete.
99      * Solutions are stored in a list that can be accessed with current
100      * function. The function puts the first solution of the list in the
101      * current console command. The function initialize an internal
102      * solution counter and is required to access other solutions.
103      * @param partial the partial command
104      */
105     void FirstSolution(const char *partial = NULL);
106 
107     /**
108      * Give the next solution for auto-complete.
109      * Solutions are stored in a list that can be accessed with current
110      * function. The function puts the next solution of the list in the
111      * current console command. The function FirstSolution should have
112      * been used before to initialize internal solution counter.
113      */
114     void NextSolution(void);
115 
116     /**
117      * Give the previous solution for auto-complete.
118      * Solutions are stored in a list that can be accessed with current
119      * function. The function puts the previous solution of the list in the
120      * current console command. The function FirstSolution should have
121      * been used before to initialize internal solution counter.
122      */
123     void PrevSolution(void);
124 
125     /**
126      * Execute a command.
127      * The function execute function that is associated with command. It
128      * also check for variables and aliases, and execute them if the
129      * commandline matches with.
130      * @param commandline the command to execute with arguments
131      * @return a non zero integer if function exists, 0 if not
132      */
133     int ExecuteCommand(const char *commandline, ...);
134 
135     /**
136      * Displays the commands list.
137      * The commands list is displayed in the console. You can specify if
138      * descpription must be written.
139      * @param display_descriptions a value that indicates if description
140      *        must be given.
141      */
142     void DisplayCommands(int display_descriptions = 0);
143 
144     /**
145      * Repeat last executed command.
146      * Last command is stored and you can execute it again.
147      */
148     void RepeatLastCommand(void);
149 
150     /**
151      * Get the function associated to a command.
152      * @param cmd_name the command name
153      * @return a pointer to the associated command
154      */
155     cmd_function_t* GetFunction(char *cmd_name);
156 
157     /**
158     * Number of lines in the history.
159     * @return The history length.
160     */
161     int GetNbrCommHistLines(void);
162 
163     char* GetHistoryLine(int l);
164     void AddToCommHistory(char* s);
165 };
166 
167 #endif  /* __COMMANDS_H__ */
168