mirror of
https://github.com/javalsai/lidm.git
synced 2025-07-03 06:15:03 +02:00
Merge pull request #30 from rmntgx/code-formatting
Address code formatting rules
This commit is contained in:
commit
230c8558a7
@ -1,2 +1,4 @@
|
||||
BasedOnStyle: LLVM
|
||||
BasedOnStyle: Chromium
|
||||
IndentWidth: 2
|
||||
SpacesBeforeTrailingComments: 1
|
||||
AllowShortIfStatementsOnASingleLine: WithoutElse
|
||||
|
@ -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`)
|
||||
- Implement your feature.
|
||||
- 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:
|
||||
- Commit messages should:
|
||||
|
@ -6,6 +6,10 @@
|
||||
#include "config.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
|
||||
|
@ -74,7 +74,8 @@ struct config {
|
||||
};
|
||||
|
||||
bool line_parser(
|
||||
FILE *fd, ssize_t *blksize,
|
||||
FILE* fd,
|
||||
ssize_t* blksize,
|
||||
u_char (*cb)(char* key,
|
||||
char* value)); // might use this for parsing .desktop files too
|
||||
struct config* parse_config(char* path);
|
||||
|
52
src/auth.c
52
src/auth.c
@ -15,8 +15,10 @@
|
||||
#include "unistd.h"
|
||||
#include "util.h"
|
||||
|
||||
int pam_conversation(int num_msg, const struct pam_message **msg,
|
||||
struct pam_response **resp, void *appdata_ptr) {
|
||||
int pam_conversation(int num_msg,
|
||||
const struct pam_message** msg,
|
||||
struct pam_response** resp,
|
||||
void* appdata_ptr) {
|
||||
struct pam_response* reply =
|
||||
(struct pam_response*)malloc(sizeof(struct pam_response) * num_msg);
|
||||
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; \
|
||||
}
|
||||
|
||||
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* pamh = NULL;
|
||||
@ -64,18 +68,15 @@ void *shmalloc(size_t size) {
|
||||
|
||||
void sourceFileTry(char* file) {
|
||||
FILE* file2source = fopen(file, "r");
|
||||
if (file2source == NULL)
|
||||
return;
|
||||
if (file2source == NULL) return;
|
||||
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t read;
|
||||
|
||||
while ((read = getline(&line, &len, file2source)) != -1) {
|
||||
if (read == 0 || (read > 0 && *line == '#'))
|
||||
continue;
|
||||
if (line[read - 1] == '\n')
|
||||
line[read - 1] = '\0';
|
||||
if (read == 0 || (read > 0 && *line == '#')) continue;
|
||||
if (line[read - 1] == '\n') line[read - 1] = '\0';
|
||||
|
||||
/* printf("Retrieved line of length %zu:\n", read); */
|
||||
/* printf("%s\n", line); */
|
||||
@ -89,15 +90,15 @@ void sourceFileTry(char *file) {
|
||||
}
|
||||
}
|
||||
|
||||
if (line)
|
||||
free(line);
|
||||
if (line) free(line);
|
||||
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) {
|
||||
if (chdir(pw->pw_dir) == -1)
|
||||
print_errno("can't chdir to user home");
|
||||
if (chdir(pw->pw_dir) == -1) print_errno("can't chdir to user home");
|
||||
|
||||
setenv("HOME", pw->pw_dir, true);
|
||||
setenv("USER", pw->pw_name, true);
|
||||
@ -109,12 +110,9 @@ void moarEnv(char *user, struct session session, struct passwd *pw,
|
||||
// PATH?
|
||||
|
||||
char* xdg_session_type;
|
||||
if (session.type == SHELL)
|
||||
xdg_session_type = "tty";
|
||||
if (session.type == XORG)
|
||||
xdg_session_type = "x11";
|
||||
if (session.type == WAYLAND)
|
||||
xdg_session_type = "wayland";
|
||||
if (session.type == SHELL) xdg_session_type = "tty";
|
||||
if (session.type == XORG) xdg_session_type = "x11";
|
||||
if (session.type == WAYLAND) xdg_session_type = "wayland";
|
||||
setenv("XDG_SESSION_TYPE", xdg_session_type, true);
|
||||
|
||||
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* newbuf =
|
||||
malloc(home_len + strlen(file2sourcepath) + 2); // nullbyte and slash
|
||||
if (newbuf == NULL)
|
||||
continue; // can't bother
|
||||
if (newbuf == NULL) continue; // can't bother
|
||||
strcpy(newbuf, pw->pw_dir);
|
||||
newbuf[home_len] = '/'; // assume pw_dir doesn't start with '/' :P
|
||||
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);*/
|
||||
}
|
||||
|
||||
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 passwd* pw = getpwnam(user);
|
||||
if (pw == NULL) {
|
||||
@ -178,8 +178,7 @@ bool launch(char *user, char *passwd, struct session session, void (*cb)(void),
|
||||
if (pid == 0) { // child
|
||||
char* TERM = NULL;
|
||||
char* _GETTERM = getenv("TERM");
|
||||
if (_GETTERM != NULL)
|
||||
strcln(&TERM, _GETTERM);
|
||||
if (_GETTERM != NULL) strcln(&TERM, _GETTERM);
|
||||
if (clearenv() != 0) {
|
||||
print_errno("clearenv");
|
||||
_exit(EXIT_FAILURE);
|
||||
@ -222,8 +221,7 @@ bool launch(char *user, char *passwd, struct session session, void (*cb)(void),
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (cb != NULL)
|
||||
cb();
|
||||
if (cb != NULL) cb();
|
||||
|
||||
*reach_session = true;
|
||||
|
||||
|
@ -17,8 +17,7 @@ int chvt_str(char *str) {
|
||||
return -1;
|
||||
}
|
||||
// I'm not gonna elaborate on this....
|
||||
if (i > INT_MAX || i < INT_MIN || *err)
|
||||
return -1;
|
||||
if (i > INT_MAX || i < INT_MIN || *err) return -1;
|
||||
|
||||
return chvt((int)i);
|
||||
}
|
||||
|
21
src/config.c
21
src/config.c
@ -11,18 +11,18 @@
|
||||
// 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,
|
||||
// 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;
|
||||
if (blksize != NULL)
|
||||
opt_size = *blksize;
|
||||
if (blksize != NULL) opt_size = *blksize;
|
||||
|
||||
while (true) {
|
||||
size_t alloc_size = opt_size;
|
||||
char* buf = malloc(alloc_size);
|
||||
if (buf == NULL)
|
||||
return false;
|
||||
if (buf == NULL) return false;
|
||||
ssize_t read_size = getline(&buf, &alloc_size, fd);
|
||||
if (read_size == -1) {
|
||||
free(buf);
|
||||
@ -42,10 +42,8 @@ bool line_parser(FILE *fd, ssize_t *blksize,
|
||||
}
|
||||
if ((read = sscanf(buf, "%[^ ] = %[^\n]\n", key, value)) != 0) {
|
||||
u_char ret = cb(key, value);
|
||||
if (ret & 0b0100)
|
||||
free(key);
|
||||
if (ret & 0b0010)
|
||||
free(value);
|
||||
if (ret & 0b0100) free(key);
|
||||
if (ret & 0b0010) free(value);
|
||||
if (ret & 0b1000) {
|
||||
free(buf);
|
||||
return false;
|
||||
@ -154,8 +152,7 @@ struct config *parse_config(char *path) {
|
||||
__config->behavior.source = vec_new();
|
||||
__config->behavior.user_source = vec_new();
|
||||
|
||||
if (__config == NULL)
|
||||
return NULL;
|
||||
if (__config == NULL) return NULL;
|
||||
bool ret = line_parser(fd, (ssize_t*)&sb.st_blksize, config_line_handler);
|
||||
if (!ret) {
|
||||
free(__config);
|
||||
|
15
src/efield.c
15
src/efield.c
@ -22,16 +22,13 @@ void field_trim(struct editable_field *field, u_char pos) {
|
||||
|
||||
void field_update(struct editable_field* field, char* update) {
|
||||
u_char insert_len = strlen(update);
|
||||
if (insert_len == 0)
|
||||
return;
|
||||
if (insert_len == 0) return;
|
||||
|
||||
if (field->pos > field->length)
|
||||
field->pos = field->length; // WTF
|
||||
if (field->pos > field->length) field->pos = field->length; // WTF
|
||||
if (insert_len == 1) {
|
||||
// backspace
|
||||
if (*update == 127) {
|
||||
if (field->pos == 0)
|
||||
return;
|
||||
if (field->pos == 0) return;
|
||||
if (field->pos < field->length) {
|
||||
memmove(&field->content[field->pos - 1], &field->content[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
|
||||
bool field_seek(struct editable_field* field, char seek) {
|
||||
if (field->length == 0)
|
||||
return false;
|
||||
if (field->length == 0) return false;
|
||||
|
||||
if (seek < 0 && -seek > field->pos)
|
||||
field->pos = 0;
|
||||
@ -71,8 +67,7 @@ bool field_seek(struct editable_field *field, char seek) {
|
||||
else
|
||||
field->pos += seek;
|
||||
|
||||
if (field->pos > field->length)
|
||||
field->pos = field->length;
|
||||
if (field->pos > field->length) field->pos = field->length;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -12,8 +12,7 @@
|
||||
#include "util.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc == 2)
|
||||
chvt_str(argv[1]);
|
||||
if (argc == 2) chvt_str(argv[1]);
|
||||
|
||||
char* conf_override = getenv("LIDM_CONF");
|
||||
struct config* config =
|
||||
@ -28,6 +27,5 @@ int main(int argc, char *argv[]) {
|
||||
struct Vector sessions = get_avaliable_sessions();
|
||||
|
||||
int ret = load(&users, &sessions);
|
||||
if (ret == 0)
|
||||
execl(argv[0], argv[0], NULL);
|
||||
if (ret == 0) execl(argv[0], argv[0], NULL);
|
||||
}
|
||||
|
@ -19,8 +19,10 @@ static const struct source_dir sources[] = {
|
||||
{WAYLAND, "/usr/share/wayland-sessions"},
|
||||
};
|
||||
|
||||
static struct session __new_session(enum session_type type, char *name,
|
||||
const char *exec, const char *tryexec) {
|
||||
static struct session __new_session(enum session_type type,
|
||||
char* name,
|
||||
const char* exec,
|
||||
const char* tryexec) {
|
||||
struct session __session;
|
||||
__session.type = type;
|
||||
strcln(&__session.name, name);
|
||||
@ -36,8 +38,7 @@ static struct Vector *cb_sessions = NULL;
|
||||
// implement it
|
||||
static enum session_type session_type;
|
||||
static int fn(const char* fpath, const struct stat* sb, int typeflag) {
|
||||
if (sb == NULL || !S_ISREG(sb->st_mode))
|
||||
return 0;
|
||||
if (sb == NULL || !S_ISREG(sb->st_mode)) return 0;
|
||||
|
||||
/*printf("gonna open %s\n", fpath);*/
|
||||
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* 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
|
||||
// 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);
|
||||
@ -112,12 +114,9 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
|
||||
vec_push(cb_sessions, session_i);
|
||||
}
|
||||
|
||||
if (name_buf != NULL)
|
||||
free(name_buf);
|
||||
if (exec_buf != NULL)
|
||||
free(exec_buf);
|
||||
if (tryexec_buf != NULL)
|
||||
free(tryexec_buf);
|
||||
if (name_buf != NULL) free(name_buf);
|
||||
if (exec_buf != NULL) free(exec_buf);
|
||||
if (tryexec_buf != NULL) free(tryexec_buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
34
src/ui.c
34
src/ui.c
@ -133,8 +133,7 @@ static void ofield_toedit(struct opt_field *ofield, char *init) {
|
||||
ofield->efield = field_new(init);
|
||||
}
|
||||
static void ofield_type(struct opt_field* ofield, char* new, char* startstr) {
|
||||
if (ofield->current_opt != 0)
|
||||
ofield_toedit(ofield, startstr);
|
||||
if (ofield->current_opt != 0) ofield_toedit(ofield, startstr);
|
||||
field_update(&ofield->efield, new);
|
||||
}
|
||||
// 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)
|
||||
return false;
|
||||
if (ofield->opts == 0) return false;
|
||||
|
||||
ofield_opt_seek(ofield, seek);
|
||||
|
||||
@ -186,12 +184,9 @@ struct Vector *gsessions;
|
||||
|
||||
// not *that* OF tho
|
||||
struct opt_field* get_of(enum input from) {
|
||||
if (from == SESSION)
|
||||
return &of_session;
|
||||
if (from == USER)
|
||||
return &of_user;
|
||||
if (from == PASSWD)
|
||||
return &of_passwd;
|
||||
if (from == SESSION) return &of_session;
|
||||
if (from == USER) return &of_user;
|
||||
if (from == PASSWD) return &of_passwd;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -263,7 +258,9 @@ void print_field(enum input focused_input) {
|
||||
ffield_cursor_focus();
|
||||
}
|
||||
|
||||
void print_ffield() { print_field(focused_input); }
|
||||
void print_ffield() {
|
||||
print_field(focused_input);
|
||||
}
|
||||
void print_ofield(struct opt_field* ofield) {
|
||||
enum input input;
|
||||
if (ofield == &of_session)
|
||||
@ -291,8 +288,7 @@ void ffield_move(bool direction) {
|
||||
// tf I'm doing
|
||||
void ffield_change_opt(bool direction) {
|
||||
struct opt_field* ffield = get_of(focused_input);
|
||||
if (focused_input == PASSWD)
|
||||
ffield = &of_session;
|
||||
if (focused_input == PASSWD) ffield = &of_session;
|
||||
if (!ofield_opt_seek(ffield, direction ? 1 : -1)) {
|
||||
if (focused_input == PASSWD || focused_input == SESSION)
|
||||
ofield_opt_seek(&of_user, direction ? 1 : -1);
|
||||
@ -407,8 +403,7 @@ int load(struct Vector *users, struct Vector *sessions) {
|
||||
ffield_type(seq);
|
||||
}
|
||||
|
||||
if (esc != 0)
|
||||
esc--;
|
||||
if (esc != 0) esc--;
|
||||
}
|
||||
}
|
||||
|
||||
@ -424,7 +419,8 @@ static void clean_line(struct uint_point origin, uint line) {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
clean_line(origin, 5);
|
||||
const char* session_type;
|
||||
@ -458,7 +454,8 @@ static void print_session(struct uint_point origin, struct session session,
|
||||
}
|
||||
|
||||
// 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) {
|
||||
clean_line(origin, 7);
|
||||
printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm",
|
||||
@ -539,7 +536,8 @@ static void print_footer() {
|
||||
|
||||
uint row = window.ws_row - 1;
|
||||
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",
|
||||
theme.colors.e_key, theme.colors.fg, row, col,
|
||||
key_names[functions.poweroff], key_names[functions.reboot],
|
||||
|
@ -27,8 +27,7 @@ struct Vector get_human_users() {
|
||||
|
||||
struct passwd* pwd;
|
||||
while ((pwd = getpwent()) != NULL) {
|
||||
if (!(pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", 6) == 0))
|
||||
continue;
|
||||
if (!(pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", 6) == 0)) continue;
|
||||
|
||||
struct user* user_i = malloc(sizeof(struct user));
|
||||
*user_i = __new_user(pwd);
|
||||
|
15
src/util.c
15
src/util.c
@ -18,8 +18,7 @@ void strcln(char **dest, const char *source) {
|
||||
|
||||
enum keys find_keyname(char* name) {
|
||||
for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) {
|
||||
if (strcmp(key_names[i], name) == 0)
|
||||
return (enum keys)i;
|
||||
if (strcmp(key_names[i], name) == 0) return (enum keys)i;
|
||||
}
|
||||
|
||||
perror("key not found");
|
||||
@ -78,8 +77,7 @@ struct Vector vec_new() {
|
||||
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;
|
||||
if (vec->length > size) vec->length = size;
|
||||
vec->capacity = size;
|
||||
vec->pages = new_location;
|
||||
} else {
|
||||
@ -91,7 +89,8 @@ int vec_resize(struct Vector *vec, size_t size) {
|
||||
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...
|
||||
new_capacity = new_capacity + (new_capacity >> 1) +
|
||||
1; // cap * 1.5 + 1; 0 1 2 4 7 11...
|
||||
}
|
||||
return vec_resize(vec, new_capacity);
|
||||
}
|
||||
@ -132,15 +131,13 @@ void vec_reset(struct Vector *vec) {
|
||||
}
|
||||
|
||||
void* vec_pop(struct Vector* vec) {
|
||||
if (vec->length == 0)
|
||||
return NULL;
|
||||
if (vec->length == 0) return NULL;
|
||||
|
||||
return vec->pages[--vec->length];
|
||||
}
|
||||
|
||||
void* vec_get(struct Vector* vec, size_t index) {
|
||||
if (index >= vec->length)
|
||||
return NULL;
|
||||
if (index >= vec->length) return NULL;
|
||||
|
||||
return vec->pages[index];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user