1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/* CON_consolecommands.c
* Written By: Garrett Banuk <mongoose@mongeese.org>
* This is free, just be sure to give me credit when using it
* in any of your programs.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SDL.h"
#include "CON_console.h"
#include "internal.h"
/* Linked list of all the commands. */
static CommandInfo *Commands = NULL;
static int SendCommand = 0;
/* executes the command passed in from the string */
void CON_CommandExecute(ConsoleInformation *console)
{
char Command[CON_CHARS_PER_LINE];
char *BackStrings = console->Command;
CommandInfo *CurrentCommand = Commands;
/* Get the command out of the string */
if(EOF == sscanf(BackStrings, "%s", Command))
return;
CurrentCommand = Commands;
while(CurrentCommand) {
if(0 == strcmp(Command, CurrentCommand->CommandWord)) {
if (SendCommand)
CurrentCommand->CommandCallback(console, BackStrings);
else
CurrentCommand->CommandCallback(console, BackStrings+strlen(Command)+1);
return;
}
CurrentCommand = CurrentCommand->NextCommand;
}
CON_Out(console, "Unknown Command \"%s\"", Command);
}
/* Use this to add commands.
* Pass in a pointer to a funtion that will take a string which contains the
* arguments passed to the command. Second parameter is the command to execute
* on.
*/
void CON_AddCommand(void(*CommandCallback)(ConsoleInformation *console, char *Parameters), const char *CommandWord)
{
CommandInfo **CurrentCommand = &Commands;
while(*CurrentCommand)
CurrentCommand = &((*CurrentCommand)->NextCommand);
/* Add a command to the list */
*CurrentCommand = (CommandInfo*)malloc(sizeof(CommandInfo));
(*CurrentCommand)->CommandCallback = CommandCallback;
(*CurrentCommand)->CommandWord = (char*)malloc(strlen(CommandWord)+1);
strcpy((*CurrentCommand)->CommandWord, CommandWord);
(*CurrentCommand)->NextCommand = NULL;
}
/* tab completes commands
* It takes a string to complete and a pointer to the strings length. The strings
* length will be modified if the command is completed. */
void CON_TabCompletion(ConsoleInformation *console)
{
int Matches = 0;
int *location = &console->CursorPos;
char *CommandLine = console->LCommand;
char temp[CON_CHARS_PER_LINE];
CommandInfo *CurrentCommand;
// Don't try to complete if nothing is entered
if(*location == 0)
return;
/* Find all the commands that match */
CurrentCommand = Commands;
while(CurrentCommand)
{
if(0 == strncmp(CommandLine, CurrentCommand->CommandWord, strlen(CommandLine)))
Matches++;
CurrentCommand = CurrentCommand->NextCommand;
}
/* if we got one match, find it again and execute it */
if(Matches == 1)
{
CurrentCommand = Commands;
while(CurrentCommand)
{
if(0 == strncmp(CommandLine, CurrentCommand->CommandWord, strlen(CommandLine)))
{
strcpy(CommandLine, CurrentCommand->CommandWord);
CommandLine[strlen(CurrentCommand->CommandWord)] = ' ';
*location = strlen(CurrentCommand->CommandWord)+1;
CON_UpdateConsole(console);
break;
}
CurrentCommand = CurrentCommand->NextCommand;
}
}
else if(Matches > 1)/* multiple matches so print them out to the user */
{
CON_NewLineConsole(console);
CurrentCommand = Commands;
while(CurrentCommand)
{
if(0 == strncmp(CommandLine, CurrentCommand->CommandWord, strlen(CommandLine))) {
strcpy(temp, " ");
strcpy(&temp[5], CurrentCommand->CommandWord);
CON_Out(console, temp);
}
CurrentCommand = CurrentCommand->NextCommand;
}
}
else if(Matches == 0) {
// Thats not very interesting..
//CON_Out(console, "No match");
}
}
/* Lists all the commands to be used in the console */
void CON_ListCommands(ConsoleInformation *console)
{
CommandInfo *CurrentCommand = Commands;
CON_Out(console, " ");
while(CurrentCommand)
{
CON_Out(console, " %s", CurrentCommand->CommandWord);
CurrentCommand = CurrentCommand->NextCommand;
}
}
/* sends the command word used and the parameters back */
void CON_SendFullCommand(int sendOn)
{
SendCommand = sendOn;
}
/* This frees all the memory used by the commands */
void CON_DestroyCommands()
{
CommandInfo *CurrentCommand = Commands;
CommandInfo *temp;
while(CurrentCommand)
{
temp = CurrentCommand;
CurrentCommand = CurrentCommand->NextCommand;
free(temp->CommandWord);
free(temp);
}
Commands = NULL;
}
|