2008-02-02 12:38:36 +00:00
|
|
|
#include "e_system.h"
|
2008-01-16 22:14:06 +00:00
|
|
|
#include "e_console.h"
|
|
|
|
#include "e_config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
|
|
|
|
#define CONSOLE_MAX_STR_LENGTH 255
|
|
|
|
/* the maximum number of tokens occurs in a string of length CONSOLE_MAX_STR_LENGTH with tokens size 1 separated by single spaces */
|
|
|
|
#define MAX_TOKENS (CONSOLE_MAX_STR_LENGTH+1)/2
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2008-02-11 21:44:36 +00:00
|
|
|
TOKEN_INT,
|
|
|
|
TOKEN_FLOAT,
|
|
|
|
TOKEN_STRING
|
2008-02-02 12:38:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2008-02-11 21:44:36 +00:00
|
|
|
int type;
|
2008-02-02 12:38:36 +00:00
|
|
|
const char *stored_string;
|
|
|
|
} TOKEN;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char string_storage[CONSOLE_MAX_STR_LENGTH+1];
|
|
|
|
char *next_string;
|
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
TOKEN tokens[MAX_TOKENS];
|
|
|
|
unsigned int num_tokens;
|
2008-02-02 12:38:36 +00:00
|
|
|
} LEXER_RESULT;
|
|
|
|
|
2008-01-16 22:14:06 +00:00
|
|
|
enum
|
|
|
|
{
|
2008-02-11 21:44:36 +00:00
|
|
|
STATE_START,
|
|
|
|
STATE_INT,
|
|
|
|
STATE_FLOAT,
|
|
|
|
STATE_POT_FLOAT,
|
2008-02-11 21:40:48 +00:00
|
|
|
STATE_POT_NEGATIVE,
|
2008-02-11 21:44:36 +00:00
|
|
|
STATE_STRING,
|
|
|
|
STATE_QUOTED,
|
|
|
|
STATE_ESCAPE
|
2008-01-16 22:14:06 +00:00
|
|
|
};
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
static const char *store_string(LEXER_RESULT *res, const char *str, int len)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
|
|
|
const char *ptr = res->next_string;
|
2008-01-17 23:04:53 +00:00
|
|
|
int escaped = 0;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-01-17 23:04:53 +00:00
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
if (!escaped && *str == '\\')
|
|
|
|
{
|
|
|
|
escaped = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
escaped = 0;
|
|
|
|
|
|
|
|
*res->next_string++ = *str;
|
|
|
|
}
|
|
|
|
|
|
|
|
str++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
*res->next_string++ = 0;
|
|
|
|
|
|
|
|
/*
|
2008-01-16 22:14:06 +00:00
|
|
|
memcpy(res->next_string, str, len);
|
|
|
|
res->next_string[len] = 0;
|
|
|
|
res->next_string += len+1;
|
2008-01-17 23:04:53 +00:00
|
|
|
*/
|
2008-01-16 22:14:06 +00:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
static void save_token(LEXER_RESULT *res, int *index, const char **start, const char *end, int *state, int type)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-11 21:44:36 +00:00
|
|
|
/* printf("Saving token with length %d\n", end - *start); */
|
|
|
|
TOKEN *tok = &res->tokens[*index];
|
2008-01-16 22:14:06 +00:00
|
|
|
tok->stored_string = store_string(res, *start, end - *start);
|
2008-02-11 21:44:36 +00:00
|
|
|
tok->type = type;
|
|
|
|
++res->num_tokens;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
*start = end + 1;
|
|
|
|
*state = STATE_START;
|
|
|
|
++*index;
|
2008-01-16 22:14:06 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 23:04:53 +00:00
|
|
|
static int digit(char c)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-11 21:44:36 +00:00
|
|
|
return '0' <= c && c <= '9';
|
2008-01-16 22:14:06 +00:00
|
|
|
}
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
static int lex(const char *line, LEXER_RESULT *res)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-11 21:44:36 +00:00
|
|
|
int state = STATE_START, i = 0;
|
2008-01-16 22:14:06 +00:00
|
|
|
int length_left = CONSOLE_MAX_STR_LENGTH;
|
2008-02-11 21:44:36 +00:00
|
|
|
const char *start, *c;
|
|
|
|
res->num_tokens = 0;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
mem_zero(res, sizeof(*res));
|
2008-01-16 22:14:06 +00:00
|
|
|
res->next_string = res->string_storage;
|
2008-01-17 23:04:53 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
for (c = start = line; *c != '\0' && res->num_tokens < MAX_TOKENS && length_left; ++c, --length_left)
|
|
|
|
{
|
|
|
|
/* printf("State: %d.. c: %c\n", state, *c); */
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case STATE_START:
|
|
|
|
if (*c == ' ')
|
|
|
|
start = c + 1;
|
|
|
|
else if (digit(*c))
|
|
|
|
state = STATE_INT;
|
|
|
|
else if (*c == '-')
|
|
|
|
state = STATE_POT_NEGATIVE;
|
|
|
|
else if (*c == '.')
|
|
|
|
state = STATE_POT_FLOAT;
|
|
|
|
else if (*c == '"')
|
2008-01-17 23:04:53 +00:00
|
|
|
state = STATE_QUOTED;
|
|
|
|
else
|
2008-02-11 21:44:36 +00:00
|
|
|
state = STATE_STRING;
|
|
|
|
break;
|
2008-02-11 21:40:48 +00:00
|
|
|
|
|
|
|
case STATE_POT_NEGATIVE:
|
|
|
|
if (digit(*c))
|
|
|
|
state = STATE_INT;
|
|
|
|
else if (*c == '.')
|
|
|
|
state = STATE_POT_FLOAT;
|
|
|
|
else if (*c == ' ')
|
2008-02-11 21:44:36 +00:00
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
2008-02-11 21:40:48 +00:00
|
|
|
else
|
|
|
|
state = STATE_STRING;
|
|
|
|
break;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
case STATE_INT:
|
|
|
|
if (digit(*c))
|
|
|
|
;
|
|
|
|
else if (*c == '.')
|
|
|
|
state = STATE_FLOAT;
|
|
|
|
else if (*c == ' ')
|
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_INT);
|
|
|
|
else
|
|
|
|
state = STATE_STRING;
|
|
|
|
break;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
case STATE_FLOAT:
|
|
|
|
if (digit(*c))
|
|
|
|
;
|
|
|
|
else if (*c == ' ')
|
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_FLOAT);
|
|
|
|
else
|
|
|
|
state = STATE_STRING;
|
|
|
|
break;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
case STATE_POT_FLOAT:
|
|
|
|
if (digit(*c))
|
|
|
|
state = STATE_FLOAT;
|
|
|
|
else if (*c == ' ')
|
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
|
|
|
else
|
|
|
|
state = STATE_STRING;
|
|
|
|
break;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
case STATE_STRING:
|
|
|
|
if (*c == ' ')
|
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
|
|
|
break;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
case STATE_QUOTED:
|
|
|
|
if (*c == '"')
|
2008-01-17 23:04:53 +00:00
|
|
|
{
|
|
|
|
++start;
|
2008-02-11 21:44:36 +00:00
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
2008-01-17 23:04:53 +00:00
|
|
|
}
|
2008-02-11 21:44:36 +00:00
|
|
|
else if (*c == '\\')
|
|
|
|
state = STATE_ESCAPE;
|
|
|
|
break;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
case STATE_ESCAPE:
|
|
|
|
if (*c != ' ')
|
|
|
|
state = STATE_QUOTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case STATE_INT:
|
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_INT);
|
|
|
|
break;
|
|
|
|
case STATE_FLOAT:
|
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_FLOAT);
|
|
|
|
break;
|
|
|
|
case STATE_STRING:
|
|
|
|
case STATE_QUOTED:
|
|
|
|
case STATE_POT_FLOAT:
|
2008-02-11 21:41:31 +00:00
|
|
|
case STATE_POT_NEGATIVE:
|
2008-02-11 21:44:36 +00:00
|
|
|
save_token(res, &i, &start, c, &state, TOKEN_STRING);
|
|
|
|
break;
|
|
|
|
case STATE_ESCAPE:
|
2008-02-11 21:40:48 +00:00
|
|
|
dbg_msg("console/lexer", "Misplaced escape character");
|
2008-02-11 21:44:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-01-16 22:14:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
int console_result_string(void *res, int index, const char **str)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
LEXER_RESULT *result = (LEXER_RESULT *)res;
|
|
|
|
|
2008-01-16 22:14:06 +00:00
|
|
|
if (index < 0 || index >= result->num_tokens)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
TOKEN *t = &result->tokens[index];
|
2008-01-16 22:14:06 +00:00
|
|
|
*str = t->stored_string;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
int console_result_int(void *res, int index, int *i)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
LEXER_RESULT *result = (LEXER_RESULT *)res;
|
|
|
|
|
2008-01-16 22:14:06 +00:00
|
|
|
if (index < 0 || index >= result->num_tokens)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
TOKEN *t = &result->tokens[index];
|
2008-01-16 22:14:06 +00:00
|
|
|
const char *str;
|
|
|
|
|
|
|
|
if (t->type != TOKEN_INT)
|
|
|
|
return -2;
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
console_result_string(result, index, &str);
|
2008-01-16 22:14:06 +00:00
|
|
|
|
|
|
|
*i = atoi(str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
int console_result_float(void *res, int index, float *f)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
LEXER_RESULT *result = (LEXER_RESULT *)res;
|
|
|
|
|
2008-01-16 22:14:06 +00:00
|
|
|
if (index < 0 || index >= result->num_tokens)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
TOKEN *t = &result->tokens[index];
|
2008-01-16 22:14:06 +00:00
|
|
|
const char *str;
|
|
|
|
|
|
|
|
if (t->type != TOKEN_INT && t->type != TOKEN_FLOAT)
|
|
|
|
return -2;
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
console_result_string(result, index, &str);
|
2008-01-16 22:14:06 +00:00
|
|
|
|
|
|
|
*f = atof(str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static COMMAND *first_command = 0x0;
|
|
|
|
|
|
|
|
COMMAND *console_find_command(const char *name)
|
|
|
|
{
|
|
|
|
COMMAND *cmd;
|
|
|
|
for (cmd = first_command; cmd; cmd = cmd->next)
|
|
|
|
if (strcmp(cmd->name, name) == 0)
|
|
|
|
return cmd;
|
|
|
|
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void console_register(COMMAND *cmd)
|
|
|
|
{
|
|
|
|
cmd->next = first_command;
|
|
|
|
first_command = cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
static int console_validate(COMMAND *command, LEXER_RESULT *result)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
|
|
|
const char *c = command->params;
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
const char *dummy_s;
|
|
|
|
int dummy_i;
|
|
|
|
float dummy_f;
|
|
|
|
|
|
|
|
while (*c && *c != '?')
|
|
|
|
{
|
|
|
|
switch (*c)
|
|
|
|
{
|
|
|
|
case 's':
|
2008-02-02 12:38:36 +00:00
|
|
|
if (console_result_string(result, i, &dummy_s))
|
2008-01-16 22:14:06 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
case 'i':
|
2008-02-02 12:38:36 +00:00
|
|
|
if (console_result_int(result, i, &dummy_i))
|
2008-01-16 22:14:06 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
2008-02-02 12:38:36 +00:00
|
|
|
if (console_result_float(result, i, &dummy_f))
|
2008-01-16 22:14:06 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default:
|
2008-01-16 22:15:56 +00:00
|
|
|
/* unknown char, so just continue... */
|
2008-01-16 22:14:06 +00:00
|
|
|
c++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void (*print_callback)(const char *) = 0x0;
|
|
|
|
|
|
|
|
void console_register_print_callback(void (*callback)(const char *))
|
|
|
|
{
|
|
|
|
print_callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void console_print(const char *str)
|
|
|
|
{
|
|
|
|
if (print_callback)
|
|
|
|
print_callback(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void console_execute(const char *str)
|
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
LEXER_RESULT result;
|
2008-01-16 22:14:06 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if ((error = lex(str, &result)))
|
|
|
|
printf("ERROR: %d\n", error);
|
|
|
|
else if (result.num_tokens > 0)
|
|
|
|
{
|
|
|
|
const char *name;
|
2008-01-16 22:15:56 +00:00
|
|
|
COMMAND *command;
|
2008-02-02 12:38:36 +00:00
|
|
|
console_result_string(&result, 0, &name);
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-01-16 22:15:56 +00:00
|
|
|
command = console_find_command(name);
|
2008-01-16 22:14:06 +00:00
|
|
|
|
|
|
|
if (command)
|
|
|
|
{
|
|
|
|
if (console_validate(command, &result))
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "Invalid arguments... Usage: %s %s", command->name, command->params);
|
|
|
|
console_print(buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
command->callback(&result, command->user_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "No such command: %s.", name);
|
|
|
|
console_print(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
static void echo_command(void *result, void *user_data)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
|
|
|
const char *str;
|
2008-02-02 12:38:36 +00:00
|
|
|
console_result_string(result, 1, &str);
|
2008-01-16 22:14:06 +00:00
|
|
|
console_print(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
typedef struct
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
CONFIG_INT_GETTER getter;
|
|
|
|
CONFIG_INT_SETTER setter;
|
|
|
|
} INT_VARIABLE_DATA;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
typedef struct
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
CONFIG_STR_GETTER getter;
|
|
|
|
CONFIG_STR_SETTER setter;
|
|
|
|
} STR_VARIABLE_DATA;
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
static void int_variable_command(void *result, void *user_data)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
INT_VARIABLE_DATA *data = (INT_VARIABLE_DATA *)user_data;
|
2008-01-16 22:14:06 +00:00
|
|
|
int new_val;
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
if (console_result_int(result, 1, &new_val))
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "Value: %d", data->getter(&config));
|
|
|
|
console_print(buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->setter(&config, new_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
static void str_variable_command(void *result, void *user_data)
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
2008-02-02 12:38:36 +00:00
|
|
|
STR_VARIABLE_DATA *data = (STR_VARIABLE_DATA *)user_data;
|
2008-01-16 22:14:06 +00:00
|
|
|
const char *new_val;
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
if (console_result_string(result, 1, &new_val))
|
2008-01-16 22:14:06 +00:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "Value: %s", data->getter(&config));
|
|
|
|
console_print(buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->setter(&config, new_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void console_init()
|
|
|
|
{
|
|
|
|
MACRO_REGISTER_COMMAND("echo", "s", echo_command, 0x0);
|
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
#define MACRO_CONFIG_INT(name,def,min,max) { static INT_VARIABLE_DATA data = { &config_get_ ## name, &config_set_ ## name }; MACRO_REGISTER_COMMAND(#name, "?i", int_variable_command, &data) }
|
|
|
|
#define MACRO_CONFIG_STR(name,len,def) { static STR_VARIABLE_DATA data = { &config_get_ ## name, &config_set_ ## name }; MACRO_REGISTER_COMMAND(#name, "?s", str_variable_command, &data) }
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
#include "e_config_variables.h"
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-02-11 21:44:36 +00:00
|
|
|
#undef MACRO_CONFIG_INT
|
|
|
|
#undef MACRO_CONFIG_STR
|
2008-01-16 22:14:06 +00:00
|
|
|
}
|