#include #include #include #include #include "models.h" #include "util.h" void print_help(char **argv); void setup(klist *ctx, int argc, char **argv); int main(int argc, char **argv) { klist *ctx = klist_init("test.db"); if (argc == 1) { print_help(argv); ctx->error = 1; } if (!ctx->error) { setup(ctx, argc, argv); klist_user *user = klist_user_get_by_local(ctx, getuid()); klist_list *list = NULL; int i = 0; switch (ctx->cmd) { case USER: klist_user_context *user_ctx = ctx->cmd_ctx; switch (user_ctx->cmd) { case USER_GET: if (user) { fprintf(stderr, "User: %p\nID: %lu\n", user->name, user->id); size_t lists_len = 0; klist_list **lists = klist_list_get_all_by_user(ctx, user->id, &lists_len); fprintf(stderr, "Lists: %lu\n", lists_len); for (; i < lists_len; i++) klist_list_deinit(lists[i]); } else fprintf(stderr, "No user for '%s' found.\n", getlogin()); break; case USER_CREATE: fprintf(stderr, (klist_assure_user(ctx, getuid(), getlogin())) ? "User created.\n" : "User already exists.\n"); break; case USER_DELETE: if (user) { klist_user_delete(ctx, user); fprintf(stderr, "User deleted.\n"); } else fprintf(stderr, "User not found, no changes done.\n"); break; } klist_user_context_deinit(user_ctx); break; case LIST: klist_list_context *list_ctx = ctx->cmd_ctx; if (!list_ctx->name && list_ctx->cmd != LIST_GET) { fprintf(stderr, "Missing name.\n"); break; } switch (list_ctx->cmd) { case LIST_ADD: if (!list_ctx->stages_len) { fprintf(stderr, "Missing stages.\n"); break; } list = klist_list_init(); list->name = (unsigned char *)list_ctx->name; list->desc = (unsigned char *)list_ctx->desc; list->is_preset = false; klist_list_save(ctx, list); i = 0; for (; i < list_ctx->stages_len; i++) { klist_stage *stage = klist_stage_init(); stage->name = (unsigned char *)list_ctx->stages[i]; stage->list_id = list->id; klist_stage_save(ctx, stage); klist_stage_deinit(stage); } break; case LIST_EDIT: fprintf(stderr, "Not implemented\n"); break; case LIST_GET: size_t lists_len = 0; klist_list **lists = NULL; if (list_ctx->name) { klist_list *list = klist_list_get_by_user_and_name(ctx, user->id, list_ctx->name); if (list) { lists_len = 1; lists = &list; }; } else lists = klist_list_get_all_by_user(ctx, user->id, &lists_len); if (lists) { i = 0; for (; i < lists_len; i++) { klist_list *list = lists[i]; size_t tasks_len = 0; klist_task **tasks = klist_task_get_for_list(ctx, list->id, &tasks_len); fprintf(stderr, "Name: %s\nDescription: %s\nTasks: %ld\n", (char *)list->name, (char *)list->desc, tasks_len); i = 0; for (; i < tasks_len; i++) klist_task_deinit(tasks[i]); free(tasks); } } else if (list_ctx->name) fprintf(stderr, "List '%s' not found.\n", list_ctx->name); else fprintf(stderr, "No lists found.\n"); break; case LIST_DELETE: 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); i = 0; for (; i < tasks_len; i++) { klist_task_delete(ctx, tasks[i]); klist_task_deinit(tasks[i]); } free(tasks); } } if (list_ctx) klist_list_context_deinit(list_ctx); break; case TASK: 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 case TASK_EDIT: klist_assure_user(ctx, getuid(), getlogin()); user = klist_user_get_by_local(ctx, getuid()); list = klist_list_get_by_user_and_name(ctx, user->id, task_ctx->list); if (list) { size_t stages_len = 0; klist_stage **stages = klist_stage_get_all_for_list(ctx, list->id, &stages_len); if (!task_ctx->stage) { fprintf(stderr, "Stage missing/wrong, please pass one of: "); i = 0; for (; i < stages_len; i++) fprintf(stderr, "%s ", (char *)stages[i]->name); fprintf(stderr, "\n"); break; } i = 0; ssize_t stage_id = -1; for (; i < stages_len; i++) if (strcmp((char *)stages[i]->name, (char *)task_ctx->stage) == 0) stage_id = stages[i]->id; if (stage_id == -1) { fprintf(stderr, "Stage %s not found. Use one of: ", (char *)task_ctx->stage); i = 0; for (; i < stages_len; i++) fprintf(stderr, "%s ", (char *)stages[i]->name); fprintf(stderr, "\n"); } i = 0; for (; i < stages_len; i++) klist_stage_deinit(stages[i]); free(stages); if (ctx->error) break; klist_task *task = klist_task_get_for_list_by_name(ctx, list->id, task_ctx->name); if (!task) task = klist_task_init(); task->name = (unsigned char *)task_ctx->name; if (task_ctx->desc) task->desc = (unsigned char *)task_ctx->desc; task->stage_id = stage_id; klist_task_save(ctx, task); klist_task_deinit(task); } else fprintf(stderr, "List not found.\n"); break; case TASK_GET: klist_assure_user(ctx, getuid(), getlogin()); user = klist_user_get_by_local(ctx, getuid()); list = klist_list_get_by_user_and_name(ctx, user->id, task_ctx->list); if (list) { if (task_ctx->name) { klist_task *task = klist_task_get_for_list_by_name(ctx, list->id, task_ctx->name); if (task) { fprintf(stderr, "Name: %s\n", (char *)task->name); fprintf(stderr, "Description: %s\n", (char *)task->desc); klist_stage *stage = klist_stage_get_by_id(ctx, task->stage_id); fprintf(stderr, "Stage: %s\n", (char *)stage->name); klist_stage_deinit(stage); klist_task_deinit(task); } else fprintf(stderr, "Task not found.\n"); } else { size_t tasks_len = 0; klist_task **tasks = klist_task_get_for_list(ctx, list->id, &tasks_len); i = 0; for (; i < tasks_len; i++) { fprintf(stderr, "Name: %s\n", (char *)tasks[i]->name); fprintf(stderr, "Description: %s\n", (char *)tasks[i]->desc); klist_stage *stage = klist_stage_get_by_id(ctx, tasks[i]->stage_id); fprintf(stderr, "Stage: %s\n", (char *)stage->name); klist_stage_deinit(stage); klist_task_deinit(tasks[i]); if ((i + 1) < tasks_len) fprintf(stderr, "\n"); } } } else fprintf(stderr, "List not found"); case TASK_DELETE: klist_assure_user(ctx, getuid(), getlogin()); user = klist_user_get_by_local(ctx, getuid()); list = klist_list_get_by_user_and_name(ctx, user->id, task_ctx->list); if (list) { if (task_ctx) { klist_task *task = klist_task_get_for_list_by_name(ctx, list->id, task_ctx->name); if (task) { fprintf(stderr, "Deleting task '%s'.\n", (char *)task->name); klist_task_delete(ctx, task); klist_task_deinit(task); } else fprintf(stderr, "Task not found.\n"); } else fprintf(stderr, "Task not found, only deletion by name is " "currently supoprted.\n"); } else fprintf(stderr, "Not implemented\n"); } klist_task_context_deinit(task_ctx); default:; } if (list) klist_list_deinit(list); if (user) klist_user_deinit(user); } const int error = ctx->error; klist_deinit(ctx); return error; } void print_help(char **argv) { fprintf(stderr, "Usage: %s \n", argv[0]); } void setup(klist *ctx, int argc, char **argv) { if (strcmp(argv[1], "user") == 0) ctx->cmd = USER; else if (strcmp(argv[1], "list") == 0) ctx->cmd = LIST; else if (strcmp(argv[1], "task") == 0) ctx->cmd = TASK; else print_help(argv); int opt; switch (ctx->cmd) { case USER: optind = 2; klist_user_context *user_ctx = klist_user_context_init(); while ((opt = getopt(argc, argv, "cdg")) != -1) switch (opt) { case 'c': user_ctx->cmd = USER_CREATE; break; case 'd': user_ctx->cmd = USER_DELETE; break; case 'g': user_ctx->cmd = USER_GET; break; default: print_help(argv); } ctx->cmd_ctx = user_ctx; break; case LIST: klist_list_context *list_ctx = klist_list_context_init(); ctx->cmd_ctx = list_ctx; if (argc < 3) { list_ctx->cmd = LIST_GET; break; } optind = 3; while ((opt = getopt(argc, argv, "aden:p:s:")) != -1) switch (opt) { case 'a': list_ctx->cmd = LIST_ADD; break; case 'e': list_ctx->cmd = LIST_EDIT; break; case 'd': list_ctx->cmd = LIST_DELETE; break; case 'p': list_ctx->preset = malloc((strlen(optarg) + 1) * sizeof(char)); strcpy(list_ctx->preset, optarg); break; case 'n': list_ctx->name = malloc((strlen(optarg) + 1) * sizeof(char)); strcpy(list_ctx->name, optarg); break; default: print_help(argv); fprintf( stderr, "" "list options:\n" "-a\t\tadd a list\n" "-e\t\tedit a list\n" "-d\t\tdelete a list\n" "-p \tdefine as preset or use existing one with name " "\n" "Not providing a parameter prints all lists and existing presets.\n\n"); } break; case TASK: if (argc < 3) { fprintf(stderr, "Prints all lists.\n"); break; } klist_task_context *task_ctx = malloc(sizeof(struct klist_task_context)); optind = 3; while ((opt = getopt(argc, argv, "aeds:")) != -1) switch (opt) { case 'a': task_ctx->cmd = TASK_ADD; break; case 'e': task_ctx->cmd = TASK_EDIT; break; case 'd': task_ctx->cmd = TASK_DELETE; break; default: print_help(argv); fprintf(stderr, "" "task options:" "-a\tadd a task" "-e\tedit a task" "-d\tdelete a task" "Not providing a parameter prints all tasks of a list."); } ctx->cmd_ctx = task_ctx; break; default: fprintf(stderr, "How did we land here?! Pls report argv[1] = %s\n", argv[1]); print_help(argv); } }