146 lines
4.1 KiB
C
146 lines
4.1 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#include <bits/types/FILE.h>
|
|
#include <stdio.h>
|
|
#include <curses.h>
|
|
#include <string.h>
|
|
#include "defines.h"
|
|
|
|
unsigned int color_count;
|
|
color *colors;
|
|
|
|
extern file *rgt_content;
|
|
extern unsigned long rgt_file_count;
|
|
|
|
extern unsigned int settings;
|
|
extern unsigned int status;
|
|
|
|
void parse_colors(char *line, short *fg, short *bg){
|
|
int tmp;
|
|
/* cuts everything before the space, then uses ';' as the token delimeter */
|
|
char *token = strtok(strrchr(line, ' '), ";");
|
|
while (token != NULL) {
|
|
sscanf(token, "%d", &tmp);
|
|
/* magic numbers are ansi color codes */
|
|
if ( tmp > 29 && tmp < 38) {
|
|
*fg = tmp - 30;
|
|
} else if ( tmp > 39 && tmp < 48) {
|
|
*bg = tmp - 40;
|
|
}
|
|
token = strtok(NULL, ";");
|
|
}
|
|
}
|
|
|
|
|
|
void colors_init() {
|
|
if (has_colors()) {
|
|
settings |= SETTINGS_HAS_COLOR;
|
|
start_color();
|
|
color_count = 0;
|
|
|
|
|
|
init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */
|
|
if (status & STATUS_USER_ROOT) {
|
|
init_pair(10, COLOR_RED, COLOR_BLACK); /* path */
|
|
} else {
|
|
init_pair(10, COLOR_GREEN, COLOR_BLACK); /* path */
|
|
}
|
|
|
|
|
|
|
|
FILE *dircolors = fopen("/etc/DIR_COLORS", "r");
|
|
if (dircolors) {
|
|
char *line;
|
|
char *token;
|
|
char *extension;
|
|
int tmp = 0;
|
|
size_t size = 0;
|
|
short fg;
|
|
short bg;
|
|
|
|
char supported_filetype_count = 9;
|
|
char initial_pass = 0;
|
|
while ((tmp = getline(&line, &size, dircolors)) != -1 && initial_pass != supported_filetype_count) {
|
|
fg = 7;
|
|
bg = 0;
|
|
token = strtok(line, " ");
|
|
if (token[0] != '#' && token[0] != '\n') {
|
|
if (!strcmp(token, "DIR")) {
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(1, fg, bg); /* directory */
|
|
} else if (!strcmp(token, "EXEC")){
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(2, fg, bg); /* exec */
|
|
} else if (!strcmp(token, "RESET")){
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(3, fg, bg); /* regular file */
|
|
} else if (!strcmp(token, "LINK")){
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(4, fg, bg); /* symlink */
|
|
} else if (!strcmp(token, "BLK")){
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(5, fg, bg); /* block device */
|
|
} else if (!strcmp(token, "CHR")){
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(6, fg, bg); /* character device */
|
|
} else if (!strcmp(token, "SOCK")){
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(7, fg, bg); /* socket */
|
|
} else if (!strcmp(token, "FIFO")){
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(8, fg, bg); /* fifo */
|
|
} else if (!strcmp(token, "ORPHAN")){
|
|
initial_pass++;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(9, fg, bg); /* orphan */
|
|
}
|
|
}
|
|
}
|
|
|
|
/* checks for only extensions (*.[extension], includes the '.') as the filetypes are handled seperately */
|
|
while ((tmp = getline(&line, &size, dircolors)) != -1) {
|
|
if (line[0] == '.') {
|
|
color_count++;
|
|
}
|
|
}
|
|
rewind(dircolors);
|
|
|
|
colors = malloc(sizeof(color) * color_count);
|
|
unsigned int i = 0;
|
|
/* proper pass, reads all defined extensions within /etc/DIR_COLORS */
|
|
while ((tmp = getline(&line, &size, dircolors)) != -1) {
|
|
fg = 7;
|
|
bg = 0;
|
|
if (line[0] == '.') {
|
|
extension = strtok(line, " ");
|
|
colors[i].file_extension = malloc(sizeof(extension));
|
|
strcpy(colors[i].file_extension, extension);
|
|
|
|
colors[i].color_pair = i+11;
|
|
parse_colors(line, &fg, &bg);
|
|
init_pair(i+11, fg, bg);
|
|
i++;
|
|
|
|
}
|
|
}
|
|
} else {
|
|
init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */
|
|
init_pair(1, COLOR_BLUE, COLOR_BLACK); /* directory */
|
|
init_pair(2, COLOR_WHITE, COLOR_BLACK); /* regular file */
|
|
init_pair(3, COLOR_YELLOW, COLOR_BLACK); /* symlink */
|
|
init_pair(4, COLOR_CYAN, COLOR_BLACK); /* block device */
|
|
init_pair(5, COLOR_YELLOW, COLOR_BLACK); /* character device */
|
|
init_pair(6, COLOR_MAGENTA, COLOR_BLACK); /* fifo */
|
|
init_pair(7, COLOR_YELLOW, COLOR_BLACK); /* socket */
|
|
init_pair(8, COLOR_BLACK, COLOR_BLUE); /* reserved */
|
|
}
|
|
}
|
|
}
|