Merge pull request #24 from javalsai/code-quality

Code quality & Fixes
This commit is contained in:
javalsai 2024-11-01 13:33:20 +01:00 committed by GitHub
commit 1e0ffcdf2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 122 additions and 98 deletions

View File

@ -136,8 +136,9 @@ jobs:
- uses: uraimo/run-on-arch-action@v2 - uses: uraimo/run-on-arch-action@v2
with: with:
arch: aarch64 arch: none
distro: ubuntu22.04 distro: none
base_image: '--platform=linux/aarch64 ubuntu:22.04'
githubToken: ${{ github.token }} githubToken: ${{ github.token }}
install: | install: |
apt-get update && \ apt-get update && \
@ -180,8 +181,9 @@ jobs:
- uses: uraimo/run-on-arch-action@v2 - uses: uraimo/run-on-arch-action@v2
with: with:
arch: armv7 arch: none
distro: ubuntu22.04 distro: none
base_image: '--platform=linux/arm/v7 ubuntu:22.04'
githubToken: ${{ github.token }} githubToken: ${{ github.token }}
install: | install: |
apt-get update && \ apt-get update && \
@ -224,8 +226,9 @@ jobs:
- uses: uraimo/run-on-arch-action@v2 - uses: uraimo/run-on-arch-action@v2
with: with:
arch: riscv64 arch: none
distro: ubuntu22.04 distro: none
base_image: '--platform=linux/riscv64 riscv64/ubuntu:22.04'
githubToken: ${{ github.token }} githubToken: ${{ github.token }}
install: | install: |
apt-get update && \ apt-get update && \

View File

@ -3,6 +3,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <util.h>
enum session_type { enum session_type {
XORG, XORG,
WAYLAND, WAYLAND,
@ -16,11 +18,6 @@ struct session {
enum session_type type; enum session_type type;
}; };
struct sessions_list { struct Vector get_avaliable_sessions();
u_int16_t length;
struct session *sessions;
};
struct sessions_list *get_avaliable_sessions();
#endif #endif

View File

@ -2,9 +2,10 @@
#define _UIH_ #define _UIH_
#include <config.h> #include <config.h>
#include <util.h>
void setup(struct config); void setup(struct config);
int load(struct users_list *, struct sessions_list *); int load(struct Vector * users, struct Vector * sessions);
void print_err(const char *); void print_err(const char *);
void print_errno(const char *); void print_errno(const char *);

View File

@ -3,17 +3,14 @@
#include <sys/types.h> #include <sys/types.h>
#include <util.h>
struct user { struct user {
char *shell; char *shell;
char *username; char *username;
char *display_name; char *display_name;
}; };
struct users_list { struct Vector get_human_users();
u_int16_t length;
struct user *users;
};
struct users_list *get_human_users();
#endif #endif

View File

@ -3,6 +3,7 @@
#include <keys.h> #include <keys.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
enum keys find_keyname(char *); enum keys find_keyname(char *);
@ -10,4 +11,19 @@ enum keys find_ansi(char *);
void read_press(u_char *, char *); void read_press(u_char *, char *);
void strcln(char **dest, const char *source); void strcln(char **dest, const char *source);
struct Vector {
uint32_t length;
uint32_t alloc_len;
uint16_t alloc_size;
void** pages;
};
struct Vector vec_new();
int vec_push(struct Vector*, void* item);
void vec_free(struct Vector*);
void vec_clear(struct Vector*);
void vec_reset(struct Vector*);
void* vec_pop(struct Vector*); // won't free it, nor shrink vec list space
void* vec_get(struct Vector*, uint32_t index);
#endif #endif

View File

@ -9,6 +9,7 @@
#include <sessions.h> #include <sessions.h>
#include <ui.h> #include <ui.h>
#include <users.h> #include <users.h>
#include <util.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 2) if (argc == 2)
@ -23,10 +24,10 @@ int main(int argc, char *argv[]) {
} }
setup(*config); setup(*config);
struct users_list *users = get_human_users(); struct Vector users = get_human_users();
struct sessions_list *sessions = get_avaliable_sessions(); struct Vector sessions = get_avaliable_sessions();
int ret = load(users, sessions); int ret = load(&users, &sessions);
if (ret == 0) if (ret == 0)
execl(argv[0], argv[0], NULL); execl(argv[0], argv[0], NULL);
} }

View File

