aboutsummaryrefslogtreecommitdiffstats
path: root/src/actions.c
diff options
context:
space:
mode:
authorAnastasis Grammenos <anastasis.gramm2@gmail.com>2018-10-08 23:47:07 +0300
committerAnastasis Grammenos <anastasis.gramm2@gmail.com>2018-10-08 23:47:07 +0300
commitfe781e5ae7116733e5b335a0ac016af97266db5f (patch)
treed5cf6ebfc27b84745df5d6e3db04b04d0b524706 /src/actions.c
parentf67f2c3d5b67f54dae2c2c2754f43b4351445c80 (diff)
downloadck-fe781e5ae7116733e5b335a0ac016af97266db5f.tar.gz
ck-fe781e5ae7116733e5b335a0ac016af97266db5f.tar.bz2
ck-fe781e5ae7116733e5b335a0ac016af97266db5f.zip
Way better Edit
Diffstat (limited to 'src/actions.c')
-rw-r--r--src/actions.c102
1 files changed, 62 insertions, 40 deletions
diff --git a/src/actions.c b/src/actions.c
index d5f9a29..4ff6816 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -102,7 +102,6 @@ int run_DEL(UserOpt * opt, Conf *conf) {
}
int run_EDIT(UserOpt *opt, Conf *conf) {
- printf("Running %s\n", "edit");
DB db = open_DB(opt);
if (db.ptr == NULL) {
if (db.error == SQL_ERR_NO_TABLES) {
@@ -112,31 +111,52 @@ int run_EDIT(UserOpt *opt, Conf *conf) {
}
list_rewind(opt->args);
- char confPath[STR_M];
- if (list_size(opt->args) == 1) {
- char confName[STR_M];
- int secret = 0;
- if (edit_get_prime_config_from_program(&db, list_get(opt->args), confName, &secret) == 1) {
- str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName);
- printf("%s\n", confPath);
- } else {
- PRINT_ERR("No primary config");
- close_DB(&db);
- return 1;
+ 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;
+ }
}
- } else {
- close_DB(&db);
- char confName[STR_L];
- switch (edit_get_config_or_suggestions(opt->args, confName)) {
- case ERC_OK:
- return 0;
- case ERC_ERR:
- return 1;
- case ERC_SUGGESTIONS:
- return 1;
+ }
+ /* 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];
@@ -145,6 +165,9 @@ int run_EDIT(UserOpt *opt, Conf *conf) {
strcat(command, confPath);
system(command);
return 0;
+ error:
+ close_DB(&db);
+ return 1;
}
int run_LIST(UserOpt *opt, Conf *conf) {
@@ -157,45 +180,44 @@ int run_LIST(UserOpt *opt, Conf *conf) {
return 1;
}
- cklist *list_type = list_make_new();
+ cklist *the_list = list_make_new();
ListOpt listOpt = list_make_options(opt->args);
if (listOpt.err) {
- close_DB(&db);
- list_free(list_type);
- return 1;
+ goto error;
}
switch(listOpt._lt) {
case LT_PATH:
- list_get_paths(&db, list_type, listOpt.attr);
+ list_get_paths(&db, the_list, listOpt.attr);
break;
case LT_PROGRAM:
- list_get_programs(&db, list_type);
+ list_get_programs(&db, the_list);
break;
case LT_TREE:
- list_get_path_program_tree(&db, list_type, listOpt.attr);
- list_print(list_type);
- close_DB(&db);
- list_free(list_type);
- return 0;
+ list_get_path_program_tree(&db, the_list, listOpt.attr);
+ list_print(the_list);
+ goto close;
case LT_NONE:
- close_DB(&db);
- list_free(list_type);
- return 1;
+ goto error;
}
switch(listOpt._lst) {
case LST_PLAIN:
- list_print(list_type);
+ list_print(the_list);
break;
case LST_LISP:
- list_print_lisp(list_type);
+ list_print_lisp(the_list);
break;
case LST_PYTHON:
- list_print_python(list_type);
+ list_print_python(the_list);
}
+ close:
close_DB(&db);
- list_free(list_type);
+ list_free(the_list);
return 0;
+ error:
+ close_DB(&db);
+ list_free(the_list);
+ return 1;
}
FILE *popen(const char *command, const char *mode);