more config and clang-tidy stuff

This commit is contained in:
theBreadCompany 2025-06-17 01:14:20 +02:00
parent 5a8e3a073a
commit d05b7735b2
9 changed files with 94 additions and 75 deletions

View file

@ -15,7 +15,7 @@ char *klist_config_key_to_string(KLIST_CONFIG_KEY key) {
return "";
}
KLIST_CONFIG_KEY klist_config_string_to_key(char *key) {
KLIST_CONFIG_KEY klist_config_string_to_key(const char *key) {
if (!strcmp(key, klist_config_key_to_string(CONFIG_DATABASE_NAME)))
return CONFIG_DATABASE_NAME;
return _CONFIG_KEY_COUNT;
@ -62,17 +62,17 @@ void klist_config_parse(klist_config *ctx, FILE *config) {
while ((c = fgetc(config)) != EOF) {
switch (c) {
case '\n':
ignore_line = true;
ignore_line = false;
if (!key)
continue;
if (!val) {
fprintf(stderr, "ignoring key '%s'", key);
}
if (!strcmp(key, "database")) {
ctx->db_file = malloc(strlen(val));
strcpy(ctx->db_file, val);
if (!strcmp(key, klist_config_key_to_string(CONFIG_DATABASE_NAME))) {
*ctx[CONFIG_DATABASE_NAME] = malloc((strlen(val) + 1) * sizeof(char));
strcpy(*ctx[CONFIG_DATABASE_NAME], val);
} else {
fprintf(stderr, "ignoring unknown key '%s'", key);
fprintf(stderr, "[init] ignoring unknown key '%s'", key);
}
free(key);
free(val);
@ -80,6 +80,8 @@ void klist_config_parse(klist_config *ctx, FILE *config) {
key = NULL, val = NULL;
break;
case '=':
if (ignore_line)
continue;
if (!found_key)
found_key = true;
else {
@ -94,9 +96,11 @@ void klist_config_parse(klist_config *ctx, FILE *config) {
if (ignore_line)
continue;
if (found_key) {
if (val)
val = realloc(val, ++val_len * sizeof(char));
else {
if (val) {
char *_val = realloc(val, ++val_len * sizeof(char));
if (_val)
val = _val;
} else {
val_len = 2;
val = malloc(val_len * sizeof(char));
}
@ -104,18 +108,23 @@ void klist_config_parse(klist_config *ctx, FILE *config) {
val[val_len - 1] = '\0';
} else {
if (key)
key = realloc(key, ++key_len * sizeof(char));
else {
if (key) {
char *_key = realloc(key, ++key_len * sizeof(char));
if (_key)
key = _key;
} else {
key_len = 2;
key = malloc(key_len * sizeof(char));
}
key[key_len - 2] = (char)c;
key[key_len - 1] = '\0';
}
}
}
if (key)
free(key);
if (val)
free(val);
}
void klist_config_setup(char *name, FILE *config) {
@ -142,6 +151,14 @@ void klist_config_setup(char *name, FILE *config) {
free(data_home);
// fseek(config, 0, SEEK_END);
fprintf(config, "# -- klist config file --\n"
"# please note that keys and values are case and space "
"sensitive, only key=value is parsable\n"
"# valid keys: ");
int i = 0;
for (; i < _CONFIG_KEY_COUNT; i++)
fprintf(config, "%s,", klist_config_key_to_string(i));
fprintf(config, "\n\n");
fprintf(config, "%s=%s/tasks.db\n",
klist_config_key_to_string(CONFIG_DATABASE_NAME), db_file);
fclose(config);

View file

@ -4,10 +4,10 @@
enum KLIST_CONFIG_KEY { CONFIG_DATABASE_NAME, _CONFIG_KEY_COUNT };
typedef enum KLIST_CONFIG_KEY KLIST_CONFIG_KEY;
struct klist_config {
char *db_file;
};
typedef struct klist_config klist_config;
typedef char *klist_config[_CONFIG_KEY_COUNT];
char *klist_config_key_to_string(KLIST_CONFIG_KEY key);
KLIST_CONFIG_KEY klist_config_string_to_key(const char *key);
char *klist_config_name(char *);
void klist_config_parse(klist_config *, FILE *);

View file

@ -2,16 +2,16 @@
#include "klist.h"
int klist_app_user_get(klist *);
int klist_app_user_create(klist *);
int klist_app_user_delete(klist *);
int klist_app_user_get(const klist *);
int klist_app_user_create(const klist *);
int klist_app_user_delete(const klist *);
int klist_app_list_add(klist *);
int klist_app_list_edit(klist *);
int klist_app_list_get(klist *);
int klist_app_list_delete(klist *);
int klist_app_list_add(const klist *);
int klist_app_list_edit(const klist *);
int klist_app_list_get(const klist *);
int klist_app_list_delete(const klist *);
int klist_app_task_add(klist *);
int klist_app_task_edit(klist *);
int klist_app_task_get(klist *);
int klist_app_task_delete(klist *);
int klist_app_task_add(const klist *);
int klist_app_task_edit(const klist *);
int klist_app_task_get(const klist *);
int klist_app_task_delete(const klist *);

View file

@ -12,7 +12,7 @@ void setup(klist *ctx, int argc, char **argv);
int main(int argc, char **argv) {
char *name = "klist";
klist_config *config = malloc(sizeof(klist_config));
klist_config config;
char *config_file_name = klist_config_name(name);
@ -23,11 +23,12 @@ int main(int argc, char **argv) {
} else
config_file = fopen(config_file_name, "a+");
klist_config_parse(config, config_file);
klist_config_parse(&config, config_file);
klist *ctx = klist_init(config->db_file);
klist *ctx = klist_init(config[CONFIG_DATABASE_NAME]);
free(config);
free(config_file_name);
free(config[CONFIG_DATABASE_NAME]);
if (argc == 1) {
print_help(ctx, argv);
@ -77,7 +78,7 @@ int main(int argc, char **argv) {
klist_task_context *task_ctx = ctx->cmd_ctx;
switch (((klist_task_context *)ctx->cmd_ctx)->cmd) {
case TASK_ADD: // basically the same things happen and edit can create if
// nescessary
// necessary
case TASK_EDIT:
klist_app_task_edit(ctx);
break;
@ -161,10 +162,13 @@ void setup(klist *ctx, int argc, char **argv) {
case 's':
char *stage = NULL;
while ((stage = strsep(&optarg, ","))) {
list_ctx->stages = realloc(list_ctx->stages,
char **stages = realloc(list_ctx->stages,
(++list_ctx->stages_len) * sizeof(char *));
if (stages) {
list_ctx->stages = stages;
list_ctx->stages[list_ctx->stages_len - 1] = strdup(stage);
}
}
break;
default:
print_help(ctx, argv);
@ -219,7 +223,8 @@ void setup(klist *ctx, int argc, char **argv) {
"-d\tdelete a task"
"Not providing a parameter prints all tasks of a list.");
}
if (!task_ctx->cmd) task_ctx->cmd = TASK_ADD;
if (!task_ctx->cmd)
task_ctx->cmd = TASK_ADD;
ctx->cmd_ctx = task_ctx;
break;
default:

View file

@ -4,11 +4,11 @@
#include <string.h>
#include <unistd.h>
int klist_app_user_get(klist *ctx) {
int klist_app_user_get(const klist *ctx) {
klist_user *user = klist_user_get_by_local(ctx, getuid());
if (user) {
printf("User: %s\nID: %lu\n", (char*)user->name, user->id);
printf("User: %s\nID: %lu\n", (char *)user->name, user->id);
size_t lists_len = 0;
klist_list **lists = klist_list_get_all_by_user(ctx, user->id, &lists_len);
printf("Lists: %lu\n", lists_len);
@ -20,7 +20,7 @@ int klist_app_user_get(klist *ctx) {
return 0;
}
int klist_app_user_create(klist *ctx) {
int klist_app_user_create(const klist *ctx) {
klist_print(ctx, KLIST_LOG_INFO,
(klist_assure_user(ctx, getuid(), getlogin()))
? "User created.\n"
@ -28,7 +28,7 @@ int klist_app_user_create(klist *ctx) {
return 0;
}
int klist_app_user_delete(klist *ctx) {
int klist_app_user_delete(const klist *ctx) {
klist_user *user = klist_user_get_by_local(ctx, getuid());
if (user) {
klist_user_delete(ctx, user);
@ -38,7 +38,7 @@ int klist_app_user_delete(klist *ctx) {
return 0;
}
int klist_app_list_add(klist *ctx) {
int klist_app_list_add(const klist *ctx) {
klist_list_context *list_ctx = ctx->cmd_ctx;
if (!list_ctx->stages_len) {
klist_print(ctx, KLIST_LOG_ERROR, "Missing stages.\n");
@ -67,12 +67,12 @@ int klist_app_list_add(klist *ctx) {
return 0;
}
int klist_app_list_edit(klist *ctx) {
int klist_app_list_edit(const klist *ctx) {
klist_print(ctx, KLIST_LOG_ERROR, "Not implemented\n");
return 0;
}
int klist_app_list_get(klist *ctx) {
int klist_app_list_get(const klist *ctx) {
klist_list_context *list_ctx = ctx->cmd_ctx;
klist_user *user = klist_user_get_by_local(ctx, getuid());
@ -99,9 +99,9 @@ int klist_app_list_get(klist *ctx) {
printf("Name: %s\n", (char *)list->name);
printf("Description: %s\n", list->desc ? (char *)list->desc : "N/A");
printf("Tasks: %ld\n", tasks_len);
i = 0;
for (; i < tasks_len; i++)
klist_task_deinit(tasks[i]);
int j = 0;
for (; j < tasks_len; j++)
klist_task_deinit(tasks[j]);
free(tasks);
klist_list_deinit(list);
}
@ -113,7 +113,7 @@ int klist_app_list_get(klist *ctx) {
return 0;
}
int klist_app_list_delete(klist *ctx) {
int klist_app_list_delete(const klist *ctx) {
klist_list_context *list_ctx = ctx->cmd_ctx;
klist_assure_user(ctx, getuid(), getlogin());
klist_user *user = klist_user_get_by_local(ctx, getuid());
@ -124,8 +124,6 @@ int klist_app_list_delete(klist *ctx) {
klist_list *list =
klist_list_get_by_user_and_name(ctx, user->id, list_ctx->name);
list = klist_list_get_by_user_and_name(ctx, user->id, list_ctx->name);
if (list) {
size_t tasks_len = 0;
klist_task **tasks = klist_task_get_for_list(ctx, list->id, &tasks_len);
@ -139,9 +137,9 @@ int klist_app_list_delete(klist *ctx) {
return 0;
}
int klist_app_task_add(klist *ctx) { return klist_app_task_edit(ctx); }
int klist_app_task_add(const klist *ctx) { return klist_app_task_edit(ctx); }
int klist_app_task_edit(klist *ctx) {
int klist_app_task_edit(const klist *ctx) {
klist_task_context *task_ctx = ctx->cmd_ctx;
klist_assure_user(ctx, getuid(), getlogin());
klist_user *user = klist_user_get_by_local(ctx, getuid());
@ -155,32 +153,29 @@ int klist_app_task_edit(klist *ctx) {
list = klist_list_get_by_user_and_name(ctx, user->id, task_ctx->list);
if (list) {
size_t stages_len = 0;
int i = 0;
klist_stage **stages =
klist_stage_get_all_for_list(ctx, list->id, &stages_len);
if (!task_ctx->stage) {
klist_print(ctx, KLIST_LOG_ERROR,
"Stage missing/wrong, please pass one of: ");
int i = 0;
for (; i < stages_len; i++)
for (i = 0; i < stages_len; i++)
fprintf(ctx->log_target, "%s ", (char *)stages[i]->name);
fprintf(ctx->log_target, "\n");
}
int i = 0;
ssize_t stage_id = -1;
for (; i < stages_len; i++)
long stage_id = -1;
for (i = 0; i < stages_len; i++)
if (strcmp((char *)stages[i]->name, task_ctx->stage) == 0)
stage_id = stages[i]->id;
if (stage_id == -1) {
klist_print(ctx, KLIST_LOG_ERROR,
"Stage %s not found. Use one of: ", task_ctx->stage);
i = 0;
for (; i < stages_len; i++)
for (i = 0; i < stages_len; i++)
fprintf(ctx->log_target, "%s ", (char *)stages[i]->name);
fprintf(ctx->log_target, "\n");
}
i = 0;
for (; i < stages_len; i++)
for (i = 0; i < stages_len; i++)
klist_stage_deinit(stages[i]);
free(stages);
@ -206,7 +201,7 @@ int klist_app_task_edit(klist *ctx) {
return 0;
}
int klist_app_task_get(klist *ctx) {
int klist_app_task_get(const klist *ctx) {
klist_task_context *task_ctx = ctx->cmd_ctx;
klist_assure_user(ctx, getuid(), getlogin());
klist_user *user = klist_user_get_by_local(ctx, getuid());
@ -247,7 +242,7 @@ int klist_app_task_get(klist *ctx) {
return 0;
}
int klist_app_task_delete(klist *ctx) {
int klist_app_task_delete(const klist *ctx) {
klist_task_context *task_ctx = ctx->cmd_ctx;
klist_assure_user(ctx, getuid(), getlogin());
klist_user *user = klist_user_get_by_local(ctx, getuid());
@ -258,7 +253,7 @@ int klist_app_task_delete(klist *ctx) {
klist_list *list =
klist_list_get_by_user_and_name(ctx, user->id, task_ctx->list);
if (list) {
if (task_ctx) {
if (task_ctx->name) {
klist_task *task =
klist_task_get_for_list_by_name(ctx, list->id, task_ctx->name);
if (task) {

View file

@ -74,5 +74,5 @@ void klist_task_delete(const klist *, const klist_task *);
void klist_task_deinit(klist_task *);
void klist_print_user(klist *, const klist_user *);
void klist_print_list(klist *, klist_list *, klist_task **, size_t);
void klist_print_list(const klist *, const klist_list *, klist_task **, size_t);
void klist_print_task(klist *, klist_task **, size_t);

View file

@ -93,7 +93,7 @@ void klist_sql_prepare(klist *, char *);
* assuring things
*/
bool klist_assure_user(klist *, __uid_t, char *);
bool klist_assure_user(const klist *, __uid_t, const char *);
void klist_assure_list(klist *, char *);
void klist_assure_task(klist *, char *);

View file

@ -383,7 +383,7 @@ void klist_print_user(klist *ctx, const klist_user *user) {
void print_table_line(char *left, size_t cols, char *filler, size_t col_width,
char *col_delim, char *right) {
printf("%s", left);
int i = 0;
int i;
for (i = 0; i < cols; i++) {
int j = 0;
for (; j < col_width; j++)
@ -394,8 +394,8 @@ void print_table_line(char *left, size_t cols, char *filler, size_t col_width,
printf("%s\n", right);
}
void klist_print_list(klist *ctx, klist_list *list, klist_task **tasks,
size_t tasks_len) {
void klist_print_list(const klist *ctx, const klist_list *list,
klist_task **tasks, size_t tasks_len) {
struct winsize w;
int i = 0;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
@ -421,8 +421,10 @@ void klist_print_list(klist *ctx, klist_list *list, klist_task **tasks,
if (task_length > max_task_length)
max_task_length = task_length;
}
u_int content_width = (int)((double)max_task_length * 1.6 * (double)stages_len);
if (content_width) width = content_width;
u_int content_width =
(int)((double)max_task_length * 1.6 * (double)stages_len);
if (content_width)
width = content_width;
size_t stage_col_width = (width - 3 - (stages_len - 1)) / stages_len;
int tasks_max = 0;
@ -436,7 +438,9 @@ void klist_print_list(klist *ctx, klist_list *list, klist_task **tasks,
// list name
printf("");
printf(" %s", (char *)list->name);
for (i = 0; i < (stages_len * stage_col_width) - (strlen((char *)list->name + 1)); i++)
for (i = 0;
i < (stages_len * stage_col_width) - (strlen((char *)list->name + 1));
i++)
printf(" ");
printf("\n");

View file

@ -129,7 +129,7 @@ void klist_sql_seed(klist *ctx) {}
* assuring things
*/
bool klist_assure_user(klist *ctx, __uid_t id, char *name) {
bool klist_assure_user(const klist *ctx, const __uid_t id, const char *name) {
klist_user *user = klist_user_get_by_local(ctx, id);
bool created = false;
if (!user) {
@ -143,8 +143,6 @@ bool klist_assure_user(klist *ctx, __uid_t id, char *name) {
klist_user_deinit(user);
return created;
}
void klist_assure_list(klist *, char *);
void klist_assure_task(klist *, char *);
/*
* printing methods