@ -18,7 +18,6 @@ static const struct source_dir sources[] = {
{XORG, "/usr/share/xsessions"}, {XORG, "/usr/share/xsessions"},
{WAYLAND, "/usr/share/wayland-sessions"}, {WAYLAND, "/usr/share/wayland-sessions"},
}; };
static const size_t sources_size = sizeof(sources) / sizeof(sources[0]);
static struct session __new_session(enum session_type type, char *name, static struct session __new_session(enum session_type type, char *name,
const char *exec, const char *tryexec) { const char *exec, const char *tryexec) {
@ -31,24 +30,12 @@ static struct session __new_session(enum session_type type, char *name,
return __session; return __session;
} }
static const u_int8_t bs = 16; static struct Vector *cb_sessions = NULL;
static const u_int8_t unit_size = sizeof(struct session);
static u_int16_t alloc_size = bs;
static u_int16_t used_size = 0;
static struct session *sessions = NULL;
static struct sessions_list *__sessions_list = NULL;
// NOTE: commented printf's here would be nice to have debug logs if I ever // NOTE: commented printf's here would be nice to have debug logs if I ever
// implement it // implement it
static enum session_type session_type; static enum session_type session_type;
static int fn(const char *fpath, const struct stat *sb, int typeflag) { static int fn(const char *fpath, const struct stat *sb, int typeflag) {
// practically impossible to reach this
// but will prevent break
if (used_size == 0xffff)
return 0;
if (sb == NULL || !S_ISREG(sb->st_mode)) if (sb == NULL || !S_ISREG(sb->st_mode))
return 0; return 0;
@ -56,7 +43,7 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
FILE *fd = fopen(fpath, "r"); FILE *fd = fopen(fpath, "r");
if (fd == NULL) { if (fd == NULL) {
perror("fopen"); perror("fopen");
fprintf(stderr, "error opening file (r) %s\n", fpath); fprintf(stderr, "error opening file (r) '%s'\n", fpath);
return 0; return 0;
} }
@ -66,8 +53,8 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
char *name_buf = NULL; char *name_buf = NULL;
char *exec_buf = NULL; char *exec_buf = NULL;
char *tryexec_buf = NULL; char *tryexec_buf = NULL;
// This should be made a specific function
while (true) { while (true) {
/*printf(".");*/
char *buf = malloc(sb->st_blksize); char *buf = malloc(sb->st_blksize);
ssize_t read_size = getline(&buf, &alloc_size, fd); ssize_t read_size = getline(&buf, &alloc_size, fd);
if (read_size == -1) { if (read_size == -1) {
@ -102,17 +89,10 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
// just add this to the list // just add this to the list
if (name_buf != NULL && exec_buf != NULL) { if (name_buf != NULL && exec_buf != NULL) {
/*printf("gonna add to session list\n");*/ struct session *session_i = malloc(sizeof (struct session));
if (used_size >= alloc_size) { *session_i = __new_session(session_type, name_buf, exec_buf,
alloc_size += bs; tryexec_buf == NULL ? "" : tryexec_buf);
sessions = realloc(sessions, alloc_size * unit_size); vec_push(cb_sessions, session_i);
}
/*printf("n %s\ne %s\nte %s\n", name_buf, exec_buf, tryexec_buf);*/
sessions[used_size] = __new_session(session_type, name_buf, exec_buf,
tryexec_buf == NULL ? "" : tryexec_buf);
used_size++;
} }
if (name_buf != NULL) if (name_buf != NULL)
@ -125,23 +105,17 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
return 0; return 0;
} }
static struct sessions_list __list;
// This code is designed to be run purely single threaded // This code is designed to be run purely single threaded
struct sessions_list *get_avaliable_sessions() { struct Vector get_avaliable_sessions() {
if (sessions != NULL) struct Vector sessions = vec_new();
return __sessions_list;
else
sessions = malloc(alloc_size * unit_size);
for (uint i = 0; i < sources_size; i++) { cb_sessions = &sessions;
session_type = sources[i].type; for (uint i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) {
/*printf("recurring into %s\n", sources[i].dir);*/ /*printf("recurring into %s\n", sources[i].dir);*/
session_type = sources[i].type;
ftw(sources[i].dir, &fn, 1); ftw(sources[i].dir, &fn, 1);
} }
cb_sessions = NULL;
sessions = realloc(sessions, used_size * unit_size); return sessions;
__list.length = used_size;
__list.sessions = sessions;
return __sessions_list = &__list;
} }

View File

@ -179,8 +179,8 @@ struct opt_field of_session;
struct opt_field of_user; struct opt_field of_user;
struct opt_field of_passwd; struct opt_field of_passwd;
struct users_list *gusers; struct Vector *gusers;
struct sessions_list *gsessions; struct Vector *gsessions;
// not *that* OF tho // not *that* OF tho
struct opt_field *get_of(enum input from) { struct opt_field *get_of(enum input from) {
@ -217,7 +217,7 @@ void ffield_cursor_focus() {
struct user get_current_user() { struct user get_current_user() {
if (of_user.current_opt != 0) if (of_user.current_opt != 0)
return gusers->users[of_user.current_opt - 1]; return *(struct user*)vec_get(gusers, of_user.current_opt - 1);
else { else {
struct user custom_user; struct user custom_user;
custom_user.shell = "/usr/bin/bash"; custom_user.shell = "/usr/bin/bash";
@ -237,7 +237,7 @@ struct session get_current_session() {
shell_session.exec = shell_session.name = get_current_user().shell; shell_session.exec = shell_session.name = get_current_user().shell;
return shell_session; return shell_session;
} else } else
return gsessions->sessions[of_session.current_opt - 1]; return *(struct session*)vec_get(gsessions, of_session.current_opt - 1);
} else { } else {
struct session custom_session; struct session custom_session;
custom_session.type = SHELL; custom_session.type = SHELL;
@ -320,7 +320,7 @@ void ffield_type(char *text) {
print_ffield(); print_ffield();
} }
int load(struct users_list *users, struct sessions_list *sessions) { int load(struct Vector *users, struct Vector *sessions) {
/// SETUP /// SETUP
gusers = users; gusers = users;
gsessions = sessions; gsessions = sessions;

View File

@ -20,43 +20,19 @@ static struct user __new_user(struct passwd *p) {
return __user; return __user;
} }
static const u_int8_t bs = 16;
static const u_int8_t unit_size = sizeof(struct user);
static u_int16_t alloc_size = bs;
static u_int16_t used_size = 0;
static struct user *users = NULL;
static struct users_list *__users_list = NULL;
struct users_list __list;
// This code is designed to be run purely single threaded // This code is designed to be run purely single threaded
struct users_list *get_human_users() { struct Vector get_human_users() {
if (users != NULL) struct Vector users = vec_new();
return __users_list;
else
users = malloc(alloc_size * unit_size);
struct passwd *pwd; struct passwd *pwd;
while ((pwd = getpwent()) != NULL) { while ((pwd = getpwent()) != NULL) {
// practically impossible to reach this (== 0xffff) if (!(pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", 6) == 0))
// but will prevent break
if (used_size == 0xffff ||
!(pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", 6) == 0))
continue; continue;
if (used_size >= alloc_size) { struct user *user_i = malloc(sizeof(struct user));
alloc_size += bs; *user_i = __new_user(pwd);
users = realloc(users, alloc_size * unit_size); vec_push(&users, user_i);
}
users[used_size] = __new_user(pwd);
used_size++;
} }
users = realloc(users, used_size * unit_size); return users;
__list.length = used_size;
__list.users = users;
return __users_list = &__list;
} }

View File

@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -66,3 +67,61 @@ static int selret_magic() {
timeout.tv_usec = 0; timeout.tv_usec = 0;
return select(1, &set, NULL, NULL, &timeout); return select(1, &set, NULL, NULL, &timeout);
} }
// Vector shii
struct Vector vec_new() {
struct Vector vec;
vec_clear(&vec);
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;
}
}
vec->pages[vec->length] = item;
vec->length++;
return 0;
}
void vec_free(struct Vector* vec) {
while(vec->length > 0)
free(vec->pages[--vec->length]);
vec_clear(vec);
}
void vec_clear(struct Vector* vec) {
free(vec->pages);
vec_reset(vec);
}
void vec_reset(struct Vector* vec) {
vec->length = 0;
vec->alloc_len = 0;
vec->alloc_size = 4096; // 4KiB page size?
vec->pages = NULL;
}
void* vec_pop(struct Vector* vec) {
if (vec->length == 0)
return NULL;
return vec->pages[--vec->length];
}
void* vec_get(struct Vector* vec, uint32_t index) {
if (index >= vec->length)
return NULL;
return vec->pages[index];
}