Commit d812c58d authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Add a function to interpret a string.

parent 0f51608d
......@@ -48,6 +48,9 @@ START_EXTERN_C
///Add a command in the list of possible command lines from the user.
void blc_command_add(const char *command_name, type_blc_command_cb callback, const char *prompt, const char *help, void *user_data);
///Interpret the command passed in string.
void blc_command_interpret_string(char const *input_string, size_t size);
///Wait a input from the user (this id blocking) and interprets it depending on the blc_command list.
void blc_command_interpret();
......
......@@ -134,6 +134,48 @@ void blc_command_add(const char *command_name, type_blc_command_cb callback, con
APPEND_ITEM(&blc_commands, &blc_commands_nb, &tmp_command);
}
void blc_command_interpret_string(char const *input_string, size_t size){
char const*parameter;
char *parameter2;
size_t parameter_size, parameter2_size;
const blc_command *command;
size_t name_size, line_capability=0;
FOR_EACH_INV(command, blc_commands, blc_commands_nb)
{
name_size = strlen(command->name);
if (strncmp(command->name, input_string, name_size) == 0)
{
parameter = input_string + name_size;
parameter_size = size - name_size;
if (parameter_size == 0) //No text after the command
{
parameter2 = NULL;
if (command->prompt != NULL) //A text was expected
{
fprintf(stderr, "%s:", command->prompt);
parameter2_size = getline(&parameter2, &line_capability, stdin)-1;
parameter2[parameter2_size]=0;
}
if (command->callback) command->callback(parameter2, command->user_data); //In which callback is NULL ??
FREE(parameter2);
line_capability=0;
break;
}
else
{
if (command->prompt == NULL) continue; // If we do not wait for parameter, the command must be exact.
else command->callback( parameter, command->user_data);
break;
}
}
}
if (command < blc_commands) fprintf(stderr, "Unknown command in: %s\n", input_string);
}
static void blc_command_interpret_block(){
blc_mem line, parameter;
size_t line_capability=0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment