summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrés <andmarti@gmail.com>2021-06-16 22:30:06 -0300
committerAndrés <andmarti@gmail.com>2021-06-16 22:30:06 -0300
commitce6d26d4e99a2794dacee3876dee639b70807510 (patch)
treec8de21e764b47fbc250f22e54076a121708f1b8c /src
parent793bc4899f6433690b714828423c5c298bdf0f79 (diff)
Show old and new value when chainging config variable
Diffstat (limited to 'src')
-rw-r--r--src/cmds/cmds_command.c20
-rw-r--r--src/conf.c82
-rw-r--r--src/conf.h1
-rw-r--r--src/help.c2
4 files changed, 86 insertions, 19 deletions
diff --git a/src/cmds/cmds_command.c b/src/cmds/cmds_command.c
index 7c29f1c..c20837f 100644
--- a/src/cmds/cmds_command.c
+++ b/src/cmds/cmds_command.c
@@ -914,25 +914,9 @@ void do_commandmode(struct block * sb) {
wcscpy(interp_line, inputline);
send_to_interp(interp_line);
+ // Change a config value
} else if ( ! wcsncmp(inputline, L"set ", 4) ) {
- wchar_t line [BUFFERSIZE];
- wcscpy(line, inputline);
- del_range_wchars(line, 0, 3);
-
- wchar_t * l;
- char oper[BUFFERSIZE];
- if ((l = wcschr(line, L' ')) != NULL) l[0] = L'\0';
- if ((l = wcschr(line, L'=')) != NULL) l[0] = L'\0';
-
- wcscpy(interp_line, inputline);
- send_to_interp(interp_line);
-
- wcstombs(oper, line, BUFFERSIZE);
- if (get_conf_value(oper)) {
- sc_info("Config value changed: %s", oper);
- } else if (strlen(oper) > 2 && ! wcsncmp(inputline, L"set no", 6)) {
- sc_info("Config value changed: %s", &oper[2]);
- }
+ change_config_parameter(inputline);
} else if ( ! wcsncmp(inputline, L"pad ", 4) ) {
int c = sh->curcol, cf = sh->curcol;
diff --git a/src/conf.c b/src/conf.c
index 615128d..5fa321b 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -50,6 +50,7 @@
#include <string.h>
#include <time.h>
#include "conf.h"
+#include "sc.h"
#include "utils/dictionary.h"
@@ -195,3 +196,84 @@ char * get_conf_value(const char * key) {
int get_conf_int(const char * key) {
return get_int(user_conf_d, key);
}
+
+
+/* \brief change_config_parameter
+ * parameter[in] char * cmd
+ * return int:
+ * 0 if config parameter changed
+ * 1 if config parameter was valid but previous and new values are the same
+ * -1 on error
+ */
+#include <wchar.h>
+#include <string.h>
+#include "macros.h"
+#include "cmds/cmds.h"
+#include "utils/string.h"
+#include "tui.h"
+int change_config_parameter(wchar_t * inputline) {
+ extern wchar_t interp_line[BUFFERSIZE];
+
+ // remove "set "
+ wchar_t line [BUFFERSIZE];
+ wcscpy(line, inputline);
+ del_range_wchars(line, 0, 3);
+
+ // parse value
+ wchar_t * l;
+ if ((l = wcschr(line, L' ')) != NULL) l[0] = L'\0';
+ if ((l = wcschr(line, L'=')) != NULL) l[0] = L'\0';
+
+ // check a proper config parameter exists
+ char oper[BUFFERSIZE];
+ wcstombs(oper, line, BUFFERSIZE);
+ // sent garbage after "set "..
+ if (! strlen(oper)) {
+ sc_error("Invalid command: \'%ls\'", inputline);
+ return -1;
+ }
+ char * value_bef = malloc(sizeof(char)*90);
+ value_bef[0] = '\0';
+ char * key = malloc(sizeof(char)*90);
+ key[0] = '\0';
+ char * value_aft = malloc(sizeof(char)*90);
+ value_aft[0] = '\0';
+
+ strcpy(key, oper);
+ char * s_aux = get_conf_value(key);
+ if (s_aux != NULL) strcpy(value_bef, get_conf_value(key));
+ if ((! value_bef || ! strlen(value_bef)) && strlen(oper) > 2 && ! wcsncmp(inputline, L"set no", 6)) {
+ s_aux = get_conf_value(&oper[2]);
+ if (s_aux != NULL) {
+ strcpy(value_bef, s_aux);
+ strcpy(key, &oper[2]);
+ }
+ }
+
+ if (! value_bef || ! strlen(value_bef)) {
+ sc_error("Invalid config variable: \'%s\'", oper);
+ free(value_aft);
+ free(value_bef);
+ free(key);
+ return -1;
+ }
+
+ // we try to change config value
+ wcscpy(interp_line, inputline);
+ send_to_interp(interp_line);
+ s_aux = get_conf_value(key);
+ if (s_aux != NULL) strcpy(value_aft, s_aux);
+ // check it was changed
+ if (! strcmp(value_bef, value_aft)) { sc_info("Config variable \'%s\' unchanged. Current value is \'%s\'", key, value_aft);
+ free(value_aft);
+ free(value_bef);
+ free(key);
+ return 1;
+ }
+ // inform so
+ sc_info("Config variable \'%s\' changed. Value \'%s\' to \'%s\'", key, value_bef, value_aft);
+ free(value_aft);
+ free(value_bef);
+ free(key);
+ return 0;
+}
diff --git a/src/conf.h b/src/conf.h
index d81343b..a9cdd20 100644
--- a/src/conf.h
+++ b/src/conf.h
@@ -47,4 +47,5 @@ extern struct dictionary * user_conf_d;
void store_default_config_values();
char * get_conf_value(const char * key);
int get_conf_int(const char * key);
+int change_config_parameter(wchar_t * inputline);
char * get_conf_values(char * salida);
diff --git a/src/help.c b/src/help.c
index 1967299..9eb3df9 100644
--- a/src/help.c
+++ b/src/help.c
@@ -594,4 +594,4 @@ void show_usage_and_quit(){
put(user_conf_d, "quit_afterload", "1");
}
-char * rev = "version 0.8.3-main";
+char * rev = "version 0.8.3-dev";