Commit 6c2c99b8 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Do not exit() but blc_ask_exit when the input pipe in closed

parent d812c58d
......@@ -136,98 +136,69 @@ void blc_command_add(const char *command_name, type_blc_command_cb callback, con
}
void blc_command_interpret_string(char const *input_string, size_t size){
char const*parameter;
char *parameter2;
size_t parameter_size, parameter2_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;
ssize_t tmp_size;
const blc_command *command;
int name_size;
do{
tmp_size = getline(&line.chars, &line_capability, stdin);
}while ((tmp_size==-1) && (errno==EINTR));
if (tmp_size==-1)
{
if ((errno==0) || (errno==ETIMEDOUT)) exit(EXIT_SUCCESS); //stdin closed
else EXIT_ON_SYSTEM_ERROR("Command interpret: getline");
}
line.size=tmp_size-1;
line.chars[line.size]=0; // Removing the last return char (\n)
FOR_EACH_INV(command, blc_commands, blc_commands_nb)
{
name_size = strlen(command->name);
if (strncmp(command->name, line.chars, name_size) == 0)
if (strncmp(command->name, input_string, name_size) == 0)
{
parameter.chars = line.chars + name_size;
parameter.size = line.size - name_size;
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
{
parameter.chars = NULL;
parameter2 = NULL;
if (command->prompt != NULL) //A text was expected
{
fprintf(stderr, "%s:", command->prompt);
parameter.size = getline(&parameter.chars, &line_capability, stdin)-1;
parameter.chars[parameter.size]=0;
parameter2_size = getline(&parameter2, &line_capability, stdin)-1;
parameter2[parameter2_size]=0;
}
if (command->callback) command->callback( parameter.chars, command->user_data); //In which callback is NULL ??
parameter.~blc_mem();
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.chars, command->user_data);
else command->callback( parameter, command->user_data);
break;
}
}
}
if (command < blc_commands) fprintf(stderr, "Unknown command in: %s\n", line.chars);
parameter.chars=NULL; //Avoid to be destructed twice
if (command < blc_commands) fprintf(stderr, "Unknown command in: %s\n", input_string);
}
static void blc_command_interpret_block(){
char *line=NULL;
size_t line_capability=0;
ssize_t line_size;
do{
line_size = getline(&line, &line_capability, stdin);
}while ((line_size==-1) && (errno==EINTR));
if (line_size==-1)
{
if ((errno==0) || (errno==ETIMEDOUT)) {
free(line);
blc_command_ask_quit(); //stdin closed
}
else EXIT_ON_SYSTEM_ERROR("Command interpret: getline");
}
else{
line_size=line_size-1;
line[line_size]=0; // Removing the last return char (\n)
blc_command_interpret_string(line, line_size);
}
}
void blc_command_interpret(){
......@@ -272,7 +243,7 @@ void *command_thread_interpret_loop(void *){
void blc_command_interpret_thread(char const *option, void (*ask_quit_funcion)()){
pthread_t thread;
if (ask_quit_funcion==NULL) ask_quit_funcion=blc_command_ask_quit;
blc_command_loop_period=-2;
if (strchr(option, 'h')) blc_command_add("h", (type_blc_command_cb)blc_command_display_help, NULL, "display this help", NULL);
......@@ -300,7 +271,7 @@ void blc_command_loop_init(long loop_period)
int blc_command_loop_start(){
if (iterations_nb==0) us_time_diff(&timer);
else blc_period+=blc_current_duration+us_time_diff(&timer);
switch (blc_command_loop_period){
case -1: blc_command_interpret();break;
case -2:break;
......@@ -331,9 +302,9 @@ void blc_command_update_int_cb(char const *argument, void *int_pt){
if (argument==NULL || argument[0]==0) fprintf(stderr, "%d\n", *(int*)int_pt);
else{
value=strtod(argument, &endptr);
if (endptr!=argument+strlen(argument)) fprintf(stderr, "Error reading '%s' as an int. The value is still:%d\n", argument, *(int*)int_pt);
else *(int*)int_pt=value;
value=strtod(argument, &endptr);
if (endptr!=argument+strlen(argument)) fprintf(stderr, "Error reading '%s' as an int. The value is still:%d\n", argument, *(int*)int_pt);
else *(int*)int_pt=value;
}
}
......@@ -343,9 +314,9 @@ void blc_command_update_float_cb(char const *argument, void *float_pt){
if (argument==NULL || argument[0]==0) fprintf(stderr, "%f\n", *(float*)float_pt);
else{
value=strtof(argument, &endptr);
if (endptr!=argument+strlen(argument)) fprintf(stderr, "Error reading '%s' as a float. The value is still:%f\n", argument, *(float*)float_pt);
else *(float*)float_pt=value;
value=strtof(argument, &endptr);
if (endptr!=argument+strlen(argument)) fprintf(stderr, "Error reading '%s' as a float. The value is still:%f\n", argument, *(float*)float_pt);
else *(float*)float_pt=value;
}
}
......@@ -355,9 +326,9 @@ void blc_command_update_double_cb(char const *argument, void *double_pt){
if (argument==NULL || argument[0]==0) fprintf(stderr, "%lf\n", *(double*)double_pt);
else{
value=strtod(argument, &endptr);
if (endptr!=argument+strlen(argument)) fprintf(stderr, "Error reading '%s' as a double. The value is still:%lf\n", argument, *(double*)double_pt);
else *(double*)double_pt=value;
value=strtod(argument, &endptr);
if (endptr!=argument+strlen(argument)) fprintf(stderr, "Error reading '%s' as a double. The value is still:%lf\n", argument, *(double*)double_pt);
else *(double*)double_pt=value;
}
}
......
......@@ -160,20 +160,6 @@ void blc_program_add_multiple_parameters(char ***result_list, char const *name,
APPEND_ITEM(&blc_program_parameters, &blc_program_parameters_nb, &parameter);
}
/*
void blc_program_add_channel_parameter(blc_channel *channel, int required_nb, char const *help)
{
struct program_parameter parameter;
parameter.type=BLC_CHANNEL;
parameter.channel=channel;
parameter.name="blc_channel";
parameter.required_nb=required_nb;
parameter.help=help;
APPEND_ITEM(&blc_program_parameters, &blc_program_parameters_nb, &parameter);
}*/
void blc_program_add_option(char const **result, char letter, char const *long_option, char const *parameter, char const *help, char const* default_value){
type_program_option tmp_program_option;
......@@ -187,20 +173,6 @@ void blc_program_add_option(char const **result, char letter, char const *long_o
tmp_program_option.default_value = default_value;
APPEND_ITEM(&blc_program_options, &blc_program_options_nb, &tmp_program_option);
}
/*
void blc_program_add_channel_option(blc_channel *channel, char letter, char const *long_option, char const *help, char const* default_value)
{
type_program_option tmp_program_option;
tmp_program_option.type = BLC_CHANNEL;
tmp_program_option.channel = channel;
tmp_program_option.letter = letter;
tmp_program_option.name = long_option;
tmp_program_option.parameter = "blc_channel";
tmp_program_option.help = help;
tmp_program_option.default_value=default_value;
APPEND_ITEM(&blc_program_options, &blc_program_options_nb, &tmp_program_option);
}*/
void blc_program_add_input_pipe_option(FILE *file, char letter, char const *long_option, char const *help, char* default_value)
{
......@@ -511,11 +483,5 @@ void blc_program_init(int *argc, char ***argv, void (*exit_cb)(void))
void blc_quit()
{
fprintf(stderr, "Quitting %s\n", blc_program_name);
/* Now to quit the other appli should detect that the pipe is closed
* if (blc_output_terminal==0){
printf("q\n");
fflush(stdout);
}*/
}
END_EXTERN_C
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