diff --git a/src/common/args.c b/src/common/args.c index c686321..563c4d2 100644 --- a/src/common/args.c +++ b/src/common/args.c @@ -5,6 +5,8 @@ #include "args.h" #include "global.h" +#define NUM_COLORS 4 + #define COLOR_STR_INTEL "intel" #define COLOR_STR_AMD "amd" #define COLOR_STR_IBM "ibm" @@ -25,7 +27,7 @@ struct args_struct { bool verbose_flag; bool version_flag; STYLE style; - struct colors* colors; + struct color** colors; }; const char args_chr[] = { @@ -54,7 +56,7 @@ STYLE get_style() { return args.style; } -struct colors* get_colors() { +struct color** get_colors() { return args.colors; } @@ -100,24 +102,19 @@ STYLE parse_style(char* style) { return i; } -void free_colors_struct(struct colors* cs) { - free(cs->c1); - free(cs->c2); - free(cs->c3); - free(cs->c4); +void free_colors_struct(struct color** cs) { + for(int i=0; i < NUM_COLORS; i++) { + free(cs[i]); + } free(cs); } -bool parse_color(char* optarg_str, struct colors** cs) { - *cs = emalloc(sizeof(struct colors)); - (*cs)->c1 = emalloc(sizeof(struct color)); - (*cs)->c2 = emalloc(sizeof(struct color)); - (*cs)->c3 = emalloc(sizeof(struct color)); - (*cs)->c4 = emalloc(sizeof(struct color)); - struct color** c1 = &((*cs)->c1); - struct color** c2 = &((*cs)->c2); - struct color** c3 = &((*cs)->c3); - struct color** c4 = &((*cs)->c4); +bool parse_color(char* optarg_str, struct color*** cs) { + for(int i=0; i < NUM_COLORS; i++) { + (*cs)[i] = emalloc(sizeof(struct color)); + } + + struct color** c = *cs; int32_t ret; char* str_to_parse = NULL; bool free_ptr; @@ -148,40 +145,29 @@ bool parse_color(char* optarg_str, struct colors** cs) { } ret = sscanf(str_to_parse, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d", - &(*c1)->R, &(*c1)->G, &(*c1)->B, - &(*c2)->R, &(*c2)->G, &(*c2)->B, - &(*c3)->R, &(*c3)->G, &(*c3)->B, - &(*c4)->R, &(*c4)->G, &(*c4)->B); + &c[0]->R, &c[0]->G, &c[0]->B, + &c[1]->R, &c[1]->G, &c[1]->B, + &c[2]->R, &c[2]->G, &c[2]->B, + &c[3]->R, &c[3]->G, &c[3]->B); if(ret != 12) { printErr("Expected to read 12 values for color but read %d", ret); return false; } - //TODO: Refactor c1->R c2->R ... to c[i]->R - if((*c1)->R < 0 || (*c1)->R > 255) { - printErr("Red in color 1 is invalid. Must be in range (0, 255)"); - return false; - } - if((*c1)->G < 0 || (*c1)->G > 255) { - printErr("Green in color 1 is invalid. Must be in range (0, 255)"); - return false; - } - if((*c1)->B < 0 || (*c1)->B > 255) { - printErr("Blue in color 1 is invalid. Must be in range (0, 255)"); - return false; - } - if((*c2)->R < 0 || (*c2)->R > 255) { - printErr("Red in color 2 is invalid. Must be in range (0, 255)"); - return false; - } - if((*c2)->G < 0 || (*c2)->G > 255) { - printErr("Green in color 2 is invalid. Must be in range (0, 255)"); - return false; - } - if((*c2)->B < 0 || (*c2)->B > 255) { - printErr("Blue in color 2 is invalid. Must be in range (0, 255)"); - return false; + for(int i=0; i < NUM_COLORS; i++) { + if(c[i]->R < 0 || c[i]->R > 255) { + printErr("Red in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->R); + return false; + } + if(c[i]->G < 0 || c[i]->G > 255) { + printErr("Green in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->G); + return false; + } + if(c[i]->B < 0 || c[i]->B > 255) { + printErr("Blue in color %d is invalid: %d; must be in range (0, 255)", i+1, c[i]->B); + return false; + } } if(free_ptr) free (str_to_parse); @@ -244,8 +230,8 @@ bool parse_args(int argc, char* argv[]) { return false; } color_flag = true; + args.colors = emalloc(sizeof(struct color *) * NUM_COLORS); if(!parse_color(optarg, &args.colors)) { - printErr("Color parsing failed"); return false; } } diff --git a/src/common/args.h b/src/common/args.h index 3614718..0d6f270 100644 --- a/src/common/args.h +++ b/src/common/args.h @@ -10,13 +10,6 @@ struct color { int32_t B; }; -struct colors { - struct color* c1; - struct color* c2; - struct color* c3; - struct color* c4; -}; - enum { STYLE_EMPTY, STYLE_FANCY, @@ -48,8 +41,8 @@ bool show_raw(); bool show_debug(); bool show_version(); bool verbose_enabled(); -void free_colors_struct(struct colors* cs); -struct colors* get_colors(); +void free_colors_struct(struct color** cs); +struct color** get_colors(); STYLE get_style(); #endif diff --git a/src/common/printer.c b/src/common/printer.c index dd5a4b4..f256fc0 100644 --- a/src/common/printer.c +++ b/src/common/printer.c @@ -150,7 +150,7 @@ char* rgb_to_ansi(struct color* c, bool background, bool bold) { return str; } -struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) { +struct ascii* set_ascii(VENDOR vendor, STYLE style, struct color** cs) { char *COL_FANCY_1, *COL_FANCY_2, *COL_FANCY_3, *COL_FANCY_4, *COL_RETRO_1, *COL_RETRO_2, *COL_RETRO_3, *COL_RETRO_4; struct ascii* art = emalloc(sizeof(struct ascii)); art->n_attributes_set = 0; @@ -278,10 +278,10 @@ struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) { break; case STYLE_FANCY: if(cs != NULL) { - COL_FANCY_1 = rgb_to_ansi(cs->c1, true, true); - COL_FANCY_2 = rgb_to_ansi(cs->c2, true, true); - COL_FANCY_3 = rgb_to_ansi(cs->c3, false, true); - COL_FANCY_4 = rgb_to_ansi(cs->c4, false, true); + COL_FANCY_1 = rgb_to_ansi(cs[0], true, true); + COL_FANCY_2 = rgb_to_ansi(cs[1], true, true); + COL_FANCY_3 = rgb_to_ansi(cs[2], false, true); + COL_FANCY_4 = rgb_to_ansi(cs[3], false, true); } art->ascii_chars[0] = ' '; art->ascii_chars[1] = ' '; @@ -298,10 +298,10 @@ struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) { break; case STYLE_RETRO: if(cs != NULL) { - COL_RETRO_1 = rgb_to_ansi(cs->c1, false, true); - COL_RETRO_2 = rgb_to_ansi(cs->c2, false, true); - COL_RETRO_3 = rgb_to_ansi(cs->c3, false, true); - COL_RETRO_4 = rgb_to_ansi(cs->c4, false, true); + COL_RETRO_1 = rgb_to_ansi(cs[0], false, true); + COL_RETRO_2 = rgb_to_ansi(cs[1], false, true); + COL_RETRO_3 = rgb_to_ansi(cs[2], false, true); + COL_RETRO_4 = rgb_to_ansi(cs[3], false, true); } strcpy(art->color1_ascii,COL_RETRO_1); strcpy(art->color2_ascii,COL_RETRO_2); @@ -441,7 +441,7 @@ void print_ascii(struct ascii* art) { } -bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) { +bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct color** cs) { struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs); if(art == NULL) return false; @@ -562,7 +562,7 @@ void print_ascii(struct ascii* art) { print_ascii_ppc(art, longest_attribute); } -bool print_cpufetch_ppc(struct cpuInfo* cpu, STYLE s, struct colors* cs) { +bool print_cpufetch_ppc(struct cpuInfo* cpu, STYLE s, struct color** cs) { struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs); if(art == NULL) return false; @@ -727,7 +727,7 @@ void print_ascii(struct ascii* art) { } -bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) { +bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct color** cs) { struct ascii* art = set_ascii(get_soc_vendor(cpu->soc), s, cs); if(art == NULL) return false; @@ -812,7 +812,7 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) { } #endif -bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs) { +bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct color** cs) { // Sanity check of ASCII arts int len = sizeof(ASCII_ARRAY) / sizeof(ASCII_ARRAY[0]); for(int i=0; i < len; i++) { diff --git a/src/common/printer.h b/src/common/printer.h index b08d69c..f7159a4 100644 --- a/src/common/printer.h +++ b/src/common/printer.h @@ -22,6 +22,6 @@ typedef int STYLE; void print_levels(struct cpuInfo* cpu); #endif -bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs); +bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct color** cs); #endif