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

Change name of blc_program_option to blc_program_args

parent 92182c87
......@@ -67,14 +67,14 @@ void blc_program_add_output_pipe_option(FILE *file, char letter, char const *lon
void blc_program_add_option( char const**result, char letter, char const *long_option, char const *parameter_type, const char *help, char const* default_value);
///Interpret the program arguments and update the results values as it should.
///The program name will be set in **blc_program**.
void blc_program_option_interpret(int *argc, char **argv[]);
///The program name will be set in **blc_program_name**.
void blc_program_parse_args(int *argc, char ***argv);
///Do program_option_interpret but print the name of the program (argv[0]) underlined with '='.
void blc_program_option_interpret_and_print_title(int *argc, char ***argv);
void blc_program_parse_args_and_print_title(int *argc, char ***argv);
///Display the different possible argument for the program.
void blc_program_option_display_help();
void blc_program_args_display_help();
/** Start a textual program.
* This has to be called after defining the parameters of the program
......
......@@ -32,11 +32,8 @@ static int posting_semaphores_nb;
static pthread_t loop_thread, loop_check_stuck_thread;
static pthread_mutex_t mutex_lock_keyboard=PTHREAD_MUTEX_INITIALIZER;
#include "blc_program.h"
type_blc_status blc_status=BLC_RUN;
void blc_fprint_stats(FILE *file)
......
......@@ -199,60 +199,7 @@ void blc_program_add_output_pipe_option(FILE *file, char letter, char const *lon
APPEND_ITEM(&blc_program_options, &blc_program_options_nb, &tmp_program_option);
}
static void blc_program_interpret_parameters(int *argc, char **argv[]){
char *tmp_parameter=NULL, *tmp_arg;
int missing_parameters_nb;
int i, j;
size_t linecap;
ssize_t parameter_read;
FOR(i, blc_program_parameters_nb){
switch(blc_program_parameters[i].type){
case STRING :
if ((*argc)==0){//There is no more argument to interpret
if (blc_program_parameters[i].required_nb){
if (isatty(STDIN_FILENO)) fprintf(stderr, "%s: %s? ", blc_program_parameters[i].help, blc_program_parameters[i].name);
SYSTEM_ERROR_CHECK(parameter_read=getline(&tmp_parameter, &linecap, stdin), -1, "Reading input for parameter");
tmp_parameter[parameter_read-1]=0;//Remove the last return;
(*blc_program_parameters[i].string_pt)=tmp_parameter;
linecap=0;
tmp_parameter=NULL;
// }else EXIT_ON_ERROR("Missing '%s' argument : %s", blc_program_parameters[i].name, blc_program_parameters[i].help);
}
else *blc_program_parameters[i].string_pt=blc_program_parameters[i].default_value;
}
else {
*blc_program_parameters[i].string_pt=(*argv)[0];
(*argc)--;
(*argv)++;
}
break;
case STRING_LIST: EXIT_ON_ERROR("STRING LIST is not yet managed");
if (isatty(STDIN_FILENO)) {
missing_parameters_nb = blc_program_parameters[i].required_nb-(*argc-optind-i);
if (missing_parameters_nb>0){
FOR(j, missing_parameters_nb){
fprintf(stderr, "%s: %s? ", blc_program_parameters[i].help, blc_program_parameters[i].name);
linecap=0;
SYSTEM_ERROR_CHECK(parameter_read=getline(&tmp_arg, &linecap, stdin), -1, "Reading input for parameter"); //getline(blc_program_parameters[i].string_pt, sizeof(blc_program_parameters[i].string_pt), stdin), NULL, "Wrong channel input name");
tmp_arg[parameter_read-1]=0;//Remove the last return;
APPEND_ITEM(argv, argc, &tmp_arg);
}
parameter_read=0;
APPEND_ITEM(argv, argc, &parameter_read);
argc--;//The last NULL parameter does not count
}
}
else EXIT_ON_ERROR("Missing '%s' arguments : %s you must have %d arguments", blc_program_parameters[i].name, blc_program_parameters[i].help, missing_parameters_nb);
*(blc_program_parameters[i].string_list_pt)=&(*argv)[i+optind];
break;
}
}
}
void blc_program_option_interpret(int *argc, char **argv[])
static void blc_program_option_interpret(int *argc, char **argv[])
{
char *optstring;
int ret, option_index;
......@@ -344,19 +291,73 @@ void blc_program_option_interpret(int *argc, char **argv[])
}
(*argc)-=optind;
(*argv)+=optind;
}
static void blc_program_interpret_parameters(int *argc, char **argv[]){
char *tmp_parameter=NULL, *tmp_arg;
int missing_parameters_nb;
int i, j;
size_t linecap;
ssize_t parameter_read;
//Now, parse parameters
blc_program_interpret_parameters(argc, argv);
FOR(i, blc_program_parameters_nb){
switch(blc_program_parameters[i].type){
case STRING :
if ((*argc)==0){//There is no more argument to interpret
if (blc_program_parameters[i].required_nb){
if (isatty(STDIN_FILENO)) fprintf(stderr, "%s: %s? ", blc_program_parameters[i].help, blc_program_parameters[i].name);
SYSTEM_ERROR_CHECK(parameter_read=getline(&tmp_parameter, &linecap, stdin), -1, "Reading input for parameter");
tmp_parameter[parameter_read-1]=0;//Remove the last return;
(*blc_program_parameters[i].string_pt)=tmp_parameter;
linecap=0;
tmp_parameter=NULL;
// }else EXIT_ON_ERROR("Missing '%s' argument : %s", blc_program_parameters[i].name, blc_program_parameters[i].help);
}
else *blc_program_parameters[i].string_pt=blc_program_parameters[i].default_value;
}
else {
*blc_program_parameters[i].string_pt=(*argv)[0];
(*argc)--;
(*argv)++;
}
break;
case STRING_LIST: EXIT_ON_ERROR("STRING LIST is not yet managed");
if (isatty(STDIN_FILENO)) {
missing_parameters_nb = blc_program_parameters[i].required_nb-(*argc-optind-i);
if (missing_parameters_nb>0){
FOR(j, missing_parameters_nb){
fprintf(stderr, "%s: %s? ", blc_program_parameters[i].help, blc_program_parameters[i].name);
linecap=0;
SYSTEM_ERROR_CHECK(parameter_read=getline(&tmp_arg, &linecap, stdin), -1, "Reading input for parameter"); //getline(blc_program_parameters[i].string_pt, sizeof(blc_program_parameters[i].string_pt), stdin), NULL, "Wrong channel input name");
tmp_arg[parameter_read-1]=0;//Remove the last return;
APPEND_ITEM(argv, argc, &tmp_arg);
}
parameter_read=0;
APPEND_ITEM(argv, argc, &parameter_read);
argc--;//The last NULL parameter does not count
}
}
else EXIT_ON_ERROR("Missing '%s' arguments : %s you must have %d arguments", blc_program_parameters[i].name, blc_program_parameters[i].help, missing_parameters_nb);
*(blc_program_parameters[i].string_list_pt)=&(*argv)[i+optind];
break;
}
}
}
void blc_program_option_interpret_and_print_title(int *argc, char ***argv){
underline_fprintf('=', stderr, blc_program_name);
void blc_program_parse_args(int *argc, char ***argv){
blc_program_option_interpret(argc, argv);
blc_program_interpret_parameters(argc, argv);
}
void blc_program_parse_args_and_print_title(int *argc, char ***argv){
underline_fprintf('=', stderr, blc_program_name);
blc_program_parse_args(argc, argv);
}
/*Display format as python argparse*/
void blc_program_option_display_help()
void blc_program_args_display_help()
{
struct program_parameter *parameter;
int i, option_length_max, tmp_length=0;
......@@ -478,12 +479,12 @@ void blc_program_init(int *argc, char ***argv, void (*exit_cb)(void))
}
blc_program_add_option(&help, 'h', "help", 0, "display this help", NULL);
blc_program_option_interpret_and_print_title(argc, argv);
blc_program_parse_args_and_print_title(argc, argv);
blc_program_check_full_parsing(*argc, *argv);
if (help){
blc_program_option_display_help();
blc_program_args_display_help();
exit(EXIT_SUCCESS);
}
......@@ -495,7 +496,7 @@ void blc_program_check_full_parsing(int argc, char **argv){
int i;
if (argc!=0) {
blc_program_option_display_help();
blc_program_args_display_help();
fprintf(stderr, "Unused arguments:");
FOR(i, argc) fprintf(stderr, "'%s' ", argv[i]);
fprintf(stderr, "\n");
......
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