Merge pull request #30 from rmntgx/code-formatting

Address code formatting rules
This commit is contained in:
javalsai 2025-06-06 20:51:51 +02:00 committed by GitHub
commit 230c8558a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 262 additions and 275 deletions

View File

@ -1,2 +1,4 @@
BasedOnStyle: LLVM BasedOnStyle: Chromium
IndentWidth: 2 IndentWidth: 2
SpacesBeforeTrailingComments: 1
AllowShortIfStatementsOnASingleLine: WithoutElse

View File

@ -14,7 +14,7 @@ For small fixes or incremental improvements simply fork the repo and follow the
- Configure clangd LSP by generating `compile_commands.json` (e.g. `bear -- make` or `compiledb make`) - Configure clangd LSP by generating `compile_commands.json` (e.g. `bear -- make` or `compiledb make`)
- Implement your feature. - Implement your feature.
- Check your code works as expected. - Check your code works as expected.
- Run the code formatter: `clang-format -i $(git ls-files "*.cpp" "*.h")` - Run the code formatter: `clang-format -i $(git ls-files "*.c" "*.h")`
3. Commit your changes to a new branch (not `master`, one change per branch) and push it: 3. Commit your changes to a new branch (not `master`, one change per branch) and push it:
- Commit messages should: - Commit messages should:

View File

@ -6,6 +6,10 @@
#include "config.h" #include "config.h"
#include "sessions.h" #include "sessions.h"
bool launch(char *user, char *passwd, struct session session, void (*cb)(void), struct behavior* behavior); bool launch(char* user,
char* passwd,
struct session session,
void (*cb)(void),
struct behavior* behavior);
#endif #endif

View File

