1 /** @file command.h
2  *
3  * @authors Copyright (c) 2014-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
4  *
5  * @par License
6  * GPL: http://www.gnu.org/licenses/gpl.html
7  *
8  * <small>This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version. This program is distributed in the hope that it
12  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details. You should have received a copy of the GNU
15  * General Public License along with this program; if not, see:
16  * http://www.gnu.org/licenses</small>
17  */
18 
19 #ifndef LIBDOOMSDAY_CONSOLE_CMD_H
20 #define LIBDOOMSDAY_CONSOLE_CMD_H
21 
22 #include "../libdoomsday.h"
23 #include "var.h"
24 #include <de/types.h>
25 
26 #ifdef __cplusplus
27 #  include <de/String>
28 #endif
29 
30 #define DENG_MAX_ARGS            256
31 
32 typedef struct {
33     char cmdLine[2048];
34     int argc;
35     char *argv[DENG_MAX_ARGS];
36 } cmdargs_t;
37 
38 /**
39  * Console command template. Used with Con_AddCommand().
40  * @ingroup ccmd
41  */
42 typedef struct ccmdtemplate_s {
43     /// Name of the command.
44     const char* name;
45 
46     /// Argument template.
47     const char* argTemplate;
48 
49     /// Execute function.
50     int (*execFunc) (byte src, int argc, char** argv);
51 
52     /// @ref consoleCommandFlags
53     unsigned int flags;
54 } ccmdtemplate_t;
55 
56 typedef struct ccmd_s {
57     /// Next command in the global list.
58     struct ccmd_s *next;
59 
60     /// Next and previous overloaded versions of this command (if any).
61     struct ccmd_s *nextOverload, *prevOverload;
62 
63     /// Execute function.
64     int (*execFunc) (byte src, int argc, char **argv);
65 
66     /// Name of the command.
67     char const *name;
68 
69     /// @ref consoleCommandFlags
70     int flags;
71 
72     /// Minimum and maximum number of arguments. Used with commands
73     /// that utilize an engine-validated argument list.
74     int minArgs, maxArgs;
75 
76     /// List of argument types for this command.
77     cvartype_t args[DENG_MAX_ARGS];
78 } ccmd_t;
79 
80 /**
81  * @defgroup consoleCommandFlags Console Command Flags
82  * @ingroup ccmd apiFlags
83  */
84 ///@{
85 #define CMDF_NO_NULLGAME    0x00000001 ///< Not available unless a game is loaded.
86 #define CMDF_NO_DEDICATED   0x00000002 ///< Not available in dedicated server mode.
87 ///@}
88 
89 /**
90  * @defgroup consoleUsageFlags Console Command Usage Flags
91  * @ingroup ccmd apiFlags
92  * The method(s) that @em CANNOT be used to invoke a console command, used with
93  * @ref commandSource.
94  */
95 ///@{
96 #define CMDF_DDAY           0x00800000
97 #define CMDF_GAME           0x01000000
98 #define CMDF_CONSOLE        0x02000000
99 #define CMDF_BIND           0x04000000
100 #define CMDF_CONFIG         0x08000000
101 #define CMDF_PROFILE        0x10000000
102 #define CMDF_CMDLINE        0x20000000
103 #define CMDF_DED            0x40000000
104 #define CMDF_CLIENT         0x80000000 ///< Sent over the net from a client.
105 ///@}
106 
107 /**
108  * @defgroup commandSource Command Sources
109  * @ingroup ccmd
110  * Where a console command originated.
111  */
112 ///@{
113 #define CMDS_UNKNOWN        0
114 #define CMDS_DDAY           1 ///< Sent by the engine.
115 #define CMDS_GAME           2 ///< Sent by a game library.
116 #define CMDS_CONSOLE        3 ///< Sent via direct console input.
117 #define CMDS_BIND           4 ///< Sent from a binding/alias.
118 #define CMDS_CONFIG         5 ///< Sent via config file.
119 #define CMDS_PROFILE        6 ///< Sent via player profile.
120 #define CMDS_CMDLINE        7 ///< Sent via the command line.
121 #define CMDS_SCRIPT         8 ///< Sent based on a def in a DED file eg (state->execute).
122 ///@}
123 
124 /// Helper macro for declaring console command functions. @ingroup console
125 #define D_CMD(x)        DENG_EXTERN_C int CCmd##x(byte src, int argc, char** argv)
126 
127 /**
128  * Helper macro for registering new console commands.
129  * @ingroup ccmd
130  */
131 #define C_CMD(name, argTemplate, fn) \
132     { ccmdtemplate_t _template = { name, argTemplate, CCmd##fn, 0 }; Con_AddCommand(&_template); }
133 
134 /**
135  * Helper macro for registering new console commands.
136  * @ingroup ccmd
137  */
138 #define C_CMD_FLAGS(name, argTemplate, fn, flags) \
139     { ccmdtemplate_t _template = { name, argTemplate, CCmd##fn, flags }; Con_AddCommand(&_template); }
140 
141 #ifdef __cplusplus
142 
143 void Con_InitCommands();
144 void Con_ClearCommands(void);
145 void Con_AddKnownWordsForCommands();
146 
147 LIBDOOMSDAY_PUBLIC void Con_AddCommand(ccmdtemplate_t const *cmd);
148 
149 LIBDOOMSDAY_PUBLIC void Con_AddCommandList(ccmdtemplate_t const *cmdList);
150 
151 /**
152  * Search the console database for a named command. If one or more overloaded
153  * variants exist then return the variant registered most recently.
154  *
155  * @param name  Name of the command to search for.
156  * @return  Found command else @c 0
157  */
158 LIBDOOMSDAY_PUBLIC ccmd_t *Con_FindCommand(char const *name);
159 
160 /**
161  * Search the console database for a command. If one or more overloaded variants
162  * exist use the argument list to select the required variant.
163  *
164  * @param args
165  * @return  Found command else @c 0
166  */
167 LIBDOOMSDAY_PUBLIC ccmd_t *Con_FindCommandMatchArgs(cmdargs_t *args);
168 
169 /**
170  * @return  @c true iff @a name matches a known command or alias name.
171  */
172 LIBDOOMSDAY_PUBLIC dd_bool Con_IsValidCommand(char const *name);
173 
174 LIBDOOMSDAY_PUBLIC de::String Con_CmdAsStyledText(ccmd_t *cmd);
175 
176 LIBDOOMSDAY_PUBLIC void Con_PrintCommandUsage(ccmd_t const *ccmd, bool allOverloads = true);
177 
178 /**
179  * Returns a rich formatted, textual representation of the specified console
180  * command's argument list, suitable for logging.
181  *
182  * @param ccmd  The console command to format usage info for.
183  */
184 LIBDOOMSDAY_PUBLIC de::String Con_CmdUsageAsStyledText(ccmd_t const *ccmd);
185 
186 /**
187  * Defines a console command that behaves like a console variable but accesses
188  * the data of a de::Config variable.
189  *
190  * The purpose of this mechanism is to provide a backwards compatible way to
191  * access config variables.
192  *
193  * @note In the future, when the console uses Doomsday Script for executing commands,
194  * this kind of mapping should be much easier since one can just create a reference to
195  * the real variable and access it pretty much normally.
196  *
197  * @param consoleName     Name of the console command ("cvar").
198  * @param opts            Type template when setting the value (using the
199  *                        ccmdtemplate_t argument template format).
200  * @param configVariable  Name of the de::Config variable.
201  */
202 LIBDOOMSDAY_PUBLIC void Con_AddMappedConfigVariable(char const *consoleName,
203                                                     char const *opts,
204                                                     de::String const &configVariable);
205 
206 #endif // __cplusplus
207 
208 #endif // LIBDOOMSDAY_CONSOLE_CMD_H
209