36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 * POSSIBILITY OF SUCH DAMAGE. |
38 * POSSIBILITY OF SUCH DAMAGE. |
39 */ |
39 */ |
40 |
40 |
41 #define __COMMANDS_CXX__ |
|
42 #include <stdlib.h> |
41 #include <stdlib.h> |
43 #include <stdio.h> |
42 #include <stdio.h> |
44 #include <string.h> |
43 #include <string.h> |
45 #include "common.h" |
44 #include "main.h" |
46 #include "scriptreader.h" |
45 #include "scriptreader.h" |
47 #include "str.h" |
46 #include "str.h" |
48 #include "commands.h" |
47 #include "commands.h" |
|
48 |
|
49 CommandDef* g_CommDef; |
49 |
50 |
50 // ============================================================================ |
51 // ============================================================================ |
51 // Reads command definitions from commands.def and stores them to memory. |
52 // Reads command definitions from commands.def and stores them to memory. |
52 void ReadCommands () { |
53 void ReadCommands () { |
53 ScriptReader* r = new ScriptReader ("commands.def"); |
54 ScriptReader* r = new ScriptReader ("commands.def"); |
54 g_CommDef = NULL; |
55 g_CommDef = null; |
55 CommandDef* curdef = g_CommDef; |
56 CommandDef* curdef = g_CommDef; |
56 unsigned int numCommDefs = 0; |
57 unsigned int numCommDefs = 0; |
57 |
58 |
58 while (r->PeekNext().len()) { |
59 while (r->PeekNext().len()) { |
59 CommandDef* comm = new CommandDef; |
60 CommandDef* comm = new CommandDef; |
60 comm->next = NULL; |
61 comm->next = null; |
61 |
62 |
62 // Number |
63 // Number |
63 r->MustNumber (); |
64 r->MustNumber (); |
64 comm->number = r->token; |
65 comm->number = r->token.to_long(); |
65 |
66 |
66 r->MustNext (":"); |
67 r->MustNext (":"); |
67 |
68 |
68 // Name |
69 // Name |
69 r->MustNext (); |
70 r->MustNext (); |
81 |
82 |
82 r->MustNext (":"); |
83 r->MustNext (":"); |
83 |
84 |
84 // Num args |
85 // Num args |
85 r->MustNumber (); |
86 r->MustNumber (); |
86 comm->numargs = r->token; |
87 comm->numargs = r->token.to_long(); |
87 |
88 |
88 r->MustNext (":"); |
89 r->MustNext (":"); |
89 |
90 |
90 // Max args |
91 // Max args |
91 r->MustNumber (); |
92 r->MustNumber (); |
92 comm->maxargs = r->token; |
93 comm->maxargs = r->token.to_long(); |
93 |
94 |
94 if (comm->maxargs > MAX_MAXARGS) |
95 if (comm->maxargs > MAX_MAXARGS) |
95 r->ParserError ("maxargs (%d) greater than %d!", comm->maxargs, MAX_MAXARGS); |
96 r->ParserError ("maxargs (%d) greater than %d!", comm->maxargs, MAX_MAXARGS); |
96 |
97 |
97 // Argument types |
98 // Argument types |
161 printf ("%d command definitions read.\n", numCommDefs); |
162 printf ("%d command definitions read.\n", numCommDefs); |
162 } |
163 } |
163 |
164 |
164 // ============================================================================ |
165 // ============================================================================ |
165 // Finds a command by name |
166 // Finds a command by name |
166 CommandDef* FindCommand (str fname) { |
167 CommandDef* FindCommand (string fname) { |
167 CommandDef* comm; |
168 CommandDef* comm; |
168 ITERATE_COMMANDS (comm) { |
169 ITERATE_COMMANDS (comm) { |
169 if (!fname.icompare (comm->name)) |
170 if (fname.to_uppercase() == comm->name.to_uppercase()) |
170 return comm; |
171 return comm; |
171 } |
172 } |
172 |
173 |
173 return NULL; |
174 return null; |
174 } |
175 } |
175 |
176 |
176 // ============================================================================ |
177 // ============================================================================ |
177 // Returns the prototype of the command |
178 // Returns the prototype of the command |
178 str GetCommandPrototype (CommandDef* comm) { |
179 string GetCommandPrototype (CommandDef* comm) { |
179 str text; |
180 string text; |
180 text += GetTypeName (comm->returnvalue); |
181 text += GetTypeName (comm->returnvalue); |
181 text += ' '; |
182 text += ' '; |
182 text += comm->name; |
183 text += comm->name; |
183 text += '('; |
184 text += '('; |
184 |
185 |