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

Add command '|' in order to pass text to next process

parent 667b586d
......@@ -97,7 +97,7 @@ void blc_command_add(const char *command_name, type_blc_command_cb callback, con
tmp_command.user_data = user_data;
FOR_INV(i, blc_commands_nb)
if (strcmp(blc_commands[i].name, command_name) == 0) EXIT_ON_ERROR("Command '%s' is already defined.", command_name);
if (strcmp(blc_commands[i].name, command_name) == 0) EXIT_ON_ERROR("%s: command '%s' is already defined.", blc_program_name, command_name);
APPEND_ITEM(&blc_commands, &blc_commands_nb, &tmp_command);
......@@ -110,19 +110,14 @@ void blc_command_interpret_string(char const *input_string, size_t size){
const blc_command *command;
size_t name_size, line_capability=0;
FOR_EACH_INV(command, blc_commands, blc_commands_nb)
FOR_EACH_INV(command, blc_commands, blc_commands_nb){
name_size = strlen(command->name);
if (strncmp(command->name, input_string, name_size) == 0)
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
if (parameter_size == 0){ //No text after the command
parameter2 = NULL;
if (command->prompt != NULL) //A text was expected
if ((command->prompt != NULL) && (command->prompt[0]!='|')) {//A text was expected
fprintf(stderr, "%s:", command->prompt);
parameter2_size = getline(&parameter2, &line_capability, stdin)-1;
......@@ -132,15 +127,14 @@ void blc_command_interpret_string(char const *input_string, size_t size){
if (command->prompt == NULL) continue; // If we do not wait for parameter, the command must be exact.
else command->callback( parameter, command->user_data);
if ((command->prompt != NULL) && (command->prompt[0]!='|')) continue; // If we do not wait for parameter, the command must be exact.
else command->callback(parameter, command->user_data);
if (command < blc_commands) fprintf(stderr, "Unknown command in: %s\n", input_string);
if (command < blc_commands) fprintf(stderr, "%s: unknown command in: '%s'\n", blc_program_name, input_string);
void blc_command_interpret_block(){
......@@ -18,7 +18,6 @@ static int intermediate_iteration;
long blc_command_loop_period=-2; //unitialised
int blc_loop_iteration;
static void(**waiting_callbacks)(void*)=NULL;
static void **waiting_callabacks_user_data=NULL;
static int waiting_callbacks_nb=0;
......@@ -64,7 +63,6 @@ void blc_loop_add_waiting_callback( void(*waiting_callback)(void*), void *user_d
int blc_loop_try_add_waiting_semaphore(void *semaphore){
if (semaphore==NULL) return 0;
APPEND_ITEM(&waiting_semaphores, &waiting_semaphores_nb, &semaphore);
......@@ -86,11 +84,15 @@ void blc_loop_add_posting_semaphore(void *semaphore){
if (blc_loop_try_add_posting_semaphore(semaphore)==0) EXIT_ON_ERROR("The semaphore you want to add is NULL");
void blc_command_send_to_stdout(char const *argument){
if (argument) SYSTEM_ERROR_CHECK(printf("%s", argument), -1, NULL);
SYSTEM_ERROR_CHECK(printf("\n"), -1, NULL);
SYSTEM_ERROR_CHECK(fflush(stdout), -1, NULL);
void blc_command_ask_quit(){
sem_t **sem;
if (blc_status==BLC_PAUSE) BLC_PTHREAD_CHECK(pthread_mutex_unlock(&mutex_lock_keyboard), NULL);
......@@ -115,7 +117,6 @@ void pause_cb()
static void *loop_check_stuck(void*){
int iteration;
int checking_time=5;
......@@ -162,16 +163,18 @@ void blc_command_loop_init(long loop_period)
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);
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 (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
fprintf(stderr, "=== %s: running ===\n", blc_program_name);
blc_command_add("|", (type_blc_command_cb)blc_command_send_to_stdout, "|text", "send the text to stdout", NULL);
SYSTEM_ERROR_CHECK(pthread_create(&loop_thread, NULL, command_thread_interpret_loop, NULL), -1, NULL);
......@@ -219,7 +222,7 @@ void blc_command_loop_end(){
ratio=div(diff, 1e6);
time_left.tv_sec = ratio.quot;
time_left.tv_nsec = ratio.rem*1000;
if (diff < 0) color_fprintf(BLC_YELLOW, stderr, "\rMissing %.3fms in the BLC_COMMAND_LOOP", -diff/1000.f);
if (diff < 0) color_fprintf(BLC_YELLOW, stderr, "\r%s: Missing %.3fms in the BLC_COMMAND_LOOP requesting refresh of %.3fms\n", blc_program_name, -diff/1000.f, blc_command_loop_period/1000.f);
else SYSTEM_ERROR_RETRY_ON_SPECIFIC_ERRNO(nanosleep(&time_left, &tmp_rem), -1, EINTR, "Time left %.3fms", tmp_rem.tv_nsec/1e6f + tmp_rem.tv_sec*1000);
/* Warning the nanosleep can be overtimed of 10ms !!! it is the same for usleep !! */
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