Commit efec641a authored by David Lawrence Ramsey's avatar David Lawrence Ramsey
Browse files

control characters can only be properly filtered out in both non-UTF-8

and UTF-8 mode inside the output routines, so do it there instead of
inside the input routines


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2382 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
No related merge requests found
Showing with 76 additions and 82 deletions
+76 -82
...@@ -94,17 +94,17 @@ CVS code - ...@@ -94,17 +94,17 @@ CVS code -
support to a few more functions as well, and move multibyte support to a few more functions as well, and move multibyte
character-specific functions to their own source file. New character-specific functions to their own source file. New
file chars.c; new functions is_alnum_char(), file chars.c; new functions is_alnum_char(),
is_alnum_mbchar(), is_alnum_wchar(), is_ascii_char(), is_alnum_mbchar(), is_alnum_wchar(), is_blank_mbchar(),
is_blank_mbchar(), is_blank_wchar(), is_cntrl_mbchar(), is_blank_wchar(), is_cntrl_mbchar(), is_cntrl_wchar(),
is_cntrl_wchar(), control_mbrep(), control_wrep(), mbwidth(), control_mbrep(), control_wrep(), mbwidth(), mb_cur_max(),
mb_cur_max(), make_mbchar(), mbstrlen(), mbstrnlen(), make_mbchar(), mbstrlen(), mbstrnlen(), mbstrcasecmp(),
mbstrcasecmp(), mbstrncasecmp(), mbstrcasestr(), and mbstrncasecmp(), mbstrcasestr(), and mbrevstrcasestr();
mbrevstrcasestr(); changes to help_init(), is_byte() (moved to changes to help_init(), is_byte() (moved to chars.c),
chars.c), is_blank_char() (moved to chars.c), is_cntrl_char() is_blank_char() (moved to chars.c), is_cntrl_char() (moved to
(moved to chars.c), nstricmp() (renamed nstrcasecmp() and chars.c), nstricmp() (renamed nstrcasecmp() and moved to
moved to chars.c), nstrnicmp() (renamed nstrncasecmp() and chars.c), nstrnicmp() (renamed nstrncasecmp() and moved to
moved to chars.c), nstristr() (renamed nstrcasestr() and moved chars.c), nstristr() (renamed nstrcasestr() and moved to
to chars.c), revstrstr() (moved to chars.c), revstristr() chars.c), revstrstr() (moved to chars.c), revstristr()
(renamed revstrcasestr() and moved to chars.c), nstrnlen() (renamed revstrcasestr() and moved to chars.c), nstrnlen()
(moved to chars.c), parse_char() (renamed parse_mbchar() and (moved to chars.c), parse_char() (renamed parse_mbchar() and
moved to chars.c), move_left() (renamed move_mbleft() and moved to chars.c), move_left() (renamed move_mbleft() and
......
...@@ -291,7 +291,7 @@ AC_MSG_WARN([*** Can not use slang when cross-compiling])), ...@@ -291,7 +291,7 @@ AC_MSG_WARN([*** Can not use slang when cross-compiling])),
esac], [AC_MSG_RESULT(no)]) esac], [AC_MSG_RESULT(no)])
dnl Checks for functions dnl Checks for functions
AC_CHECK_FUNCS(snprintf vsnprintf isascii isblank iswalnum iswblank iswspace strcasecmp strncasecmp strcasestr strnlen getline getdelim mblen mbtowc wctomb wcwidth) AC_CHECK_FUNCS(snprintf vsnprintf isblank iswalnum iswblank iswspace strcasecmp strncasecmp strcasestr strnlen getline getdelim mblen mbtowc wctomb wcwidth)
if test "x$ac_cv_func_snprintf" = "xno" -o "x$ac_cv_func_vsnprintf" = "xno" if test "x$ac_cv_func_snprintf" = "xno" -o "x$ac_cv_func_vsnprintf" = "xno"
then then
AM_PATH_GLIB_2_0(2.0.0,, AM_PATH_GLIB_2_0(2.0.0,,
......
...@@ -79,18 +79,6 @@ bool is_alnum_wchar(wchar_t wc) ...@@ -79,18 +79,6 @@ bool is_alnum_wchar(wchar_t wc)
{ {
return iswalnum(wc); return iswalnum(wc);
} }
/* This function is equivalent to isascii(). */
bool is_ascii_char(int c)
{
return
#ifdef HAVE_ISASCII
isascii(c)
#else
((unsigned int)c == (signed char)c)
#endif
;
}
#endif #endif
/* This function is equivalent to isblank(). */ /* This function is equivalent to isblank(). */
......
...@@ -1210,14 +1210,15 @@ void do_verbatim_input(void) ...@@ -1210,14 +1210,15 @@ void do_verbatim_input(void)
/* Read in all the verbatim characters. */ /* Read in all the verbatim characters. */
kbinput = get_verbatim_kbinput(edit, &kbinput_len); kbinput = get_verbatim_kbinput(edit, &kbinput_len);
/* Display all the verbatim characters at once. */ /* Display all the verbatim characters at once, not filtering out
* control characters. */
output = charalloc(kbinput_len + 1); output = charalloc(kbinput_len + 1);
for (i = 0; i < kbinput_len; i++) for (i = 0; i < kbinput_len; i++)
output[i] = (char)kbinput[i]; output[i] = (char)kbinput[i];
output[i] = '\0'; output[i] = '\0';
do_output(output, kbinput_len); do_output(output, kbinput_len, TRUE);
free(output); free(output);
} }
...@@ -1313,7 +1314,7 @@ void do_tab(void) ...@@ -1313,7 +1314,7 @@ void do_tab(void)
{ {
char *kbinput = "\t"; char *kbinput = "\t";
do_output(kbinput, 1); do_output(kbinput, 1, TRUE);
} }
/* Someone hits return *gasp!* */ /* Someone hits return *gasp!* */
...@@ -3615,18 +3616,11 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool ...@@ -3615,18 +3616,11 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
); );
if (allow_funcs) { if (allow_funcs) {
/* If we got a character, and it isn't a shortcut, toggle, or /* If we got a character, and it isn't a shortcut or toggle,
* control character, it's a normal text character. Display the * it's a normal text character. Display the warning if we're
* warning if we're in view mode, or add the character to the * in view mode, or add the character to the input buffer if
* input buffer if we're not. */ * we're not. */
if (input != ERR && *s_or_t == FALSE && ( if (input != ERR && *s_or_t == FALSE) {
#ifdef NANO_WIDE
/* Keep non-ASCII control characters if we're in UTF-8
* mode, since they might be part of a UTF-8
* sequence. */
(!ISSET(NO_UTF8) && !is_ascii_char(input)) ||
#endif
!is_cntrl_char(input))) {
if (ISSET(VIEW_MODE)) if (ISSET(VIEW_MODE))
print_view_warning(); print_view_warning();
else { else {
...@@ -3645,7 +3639,7 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool ...@@ -3645,7 +3639,7 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
if (*s_or_t == TRUE || get_buffer_len() == 0) { if (*s_or_t == TRUE || get_buffer_len() == 0) {
if (kbinput != NULL) { if (kbinput != NULL) {
/* Display all the characters in the input buffer at /* Display all the characters in the input buffer at
* once. */ * once, filtering out control characters. */
char *output = charalloc(kbinput_len + 1); char *output = charalloc(kbinput_len + 1);
size_t i; size_t i;
...@@ -3653,7 +3647,7 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool ...@@ -3653,7 +3647,7 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
output[i] = (char)kbinput[i]; output[i] = (char)kbinput[i];
output[i] = '\0'; output[i] = '\0';
do_output(output, kbinput_len); do_output(output, kbinput_len, FALSE);
free(output); free(output);
...@@ -3776,8 +3770,9 @@ bool do_mouse(void) ...@@ -3776,8 +3770,9 @@ bool do_mouse(void)
#endif /* !DISABLE_MOUSE */ #endif /* !DISABLE_MOUSE */
/* The user typed kbinput_len multibyte characters. Add them to the /* The user typed kbinput_len multibyte characters. Add them to the
* edit buffer. */ * edit buffer, filtering out all control characters if allow_cntrls is
void do_output(char *output, size_t output_len) * TRUE. */
void do_output(char *output, size_t output_len, bool allow_cntrls)
{ {
size_t current_len = strlen(current->data), i = 0; size_t current_len = strlen(current->data), i = 0;
bool old_constupdate = ISSET(CONSTUPDATE); bool old_constupdate = ISSET(CONSTUPDATE);
...@@ -3794,14 +3789,18 @@ void do_output(char *output, size_t output_len) ...@@ -3794,14 +3789,18 @@ void do_output(char *output, size_t output_len)
UNSET(CONSTUPDATE); UNSET(CONSTUPDATE);
while (i < output_len) { while (i < output_len) {
/* Null to newline, if needed. */ /* If allow_cntrls is FALSE, filter out nulls and newlines,
if (output[i] == '\0') * since they're control characters. */
output[i] = '\n'; if (allow_cntrls) {
/* Newline to Enter, if needed. */ /* Null to newline, if needed. */
else if (output[i] == '\n') { if (output[i] == '\0')
do_enter(); output[i] = '\n';
i++; /* Newline to Enter, if needed. */
continue; else if (output[i] == '\n') {
do_enter();
i++;
continue;
}
} }
/* Interpret the next multibyte character. If it's an invalid /* Interpret the next multibyte character. If it's an invalid
...@@ -3811,6 +3810,10 @@ void do_output(char *output, size_t output_len) ...@@ -3811,6 +3810,10 @@ void do_output(char *output, size_t output_len)
i += char_buf_len; i += char_buf_len;
/* If allow_cntrls is FALSE, filter out a control character. */
if (!allow_cntrls && is_cntrl_mbchar(output + i - char_buf_len))
continue;
/* When a character is inserted on the current magicline, it /* When a character is inserted on the current magicline, it
* means we need a new one! */ * means we need a new one! */
if (filebot == current) if (filebot == current)
......
...@@ -156,7 +156,6 @@ bool is_alnum_char(int c); ...@@ -156,7 +156,6 @@ bool is_alnum_char(int c);
bool is_alnum_mbchar(const char *c); bool is_alnum_mbchar(const char *c);
#ifdef NANO_WIDE #ifdef NANO_WIDE
bool is_alnum_wchar(wchar_t wc); bool is_alnum_wchar(wchar_t wc);
bool is_ascii_char(int c);
#endif #endif
bool is_blank_char(int c); bool is_blank_char(int c);
bool is_blank_mbchar(const char *c); bool is_blank_mbchar(const char *c);
...@@ -463,7 +462,7 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool ...@@ -463,7 +462,7 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
bool do_mouse(void); bool do_mouse(void);
#endif #endif
void do_output(char *output, size_t output_len); void do_output(char *output, size_t output_len, bool allow_cntrls);
/* Public functions in rcfile.c. */ /* Public functions in rcfile.c. */
#ifdef ENABLE_NANORC #ifdef ENABLE_NANORC
...@@ -626,7 +625,7 @@ void do_statusbar_prev_word(void); ...@@ -626,7 +625,7 @@ void do_statusbar_prev_word(void);
#endif #endif
void do_statusbar_verbatim_input(bool *got_enter); void do_statusbar_verbatim_input(bool *got_enter);
void do_statusbar_output(char *output, size_t output_len, bool void do_statusbar_output(char *output, size_t output_len, bool
*got_enter); *got_enter, bool allow_cntrls);
size_t xplustabs(void); size_t xplustabs(void);
size_t actual_x(const char *str, size_t xplus); size_t actual_x(const char *str, size_t xplus);
size_t strnlenpt(const char *buf, size_t size); size_t strnlenpt(const char *buf, size_t size);
......
...@@ -1676,18 +1676,11 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t, ...@@ -1676,18 +1676,11 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
*s_or_t = have_shortcut; *s_or_t = have_shortcut;
if (allow_funcs) { if (allow_funcs) {
/* If we got a character, and it isn't a shortcut, toggle, or /* If we got a character, and it isn't a shortcut or toggle,
* control character, it's a normal text character. Display the * it's a normal text character. Display the warning if we're
* warning if we're in view mode, or add the character to the * in view mode, or add the character to the input buffer if
* input buffer if we're not. */ * we're not. */
if (input != ERR && *s_or_t == FALSE && ( if (input != ERR && *s_or_t == FALSE) {
#ifdef NANO_WIDE
/* Keep non-ASCII control characters if we're in UTF-8
* mode, since they might be part of a UTF-8
* sequence. */
(!ISSET(NO_UTF8) && !is_ascii_char(input)) ||
#endif
!is_cntrl_char(input))) {
/* If we're using restricted mode, the filename isn't blank, /* If we're using restricted mode, the filename isn't blank,
* and we're at the "Write File" prompt, disable text * and we're at the "Write File" prompt, disable text
* input. */ * input. */
...@@ -1707,7 +1700,7 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t, ...@@ -1707,7 +1700,7 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
if (kbinput != NULL) { if (kbinput != NULL) {
/* Display all the characters in the input buffer at /* Display all the characters in the input buffer at
* once. */ * once, filtering out control characters. */
char *output = charalloc(kbinput_len + 1); char *output = charalloc(kbinput_len + 1);
size_t i; size_t i;
bool got_enter; bool got_enter;
...@@ -1717,7 +1710,8 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t, ...@@ -1717,7 +1710,8 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
output[i] = (char)kbinput[i]; output[i] = (char)kbinput[i];
output[i] = '\0'; output[i] = '\0';
do_statusbar_output(output, kbinput_len, &got_enter); do_statusbar_output(output, kbinput_len, &got_enter,
FALSE);
free(output); free(output);
...@@ -2037,20 +2031,21 @@ void do_statusbar_verbatim_input(bool *got_enter) ...@@ -2037,20 +2031,21 @@ void do_statusbar_verbatim_input(bool *got_enter)
/* Read in all the verbatim characters. */ /* Read in all the verbatim characters. */
kbinput = get_verbatim_kbinput(bottomwin, &kbinput_len); kbinput = get_verbatim_kbinput(bottomwin, &kbinput_len);
/* Display all the verbatim characters at once. */ /* Display all the verbatim characters at once, not filtering out
* control characters. */
output = charalloc(kbinput_len + 1); output = charalloc(kbinput_len + 1);
for (i = 0; i < kbinput_len; i++) for (i = 0; i < kbinput_len; i++)
output[i] = (char)kbinput[i]; output[i] = (char)kbinput[i];
output[i] = '\0'; output[i] = '\0';
do_statusbar_output(output, kbinput_len, got_enter); do_statusbar_output(output, kbinput_len, got_enter, TRUE);
free(output); free(output);
} }
void do_statusbar_output(char *output, size_t output_len, bool void do_statusbar_output(char *output, size_t output_len, bool
*got_enter) *got_enter, bool allow_cntrls)
{ {
size_t answer_len = strlen(answer), i = 0; size_t answer_len = strlen(answer), i = 0;
char *char_buf = charalloc(mb_cur_max()); char *char_buf = charalloc(mb_cur_max());
...@@ -2061,17 +2056,22 @@ void do_statusbar_output(char *output, size_t output_len, bool ...@@ -2061,17 +2056,22 @@ void do_statusbar_output(char *output, size_t output_len, bool
*got_enter = FALSE; *got_enter = FALSE;
while (i < output_len) { while (i < output_len) {
/* Null to newline, if needed. */ /* If allow_cntrls is FALSE, filter out nulls and newlines,
if (output[i] == '\0') * since they're control characters. */
output[i] = '\n'; if (allow_cntrls) {
/* Newline to Enter, if needed. */ /* Null to newline, if needed. */
else if (output[i] == '\n') { if (output[i] == '\0')
/* Set got_enter to TRUE to indicate that we got the Enter output[i] = '\n';
* key, put back the rest of the characters in output so /* Newline to Enter, if needed. */
* that they can be parsed and output again, and get out. */ else if (output[i] == '\n') {
*got_enter = TRUE; /* Set got_enter to TRUE to indicate that we got the
unparse_kbinput(output + i, output_len - i); * Enter key, put back the rest of the characters in
return; * output so that they can be parsed and output again,
* and get out. */
*got_enter = TRUE;
unparse_kbinput(output + i, output_len - i);
return;
}
} }
/* Interpret the next multibyte character. If it's an invalid /* Interpret the next multibyte character. If it's an invalid
...@@ -2081,6 +2081,10 @@ void do_statusbar_output(char *output, size_t output_len, bool ...@@ -2081,6 +2081,10 @@ void do_statusbar_output(char *output, size_t output_len, bool
i += char_buf_len; i += char_buf_len;
/* If allow_cntrls is FALSE, filter out a control character. */
if (!allow_cntrls && is_cntrl_mbchar(output + i - char_buf_len))
continue;
/* More dangerousness fun =) */ /* More dangerousness fun =) */
answer = charealloc(answer, answer_len + (char_buf_len * 2)); answer = charealloc(answer, answer_len + (char_buf_len * 2));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment