diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-11-18 20:58:35 +0200 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-11-18 20:58:35 +0200 |
commit | 7d10e2377b01ac3e38b412f6bc2fa2e384804057 (patch) | |
tree | d89d0139b17786496e02335412114e2f97f82594 /src/edit.c | |
parent | a9df05320a071112a41654abc761ee1389400782 (diff) | |
download | ck-7d10e2377b01ac3e38b412f6bc2fa2e384804057.tar.gz ck-7d10e2377b01ac3e38b412f6bc2fa2e384804057.tar.bz2 ck-7d10e2377b01ac3e38b412f6bc2fa2e384804057.zip |
Update edit and add edit test
Diffstat (limited to 'src/edit.c')
-rw-r--r-- | src/edit.c | 181 |
1 files changed, 143 insertions, 38 deletions
@@ -81,7 +81,6 @@ static int edit_get_config(DB *db, const char *pName, char *ret, const char *cNa } } free(tmp); - break; } else { /* Since we are here, it means there is only 1 config for the selected program */ @@ -100,78 +99,184 @@ static int edit_get_config(DB *db, const char *pName, char *ret, const char *cNa return -1; } +static EditOpt edit_make_options(cklist *args) { + list_rewind(args); + + EditOpt editOpt = { + .pName = NULL, + .cBasename = NULL, + .editor = NULL, + .cmd = NULL, + .sudo = 0, + .err = 0 + }; + + if (list_size(args)) { + editOpt.pName = list_get(args); + } + else { + editOpt.err = 1; + sERR("Please specify the program to edit.") + goto done; + } + + if (list_next(args)) { + /* if the next argument doesn't start with `-` treat it as + a config basename */ + if (strncmp("-", list_get(args), strlen("-")) != 0) { + editOpt.cBasename = list_get(args); + if (!list_next(args)) { + goto done; + } + } + do { + if (strcmp(list_get(args), "-s") == 0) { + editOpt.sudo = 1; + } + else if (strcmp(list_get(args), "--editor") == 0) { + if (editOpt.cmd) { + sERR("Use only one of --command and --editor"); + editOpt.err = 1; + break; + } + if (!list_next(args)) { + sERR("Specify the editor to use.") + editOpt.err = 1; + break; + } + editOpt.editor = list_get(args); + } + else if (strcmp(list_get(args), "--command") == 0) { + if (editOpt.editor) { + sERR("Use only one of --command and --editor"); + editOpt.err = 1; + break; + } + if (!list_next(args)) { + sERR("Specify the command to use.") + editOpt.err = 1; + break; + } + editOpt.cmd = list_get(args); + } + else { + editOpt.err = 1; + ERR("Unknown argument %s", list_get(args)); + break; + } + } while(list_next(args)); + } + done: + list_rewind(args); + return editOpt; +} + +static char * form_edit_command(char *ret, const EditOpt *editOpt, const char *confPath) { + char command[STR_L] = ""; + + if (editOpt->editor) { + char tmp[STR_L] = "which "; + strcat(tmp, editOpt->editor); + strcat(tmp, " > /dev/null 2>&1"); + if (system(tmp) != 0) { + ERR("Editor %s not found.", editOpt->editor); + HELP("If you want to run the command either way use --command instead of --editor.") + return NULL; + } + else { + strcpy(command, editOpt->editor); + } + } + else if (editOpt->cmd) { + strcpy(command, editOpt->cmd); + } + else { + char *editor = getenv("EDITOR"); + if (str_is_empty(editor)) { + if (system("which emacs > /dev/null 2>&1") != 0) { + ERR("No editor avaliable."); + HELP("Emacs not found. Please set $EDITOR to your desired editor,"); + HELP("or use the --editor flag.") + return NULL; + } + strcpy(command, "nano"); + } + else { + strcpy(command, editor); + } + } + + strcat(command, " "); + strcat(command, confPath); + + if (editOpt->sudo) { + char tmp[STR_L] = "sudo "; + strcat(tmp, command); + return strcpy(ret, tmp); + } + + return strcpy(ret, command); +} + int run_EDIT(UserOpt *opt, Conf *conf) { + char confPath[STR_L] = ""; + char confName[STR_M] = ""; + int secret = 0; 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); + EditOpt editOpt = edit_make_options(opt->args); + if (editOpt.err) { + goto error; + } + + if (!program_exists(&db, editOpt.pName)) { + ERR("Program %s doesn't exist in the database.", editOpt.pName); goto error; } - /* If there is no next argument */ - if (!list_next(opt->args)) { + /* If there is no config basename */ + if (!editOpt.cBasename) { /* If there is no primary config*/ - if (edit_get_prime_config_from_program(&db, pName, confName, &secret) == -1) { + if (edit_get_prime_config_from_program(&db, editOpt.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); + if (get_config_number(&db, editOpt.pName) == 1) { + if (edit_get_config(&db, editOpt.pName, confName, NULL, &secret)) { + ERR("Coudln't find config file for %s", editOpt.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); + print_suggested_configs(&db, editOpt.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); + if (edit_get_config(&db, editOpt.pName, confName, editOpt.cBasename, &secret)) { + ERR("Program %s doesn't have a config named %s", editOpt.pName, editOpt.cBasename); + print_suggested_configs(&db, editOpt.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); + if (form_edit_command(command, &editOpt, confPath)) { + hLOG("Running: %s", command); + return system(command); } - - - 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]"); + HELP("ck edit PROGRAM_NAME [CONFIG_BASENAME] [--editor EDITOR] [--command COMMAND] [-s]"); } |