diff options
Diffstat (limited to 'src/nvim/cmdhist.c')
-rw-r--r-- | src/nvim/cmdhist.c | 79 |
1 files changed, 38 insertions, 41 deletions
diff --git a/src/nvim/cmdhist.c b/src/nvim/cmdhist.c index 2df82d9355..4556b74396 100644 --- a/src/nvim/cmdhist.c +++ b/src/nvim/cmdhist.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - // cmdhist.c: Functions for the history of the command-line. #include <assert.h> @@ -10,24 +7,25 @@ #include <stdio.h> #include <string.h> -#include "nvim/ascii.h" +#include "nvim/ascii_defs.h" #include "nvim/charset.h" +#include "nvim/cmdexpand_defs.h" #include "nvim/cmdhist.h" #include "nvim/eval/typval.h" #include "nvim/ex_cmds.h" #include "nvim/ex_cmds_defs.h" #include "nvim/ex_getln.h" +#include "nvim/func_attr.h" #include "nvim/gettext.h" #include "nvim/globals.h" -#include "nvim/macros.h" +#include "nvim/macros_defs.h" #include "nvim/memory.h" #include "nvim/message.h" -#include "nvim/option_defs.h" -#include "nvim/pos.h" +#include "nvim/option_vars.h" #include "nvim/regexp.h" #include "nvim/strings.h" -#include "nvim/types.h" -#include "nvim/vim.h" +#include "nvim/types_defs.h" +#include "nvim/vim_defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "cmdhist.c.generated.h" @@ -204,7 +202,7 @@ static inline void clear_hist_entry(histentry_T *hisptr) /// If 'move_to_front' is true, matching entry is moved to end of history. /// /// @param move_to_front Move the entry to the front if it exists -static int in_history(int type, char *str, int move_to_front, int sep) +static int in_history(int type, const char *str, int move_to_front, int sep) { int last_i = -1; @@ -238,7 +236,7 @@ static int in_history(int type, char *str, int move_to_front, int sep) } list_T *const list = history[type][i].additional_elements; - str = history[type][i].hisstr; + char *const save_hisstr = history[type][i].hisstr; while (i != hisidx[type]) { if (++i >= hislen) { i = 0; @@ -248,7 +246,7 @@ static int in_history(int type, char *str, int move_to_front, int sep) } tv_list_unref(list); history[type][i].hisnum = ++hisnum[type]; - history[type][i].hisstr = str; + history[type][i].hisstr = save_hisstr; history[type][i].timestamp = os_time(); history[type][i].additional_elements = NULL; return true; @@ -295,7 +293,7 @@ static int last_maptick = -1; // last seen maptick /// @param histype may be one of the HIST_ values. /// @param in_map consider maptick when inside a mapping /// @param sep separator character used (search hist) -void add_to_history(int histype, char *new_entry, int in_map, int sep) +void add_to_history(int histype, const char *new_entry, int in_map, int sep) { histentry_T *hisptr; @@ -368,7 +366,6 @@ static int get_history_idx(int histype) static int calc_hist_idx(int histype, int num) { int i; - int wrapped = false; if (hislen == 0 || histype < 0 || histype >= HIST_COUNT || (i = hisidx[histype]) < 0 || num == 0) { @@ -377,6 +374,7 @@ static int calc_hist_idx(int histype, int num) histentry_T *hist = history[histype]; if (num > 0) { + int wrapped = false; while (hist[i].hisnum > num) { if (--i < 0) { if (wrapped) { @@ -454,13 +452,14 @@ static int del_history_entry(int histype, char *str) regmatch.rm_ic = false; // always match case bool found = false; - int i = idx, last = idx; + int i = idx; + int last = idx; do { histentry_T *hisptr = &history[histype][i]; if (hisptr->hisstr == NULL) { break; } - if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) { + if (vim_regexec(®match, hisptr->hisstr, 0)) { found = true; hist_free_entry(hisptr); } else { @@ -519,14 +518,12 @@ static int del_history_idx(int histype, int idx) /// "histadd()" function void f_histadd(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { - HistoryType histype; - rettv->vval.v_number = false; if (check_secure()) { return; } const char *str = tv_get_string_chk(&argvars[0]); // NULL on type error - histype = str != NULL ? get_histtype(str, strlen(str), false) : HIST_INVALID; + HistoryType histype = str != NULL ? get_histtype(str, strlen(str), false) : HIST_INVALID; if (histype == HIST_INVALID) { return; } @@ -538,7 +535,7 @@ void f_histadd(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } init_history(); - add_to_history(histype, (char *)str, false, NUL); + add_to_history(histype, str, false, NUL); rettv->vval.v_number = true; } @@ -568,14 +565,12 @@ void f_histdel(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) /// "histget()" function void f_histget(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { - HistoryType type; - int idx; - const char *const str = tv_get_string_chk(&argvars[0]); // NULL on type error if (str == NULL) { rettv->vval.v_string = NULL; } else { - type = get_histtype(str, strlen(str), false); + int idx; + HistoryType type = get_histtype(str, strlen(str), false); if (argvars[1].v_type == VAR_UNKNOWN) { idx = get_history_idx(type); } else { @@ -592,8 +587,8 @@ void f_histnr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { const char *const histname = tv_get_string_chk(&argvars[0]); HistoryType i = histname == NULL - ? HIST_INVALID - : get_histtype(histname, strlen(histname), false); + ? HIST_INVALID + : get_histtype(histname, strlen(histname), false); if (i != HIST_INVALID) { i = get_history_idx(i); } @@ -603,18 +598,15 @@ void f_histnr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) /// :history command - print a history void ex_history(exarg_T *eap) { - histentry_T *hist; int histype1 = HIST_CMD; int histype2 = HIST_CMD; int hisidx1 = 1; int hisidx2 = -1; - int idx; - int i, j, k; char *end; char *arg = eap->arg; if (hislen == 0) { - msg(_("'history' option is zero")); + msg(_("'history' option is zero"), 0); return; } @@ -626,7 +618,7 @@ void ex_history(exarg_T *eap) } histype1 = get_histtype(arg, (size_t)(end - arg), false); if (histype1 == HIST_INVALID) { - if (STRNICMP(arg, "all", end - (char *)arg) == 0) { + if (STRNICMP(arg, "all", end - arg) == 0) { histype1 = 0; histype2 = HIST_COUNT - 1; } else { @@ -640,19 +632,24 @@ void ex_history(exarg_T *eap) end = arg; } if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) { - semsg(_(e_trailing_arg), end); + if (*end != NUL) { + semsg(_(e_trailing_arg), end); + } else { + semsg(_(e_val_too_large), arg); + } return; } for (; !got_int && histype1 <= histype2; histype1++) { - STRCPY(IObuff, "\n # "); + xstrlcpy(IObuff, "\n # ", IOSIZE); assert(history_names[histype1] != NULL); - STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); + xstrlcat(IObuff, history_names[histype1], IOSIZE); + xstrlcat(IObuff, " history", IOSIZE); msg_puts_title(IObuff); - idx = hisidx[histype1]; - hist = history[histype1]; - j = hisidx1; - k = hisidx2; + int idx = hisidx[histype1]; + histentry_T *hist = history[histype1]; + int j = hisidx1; + int k = hisidx2; if (j < 0) { j = (-j > hislen) ? 0 : hist[(hislen + j + idx + 1) % hislen].hisnum; } @@ -660,7 +657,7 @@ void ex_history(exarg_T *eap) k = (-k > hislen) ? 0 : hist[(hislen + k + idx + 1) % hislen].hisnum; } if (idx >= 0 && j <= k) { - for (i = idx + 1; !got_int; i++) { + for (int i = idx + 1; !got_int; i++) { if (i == hislen) { i = 0; } @@ -673,9 +670,9 @@ void ex_history(exarg_T *eap) trunc_string(hist[i].hisstr, IObuff + strlen(IObuff), Columns - 10, IOSIZE - (int)strlen(IObuff)); } else { - STRCAT(IObuff, hist[i].hisstr); + xstrlcat(IObuff, hist[i].hisstr, IOSIZE); } - msg_outtrans(IObuff); + msg_outtrans(IObuff, 0); } if (i == idx) { break; |