#include #include #include #include #include "config.h" #include "process.h" #include "util.h" void print_help(klist *ctx, char **argv); void setup(klist *ctx, int argc, char **argv); int main(int argc, char **argv) { char *name = argv[0]; char *config_home = getenv("XDG_CONFIG_HOME"); int config_file_len = snprintf(NULL, 0, "%s/%s", config_home, name); char *config_file_name = malloc(config_file_len * sizeof(char)); snprintf(config_file_name, config_file_len, "%s/%s", config_home, name); klist_config *config = malloc(sizeof(klist_config)); FILE *config_file = NULL; if (access(config_file_name, F_OK) == -1) { config_file = fopen(config_file_name, "w"); klist_config_setup(argv[0], config_file); } else config_file = fopen(config_file_name, "a+"); klist_config_parse(config, config_file); klist *ctx = klist_init(config->db_file); free(config); if (argc == 1) { print_help(ctx, argv); ctx->error = 1; } if (!ctx->error) { setup(ctx, argc, argv); switch (ctx->cmd) { case USER: klist_user_context *user_ctx = ctx->cmd_ctx; switch (user_ctx->cmd) { case USER_GET: klist_app_user_get(ctx); break; case USER_CREATE: klist_app_user_create(ctx); break; case USER_DELETE: klist_app_user_delete(ctx); 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) { klist_print(ctx, KLIST_LOG_ERROR, "Missing name.\n"); break; } switch (list_ctx->cmd) { case LIST_ADD: klist_app_list_add(ctx); break; case LIST_EDIT: break; case LIST_GET: klist_app_list_get(ctx); break; case LIST_DELETE: klist_app_list_delete(ctx); } 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_app_task_edit(ctx); break; case TASK_GET: klist_app_task_get(ctx); break; case TASK_DELETE: klist_app_task_delete(ctx); } klist_task_context_deinit(task_ctx); default:; } } const int error = ctx->error; klist_deinit(ctx); return error; } void print_help(klist *ctx, char **argv) { printf("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(ctx, 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(ctx, 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 = 2; 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 = strdup(optarg); break; case 'n': list_ctx->name = strdup(optarg); break; case 's': char *stage = NULL; while ((stage = strsep(&optarg, ","))) { list_ctx->stages = realloc(list_ctx->stages, (++list_ctx->stages_len) * sizeof(char *)); list_ctx->stages[list_ctx->stages_len - 1] = strdup(stage); } break; default: print_help(ctx, argv); klist_print( ctx, KLIST_LOG_ERROR, "" "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: klist_task_context *task_ctx = klist_task_context_init(ctx); optind = 2; while ((opt = getopt(argc, argv, "aedgl:n:s:")) != -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; case 'g': task_ctx->cmd = TASK_GET; break; case 'l': task_ctx->list = strdup(optarg); break; case 'n': task_ctx->name = strdup(optarg); break; case 's': task_ctx->stage = strdup(optarg); break; default: print_help(ctx, argv); klist_print(ctx, KLIST_LOG_ERROR, "" "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: klist_print(ctx, KLIST_LOG_ERROR, "How did we land here?! Pls report argv[1] = %s\n", argv[1]); print_help(ctx, argv); } }