/* actions.c - ck actions ----------------------------------------------*- C -*- * * This file is part of ck, the config keeper * * ----------------------------------------------------------------------------- * * Copyright (C) 2018 Anastasis Grammenos * GPLv3 (see LICENCE for the full notice) * * -------------------------------------------------------------------------- */ #include #include #include "actions.h" #include "actionhelper.h" #include "dblayer.h" #include "ckutil.h" #include "cklist.h" #include "ckerrlog.h" ERRLOG(action); int run_INIT(UserOpt * opt, Conf *conf) { UNUSED(conf); if (db_exists(opt)) { printf("Current configuration file location: %s\n", opt->confDir); PRINT_ERR("ck is already initialized."); return 1; } if (init_create_config_file(opt)) { return 1; } DB db = init_make_DB(opt); if (db.error == SQL_NO_ERR) { init_make_tables(&db); } sqlite3_close(db.ptr); return 0; } int run_ADD(UserOpt * opt, Conf *conf) { DB db = open_DB(opt); if (db.ptr == NULL) { if (db.error == SQL_ERR_NO_TABLES) { PRINT_ERR("The database file is currupted. Run ck init anew."); } goto error; } AddOpt addOpt = add_make_options(opt->args); switch (addOpt.err) { case ADD_NO_ERR: break; case ADD_ERR_WRONG_CONFIG: ERR("The config file specified doesn't exist or is a link."); goto error; case ADD_ERR_WRONG_FLAGS: ERR("Flags are: -s for secret and -p for primary."); goto error; } add_print_opts(&addOpt); /* Try adding the new config to the DB */ if (add_transaction_try(&db, &addOpt)) { goto error; } add_make_link(&addOpt, conf); char err[STR_M]; if (add_err_message(err)) { PRINT_ERR(err); error: close_DB(&db); return 1; } close_DB(&db); return 0; } int run_DEL(UserOpt * opt, Conf *conf) { printf("Running del\n"); DB db = open_DB(opt); if (db.ptr == NULL) { if (db.error == SQL_ERR_NO_TABLES) { PRINT_ERR("The database file is currupted. Run ck init anew."); } goto error; } DelOpt delOpt = del_make_options(opt->args); switch(delOpt.err) { case DEL_NO_ERR: del_transaction_try(&db, delOpt.arg, delOpt.isConf); close_DB(&db); return 0; case DEL_ERR_WRONG_ARGS: printf("wrong del args\n"); break; case DEL_ERR_WRONG_PATH: printf("path %s doesnt exist\n", delOpt.arg); } error: close_DB(&db); return 1; } int run_EDIT(UserOpt *opt, Conf *conf) { DB db = open_DB(opt); if (db.ptr == NULL) { if (db.error == SQL_ERR_NO_TABLES) { PRINT_ERR("The database file is currupted. Run ck init anew."); } return 1; } list_rewind(opt->args); char confPath[STR_L]; char confName[STR_M]; int secret = 0; /* Since we are here, args have to be 1 or 2 */ char *pName = list_get(opt->args); if (!program_exists(&db, pName)) { ERR("Program %s doesn't exist in the database.", pName); goto error; } /* If there is no next argument */ if (!list_next(opt->args)) { /* If there is no primary config*/ if (edit_get_prime_config_from_program(&db, pName, confName, &secret) == -1) { /* If the program has only one config */ if (get_config_number(&db, pName) == 1) { if (edit_get_config(&db, pName, confName, NULL, &secret)) { ERR("Coudln't find config file for %s", pName); goto error; } } /* If the program has many configs */ else { HELP("Ambiguous config. Please type the config name after the program."); cklist *paths = list_make_new(); edit_get_avaliable_paths(&db, paths, pName); list_print(paths); list_free(paths); goto error; } } } /* If there are more arguments */ else { char *cName = list_get(opt->args); if (edit_get_config(&db, pName, confName, cName, &secret)) { ERR("Program %s doesn't have a config named %s", pName, cName); cklist *paths = list_make_new(); edit_get_avaliable_paths(&db, paths, pName); list_print(paths); list_free(paths); goto error; } } close_DB(&db); str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName); HELP("Editing %s", confPath); char *editor = getenv("EDITOR"); char command[STR_L]; strcpy(command, str_is_empty(editor) ? "nano" : editor); strcat(command, " "); strcat(command, confPath); system(command); return 0; error: close_DB(&db); return 1; } int run_LIST(UserOpt *opt, Conf *conf) { UNUSED(conf); DB db = open_DB(opt); if (db.ptr == NULL) { if (db.error == SQL_ERR_NO_TABLES) { printf("no tables\n"); } return 1; } cklist *the_list = list_make_new(); ListOpt listOpt = list_make_options(opt->args); if (listOpt.err) { goto error; } switch(listOpt._lt) { case LT_PATH: list_get_paths(&db, the_list, listOpt.attr); break; case LT_PROGRAM: list_get_programs(&db, the_list); break; case LT_TREE: list_get_path_program_tree(&db, the_list, listOpt.attr); list_print(the_list); goto close; case LT_NONE: goto error; } switch(listOpt._lst) { case LST_PLAIN: list_print(the_list); break; case LST_LISP: list_print_lisp(the_list); break; case LST_PYTHON: list_print_python(the_list); } close: close_DB(&db); list_free(the_list); return 0; error: close_DB(&db); list_free(the_list); return 1; } FILE *popen(const char *command, const char *mode); int pclose(FILE *stream); int run_SEARCH(UserOpt *opt, Conf *conf) { if (system("which grep > /dev/null") != 0) { return 2; } DB db = open_DB(opt); cklist *paths = list_make_new(); list_get_paths(&db, paths, 0); close_DB(&db); if (list_size(paths) && list_size(opt->args)) { do { FILE *cmd; char result[STR_L] = ""; char command[STR_L] = "grep -H -n \""; strcat(command, list_get(opt->args)); strcat(command, "\" "); strcat(command, list_get(paths)); cmd = popen(command, "r"); if (cmd == NULL) { list_free(paths); return 1; } while (fgets(result, sizeof(result), cmd)) { printf("%s", result); } pclose(cmd); } while(list_next(paths)); } list_free(paths); return 0; } int run_HELP(UserOpt *opt, Conf *conf) { printf("Running %s\n", "help"); return 1; }