1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "args.h"
6 #include "printer.h"
7 #include "global.h"
8 
9 #ifdef ARCH_X86
10   static const char* ARCH_STR = "x86_64 build";
11   #include "../x86/cpuid.h"
12 #elif ARCH_PPC
13   static const char* ARCH_STR = "PowerPC build";
14   #include "../ppc/ppc.h"
15 #elif ARCH_ARM
16   static const char* ARCH_STR = "ARM build";
17   #include "../arm/midr.h"
18 #endif
19 
20 #ifdef __linux__
21   #ifdef __ANDROID__
22     static const char* OS_STR = "Android";
23   #else
24     static const char* OS_STR = "Linux";
25   #endif
26 #elif __FreeBSD__
27   static const char* OS_STR = "FreeBSD";
28 #elif _WIN32
29   static const char* OS_STR = "Windows";
30 #elif defined __APPLE__ || __MACH__
31   static const char* OS_STR = "macOS";
32 #else
33   static const char* OS_STR = "Unknown OS";
34 #endif
35 
36 static const char* VERSION = "1.00";
37 
print_help(char * argv[])38 void print_help(char *argv[]) {
39   const char **t = args_str;
40   const char *c = args_chr;
41   int max_len = max_arg_str_length();
42 
43   printf("Usage: %s [OPTION]...\n", argv[0]);
44   printf("Simple yet fancy CPU architecture fetching tool\n\n");
45 
46   printf("OPTIONS: \n");
47   printf("  -%c, --%s %*s Set the color scheme (by default, cpufetch uses the system color scheme)\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), "");
48   printf("  -%c, --%s %*s Set the style of CPU logo\n", c[ARG_STYLE], t[ARG_STYLE], (int) (max_len-strlen(t[ARG_STYLE])), "");
49 #ifdef ARCH_X86
50   printf("  -%c, --%s %*s Print CPU model and cpuid levels (debug purposes)\n", c[ARG_DEBUG], t[ARG_DEBUG], (int) (max_len-strlen(t[ARG_DEBUG])), "");
51 #elif ARCH_PPC
52   printf("  -%c, --%s %*s Print PVR register (debug purposes)\n", c[ARG_DEBUG], t[ARG_DEBUG], (int) (max_len-strlen(t[ARG_DEBUG])), "");
53 #elif ARCH_ARM
54   printf("  -%c, --%s %*s Print main ID register values (debug purposes)\n", c[ARG_DEBUG], t[ARG_DEBUG], (int) (max_len-strlen(t[ARG_DEBUG])), "");
55 #endif
56   printf("      --%s %*s Show the short version of the logo\n", t[ARG_LOGO_SHORT], (int) (max_len-strlen(t[ARG_LOGO_SHORT])), "");
57   printf("      --%s %*s Show the long version of the logo\n", t[ARG_LOGO_LONG], (int) (max_len-strlen(t[ARG_LOGO_LONG])), "");
58   printf("  -%c, --%s %*s Print extra information (if available) about how cpufetch tried fetching information\n", c[ARG_VERBOSE], t[ARG_VERBOSE], (int) (max_len-strlen(t[ARG_VERBOSE])), "");
59 #ifdef ARCH_X86
60   printf("      --%s %*s Show the old Intel logo\n", t[ARG_LOGO_INTEL_OLD], (int) (max_len-strlen(t[ARG_LOGO_INTEL_OLD])), "");
61   printf("      --%s %*s Show the new Intel logo\n", t[ARG_LOGO_INTEL_NEW], (int) (max_len-strlen(t[ARG_LOGO_INTEL_NEW])), "");
62   printf("  -%c, --%s %*s Show the full CPU name (do not abbreviate it)\n", c[ARG_FULLCPUNAME], t[ARG_FULLCPUNAME], (int) (max_len-strlen(t[ARG_FULLCPUNAME])), "");
63   printf("  -%c, --%s %*s Print raw cpuid data (debug purposes)\n", c[ARG_RAW], t[ARG_RAW], (int) (max_len-strlen(t[ARG_RAW])), "");
64 #endif
65   printf("  -%c, --%s %*s Print this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), "");
66   printf("  -%c, --%s %*s Print cpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), "");
67 
68   printf("\nCOLORS: \n");
69   printf("  * \"intel\":     Use Intel default color scheme \n");
70   printf("  * \"amd\":       Use AMD default color scheme \n");
71   printf("  * \"ibm\",       Use IBM default color scheme \n");
72   printf("  * \"arm\":       Use ARM default color scheme \n");
73   printf("  * custom:      If the argument of --color does not match any of the previous strings, a custom scheme can be specified.\n");
74   printf("                 5 colors must be given in RGB with the format: R,G,B:R,G,B:...\n");
75   printf("                 The first 3 colors are the CPU art color and the next 2 colors are the text colors\n");
76 
77   printf("\nSTYLES: \n");
78   printf("  * \"fancy\":     Default style\n");
79   printf("  * \"retro\":     Old cpufetch style\n");
80   printf("  * \"legacy\":    Fallback style for terminals that do not support colors\n");
81 
82   printf("\nLOGOS: \n");
83   printf("    cpufetch will try to adapt the logo size and the text to the terminal width. When the output (logo and text) is wider than\n");
84   printf("    the terminal width, cpufetch will print a smaller version of the logo (if it exists). This behavior can be overridden by\n");
85   printf("    --logo-short  and --logo-long, which always sets the logo size as specified by the user, even if it is too big. After the\n");
86   printf("    logo selection (either automatically or set by the user), cpufetch will check again if the output fits in the terminal.\n");
87   printf("    If not, it will use a shorter name for the fields (the left part of the text). If, after all of this, the output still does\n");
88   printf("    not fit, cpufetch will cut the text and will only print the text until there is no space left in each line\n");
89 
90   printf("\nEXAMPLES: \n");
91   printf("    Run cpufetch with Intel color scheme:\n");
92   printf("      ./cpufetch --color intel\n");
93   printf("    Run cpufetch with a custom color scheme:\n");
94   printf("      ./cpufetch --color 239,90,45:210,200,200:0,0,0:100,200,45:0,200,200\n");
95 
96   printf("\nBUGS: \n");
97   printf("    Report bugs to https://github.com/Dr-Noob/cpufetch/issues\n");
98 
99   printf("\nNOTE: \n");
100   printf("    Peak performance information is NOT accurate. cpufetch computes peak performance using the max\n");
101   printf("    frequency of the CPU. However, to compute the peak performance, you need to know the frequency of the\n");
102   printf("    CPU running AVX code. This value is not be fetched by cpufetch since it depends on each specific CPU.\n");
103   printf("    To correctly measure peak performance, see: https://github.com/Dr-Noob/peakperf\n");
104 }
105 
print_version()106 void print_version() {
107   printf("cpufetch v%s (%s %s)\n",VERSION, OS_STR, ARCH_STR);
108 }
109 
main(int argc,char * argv[])110 int main(int argc, char* argv[]) {
111   if(!parse_args(argc,argv))
112     return EXIT_FAILURE;
113 
114   if(show_help()) {
115     print_help(argv);
116     return EXIT_SUCCESS;
117   }
118 
119   if(show_version()) {
120     print_version();
121     return EXIT_SUCCESS;
122   }
123 
124   set_log_level(verbose_enabled());
125 
126   struct cpuInfo* cpu = get_cpu_info();
127   if(cpu == NULL)
128     return EXIT_FAILURE;
129 
130   if(show_debug()) {
131     print_version();
132     print_debug(cpu);
133     return EXIT_SUCCESS;
134   }
135 
136   if(show_raw()) {
137   #ifdef ARCH_X86
138     print_version();
139     print_raw(cpu);
140     return EXIT_SUCCESS;
141   #else
142     printErr("raw option is valid only in x86_64");
143     return EXIT_FAILURE;
144   #endif
145   }
146 
147   if(print_cpufetch(cpu, get_style(), get_colors(), show_full_cpu_name()))
148     return EXIT_SUCCESS;
149   else
150     return EXIT_FAILURE;
151 }
152