@ -74,7 +74,8 @@ struct config {
}; };
bool line_parser( bool line_parser(
FILE *fd, ssize_t *blksize, FILE* fd,
ssize_t* blksize,
u_char (*cb)(char* key, u_char (*cb)(char* key,
char* value)); // might use this for parsing .desktop files too char* value)); // might use this for parsing .desktop files too
struct config* parse_config(char* path); struct config* parse_config(char* path);

View File

@ -15,8 +15,10 @@
#include "unistd.h" #include "unistd.h"
#include "util.h" #include "util.h"
int pam_conversation(int num_msg, const struct pam_message **msg, int pam_conversation(int num_msg,
struct pam_response **resp, void *appdata_ptr) { const struct pam_message** msg,
struct pam_response** resp,
void* appdata_ptr) {
struct pam_response* reply = struct pam_response* reply =
(struct pam_response*)malloc(sizeof(struct pam_response) * num_msg); (struct pam_response*)malloc(sizeof(struct pam_response) * num_msg);
for (size_t i = 0; i < num_msg; i++) { for (size_t i = 0; i < num_msg; i++) {
@ -39,7 +41,9 @@ int pam_conversation(int num_msg, const struct pam_message **msg,
return NULL; \ return NULL; \
} }
void clear_screen() { printf("\x1b[H\x1b[J"); } void clear_screen() {
printf("\x1b[H\x1b[J");
}
pam_handle_t* get_pamh(char* user, char* passwd) { pam_handle_t* get_pamh(char* user, char* passwd) {
pam_handle_t* pamh = NULL; pam_handle_t* pamh = NULL;
@ -64,18 +68,15 @@ void *shmalloc(size_t size) {
void sourceFileTry(char* file) { void sourceFileTry(char* file) {
FILE* file2source = fopen(file, "r"); FILE* file2source = fopen(file, "r");
if (file2source == NULL) if (file2source == NULL) return;
return;
char* line = NULL; char* line = NULL;
size_t len = 0; size_t len = 0;
ssize_t read; ssize_t read;
while ((read = getline(&line, &len, file2source)) != -1) { while ((read = getline(&line, &len, file2source)) != -1) {
if (read == 0 || (read > 0 && *line == '#')) if (read == 0 || (read > 0 && *line == '#')) continue;
continue; if (line[read - 1] == '\n') line[read - 1] = '\0';
if (line[read - 1] == '\n')
line[read - 1] = '\0';
/* printf("Retrieved line of length %zu:\n", read); */ /* printf("Retrieved line of length %zu:\n", read); */
/* printf("%s\n", line); */ /* printf("%s\n", line); */
@ -89,15 +90,15 @@ void sourceFileTry(char *file) {
} }
} }
if (line) if (line) free(line);
free(line);
fclose(file2source); fclose(file2source);
} }
void moarEnv(char *user, struct session session, struct passwd *pw, void moarEnv(char* user,
struct session session,
struct passwd* pw,
struct behavior* behavior) { struct behavior* behavior) {
if (chdir(pw->pw_dir) == -1) if (chdir(pw->pw_dir) == -1) print_errno("can't chdir to user home");
print_errno("can't chdir to user home");
setenv("HOME", pw->pw_dir, true); setenv("HOME", pw->pw_dir, true);
setenv("USER", pw->pw_name, true); setenv("USER", pw->pw_name, true);
@ -109,12 +110,9 @@ void moarEnv(char *user, struct session session, struct passwd *pw,
// PATH? // PATH?
char* xdg_session_type; char* xdg_session_type;
if (session.type == SHELL) if (session.type == SHELL) xdg_session_type = "tty";
xdg_session_type = "tty"; if (session.type == XORG) xdg_session_type = "x11";
if (session.type == XORG) if (session.type == WAYLAND) xdg_session_type = "wayland";
xdg_session_type = "x11";
if (session.type == WAYLAND)
xdg_session_type = "wayland";
setenv("XDG_SESSION_TYPE", xdg_session_type, true); setenv("XDG_SESSION_TYPE", xdg_session_type, true);
printf("\n\n\n\n\x1b[1m"); printf("\n\n\n\n\x1b[1m");
@ -130,8 +128,7 @@ void moarEnv(char *user, struct session session, struct passwd *pw,
char* file2sourcepath = (char*)vec_get(&behavior->user_source, i); char* file2sourcepath = (char*)vec_get(&behavior->user_source, i);
char* newbuf = char* newbuf =
malloc(home_len + strlen(file2sourcepath) + 2); // nullbyte and slash malloc(home_len + strlen(file2sourcepath) + 2); // nullbyte and slash
if (newbuf == NULL) if (newbuf == NULL) continue; // can't bother
continue; // can't bother
strcpy(newbuf, pw->pw_dir); strcpy(newbuf, pw->pw_dir);
newbuf[home_len] = '/'; // assume pw_dir doesn't start with '/' :P newbuf[home_len] = '/'; // assume pw_dir doesn't start with '/' :P
strcpy(&newbuf[home_len + 1], file2sourcepath); strcpy(&newbuf[home_len + 1], file2sourcepath);
@ -153,7 +150,10 @@ void moarEnv(char *user, struct session session, struct passwd *pw,
/*setenv("XDG_SEAT", "seat0", true);*/ /*setenv("XDG_SEAT", "seat0", true);*/
} }
bool launch(char *user, char *passwd, struct session session, void (*cb)(void), bool launch(char* user,
char* passwd,
struct session session,
void (*cb)(void),
struct behavior* behavior) { struct behavior* behavior) {
struct passwd* pw = getpwnam(user); struct passwd* pw = getpwnam(user);
if (pw == NULL) { if (pw == NULL) {
@ -178,8 +178,7 @@ bool launch(char *user, char *passwd, struct session session, void (*cb)(void),
if (pid == 0) { // child if (pid == 0) { // child
char* TERM = NULL; char* TERM = NULL;
char* _GETTERM = getenv("TERM"); char* _GETTERM = getenv("TERM");
if (_GETTERM != NULL) if (_GETTERM != NULL) strcln(&TERM, _GETTERM);
strcln(&TERM, _GETTERM);
if (clearenv() != 0) { if (clearenv() != 0) {
print_errno("clearenv"); print_errno("clearenv");
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
@ -222,8 +221,7 @@ bool launch(char *user, char *passwd, struct session session, void (*cb)(void),
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
if (cb != NULL) if (cb != NULL) cb();
cb();
*reach_session = true; *reach_session = true;

View File

@ -17,8 +17,7 @@ int chvt_str(char *str) {
return -1; return -1;
} }
// I'm not gonna elaborate on this.... // I'm not gonna elaborate on this....
if (i > INT_MAX || i < INT_MIN || *err) if (i > INT_MAX || i < INT_MIN || *err) return -1;
return -1;
return chvt((int)i); return chvt((int)i);
} }

View File

@ -11,18 +11,18 @@
// 0b0100: free the key // 0b0100: free the key
// 0b1000: break out of parsing (returning false) // 0b1000: break out of parsing (returning false)
// //
// This would return true if everything goes fine, false otherwise (malloc error, broke parsing, etc) // This would return true if everything goes fine, false otherwise (malloc
bool line_parser(FILE *fd, ssize_t *blksize, // error, broke parsing, etc)
bool line_parser(FILE* fd,
ssize_t* blksize,
u_char (*cb)(char* key, char* value)) { u_char (*cb)(char* key, char* value)) {
size_t opt_size = 4096; size_t opt_size = 4096;
if (blksize != NULL) if (blksize != NULL) opt_size = *blksize;
opt_size = *blksize;
while (true) { while (true) {
size_t alloc_size = opt_size; size_t alloc_size = opt_size;
char* buf = malloc(alloc_size); char* buf = malloc(alloc_size);
if (buf == NULL) if (buf == NULL) return false;
return false;
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) {
free(buf); free(buf);
@ -42,10 +42,8 @@ bool line_parser(FILE *fd, ssize_t *blksize,
} }
if ((read = sscanf(buf, "%[^ ] = %[^\n]\n", key, value)) != 0) { if ((read = sscanf(buf, "%[^ ] = %[^\n]\n", key, value)) != 0) {
u_char ret = cb(key, value); u_char ret = cb(key, value);
if (ret & 0b0100) if (ret & 0b0100) free(key);
free(key); if (ret & 0b0010) free(value);
if (ret & 0b0010)
free(value);
if (ret & 0b1000) { if (ret & 0b1000) {
free(buf); free(buf);
return false; return false;
@ -154,8 +152,7 @@ struct config *parse_config(char *path) {
__config->behavior.source = vec_new(); __config->behavior.source = vec_new();
__config->behavior.user_source = vec_new(); __config->behavior.user_source = vec_new();
if (__config == NULL) if (__config == NULL) return NULL;
return NULL;
bool ret = line_parser(fd, (ssize_t*)&sb.st_blksize, config_line_handler); bool ret = line_parser(fd, (ssize_t*)&sb.st_blksize, config_line_handler);
if (!ret) { if (!ret) {
free(__config); free(__config);

View File

@ -22,16 +22,13 @@ void field_trim(struct editable_field *field, u_char pos) {
void field_update(struct editable_field* field, char* update) { void field_update(struct editable_field* field, char* update) {
u_char insert_len = strlen(update); u_char insert_len = strlen(update);
if (insert_len == 0) if (insert_len == 0) return;
return;
if (field->pos > field->length) if (field->pos > field->length) field->pos = field->length; // WTF
field->pos = field->length; // WTF
if (insert_len == 1) { if (insert_len == 1) {
// backspace // backspace
if (*update == 127) { if (*update == 127) {
if (field->pos == 0) if (field->pos == 0) return;
return;
if (field->pos < field->length) { if (field->pos < field->length) {
memmove(&field->content[field->pos - 1], &field->content[field->pos], memmove(&field->content[field->pos - 1], &field->content[field->pos],
field->length - field->pos); field->length - field->pos);
@ -61,8 +58,7 @@ void field_update(struct editable_field *field, char *update) {
// returns bool depending if it was able to "use" the seek // returns bool depending if it was able to "use" the seek
bool field_seek(struct editable_field* field, char seek) { bool field_seek(struct editable_field* field, char seek) {
if (field->length == 0) if (field->length == 0) return false;
return false;
if (seek < 0 && -seek > field->pos) if (seek < 0 && -seek > field->pos)
field->pos = 0; field->pos = 0;
@ -71,8 +67,7 @@ bool field_seek(struct editable_field *field, char seek) {
else else
field->pos += seek; field->pos += seek;
if (field->pos > field->length) if (field->pos > field->length) field->pos = field->length;
field->pos = field->length;
return true; return true;
} }

View File

@ -12,8 +12,7 @@
#include "util.h" #include "util.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc == 2) if (argc == 2) chvt_str(argv[1]);
chvt_str(argv[1]);
char* conf_override = getenv("LIDM_CONF"); char* conf_override = getenv("LIDM_CONF");
struct config* config = struct config* config =
@ -28,6 +27,5 @@ int main(int argc, char *argv[]) {
struct Vector 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

@ -19,8 +19,10 @@ static const struct source_dir sources[] = {
{WAYLAND, "/usr/share/wayland-sessions"}, {WAYLAND, "/usr/share/wayland-sessions"},
}; };
static struct session __new_session(enum session_type type, char *name, static struct session __new_session(enum session_type type,
const char *exec, const char *tryexec) { char* name,
const char* exec,
const char* tryexec) {
struct session __session; struct session __session;
__session.type = type; __session.type = type;
strcln(&__session.name, name); strcln(&__session.name, name);
@ -36,8 +38,7 @@ static struct Vector *cb_sessions = NULL;
// 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) {
if (sb == NULL || !S_ISREG(sb->st_mode)) if (sb == NULL || !S_ISREG(sb->st_mode)) return 0;
return 0;
/*printf("gonna open %s\n", fpath);*/ /*printf("gonna open %s\n", fpath);*/
FILE* fd = fopen(fpath, "r"); FILE* fd = fopen(fpath, "r");
@ -54,7 +55,8 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
char* exec_buf = NULL; char* exec_buf = NULL;
char* tryexec_buf = NULL; char* tryexec_buf = NULL;
// This should be made a specific function // 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 // Emm, if anything goes wrong just free the inner loop and `break;` fd and
// the rest is handled after
while (true) { while (true) {
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);
@ -112,12 +114,9 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
vec_push(cb_sessions, session_i); vec_push(cb_sessions, session_i);
} }
if (name_buf != NULL) if (name_buf != NULL) free(name_buf);
free(name_buf); if (exec_buf != NULL) free(exec_buf);
if (exec_buf != NULL) if (tryexec_buf != NULL) free(tryexec_buf);
free(exec_buf);
if (tryexec_buf != NULL)
free(tryexec_buf);
return 0; return 0;
} }

View File

@ -133,8 +133,7 @@ static void ofield_toedit(struct opt_field *ofield, char *init) {
ofield->efield = field_new(init); ofield->efield = field_new(init);
} }
static void ofield_type(struct opt_field* ofield, char* new, char* startstr) { static void ofield_type(struct opt_field* ofield, char* new, char* startstr) {
if (ofield->current_opt != 0) if (ofield->current_opt != 0) ofield_toedit(ofield, startstr);
ofield_toedit(ofield, startstr);
field_update(&ofield->efield, new); field_update(&ofield->efield, new);
} }
// true if it changed anything, single opt fields return false // true if it changed anything, single opt fields return false
@ -158,8 +157,7 @@ static bool ofield_seek(struct opt_field *ofield, char seek) {
} }
} }
if (ofield->opts == 0) if (ofield->opts == 0) return false;
return false;
ofield_opt_seek(ofield, seek); ofield_opt_seek(ofield, seek);
@ -186,12 +184,9 @@ 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) {
if (from == SESSION) if (from == SESSION) return &of_session;
return &of_session; if (from == USER) return &of_user;
if (from == USER) if (from == PASSWD) return &of_passwd;
return &of_user;
if (from == PASSWD)
return &of_passwd;
return NULL; return NULL;
} }
@ -263,7 +258,9 @@ void print_field(enum input focused_input) {
ffield_cursor_focus(); ffield_cursor_focus();
} }
void print_ffield() { print_field(focused_input); } void print_ffield() {
print_field(focused_input);
}
void print_ofield(struct opt_field* ofield) { void print_ofield(struct opt_field* ofield) {
enum input input; enum input input;
if (ofield == &of_session) if (ofield == &of_session)
@ -291,8 +288,7 @@ void ffield_move(bool direction) {
// tf I'm doing // tf I'm doing
void ffield_change_opt(bool direction) { void ffield_change_opt(bool direction) {
struct opt_field* ffield = get_of(focused_input); struct opt_field* ffield = get_of(focused_input);
if (focused_input == PASSWD) if (focused_input == PASSWD) ffield = &of_session;
ffield = &of_session;
if (!ofield_opt_seek(ffield, direction ? 1 : -1)) { if (!ofield_opt_seek(ffield, direction ? 1 : -1)) {
if (focused_input == PASSWD || focused_input == SESSION) if (focused_input == PASSWD || focused_input == SESSION)
ofield_opt_seek(&of_user, direction ? 1 : -1); ofield_opt_seek(&of_user, direction ? 1 : -1);
@ -407,8 +403,7 @@ int load(struct Vector *users, struct Vector *sessions) {
ffield_type(seq); ffield_type(seq);
} }
if (esc != 0) if (esc != 0) esc--;
esc--;
} }
} }
@ -424,7 +419,8 @@ static void clean_line(struct uint_point origin, uint line) {
} }
// TODO: session_len > 32 // TODO: session_len > 32
static void print_session(struct uint_point origin, struct session session, static void print_session(struct uint_point origin,
struct session session,
bool multiple) { bool multiple) {
clean_line(origin, 5); clean_line(origin, 5);
const char* session_type; const char* session_type;
@ -458,7 +454,8 @@ static void print_session(struct uint_point origin, struct session session,
} }
// TODO: user_len > 32 // TODO: user_len > 32
static void print_user(struct uint_point origin, struct user user, static void print_user(struct uint_point origin,
struct user user,
bool multiple) { bool multiple) {
clean_line(origin, 7); clean_line(origin, 7);
printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm", printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm",
@ -539,7 +536,8 @@ static void print_footer() {
uint row = window.ws_row - 1; uint row = window.ws_row - 1;
uint col = window.ws_col - 2 - bsize; uint col = window.ws_col - 2 - bsize;
printf("\x1b[%3$d;%4$dH%8$s \x1b[%1$sm%5$s\x1b[%2$sm %9$s " printf(
"\x1b[%3$d;%4$dH%8$s \x1b[%1$sm%5$s\x1b[%2$sm %9$s "
"\x1b[%1$sm%6$s\x1b[%2$sm %10$s \x1b[%1$sm%7$s\x1b[%2$sm", "\x1b[%1$sm%6$s\x1b[%2$sm %10$s \x1b[%1$sm%7$s\x1b[%2$sm",
theme.colors.e_key, theme.colors.fg, row, col, theme.colors.e_key, theme.colors.fg, row, col,
key_names[functions.poweroff], key_names[functions.reboot], key_names[functions.poweroff], key_names[functions.reboot],

View File

@ -27,8 +27,7 @@ struct Vector get_human_users() {
struct passwd* pwd; struct passwd* pwd;
while ((pwd = getpwent()) != NULL) { while ((pwd = getpwent()) != NULL) {
if (!(pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", 6) == 0)) if (!(pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", 6) == 0)) continue;
continue;
struct user* user_i = malloc(sizeof(struct user)); struct user* user_i = malloc(sizeof(struct user));
*user_i = __new_user(pwd); *user_i = __new_user(pwd);

View File

@ -18,8 +18,7 @@ void strcln(char **dest, const char *source) {
enum keys find_keyname(char* name) { enum keys find_keyname(char* name) {
for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) { for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) {
if (strcmp(key_names[i], name) == 0) if (strcmp(key_names[i], name) == 0) return (enum keys)i;
return (enum keys)i;
} }
perror("key not found"); perror("key not found");
@ -78,8 +77,7 @@ struct Vector vec_new() {
int vec_resize(struct Vector* vec, size_t size) { int vec_resize(struct Vector* vec, size_t size) {
void** new_location = realloc(vec->pages, size * sizeof(void*)); void** new_location = realloc(vec->pages, size * sizeof(void*));
if (new_location != NULL) { if (new_location != NULL) {
if (vec->length > size) if (vec->length > size) vec->length = size;
vec->length = size;
vec->capacity = size; vec->capacity = size;
vec->pages = new_location; vec->pages = new_location;
} else { } else {
@ -91,7 +89,8 @@ int vec_resize(struct Vector *vec, size_t size) {
int vec_reserve(struct Vector* vec, size_t size) { int vec_reserve(struct Vector* vec, size_t size) {
uint32_t new_capacity = vec->capacity; uint32_t new_capacity = vec->capacity;
while (vec->length + size > new_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... new_capacity = new_capacity + (new_capacity >> 1) +
1; // cap * 1.5 + 1; 0 1 2 4 7 11...
} }
return vec_resize(vec, new_capacity); return vec_resize(vec, new_capacity);
} }
@ -132,15 +131,13 @@ void vec_reset(struct Vector *vec) {
} }
void* vec_pop(struct Vector* vec) { void* vec_pop(struct Vector* vec) {
if (vec->length == 0) if (vec->length == 0) return NULL;
return NULL;
return vec->pages[--vec->length]; return vec->pages[--vec->length];
} }
void* vec_get(struct Vector* vec, size_t index) { void* vec_get(struct Vector* vec, size_t index) {
if (index >= vec->length) if (index >= vec->length) return NULL;
return NULL;
return vec->pages[index]; return vec->pages[index];
} }