aboutsummaryrefslogtreecommitdiffstats
path: root/src/edit.c
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-11-17 19:49:52 +0200
committergramanas <anastasis.gramm2@gmail.com>2018-11-17 19:49:52 +0200
commit21c92f735ff52ff98b50f7f9d8e8ab9c46dad557 (patch)
tree3b244ec29c3b3ad96fc141461bff4d141a36227a /src/edit.c
parent8eb4df24eb30353bea82268440396e50ed8b1bbc (diff)
downloadck-21c92f735ff52ff98b50f7f9d8e8ab9c46dad557.tar.gz
ck-21c92f735ff52ff98b50f7f9d8e8ab9c46dad557.tar.bz2
ck-21c92f735ff52ff98b50f7f9d8e8ab9c46dad557.zip
Finish restructure and simplify include graph
Diffstat (limited to 'src/edit.c')
-rw-r--r--src/edit.c94
1 files changed, 90 insertions, 4 deletions
diff --git a/src/edit.c b/src/edit.c
index 1058d45..5179a54 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -1,11 +1,21 @@
+/* edit.c - the edit action --------------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
#include <libgen.h>
-#include "actions.h"
#include "dblayer.h"
-#include "queries.h"
#include "ckerrlog.h"
-int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret) {
+ERRLOG(edit);
+
+static int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret) {
int pid = get_program_id(db, pName);
/* error */
if (pid == -2) {
@@ -29,7 +39,7 @@ int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secr
return -1;
}
-int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec) {
+static int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int *sec) {
int pid = get_program_id(db, pName);
/* error */
if (pid == -2) {
@@ -89,3 +99,79 @@ int edit_get_config(DB *db, const char *pName, char *ret, const char *cName, int
}
return -1;
}
+
+int run_EDIT(UserOpt *opt, Conf *conf) {
+ DB db;
+ if (open_DB(&db, opt)) {
+ 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.");
+ print_suggested_configs(&db, pName);
+ 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);
+ print_suggested_configs(&db, pName);
+ goto error;
+ }
+ }
+ close_DB(&db);
+ str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName);
+
+ char *editor = getenv("EDITOR");
+ char command[STR_L] = "";
+ if (str_is_empty(editor)) {
+ if (system("which nano > /dev/null 2>&1") != 0) {
+ ERR("Nano not found. Please set $EDITOR to your desired editor.");
+ return -1;
+ }
+ strcpy(command, "nano");
+ } else {
+ strcpy(command, editor);
+ }
+
+
+ strcat(command, " ");
+ strcat(command, confPath);
+
+ HELP("editing...\n%s", command);
+ system(command);
+ return 0;
+ error:
+ close_DB(&db);
+ return -1;
+}
+
+void print_EDIT_help() {
+ HELP("ck edit PROGRAM_NAME [CONFIG_BASENAME]");
+}
+