forked from alba4k/albafetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
653 lines (549 loc) · 23.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include "config/config.h"
#include "info/info.h"
#include "utils/utils.h"
#include "logos.h"
#include "utils/queue.h"
#include "version.h" // generated by meson - defines COMMIT, RELEASE and VERSION
// idk hy but this is sometimes not defined
#ifndef HOST_NAME_MAX
#ifdef _POSIX_HOST_NAME_MAX
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#else
#define HOST_NAME_MAX 255
#endif
#endif
// Not sure if this makes sense
#ifndef LOGIN_NAME_MAX
#define LOGIN_NAME_MAX HOST_NAME_MAX
#endif
// TODO:
/*
* print de, shell and terminal versions
* Windows support? *BSD support?
* make ascii dynamically use the available space (aka line by line)
* start using gtk for theme and icons
* display resolution
* storage
* fix sqlite usage in packages for rpm
* steam installed packages (off by default)
* cpu temp (off by default)
* rewrite config parsing and make it decent
*/
// This contains the default config values
struct Config config = {
// Default values for boolean options (least to most significant bit)
// 0111 0101 1111 1110 1111 1001 0110 ...
.boolean_options = 0x69f7fae,
.logo = NULL,
.color = "",
.dash = ": ",
.separator = "-",
.spacing = 5,
.gpu_index = 0,
.date_format = "%02d/%02d/%d %02d:%02d:%02d",
.col_block_str = " ",
.separator_prefix = "",
.spacing_prefix = "",
.title_prefix = "",
.user_prefix = "User",
.hostname_prefix = "Hostname",
.uptime_prefix = "Uptime",
.os_prefix = "OS",
.kernel_prefix = "Kernel",
.desktop_prefix = "Desktop",
.gtk_theme_prefix = "Theme",
.icon_theme_prefix = "Icons",
.cursor_theme_prefix = "Cursor",
.shell_prefix = "Shell",
.login_shell_prefix = "Login",
.term_prefix = "Terminal",
.pkg_prefix = "Packages",
.host_prefix = "Host",
.bios_prefix = "BIOS",
.cpu_prefix = "CPU",
.gpu_prefix = "GPU",
.mem_prefix = "Memory",
.pub_prefix = "Public IP",
.loc_prefix = "Local IP",
.pwd_prefix = "Directory",
.date_prefix = "Date",
.bat_prefix = "Battery",
.colors_prefix = "",
.light_colors_prefix = "",
};
int main(int argc, char **argv) {
bool user_is_an_idiot = false; // rtfm and stfu
// are the following command line args used?
bool asking_help = false;
bool use_config = true;
bool print_logo = true;
int asking_color = 0;
int asking_bold = 0;
int asking_logo = 0;
int asking_align = 0;
// these store either the default values or the ones defined in the config
// they are needed to know what is used if no arguments are given (for --help)
bool default_bold = _bold;
char default_color[8] = "";
char default_logo[16] = "";
// the default config file is ~/.config/albafetch.conf
char config_file[LOGIN_NAME_MAX + 64] = "";
// path of a custom ascii art
char *ascii_file = NULL;
void *ascii_ptr = NULL;
// parsing the command args
for(int i = 1; i < argc; ++i) {
if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
asking_help = true;
if(strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
puts(VERSION" (built from commit "COMMIT")");
return 0;
}
else if(strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--color") == 0)
asking_color = i+1;
else if(strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--bold") == 0)
asking_bold = i+1;
else if(strcmp(argv[i], "--ascii") == 0) {
if(i+1 >= argc) { // is there such an arg?
fputs("\033[31m\033[1mERROR\033[0m: --ascii requires an extra argument!\n", stderr);
user_is_an_idiot = true;
continue;
}
else if(access(argv[i+1], F_OK)) { // is it a valid file?
fprintf(stderr, "\033[31m\033[1mERROR\033[0m: invalid file \"%s\"! Use --help for more info\n", argv[i+1]);
user_is_an_idiot = true;
continue;
}
ascii_file = argv[i+1];
continue;
}
else if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--logo") == 0)
asking_logo = i+1;
else if(strcmp(argv[i], "--align") == 0 || strcmp(argv[i], "-a") == 0)
asking_align = i+1;
else if(strcmp(argv[i], "--config") == 0 && use_config) {
if(i+1 >= argc) { // is there such an arg?
fputs("\033[31m\033[1mERROR\033[0m: --config requires an extra argument!\n", stderr);
user_is_an_idiot = true;
continue;
}
else if(access(argv[i+1], F_OK)) { // is it a valid file?
fprintf(stderr, "\033[31m\033[1mERROR\033[0m: invalid file \"%s\"! Use --help for more info\n", argv[i+1]);
user_is_an_idiot = true;
continue;
}
strncpy(config_file, argv[i+1], sizeof(config_file)-1);
continue;
}
else if(strcmp(argv[i], "--no-logo") == 0)
print_logo = false;
else if(strcmp(argv[i], "--no-config") == 0)
use_config = false;
}
char data[256] = ""; // output of each module
char printed[1024] = ""; // line-by-line output of albafetch
struct Module *modules = malloc(sizeof(struct Module));
modules->id = NULL;
modules->next = NULL;
// albafetch will first parse ~/.config/albafetch.conf
// ~/.config/albafetch/albafetch.conf if the former is not found
if(use_config) { // --no-config was not used
if(config_file[0] == 0) { // --config was not used, using the default path
char *home = getenv("HOME");
char *config_home = getenv("XDG_CONFIG_HOME");
if(config_home) { // is XDG_CONFIG_HOME set?
snprintf(config_file, sizeof(config_file), "%s/albafetch.conf", config_home);
if(access(config_file, F_OK))
snprintf(config_file, sizeof(config_file), "%s/albafetch/albafetch.conf", config_home);
}
if(home && access(config_file, F_OK)) { // is HOME set?
snprintf(config_file, sizeof(config_file), "%s/.config/albafetch.conf", home);
if(access(config_file, F_OK))
snprintf(config_file, sizeof(config_file), "%s/.config/albafetch/albafetch.conf", home);
}
if(access(config_file, F_OK)) {
strcpy(config_file, "/etc/xdg/albafetch.conf");
}
}
parse_config(config_file, modules, &ascii_ptr, &default_bold, default_color, default_logo);
}
if(ascii_file)
ascii_ptr = file_to_logo(ascii_file);
if(asking_logo) { // --logo was used
bool found = false;
if(asking_logo < argc) {
// find the matching logo
for(size_t i = 0; i < sizeof(logos)/sizeof(logos[0]); ++i)
if(strcmp(logos[i][0], argv[asking_logo]) == 0) {
config.logo = logos[i];
found = true;
}
if(found == false)
fprintf(stderr, "\033[31m\033[1mERROR\033[0m: invalid logo \"%s\"! Use --help for more info\n", argv[asking_logo]);
}
else
fputs("\033[31m\033[1mERROR\033[0m: --logo requires an extra argument!\n", stderr);
if(found)
strcpy(config.color, config.logo[1]);
else
user_is_an_idiot = true;
}
if(config.logo == NULL) { // get a logo based on the OS (--logo was not used and no logo was set by the config)
#ifdef __APPLE__
config.logo = logos[1];
#else
# ifdef __ANDROID__
config.logo = logos[2];
# else
config.logo = logos[0];
FILE *fp = fopen("/etc/os-release", "r");
if(fp == NULL)
fp = fopen("/usr/lib/os-release", "r");
if(fp != NULL) {
char os_id[48];
// check with a newline first
read_after_sequence(fp, "\nID", os_id, 48);
if(os_id[0] == 0) {
rewind(fp);
read_after_sequence(fp, "ID", os_id, 48);
}
fclose(fp);
char *end = strchr(os_id, '\n');
if(end != NULL)
*end = 0;
// because Arch Linux ARM has to be special for some reason
// edit: fedora asahi too, yay
if(strcmp(os_id, "archarm") == 0)
os_id[5] = 0;
else if(strcmp(os_id, "fedora-asahi-remix") == 0)
os_id[6] = 0;
// clean up because of some distros randomly using " or ' when they shouldnt be
if(os_id[0] == '\'' || os_id[0] == '"') {
memmove(os_id, os_id+1, strlen(os_id));
end = strchr(os_id, '\'');
if(end == NULL)
end = strchr(os_id, '"');
if(end != NULL)
*end = 0;
}
// find the matching logo
for(size_t i = 0; i < sizeof(logos)/sizeof(*logos); ++i)
if(strcmp(logos[i][0], os_id) == 0) {
config.logo = logos[i];
break;
}
}
# endif // __ANDROID__
#endif // __APPLE__
strcpy(default_logo, config.logo[0]);
strcpy(config.color, config.logo[1]);
}
if(asking_color) {
if(asking_color < argc) {
const char *colors[][2] = {
{"black", "\033[30m"},
{"red", "\033[31m"},
{"green", "\033[32m"},
{"yellow", "\033[33m"},
{"blue", "\033[34m"},
{"purple", "\033[35m"},
{"cyan", "\033[36m"},
{"gray", "\033[90m"},
{"white", "\033[37m"},
};
for(int j = 0; j < 9; ++j)
if(strcmp(argv[asking_color], *colors[j]) == 0) {
strcpy(config.color, colors[j][1]);
goto color_done;
}
fprintf(stderr, "\033[31m\033[1mERROR\033[0m: invalid color \"%s\"! Use --help for more info\n", argv[asking_color]);
}
else
fputs("\033[31m\033[1mERROR\033[0m: --color requires an extra argument!\n", stderr);
user_is_an_idiot = true;
color_done:;
}
if(asking_bold) {
if(asking_bold < argc) {
// modifying the 2nd least significant bit of boolean_options
if(strcmp(argv[asking_bold], "on") == 0) {
config.boolean_options |= ((uint64_t)1 << 1);
goto bold_done;
}
else if(strcmp(argv[asking_bold], "off") == 0) {
config.boolean_options &= ~((uint64_t)1 << 1);
goto bold_done;
}
fputs("\033[31m\033[1mERROR\033[0m: --bold should be followed by either \"on\" or \"off\"!\n", stderr);
}
else
fputs("\033[31m\033[1mERROR\033[0m: --bold requires an extra argument!\n", stderr);
user_is_an_idiot = true;
bold_done:;
}
// was it really that hard to type 'albafetch -h'?
if(user_is_an_idiot) {
destroy_array(modules);
fputs("\033[31m\033[1mFATAL\033[0m: One or multiple errors occurred! Use --help for more info\n", stderr);
return 1;
}
if(asking_help) {
// it won't be used anyway lol
destroy_array(modules);
printf("%s%salbafetch\033[0m - a system fetch utility\n",
config.color, _bold ? "\033[1m" : "");
printf("\n%s%sFLAGS\033[0m:\n",
config.color, _bold ? "\033[1m" : "");
printf("\t%s%s-h\033[0m,%s%s --help\033[0m:\t Print this help menu and exit\n",
config.color, _bold ? "\033[1m" : "", config.color, _bold ? "\033[1m" : "");
printf("\t%s%s-v\033[0m,%s%s --version\033[0m:\t Print the version and exit\n",
config.color, _bold ? "\033[1m" : "", config.color, _bold ? "\033[1m" : "");
printf("\t%s%s-c\033[0m,%s%s --color\033[0m:\t Change the output color (%s%s\033[0m)\n"
"\t\t\t [\033[30mblack\033[0m, \033[31mred\033[0m, \033[32mgreen\033[0m, \033[33myellow\033[0m,"
" \033[34mblue\033[0m, \033[35mpurple\033[0m, \033[36mcyan\033[0m, \033[90mgray\033[0m,"
" \033[37mwhite\033[0m]\n",
config.color, _bold ? "\033[1m" : "", config.color, _bold ? "\033[1m" : "", default_color[0] ? default_color : config.logo[1], default_color[0] ? "default" : "logo default");
printf("\t%s%s-b\033[0m,%s%s --bold\033[0m:\t Specifies if bold should be used in colored parts (default: %s\033[0m)\n"
"\t\t\t [\033[1mon\033[0m, off]\n",
config.color, _bold ? "\033[1m" : "", config.color, _bold ? "\033[1m" : "", default_bold ? "\033[1mon" : "off");
printf("\t%s%s-l\033[0m,%s%s --logo\033[0m:\t Changes the logo that will be displayed (default: %s)\n"
"\t\t\t [alpine, android, apple, arch, arch_small, debian, endeavouros, fedora, gentoo]\n"
"\t\t\t [linux, linuxmint, mageia, manjaro, neon, nixos, none, parrot, pop, ubuntu, windows]\n",
config.color, _bold ? "\033[1m" : "", config.color, _bold ? "\033[1m" : "", default_logo[0] ? default_logo : "OS Default");
printf("\t%s%s--ascii\033[0m:\t Specifies a file containing a custom ascii art to use as logo (default: none)\n"
"\t\t\t [path]\n", config.color, _bold ? "\033[1m" : "");
printf("\t%s%s-a\033[0m, %s%s--align\033[0m:\t Aligns the infos if set (default: %s)\n"
"\t\t\t [on, off]\n", config.color, _bold ? "\033[1m" : "", config.color, _bold ? "\033[1m" : "", _align ? "on" : "off");
printf("\t%s%s--config\033[0m:\t Specifies a custom config (default: ~/.config/albafetch.conf)\n"
"\t\t\t [path]\n", config.color, _bold ? "\033[1m" : "");
printf("\t%s%s--no-logo\033[0m:\t Prints the infos without a logo or ascii art\n",
config.color, _bold ? "\033[1m" : "");
printf("\t%s%s--no-config\033[0m:\t Ignores any provided or existing config file\n",
config.color, _bold ? "\033[1m" : "");
printf("\nReport a bug: %s%s\033[4mhttps://github.com/alba4k/albafetch/issues\033[0m\n",
config.color, _bold ? "\033[1m" : "");
return 0;
}
if(asking_align) {
if(asking_align < argc) {
if(strcmp(argv[asking_align], "on") == 0) {
config.boolean_options |= (uint64_t)1;
goto align_done;
}
else if(strcmp(argv[asking_align], "off") == 0) {
config.boolean_options &= ~((uint64_t)1);
goto align_done;
}
fputs("\033[31m\033[1mERROR\033[0m: --align should be followed by either \"on\" or \"off\"!\n", stderr);
}
else
fputs("\033[31m\033[1mERROR\033[0m: --align requires an extra argument!\n", stderr);
user_is_an_idiot = true;
align_done:;
}
// I am deeply sorry for the code you're about to see - I hope you like spaghetti
unsigned line = 1;
char format[32] = "%s\033[0m%s";
/* getting the terminal width
* I start by using stdout
* if it is not a terminal (e.g. if the user did albafetch | lolcat) I use stderr
* if stderr doesn't work either, I use stdin (albafetch 2>/dev/null | lolcat)
* now, let's assume the user is a complete moron and redirects everything. Then I just use an "infinite" width
*/
struct winsize win;
win.ws_col = 0;
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &win))
if(ioctl(STDERR_FILENO, TIOCGWINSZ, &win))
ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
if(win.ws_col == 0)
win.ws_col = -1;
struct Info {
char *id; // module identifier
char *label; // module label
int (*func)(char *); // function to run
};
struct Info module_table[] = {
// {"identifier", label, func},
{"separator", config.separator_prefix, NULL},
{"space", config.spacing_prefix, NULL},
{"title", config.title_prefix, NULL},
{"user", config.user_prefix, user},
{"hostname", config.hostname_prefix, hostname},
{"uptime", config.uptime_prefix, uptime},
{"os", config.os_prefix, os},
{"kernel", config.kernel_prefix, kernel},
{"desktop", config.desktop_prefix, desktop},
{"gtk_theme", config.gtk_theme_prefix, gtk_theme},
{"icon_theme", config.icon_theme_prefix, icon_theme},
{"cursor_theme", config.cursor_theme_prefix, cursor_theme},
{"shell", config.shell_prefix, shell},
{"login_shell", config.login_shell_prefix, login_shell},
{"term", config.term_prefix, term},
{"packages", config.pkg_prefix, packages},
{"host", config.host_prefix, host},
{"bios", config.bios_prefix, bios},
{"cpu", config.cpu_prefix, cpu},
{"gpu", config.gpu_prefix, gpu},
{"memory", config.mem_prefix, memory},
{"public_ip", config.pub_prefix, public_ip},
{"local_ip", config.loc_prefix, local_ip},
{"pwd", config.pwd_prefix, pwd},
{"date", config.date_prefix, date},
{"battery", config.bat_prefix, battery},
{"colors", config.colors_prefix, colors},
{"light_colors", config.light_colors_prefix, light_colors},
};
// this sets the default module order in case it was not set in a config file
if(modules->next == NULL) {
char *default_order[] = {
"title",
"separator",
"uptime",
"separator",
"os",
"kernel",
"desktop",
"shell",
"term",
"packages",
"separator",
"host",
"cpu",
"gpu",
"memory",
"space",
"colors",
"light_colors",
};
for(size_t i = 0; i < sizeof(default_order)/sizeof(default_order[0]); ++i)
add_module(modules, default_order[i]);
}
// filling in the linked list (with function pointers and labels) based on the ids
// I prefer doing it here than in add_module as this part only runs when it's needed
for(struct Module *current = modules->next; current; current = current->next)
for(size_t i = 0; i < sizeof(module_table)/sizeof(module_table[0]); ++i)
if(strcmp(module_table[i].id, current->id) == 0) {
current->label = module_table[i].label;
current->func = module_table[i].func;
}
if(_align) {
size_t current_len;
asking_align = 0;
// determining how far the text should be aligned
for(struct Module *current = modules->next; current; current = current->next) {
current_len = strlen_real(current->label);
if(current_len > (size_t)asking_align)
asking_align = (int)current_len;
}
asking_align += (int)strlen_real(config.dash);
snprintf(format, 32, "%%-%ds\033[0m%%s", asking_align);
}
// printing every module
for(struct Module *current = modules->next; current; current = current->next) {
if(strcmp(current->id, "separator") == 0) { // separators are handled differently
if(printed[0] == 0) // first thing being printed
continue;
// this is the length of the last printed text
const size_t len = strlen_real(printed)
- strlen_real(config.separator_prefix)
- (print_logo
? strlen_real(config.logo[2])
+ config.spacing
: 0);
printed[0] = 0;
if(print_logo) {
get_logo_line(printed, &line);
for(int i = 0; i < config.spacing; ++i)
strcat(printed, " ");
}
strcat(printed, config.color);
strcat(printed, current->label);
const size_t separator_len = strlen(config.separator);
for(size_t i = 0; i < len && strlen(printed) < 1023 - separator_len*i; ++i)
strcat(printed, config.separator);
}
else if(strcmp(current->id, "space") == 0) { // spacings are handled differently (they don't do shit)
printed[0] = 0;
if(print_logo) {
get_logo_line(printed, &line);
for(int i = 0; i < config.spacing; ++i)
strcat(printed, " ");
}
strcat(printed, config.color);
strcat(printed, current->label);
}
else if(strcmp(current->id, "title") == 0) { // titles are handled differently
char name[256];
char host[256];
if(user(name) || hostname(host))
continue;
printed[0] = 0;
if(print_logo) {
get_logo_line(printed, &line);
for(int i = 0; i < config.spacing; ++i)
strcat(printed, " ");
}
strcat(printed, config.color);
strcat(printed, current->label);
if(_title_color)
snprintf(printed+strlen(printed), 1024-strlen(printed), "%s%s%s%s@%s%s%s",
config.color,
_bold ? "\033[1m" : "",
name,
"\033[0m",
_bold ? "\033[1m" : "",
config.color,
host
);
else
snprintf(printed+strlen(printed), 1024-strlen(printed), "%s%s@%s",
"\033[0m",
name,
host
);
}
else if(current->func == NULL) { // printing a custom text
printed[0] = 0;
if(print_logo) {
get_logo_line(printed, &line);
for(int i = 0; i < config.spacing; ++i)
strcat(printed, " ");
}
strcat(printed, config.color);
strncat(printed, current->id, 1023 - strlen(printed));
}
else {
if((current->func)(data))
continue;
char label[80];
printed[0] = 0;
if(print_logo) {
get_logo_line(printed, &line);
for(int i = 0; i < config.spacing; ++i)
strcat(printed, " ");
}
strcat(printed, config.color);
strcpy(label, current->label);
if(current->label[0] && current->func != colors && current->func != light_colors)
strcat(label, config.dash);
snprintf(printed+strlen(printed), 1024-strlen(printed), format, label, data);
}
print_line(printed, win.ws_col);
}
// remaining lines
while(config.logo[line+1] && print_logo) {
printed[0] = 0;
get_logo_line(printed, &line);
print_line(printed, win.ws_col);
}
// memory clean up
free(ascii_ptr);
destroy_array(modules);
return 0;
}