diff --git a/Makefile b/Makefile index 17789e6..b529f8e 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ CXX=gcc CXXFLAGS=-g -Wall -Werror -fstack-protector-all -pedantic -Wno-unused -SOURCE=main.c standart.c extended.c cpuid.c udev.c printer.c -HEADERS=standart.h extended.h cpuid.h udev.h printer.h ascii.h +SOURCE=main.c standart.c extended.c cpuid.c udev.c printer.c args.c +HEADERS=standart.h extended.h cpuid.h udev.h printer.h ascii.h args.h OUTPUT=cpufetch diff --git a/args.c b/args.c new file mode 100644 index 0000000..db67784 --- /dev/null +++ b/args.c @@ -0,0 +1,93 @@ +#include +#include +#include +#include "args.h" + +#define ARG_STR_STYLE "style" +#define ARG_STR_HELP "help" +#define ARG_CHAR_STYLE 's' +#define ARG_CHAR_HELP 'h' +#define STYLE_STR_1 "default" +#define STYLE_STR_2 "dark" + +struct args_struct { + int help_flag; + STYLE style; +}; + +static const char* SYTLES_STR_LIST[STYLES_COUNT] = { STYLE_STR_1, STYLE_STR_2 }; +static struct args_struct args; + +STYLE parseStyle(char* style) { + int i = 0; + while(i != STYLES_COUNT && strcmp(SYTLES_STR_LIST[i],style) != 0) + i++; + + if(i == STYLES_COUNT) + return STYLE_INVALID; + return i; +} + +STYLE getStyle() { + return args.style; +} + +int showHelp() { + return args.help_flag; +} + +int parseArgs(int argc, char* argv[]) { + int c; + int digit_optind = 0; + int option_index = 0; + opterr = 0; + + args.help_flag = BOOLEAN_FALSE; + args.style = STYLE_EMPTY; + + static struct option long_options[] = { + {ARG_STR_STYLE, required_argument, 0, ARG_CHAR_STYLE}, + {ARG_STR_HELP, no_argument, 0, ARG_CHAR_HELP }, + {0, 0, 0, 0} + }; + + c = getopt_long(argc, argv,"",long_options, &option_index); + + while (c != -1) { + if(c == ARG_CHAR_STYLE) { + if(args.style != STYLE_EMPTY) { + printf("ERROR: Style option specified more than once\n"); + return BOOLEAN_FALSE; + } + args.style = parseStyle(optarg); + if(args.style == STYLE_INVALID) { + printf("ERROR: Invalid style '%s'\n",optarg); + return BOOLEAN_FALSE; + } + } + else if(c == ARG_CHAR_HELP) { + if(args.help_flag) { + printf("ERROR: Help option specified more than once\n"); + return BOOLEAN_FALSE; + } + args.help_flag = BOOLEAN_TRUE; + } + else if(c == '?') { + printf("WARNING: Invalid options\n"); + args.help_flag = BOOLEAN_TRUE; + break; + } + else + printf("Bug at line number %d in file %s\n", __LINE__, __FILE__); + + option_index = 0; + c = getopt_long(argc, argv,"",long_options, &option_index); + } + + if (optind < argc) { + printf("WARNING: Invalid options\n"); + args.help_flag = BOOLEAN_TRUE; + } + + return BOOLEAN_TRUE; +} diff --git a/args.h b/args.h new file mode 100644 index 0000000..1f1e02a --- /dev/null +++ b/args.h @@ -0,0 +1,10 @@ +#ifndef __ARGS__ +#define __ARGS__ + +#include "printer.h" + +int parseArgs(int argc, char* argv[]); +STYLE getStyle(); +int showHelp(); + +#endif diff --git a/main.c b/main.c index e3f6764..c21935c 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include #include +#include "args.h" #include "printer.h" #include "standart.h" #include "udev.h" @@ -24,14 +25,35 @@ Peak FLOPS: 512 GFLOP/s(in simple precision) ***/ -int main() { +void help(int argc, char *argv[]) +{ + printf("Usage: %s [--help] [--style STYLE]\n\ + Options: \n\ + --style Set logo style color\n\ + default: Default style color\n\ + dark: Dark style color\n\n\ + --help Print this help and exit\n", + argv[0]); +} + +int main(int argc, char* argv[]) { + if(!parseArgs(argc,argv)) + return EXIT_FAILURE; + + if(showHelp()) { + help(argc,argv); + return EXIT_SUCCESS; + } + struct cpuInfo* cpu = getCPUInfo(); if(cpu == NULL) return EXIT_FAILURE; struct cache* cach = new_cache(); struct frequency* freq = new_frequency(); - struct ascii* art = set_ascii(getCPUVendorInternal(cpu)); + struct ascii* art = set_ascii(getCPUVendorInternal(cpu),getStyle()); + if(art == NULL) + return EXIT_FAILURE; char* cpuName = getString_CPUName(); char* maxFrequency = getString_MaxFrequency(freq); diff --git a/printer.c b/printer.c index 3524c38..4407bb2 100644 --- a/printer.c +++ b/printer.c @@ -5,10 +5,14 @@ #include "printer.h" #include "ascii.h" -#define COL_INTEL_1 "\x1b[34;1m" -#define COL_INTEL_2 "\x1b[37;1m" -#define COL_AMD_1 "\x1b[30;1m" -#define COL_AMD_2 "\x1b[32;1m" +#define COL_INTEL_DEFAULT_1 "\x1b[36;1m" +#define COL_INTEL_DEFAULT_2 "\x1b[37;1m" +#define COL_INTEL_DARK_1 "\x1b[34;1m" +#define COL_INTEL_DARK_2 "\x1b[30m" +#define COL_AMD_DEFAULT_1 "\x1b[37;1m" +#define COL_AMD_DEFAULT_2 "\x1b[31;1m" +#define COL_AMD_DARK_1 "\x1b[30;1m" +#define COL_AMD_DARK_2 "\x1b[32;1m" #define RESET "\x1b[0m" #define TITLE_NAME "Name: " @@ -55,13 +59,28 @@ int setAttribute(struct ascii* art, int type, char* value) { return BOOLEAN_TRUE; } -struct ascii* set_ascii(VENDOR cpuVendor) { +struct ascii* set_ascii(VENDOR cpuVendor, STYLE style) { struct ascii* art = malloc(sizeof(struct ascii)); art->vendor = cpuVendor; if(cpuVendor == VENDOR_INTEL) { - strcpy(art->color1,COL_INTEL_1); - strcpy(art->color2,COL_INTEL_2); + /*** CHECK STYLE ***/ + switch (style) { + case STYLE_EMPTY: + case STYLE_DEFAULT: + strcpy(art->color1,COL_INTEL_DEFAULT_1); + strcpy(art->color2,COL_INTEL_DEFAULT_2); + break; + case STYLE_DARK: + strcpy(art->color1,COL_INTEL_DARK_1); + strcpy(art->color2,COL_INTEL_DARK_2); + break; + default: + //TODO Bugs function + printf("Bug at line number %d in file %s\n", __LINE__, __FILE__); + return NULL; + } + /*** COPY ASCII-ART ***/ strcpy(art->art[0],INTEL1); strcpy(art->art[1],INTEL2); strcpy(art->art[2],INTEL3); @@ -84,8 +103,22 @@ struct ascii* set_ascii(VENDOR cpuVendor) { strcpy(art->art[19],INTEL20); } else { - strcpy(art->color1,COL_AMD_1); - strcpy(art->color2,COL_AMD_2); + /*** CHECK STYLE ***/ + switch (style) { + case STYLE_EMPTY: + case STYLE_DEFAULT: + strcpy(art->color1,COL_AMD_DEFAULT_1); + strcpy(art->color2,COL_AMD_DEFAULT_2); + break; + case STYLE_DARK: + strcpy(art->color1,COL_AMD_DARK_1); + strcpy(art->color2,COL_AMD_DARK_2); + break; + default: + //TODO Bugs function + printf("Bug at line number %d in file %s\n", __LINE__, __FILE__); + return NULL; + } strcpy(art->art[0],AMD1); strcpy(art->art[1],AMD2); diff --git a/printer.h b/printer.h index 0e48630..6594057 100644 --- a/printer.h +++ b/printer.h @@ -4,12 +4,6 @@ #include "standart.h" #include "ascii.h" -struct ascii; - -struct ascii* set_ascii(VENDOR cpuVendor); -void print_ascii(struct ascii* art); -int setAttribute(struct ascii* art, int type, char* value); - #define BOOLEAN_TRUE 1 #define BOOLEAN_FALSE 0 @@ -28,4 +22,19 @@ int setAttribute(struct ascii* art, int type, char* value); #define ATTRIBUTE_L3 11 #define ATTRIBUTE_PEAK 12 +typedef int STYLE; +#define STYLES_COUNT 2 + +#define STYLE_EMPTY -2 +#define STYLE_INVALID -1 +#define STYLE_DEFAULT 0 +#define STYLE_DARK 1 + +struct ascii; + +static const int STYLES_CODE_LIST [STYLES_COUNT] = {STYLE_DEFAULT, STYLE_DARK}; +struct ascii* set_ascii(VENDOR cpuVendor, STYLE style); +void print_ascii(struct ascii* art); +int setAttribute(struct ascii* art, int type, char* value); + #endif