diff --git a/include/cmd.h b/include/cmd.h index 37a00ab..1799902 100644 --- a/include/cmd.h +++ b/include/cmd.h @@ -31,6 +31,9 @@ struct cmd_arg{ const char* lopt; const char* sopt; +#ifdef XBACKLIGHT_COMPAT_OPTIONS + const char* xopt; +#endif const char* desc; }; diff --git a/src/cmd.c b/src/cmd.c index adf8d6e..506ebd5 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -23,32 +23,37 @@ #include "cmd.h" #include "common.h" -#define NO_OPT NULL -#define GET_LONG_OPT "--get" -#define GET_SHORT_OPT "-g" -#define FADE_LONG_OPT "--fade" -#define FADE_SHORT_OPT "-f" -#define STEPS_LONG_OPT "--steps" -#define STEPS_SHORT_OPT "-s" -#define DEVICE_LONG_OPT "--device" -#define DEVICE_SHORT_OPT "-d" -#define LIST_LONG_OPT "--list" -#define LIST_SHORT_OPT "-l" -#define HELP_LONG_OPT "--help" -#define HELP_SHORT_OPT "-h" -#define HELP_LONG_OPT "--help" -#define HELP_SHORT_OPT "-h" -#define VERSION_LONG_OPT "--version" - -#define DEVICE_XBACK_OPT "-display" -#define HELP_XBACK_OPT "-help" -#define VERSION_XBACK_OPT "-version" -#define SET_XBACK_OPT "-set" -#define INC_XBACK_OPT "-inc" -#define DEC_XBACK_OPT "-dec" +#define NO_OPT NULL +#define GET_LONG_OPT "--get" +#define GET_SHORT_OPT "-g" #define GET_XBACK_OPT "-get" +#define FADE_LONG_OPT "--fade" +#define FADE_SHORT_OPT "-f" #define FADE_XBACK_OPT "-time" +#define STEPS_LONG_OPT "--steps" +#define STEPS_SHORT_OPT "-s" #define STEPS_XBACK_OPT "-steps" +#define DEVICE_LONG_OPT "--device" +#define DEVICE_SHORT_OPT "-d" +#define DEVICE_XBACK_OPT "-display" +#define LIST_LONG_OPT "--list" +#define LIST_SHORT_OPT "-l" +#define LIST_XBACK_OPT NO_OPT +#define HELP_LONG_OPT "--help" +#define HELP_SHORT_OPT "-h" +#define HELP_XBACK_OPT "-help" +#define VERSION_LONG_OPT "--version" +#define VERSION_SHORT_OPT NO_OPT +#define VERSION_XBACK_OPT "-version" +#define SET_LONG_OPT NO_OPT +#define SET_SHORT_OPT NO_OPT +#define SET_XBACK_OPT "-set" +#define INC_LONG_OPT NO_OPT +#define INC_SHORT_OPT NO_OPT +#define INC_XBACK_OPT "-inc" +#define DEC_LONG_OPT NO_OPT +#define DEC_SHORT_OPT NO_OPT +#define DEC_XBACK_OPT "-dec" #define DEVICE_DESC "select which device to control" #define FADE_DESC "change brightness over time interval" @@ -61,28 +66,36 @@ #define INC_DESC "increase backlight device by specified value" #define DEC_DESC "decrease backlight device by specified value" -#ifdef XBACKLIGHT_COMPAT_OPTIONS -#define CHECK_SHORT_OPTION(opt, arg) assert(0) -#define CHECK_LONG_OPTION(opt, arg) (!strcmp(opt##_XBACK_OPT, arg)) -#define CHECK_OPTION(opt, arg) (!strcmp(opt##_XBACK_OPT, arg)) +int strcmp_handle_null(const char* one, const char* two){ + if(!one) + return 1; + if(!two) + return -1; + return strcmp(one, two); +} +#define CHECK_SHORT_OPTION(opt, arg) (!strcmp(opt##_SHORT_OPT, arg)) +#define CHECK_LONG_OPTION(opt, arg) (!strcmp(opt##_LONG_OPT, arg)) +#define CHECK_XBACK_OPTION(opt, arg) (!strcmp(opt##_XBACK_OPT, arg)) + +#ifdef XBACKLIGHT_COMPAT_OPTIONS + +#define CHECK_OPTION(opt, arg) (!strcmp_handle_null(opt##_LONG_OPT, arg) || !strcmp_handle_null(opt##_SHORT_OPT, arg) || !strcmp_handle_null(opt##_XBACK_OPT, arg)) struct cmd_arg rexbacklight_args[] = { - {DEVICE_XBACK_OPT, NO_OPT, DEVICE_DESC}, - {SET_XBACK_OPT, NO_OPT, SET_DESC}, - {INC_XBACK_OPT, NO_OPT, INC_DESC}, - {DEC_XBACK_OPT, NO_OPT, DEC_DESC}, - {FADE_XBACK_OPT, NO_OPT, FADE_DESC}, - {STEPS_XBACK_OPT, NO_OPT, STEPS_DESC}, - {GET_XBACK_OPT, NO_OPT, GET_DESC}, - {HELP_XBACK_OPT, NO_OPT, HELP_DESC}, - {VERSION_XBACK_OPT, NO_OPT, VERSION_DESC} + {DEVICE_LONG_OPT, DEVICE_SHORT_OPT, DEVICE_XBACK_OPT, DEVICE_DESC}, + {NO_OPT, NO_OPT, SET_XBACK_OPT, SET_DESC}, + {NO_OPT, NO_OPT, INC_XBACK_OPT, INC_DESC}, + {NO_OPT, NO_OPT, DEC_XBACK_OPT, DEC_DESC}, + {FADE_LONG_OPT, FADE_SHORT_OPT, FADE_XBACK_OPT, FADE_DESC}, + {STEPS_LONG_OPT, STEPS_SHORT_OPT, STEPS_XBACK_OPT, STEPS_DESC}, + {GET_LONG_OPT, GET_SHORT_OPT, GET_XBACK_OPT, GET_DESC}, + {HELP_LONG_OPT, HELP_SHORT_OPT, HELP_XBACK_OPT, HELP_DESC}, + {VERSION_LONG_OPT, NO_OPT, VERSION_XBACK_OPT, VERSION_DESC} }; #else //XBACKLIGHT_COMPAT_OPTIONS -#define CHECK_SHORT_OPTION(opt, arg) (!strcmp(opt##_SHORT_OPT, arg)) -#define CHECK_LONG_OPTION(opt, arg) (!strcmp(opt##_LONG_OPT, arg)) -#define CHECK_OPTION(opt, arg) (!strcmp(opt##_LONG_OPT, arg) || !strcmp(opt##_SHORT_OPT, arg)) +#define CHECK_OPTION(opt, arg) (!strcmp_handle_null(opt##_LONG_OPT, arg) || !strcmp_handle_null(opt##_SHORT_OPT, arg)) struct cmd_arg rexbacklight_args[] = { {DEVICE_LONG_OPT, DEVICE_SHORT_OPT, DEVICE_DESC}, {FADE_LONG_OPT, FADE_SHORT_OPT, FADE_DESC}, @@ -171,12 +184,11 @@ struct arg_values process_cmd_args(int argc, char** argv){ free_cmd_args(&ret); return (struct arg_values){.operation = OP_USAGE}; } - else if(CHECK_LONG_OPTION(VERSION, argv[i])){ + else if(CHECK_OPTION(VERSION, argv[i])){ free_cmd_args(&ret); return (struct arg_values){.operation = OP_VERSION}; } -#ifndef XBACKLIGHT_COMPAT_OPTIONS else if(CHECK_OPTION(LIST, argv[i])){ free_cmd_args(&ret); ret.operation = OP_LIST; @@ -195,7 +207,7 @@ struct arg_values process_cmd_args(int argc, char** argv){ curr->operation = OP_SET; curr->delta = 0; } -#else //XBACKLIGHT_COMPAT_OPTIONS +#ifdef XBACKLIGHT_COMPAT_OPTIONS else if(CHECK_OPTION(SET, argv[i])){ CHECK_NEXT_ARG(); curr->operation = OP_SET; @@ -314,3 +326,4 @@ int process_op(struct arg_values* arg, float min, float current, float max){ return current; } + diff --git a/src/common.c b/src/common.c index d91deca..7c683af 100644 --- a/src/common.c +++ b/src/common.c @@ -43,13 +43,26 @@ _Noreturn void usage(int exit_val){ printf("Options:\n"); for(i = 0;i < rexbacklight_args_length;++i){ - if(!rexbacklight_args[i].lopt){ - printf(" %s\n", rexbacklight_args[i].sopt); - }else if(!rexbacklight_args[i].sopt){ - printf(" %s\n", rexbacklight_args[i].lopt); - }else{ - printf(" %s|%s\n", rexbacklight_args[i].lopt, rexbacklight_args[i].sopt); + int printed = 0; + if(rexbacklight_args[i].lopt){ + printf("%s", rexbacklight_args[i].lopt); + printed = 1; } + if(rexbacklight_args[i].sopt){ + if(printed) + printf("|"); + else + printed = 1; + printf("%s", rexbacklight_args[i].sopt); + } +#ifdef XBACKLIGHT_COMPAT_OPTIONS + if(rexbacklight_args[i].xopt){ + if(printed) + printf("|"); + printf("%s", rexbacklight_args[i].xopt); + } +#endif //XBACKLIGHT_COMPAT_OPTIONS + printf("\n"); printf(" %s\n", rexbacklight_args[i].desc); } printf("\n"); @@ -57,11 +70,9 @@ _Noreturn void usage(int exit_val){ printf(" =\n"); printf(" -\n"); printf(" +\n"); -#ifndef XBACKLIGHT_COMPAT_OPTIONS printf(" off\n"); printf(" max\n"); printf(" min\n"); -#endif //XBACKLIGHT_COMPAT_OPTIONS printf("\n%s Copyright (C) 2018 rexy712\n", executable_name); printf("This program comes with ABSOLUTELY NO WARRANTY.\n");