Commit 4bcb70ba authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Add possibility to parse multiple parameters

parent a337fbbd
......@@ -18,7 +18,7 @@ find_package(blc_core REQUIRED)
#source files
set(sources src/blc_loop.cpp src/blc_program.cpp src/blc_command.cpp)
add_definitions(${BL_DEFINITIONS} -std=c++14)
add_definitions(${BL_DEFINITIONS} -std=c++14 -Wall -W)
include_directories(include ${BL_INCLUDE_DIRS})
#shared lib
......
......@@ -113,7 +113,7 @@ void blc_command_remove(const char *command_name){
if (strcmp(blc_commands[i].name, command_name) == 0) break;
}
if (i==-1) EXIT_ON_ERROR("You try to remove a command '%s' that does not exist", command_name);
REMOVE_ITEM_POSITION(&blc_commands, &blc_commands_nb, i);
}
......@@ -151,8 +151,8 @@ void blc_command_interpret_string(char const *input_string, size_t size){
parameter2_size = getline(&parameter2, &line_capability, stdin)-1;
parameter2[parameter2_size]=0;
}
if (command->callback == NULL) EXIT_ON_ERROR("Command callback is NULL for command '%s'", command->name);
command->callback(parameter2, command->user_data);
if (command->callback == nullptr) EXIT_ON_ERROR("Command callback is NULL for command '%s'", command->name);
else command->callback(parameter2, command->user_data);
FREE(parameter2);
line_capability=0;
break; //Command has been found
......@@ -164,9 +164,9 @@ void blc_command_interpret_string(char const *input_string, size_t size){
break;
}
else continue; //Maybe it is a command with a longer name, we continue to check
/* if ((command->prompt != NULL) && (command->prompt[0]!='|')) continue; // If we do not wait for parameter, the command must be exact.
else
return;*/
/* if ((command->prompt != NULL) && (command->prompt[0]!='|')) continue; // If we do not wait for parameter, the command must be exact.
else
return;*/
}
}
}
......@@ -174,11 +174,11 @@ void blc_command_interpret_string(char const *input_string, size_t size){
}
void blc_command_interpret_block(){
char *line=NULL;
char *line=nullptr;
size_t line_capability=0;
ssize_t line_size;
//We do not consider error if it was an interruption. @TODO check wether the content of lin is correct.
//We do not consider error if it was an interruption. @TODO check wether the content of line is correct.
do{
line_size = getline(&line, &line_capability, stdin);
}while ((line_size==-1) && (errno==EINTR));
......
......@@ -106,13 +106,16 @@ static void blc_command_send_to_all(char const *argument){
}
void blc_command_ask_quit(){
if (blc_status==BLC_PAUSE) BLC_PTHREAD_CHECK(pthread_mutex_unlock(&mutex_lock_keyboard), NULL);
//In pause we unlock the keyboard
if (blc_status==BLC_PAUSE) BLC_PTHREAD_CHECK(pthread_mutex_unlock(&mutex_lock_keyboard), nullptr);
//In step by step we switch to quitting mode
if (blc_status!=BLC_STEPS) blc_status=BLC_QUIT;
if (blocking_semaphore){
if (sem_trywait(blocking_semaphore)==0){
sem_post(blocking_semaphore); // we succeed, it was not a problem, I free it.
blocking_semaphore=NULL;
blocking_semaphore=nullptr;
}
else if (errno==EAGAIN) sem_post(blocking_semaphore);
else EXIT_ON_SYSTEM_ERROR("unlocking semaphore for quitting");
......@@ -122,7 +125,7 @@ void blc_command_ask_quit(){
static void step_forward_cb(const char* steps_nb_str, void*){
int steps_nb, pos;
if (steps_nb_str){
if (sscanf(steps_nb_str, "%d%n", &steps_nb, &pos)!=1 || pos !=strlen(steps_nb_str) || steps_nb<1){
if (sscanf(steps_nb_str, "%d%n", &steps_nb, &pos)!=1 || pos != (int)strlen(steps_nb_str) || steps_nb<1){
color_eprintf(BLC_YELLOW, "'%s' is not a valid step number\n", steps_nb_str);
return;
}
......@@ -168,7 +171,7 @@ static void *loop_check_stuck(void*){
sleep(checking_time);
if(blc_status==BLC_RUN){ //in BLC_PAUSE it is normal to block
if (iteration==blc_loop_iteration) {
if (iteration==(int)blc_loop_iteration) {
color_eprintf(BLC_YELLOW, "'%s' seems blocked on iteration '%d'. The loop has been stopped for more than %ds\n", blc_program_id, iteration, checking_time);
if (blc_profile_file) fflush(blc_profile_file); //It may help for debuggage
was_stucked=1;
......@@ -192,35 +195,32 @@ static void *command_thread_interpret_loop(void *){
return NULL;
}
///This create one or two threads.
void blc_command_loop_init(long loop_period){
if (blc_command_loop_period==-3) EXIT_ON_ERROR("You already are in a blc_command_loop");
blc_loop_iteration=0;
intermediate_iteration=0;
blc_command_loop_period=loop_period;
if (blc_command_loop_period==-2){
blc_command_loop_period=-1;
}
else BLC_PTHREAD_CHECK(pthread_mutex_lock(&mutex_lock_keyboard), NULL);
/*We do not block the keyboard in -2 mode */
if (blc_command_loop_period!=-2) BLC_PTHREAD_CHECK(pthread_mutex_lock(&mutex_lock_keyboard), nullptr);
else blc_command_loop_period=-1;
if (blc_input_terminal){
blc_command_display_help(); //We display help only for keyboard
}
if (blc_input_terminal) blc_command_display_help(); //We display help only for keyboard
if (blc_command_loop_period!=-1) blc_command_add("", (type_blc_command_cb)pause_cb, NULL, "set on/off pause", NULL);
blc_command_add("h", (type_blc_command_cb)blc_command_display_help, NULL, "display this help", NULL);
blc_command_add("q", (type_blc_command_cb)blc_command_ask_quit, NULL, "quit the application", NULL);
if (blc_command_loop_period != -1) blc_command_add("", (type_blc_command_cb)pause_cb, NULL, "set on/off pause", nullptr);
blc_command_add("h", (type_blc_command_cb)blc_command_display_help, nullptr, "display this help", nullptr);
blc_command_add("q", (type_blc_command_cb)blc_command_ask_quit, nullptr, "quit the application", nullptr);
if (blc_command_loop_period!=-1) { //we lock on keyboard. But if the program lock we still read keyboard
SYSTEM_ERROR_CHECK(pthread_create(&loop_check_stuck_thread, NULL, loop_check_stuck, NULL), -1, NULL);
blc_command_add("s", (type_blc_command_cb)display_stats, NULL, "display time stats", NULL); //We did not do stat on keyboard
SYSTEM_ERROR_CHECK(pthread_create(&loop_check_stuck_thread, nullptr, loop_check_stuck, nullptr), -1, nullptr);
blc_command_add("s", (type_blc_command_cb)display_stats, nullptr, "display time stats", nullptr); //We did not do stat on keyboard
fprintf(stderr, "=== %s: running ===\n", blc_program_id);
}
blc_command_add("|", (type_blc_command_cb)blc_command_send_to_stdout, "command", "sends the command to stdout", NULL);
blc_command_add("&", (type_blc_command_cb)blc_command_send_to_all, "command", "inteprets command and sends it to stdout", NULL);
blc_command_add("|", (type_blc_command_cb)blc_command_send_to_stdout, "command", "sends the command to stdout", nullptr);
blc_command_add("&", (type_blc_command_cb)blc_command_send_to_all, "command", "inteprets command and sends it to stdout", nullptr);
BLC_PTHREAD_CHECK(pthread_create(&loop_thread, NULL, command_thread_interpret_loop, NULL), NULL);
BLC_PTHREAD_CHECK(pthread_create(&loop_thread, nullptr, command_thread_interpret_loop, nullptr), nullptr);
}
int blc_command_loop_start(){
......@@ -235,7 +235,7 @@ int blc_command_loop_start(){
pause_cb();
}
//If we quit, we quit now before blockinhs semaphores.
//If we quit, we quit now before blocking semaphores.
if (blc_status==BLC_QUIT) return 0;
//We eventually wait for keyboard return key
......
This diff is collapsed.
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