From ebabdff5cd24f2d30ab35051d1d65f7c29fa3dd3 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 13 Jul 2014 11:10:27 +0400 Subject: keymap: Make replace_termcodes and friends accept length and cpo_flags Reasons: - One does not have to do `s[len] = NUL` to work with these functions if they do not need to replace the whole string: thus `s` may be const. - One does not have to save/restore p_cpo to work with them. --- src/nvim/option.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 5efd71444a..9942d154e1 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -2964,7 +2964,8 @@ did_set_string_option ( /* 'pastetoggle': translate key codes like in a mapping */ else if (varp == &p_pt) { if (*p_pt) { - (void)replace_termcodes(p_pt, &p, TRUE, TRUE, FALSE); + (void)replace_termcodes(p_pt, STRLEN(p_pt), &p, true, true, false, + CPO_TO_CPO_FLAGS); if (p != NULL) { if (new_value_alloced) free_string_option(p_pt); @@ -4656,7 +4657,7 @@ char_u *get_highlight_default(void) /* * Translate a string like "t_xx", "" or "" to a key number. */ -static int find_key_option(char_u *arg) +static int find_key_option(const char_u *arg) { int key; int modifiers; @@ -4670,9 +4671,10 @@ static int find_key_option(char_u *arg) else { --arg; /* put arg at the '<' */ modifiers = 0; - key = find_special_key(&arg, &modifiers, TRUE, TRUE); - if (modifiers) /* can't handle modifiers here */ + key = find_special_key(&arg, STRLEN(arg), &modifiers, true, true); + if (modifiers) { // can't handle modifiers here key = 0; + } } return key; } -- cgit From 459900b1005d8fd661d03d0e0bdae771a21b017b Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 13 Jul 2014 11:15:55 +0400 Subject: option: Add findoption_len function It is like findoption(), but works with non-NUL-terminated strings. --- src/nvim/option.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 9942d154e1..b7521e38f8 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4286,10 +4286,10 @@ static void check_redraw(uint32_t flags) } /* - * Find index for option 'arg'. + * Find index for option 'arg' that has given length. * Return -1 if not found. */ -static int findoption(char_u *arg) +static int findoption_len(const char_u *const arg, const size_t len) { char *s, *p; static short quick_tab[27] = {0, 0}; /* quick access table */ @@ -4313,6 +4313,8 @@ static int findoption(char_u *arg) } } + assert(len > 0); + /* * Check for name starting with an illegal character. */ @@ -4320,20 +4322,22 @@ static int findoption(char_u *arg) return -1; int opt_idx; - is_term_opt = (arg[0] == 't' && arg[1] == '_'); + is_term_opt = (len > 2 && arg[0] == 't' && arg[1] == '_'); if (is_term_opt) opt_idx = quick_tab[26]; else opt_idx = quick_tab[CharOrdLow(arg[0])]; + // Match full name for (; (s = options[opt_idx].fullname) != NULL; opt_idx++) { - if (STRCMP(arg, s) == 0) /* match full name */ + if (STRNCMP(arg, s, len) == 0 && s[len] == NUL) break; } if (s == NULL && !is_term_opt) { opt_idx = quick_tab[CharOrdLow(arg[0])]; + // Match short name for (; options[opt_idx].fullname != NULL; opt_idx++) { s = options[opt_idx].shortname; - if (s != NULL && STRCMP(arg, s) == 0) /* match short name */ + if (s != NULL && STRNCMP(arg, s, len) == 0 && s[len] == NUL) break; s = NULL; } @@ -4401,6 +4405,15 @@ bool set_tty_option(char *name, char *value) || !strcmp(name, "ttytype"); } +/* + * Find index for option 'arg'. + * Return -1 if not found. + */ +static int findoption(char_u *arg) +{ + return findoption_len(arg, STRLEN(arg)); +} + /* * Get the value for an option. * -- cgit From 1168dbe3435580f872ae9cd44648f3b3a881896c Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 6 Nov 2014 17:25:45 +0300 Subject: option: Add find_key_option_len function --- src/nvim/option.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index b7521e38f8..f3cd144386 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4289,7 +4289,7 @@ static void check_redraw(uint32_t flags) * Find index for option 'arg' that has given length. * Return -1 if not found. */ -static int findoption_len(const char_u *const arg, const size_t len) +int findoption_len(const char_u *const arg, const size_t len) { char *s, *p; static short quick_tab[27] = {0, 0}; /* quick access table */ @@ -4670,7 +4670,7 @@ char_u *get_highlight_default(void) /* * Translate a string like "t_xx", "" or "" to a key number. */ -static int find_key_option(const char_u *arg) +int find_key_option_len(const char_u *arg, size_t len) { int key; int modifiers; @@ -4679,12 +4679,12 @@ static int find_key_option(const char_u *arg) * Don't use get_special_key_code() for t_xx, we don't want it to call * add_termcap_entry(). */ - if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3]) + if (len >= 4 && arg[0] == 't' && arg[1] == '_') key = TERMCAP2KEY(arg[2], arg[3]); else { --arg; /* put arg at the '<' */ modifiers = 0; - key = find_special_key(&arg, STRLEN(arg), &modifiers, true, true); + key = find_special_key(&arg, len + 1, &modifiers, true, true); if (modifiers) { // can't handle modifiers here key = 0; } @@ -4692,6 +4692,12 @@ static int find_key_option(const char_u *arg) return key; } +static int find_key_option(const char_u *arg) +{ + return find_key_option_len(arg, STRLEN(arg)); +} + + /* * if 'all' == 0: show changed options * if 'all' == 1: show all normal options -- cgit From 9261f1597ff9ffbbb546dc308bdeaabd191577e9 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 6 Nov 2014 17:29:32 +0300 Subject: option: Use findoption_len in do_set --- src/nvim/option.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index f3cd144386..fe205f4e63 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1177,10 +1177,9 @@ do_set ( errmsg = e_invarg; goto skip; } - arg[len] = NUL; /* put NUL after name */ if (arg[1] == 't' && arg[2] == '_') /* could be term code */ - opt_idx = findoption(arg + 1); - arg[len++] = '>'; /* restore '>' */ + opt_idx = findoption_len(arg + 1, (size_t) (len - 1)); + len++; if (opt_idx == -1) key = find_key_option(arg + 1); } else { @@ -1193,10 +1192,7 @@ do_set ( else while (ASCII_ISALNUM(arg[len]) || arg[len] == '_') ++len; - nextchar = arg[len]; - arg[len] = NUL; /* put NUL after name */ - opt_idx = findoption(arg); - arg[len] = nextchar; /* restore nextchar */ + opt_idx = findoption_len(arg, (size_t) len); if (opt_idx == -1) key = find_key_option(arg); } -- cgit From c0bab19cf5aeb2533e0995a44283088b95a5a531 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 15 Nov 2014 20:51:18 +0300 Subject: option: Allow zero-length options Code that expected NUL-terminated strings allowed them and this behaviour is actually used. --- src/nvim/option.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index fe205f4e63..314ad0b035 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -4309,13 +4309,12 @@ int findoption_len(const char_u *const arg, const size_t len) } } - assert(len > 0); - /* * Check for name starting with an illegal character. */ - if (arg[0] < 'a' || arg[0] > 'z') + if (len == 0 || arg[0] < 'a' || arg[0] > 'z') { return -1; + } int opt_idx; is_term_opt = (len > 2 && arg[0] == 't' && arg[1] == '_'); -- cgit From 191fb638f415a0a1a05eb83a87d87fc9902d7ffe Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 18 Apr 2016 02:29:45 +0300 Subject: *: Fix linter errors --- src/nvim/option.c | 60 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 27 deletions(-) (limited to 'src/nvim/option.c') diff --git a/src/nvim/option.c b/src/nvim/option.c index 314ad0b035..7b41d256f5 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1177,24 +1177,27 @@ do_set ( errmsg = e_invarg; goto skip; } - if (arg[1] == 't' && arg[2] == '_') /* could be term code */ + if (arg[1] == 't' && arg[2] == '_') { // could be term code opt_idx = findoption_len(arg + 1, (size_t) (len - 1)); + } len++; - if (opt_idx == -1) + if (opt_idx == -1) { key = find_key_option(arg + 1); + } } else { len = 0; - /* - * The two characters after "t_" may not be alphanumeric. - */ - if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3]) + // The two characters after "t_" may not be alphanumeric. + if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3]) { len = 4; - else - while (ASCII_ISALNUM(arg[len]) || arg[len] == '_') - ++len; + } else { + while (ASCII_ISALNUM(arg[len]) || arg[len] == '_') { + len++; + } + } opt_idx = findoption_len(arg, (size_t) len); - if (opt_idx == -1) + if (opt_idx == -1) { key = find_key_option(arg); + } } /* remember character after option name */ @@ -4281,14 +4284,16 @@ static void check_redraw(uint32_t flags) redraw_all_later(NOT_VALID); } -/* - * Find index for option 'arg' that has given length. - * Return -1 if not found. - */ +/// Find index for named option +/// +/// @param[in] arg Option to find index for. +/// @param[in] len Length of the option. +/// +/// @return Index of the option or -1 if option was not found. int findoption_len(const char_u *const arg, const size_t len) { - char *s, *p; - static short quick_tab[27] = {0, 0}; /* quick access table */ + char *s, *p; + static int quick_tab[27] = { 0, 0 }; // quick access table int is_term_opt; /* @@ -4318,22 +4323,25 @@ int findoption_len(const char_u *const arg, const size_t len) int opt_idx; is_term_opt = (len > 2 && arg[0] == 't' && arg[1] == '_'); - if (is_term_opt) + if (is_term_opt) { opt_idx = quick_tab[26]; - else + } else { opt_idx = quick_tab[CharOrdLow(arg[0])]; + } // Match full name for (; (s = options[opt_idx].fullname) != NULL; opt_idx++) { - if (STRNCMP(arg, s, len) == 0 && s[len] == NUL) + if (STRNCMP(arg, s, len) == 0 && s[len] == NUL) { break; + } } if (s == NULL && !is_term_opt) { opt_idx = quick_tab[CharOrdLow(arg[0])]; // Match short name for (; options[opt_idx].fullname != NULL; opt_idx++) { s = options[opt_idx].shortname; - if (s != NULL && STRNCMP(arg, s, len) == 0 && s[len] == NUL) + if (s != NULL && STRNCMP(arg, s, len) == 0 && s[len] == NUL) { break; + } s = NULL; } } @@ -4670,14 +4678,12 @@ int find_key_option_len(const char_u *arg, size_t len) int key; int modifiers; - /* - * Don't use get_special_key_code() for t_xx, we don't want it to call - * add_termcap_entry(). - */ - if (len >= 4 && arg[0] == 't' && arg[1] == '_') + // Don't use get_special_key_code() for t_xx, we don't want it to call + // add_termcap_entry(). + if (len >= 4 && arg[0] == 't' && arg[1] == '_') { key = TERMCAP2KEY(arg[2], arg[3]); - else { - --arg; /* put arg at the '<' */ + } else { + arg--; // put arg at the '<' modifiers = 0; key = find_special_key(&arg, len + 1, &modifiers, true, true); if (modifiers) { // can't handle modifiers here -- cgit