mirror of
https://github.com/javalsai/lidm.git
synced 2025-08-31 18:38:00 +02:00
Merge branch 'master' into code-formatting
This commit is contained in:
33
src/config.c
33
src/config.c
@@ -4,6 +4,14 @@
|
||||
#include "config.h"
|
||||
#include "util.h"
|
||||
|
||||
// Alr so ima explain the bitfield returned by `cb` a bit
|
||||
// 4 bits:
|
||||
// 0b0001: break out of parsing (returning true)
|
||||
// 0b0010: free the value
|
||||
// 0b0100: free the key
|
||||
// 0b1000: break out of parsing (returning false)
|
||||
//
|
||||
// This would return true if everything goes fine, false otherwise (malloc error, broke parsing, etc)
|
||||
bool line_parser(FILE *fd, ssize_t *blksize,
|
||||
u_char (*cb)(char *key, char *value)) {
|
||||
size_t opt_size = 4096;
|
||||
@@ -41,19 +49,20 @@ bool line_parser(FILE *fd, ssize_t *blksize,
|
||||
if (ret & 0b1000) {
|
||||
free(buf);
|
||||
return false;
|
||||
}
|
||||
if (ret & 0b0001) {
|
||||
free(buf);
|
||||
}
|
||||
if (ret & 0b0001) {
|
||||
free(buf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct config *__config;
|
||||
// Yanderedev code (wanna fix this with a table or smth)
|
||||
u_char config_line_handler(char *k, char *v) {
|
||||
if (strcmp(k, "colors.bg") == 0)
|
||||
__config->theme.colors.bg = v;
|
||||
@@ -98,16 +107,13 @@ u_char config_line_handler(char *k, char *v) {
|
||||
else if (strcmp(k, "functions.poweroff") == 0) {
|
||||
__config->functions.poweroff = find_keyname(v);
|
||||
return 0b0110;
|
||||
}
|
||||
else if (strcmp(k, "functions.reboot") == 0) {
|
||||
} else if (strcmp(k, "functions.reboot") == 0) {
|
||||
__config->functions.reboot = find_keyname(v);
|
||||
return 0b0110;
|
||||
}
|
||||
else if (strcmp(k, "functions.refresh") == 0) {
|
||||
} else if (strcmp(k, "functions.refresh") == 0) {
|
||||
__config->functions.refresh = find_keyname(v);
|
||||
return 0b0110;
|
||||
}
|
||||
else if (strcmp(k, "strings.f_poweroff") == 0)
|
||||
} else if (strcmp(k, "strings.f_poweroff") == 0)
|
||||
__config->strings.f_poweroff = v;
|
||||
else if (strcmp(k, "strings.f_reboot") == 0)
|
||||
__config->strings.f_reboot = v;
|
||||
@@ -126,15 +132,14 @@ u_char config_line_handler(char *k, char *v) {
|
||||
else if (strcmp(k, "behavior.include_defshell") == 0) {
|
||||
__config->behavior.include_defshell = strcmp(v, "true") == 0;
|
||||
return 0b0110;
|
||||
}
|
||||
else if (strcmp(k, "behavior.source") == 0)
|
||||
} else if (strcmp(k, "behavior.source") == 0)
|
||||
vec_push(&__config->behavior.source, v);
|
||||
else if (strcmp(k, "behavior.user_source") == 0)
|
||||
vec_push(&__config->behavior.user_source, v);
|
||||
else
|
||||
return 0b1111;
|
||||
|
||||
return 0b100;
|
||||
return 0b0100;
|
||||
}
|
||||
|
||||
struct config *parse_config(char *path) {
|
||||
|
@@ -54,6 +54,7 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
||||
char *exec_buf = NULL;
|
||||
char *tryexec_buf = NULL;
|
||||
// This should be made a specific function
|
||||
// Emm, if anything goes wrong just free the inner loop and `break;` fd and the rest is handled after
|
||||
while (true) {
|
||||
char *buf = malloc(sb->st_blksize);
|
||||
ssize_t read_size = getline(&buf, &alloc_size, fd);
|
||||
@@ -64,7 +65,19 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
||||
|
||||
uint read;
|
||||
char *key = malloc(read_size + sizeof(char));
|
||||
if(key == NULL) {
|
||||
free(buf);
|
||||
// TODO: more sophisticated error handling??
|
||||
break;
|
||||
}
|
||||
char *value = malloc(read_size + sizeof(char));
|
||||
if(value == NULL) {
|
||||
free(buf);
|
||||
free(key);
|
||||
// TODO: more sophisticated error handling??
|
||||
break;
|
||||
}
|
||||
value[0] = '\0'; // I'm not sure if sscanf would null this string out
|
||||
if ((read = sscanf(buf, "%[^=]=%[^\n]\n", key, value)) != 0) {
|
||||
if (strcmp(key, "Name") == 0) {
|
||||
found &= 0b001;
|
||||
@@ -78,13 +91,17 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
||||
} else {
|
||||
free(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
free(value);
|
||||
}
|
||||
free(key);
|
||||
free(buf);
|
||||
free(buf);
|
||||
if (found == 0b111) break;
|
||||
}
|
||||
/*printf("\nend parsing...\n");*/
|
||||
|
||||
// Generic handling of exit
|
||||
|
||||
fclose(fd);
|
||||
|
||||
// just add this to the list
|
||||
@@ -108,6 +125,7 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
||||
// This code is designed to be run purely single threaded
|
||||
struct Vector get_avaliable_sessions() {
|
||||
struct Vector sessions = vec_new();
|
||||
vec_reserve(&sessions, 8);
|
||||
|
||||
cb_sessions = &sessions;
|
||||
for (size_t i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) {
|
||||
|
7
src/ui.c
7
src/ui.c
@@ -322,6 +322,7 @@ void ffield_type(char *text) {
|
||||
print_ffield();
|
||||
}
|
||||
|
||||
static char* unknown_str = "unknown";
|
||||
int load(struct Vector *users, struct Vector *sessions) {
|
||||
/// SETUP
|
||||
gusers = users;
|
||||
@@ -331,9 +332,9 @@ int load(struct Vector *users, struct Vector *sessions) {
|
||||
char *hostname = malloc(16);
|
||||
if (gethostname(hostname, 16) != 0) {
|
||||
free(hostname);
|
||||
hostname = "unknown";
|
||||
hostname = unknown_str;
|
||||
} else {
|
||||
hostname = realloc(hostname, strlen(hostname) + 1);
|
||||
hostname[15] = '\0';
|
||||
}
|
||||
|
||||
of_session = ofield_new(sessions->length + behavior.include_defshell);
|
||||
@@ -350,12 +351,14 @@ int load(struct Vector *users, struct Vector *sessions) {
|
||||
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2,
|
||||
boxstart.x + 12 - (uint)strlen(hostname), theme.colors.e_hostname,
|
||||
hostname, theme.colors.fg);
|
||||
if(hostname != unknown_str) free(hostname);
|
||||
|
||||
// put date
|
||||
char *fmtd_time = fmt_time();
|
||||
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2,
|
||||
boxstart.x + boxw - 3 - (uint)strlen(fmtd_time), theme.colors.e_date,
|
||||
fmtd_time, theme.colors.fg);
|
||||
free(fmtd_time);
|
||||
|
||||
print_field(SESSION);
|
||||
print_field(USER);
|
||||
|
@@ -23,6 +23,7 @@ static struct user __new_user(struct passwd *p) {
|
||||
// This code is designed to be run purely single threaded
|
||||
struct Vector get_human_users() {
|
||||
struct Vector users = vec_new();
|
||||
vec_reserve(&users, 4);
|
||||
|
||||
struct passwd *pwd;
|
||||
while ((pwd = getpwent()) != NULL) {
|
||||
|
48
src/util.c
48
src/util.c
@@ -75,20 +75,41 @@ struct Vector vec_new() {
|
||||
return vec;
|
||||
}
|
||||
|
||||
int vec_push(struct Vector *vec, void *item) {
|
||||
if (vec->length >= vec->alloc_len) {
|
||||
uint32_t new_size = vec->alloc_len + vec->alloc_size;
|
||||
void **new_location = realloc(vec->pages, vec->alloc_size);
|
||||
if (new_location != NULL) {
|
||||
vec->alloc_size = new_size;
|
||||
vec->pages = new_location;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
int vec_resize(struct Vector *vec, size_t size) {
|
||||
void **new_location = realloc(vec->pages, size * sizeof(void*));
|
||||
if (new_location != NULL) {
|
||||
if (vec->length > size)
|
||||
vec->length = size;
|
||||
vec->capacity = size;
|
||||
vec->pages = new_location;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
vec->pages[vec->length] = item;
|
||||
vec->length++;
|
||||
int vec_reserve(struct Vector *vec, size_t size) {
|
||||
uint32_t new_capacity = vec->capacity;
|
||||
while (vec->length + size > new_capacity) {
|
||||
new_capacity = new_capacity + (new_capacity >> 1) + 1; // cap * 1.5 + 1; 0 1 2 4 7 11...
|
||||
}
|
||||
return vec_resize(vec, new_capacity);
|
||||
}
|
||||
|
||||
int vec_reserve_exact(struct Vector *vec, size_t size) {
|
||||
uint32_t needed_capacity = vec->length + size;
|
||||
if (vec->capacity < needed_capacity) {
|
||||
return vec_resize(vec, needed_capacity);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int vec_push(struct Vector *vec, void *item) {
|
||||
int res_ret = vec_reserve(vec, 1);
|
||||
if(res_ret != 0) return res_ret;
|
||||
|
||||
vec->pages[vec->length++] = item;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -106,8 +127,7 @@ void vec_clear(struct Vector *vec) {
|
||||
|
||||
void vec_reset(struct Vector *vec) {
|
||||
vec->length = 0;
|
||||
vec->alloc_len = 0;
|
||||
vec->alloc_size = 4096; // 4KiB page size?
|
||||
vec->capacity = 0;
|
||||
vec->pages = NULL;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user