Merge branch '4397_winput_api_fixes'

* 4397_winput_api_fixes:
  Clarify checks of input line content returned from quick dialog.
  (input_get_ctext): mew API.
  (input_destroy): remove check if WInput object is NULL. This object must exist.
  WInput: make one-line APIs inline.
  (input_is_empty): remove checks. WInput object must exist.
  Ticket #4397: sort in editor without arguments stopped working.
This commit is contained in:
Andrew Borodin 2022-08-14 17:53:05 +03:00
commit 0b86047c81
15 changed files with 99 additions and 75 deletions

View File

@ -220,14 +220,12 @@ input_push_history (WInput * in)
char *t;
gboolean empty;
/* FIXME: don't use input_get_text() to avoid extra checks */
t = g_strstrip (g_strndup (in->buffer->str, in->buffer->len));
t = g_strstrip (input_get_text (in));
empty = *t == '\0';
if (!empty)
{
g_free (t);
/* FIXME: don't use input_get_text() to avoid extra checks */
t = g_strndup (in->buffer->str, in->buffer->len);
t = input_get_text (in);
if (in->history.name != NULL && in->strip_password)
{
@ -858,12 +856,6 @@ input_save_history (const gchar * event_group_name, const gchar * event_name,
static void
input_destroy (WInput * in)
{
if (in == NULL)
{
fprintf (stderr, "Internal error: null Input *\n");
exit (EXIT_FAILURE);
}
input_complete_free (in);
/* clean history */
@ -1153,39 +1145,6 @@ input_assign_text (WInput * in, const char *text)
/* --------------------------------------------------------------------------------------------- */
/**
* Get text of input line.
*
* @param in input line
*
* @return newly allocated string that contains a copy of @in's text.
* If @in is empty, return NULL.
*/
char *
input_get_text (const WInput * in)
{
if (input_is_empty (in))
return NULL;
return g_strndup (in->buffer->str, in->buffer->len);
}
/* --------------------------------------------------------------------------------------------- */
gboolean
input_is_empty (const WInput * in)
{
if (in == NULL)
return TRUE;
/* if in != NULL, in->buffer must be created */
g_assert (in->buffer != NULL);
return in->buffer->len == 0;
}
/* --------------------------------------------------------------------------------------------- */
/* Inserts text in input line */
void
input_insert (WInput * in, const char *text, gboolean insert_extra_space)

View File

@ -91,8 +91,6 @@ cb_ret_t input_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm
void input_set_default_colors (void);
cb_ret_t input_handle_char (WInput * in, int key);
void input_assign_text (WInput * in, const char *text);
char *input_get_text (const WInput * in);
gboolean input_is_empty (const WInput * in);
void input_insert (WInput * in, const char *text, gboolean insert_extra_space);
void input_set_point (WInput * in, int pos);
void input_update (WInput * in, gboolean clear_first);
@ -104,6 +102,54 @@ void input_clean (WInput * in);
void input_complete (WInput * in);
void input_complete_free (WInput * in);
/* --------------------------------------------------------------------------------------------- */
/*** inline functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/**
* Get text of input line.
*
* @param in input line
*
* @return newly allocated string that contains a copy of @in's text.
*/
static inline char *
input_get_text (const WInput * in)
{
return g_strndup (in->buffer->str, in->buffer->len);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Get pointer to input line buffer.
*
* @param in input line
*
* @return pointer to @in->buffer->str.
*/
static inline const char *
input_get_ctext (const WInput * in)
{
return in->buffer->str;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Is input line empty or not.
*
* @param in input line
*
* @return TRUE if buffer of @in is empty, FALSE othewise.
*/
static inline gboolean
input_is_empty (const WInput * in)
{
return (in->buffer->len == 0);
}
/* --------------------------------------------------------------------------------------------- */
#endif /* MC__WIDGET_INPUT_H */

View File

@ -599,7 +599,7 @@ quick_dialog_skip (quick_dialog_t * quick_dlg, int nskip)
case quick_input:
if ((item->quick_widget->u.input.completion_flags & INPUT_COMPLETE_CD) != 0)
*item->quick_widget->u.input.result =
tilde_expand (INPUT (item->widget)->buffer->str);
tilde_expand (input_get_ctext (INPUT (item->widget)));
else
*item->quick_widget->u.input.result = input_get_text (INPUT (item->widget));
break;

View File

@ -113,7 +113,7 @@ mcdiffview_dialog_search (WDiff * dview)
g_strfreev (list_of_types);
if ((qd_result == B_CANCEL) || (exp == NULL) || (exp[0] == '\0'))
if (qd_result == B_CANCEL || exp[0] == '\0')
{
g_free (exp);
return FALSE;

View File

@ -200,7 +200,7 @@ edit_options_dialog (WDialog * h)
if (!option_cursor_beyond_eol)
g_list_foreach (GROUP (h)->widgets, edit_reset_over_col, NULL);
if (p != NULL)
if (*p != '\0')
{
option_word_wrap_line_length = atoi (p);
if (option_word_wrap_line_length <= 0)
@ -208,7 +208,7 @@ edit_options_dialog (WDialog * h)
g_free (p);
}
if (q != NULL)
if (*q != '\0')
{
option_tab_spacing = atoi (q);
if (option_tab_spacing <= 0)

View File

@ -119,7 +119,7 @@ edit_dialog_search_show (WEdit * edit)
g_strfreev (list_of_types);
if ((dialog_result == B_CANCEL) || (search_text == NULL) || (search_text[0] == '\0'))
if (dialog_result == B_CANCEL || search_text[0] == '\0')
{
g_free (search_text);
return FALSE;

View File

@ -612,7 +612,12 @@ configure_box (void)
#endif
if (quick_dialog (&qdlg) == B_ENTER)
old_esc_mode_timeout = atoi (time_out_new);
{
if (time_out_new[0] == '\0')
old_esc_mode_timeout = 0;
else
old_esc_mode_timeout = atoi (time_out_new);
}
g_free (time_out_new);
}
@ -1177,7 +1182,10 @@ configure_vfs_box (void)
if (quick_dialog (&qdlg) != B_CANCEL)
{
/* cppcheck-suppress uninitvar */
vfs_timeout = atoi (ret_timeout);
if (ret_timeout[0] == '\0')
vfs_timeout = 0;
else
vfs_timeout = atoi (ret_timeout);
g_free (ret_timeout);
if (vfs_timeout < 0 || vfs_timeout > 10000)
@ -1190,7 +1198,10 @@ configure_vfs_box (void)
/* cppcheck-suppress uninitvar */
ftpfs_proxy_host = ret_ftp_proxy;
/* cppcheck-suppress uninitvar */
ftpfs_directory_timeout = atoi (ret_directory_timeout);
if (ret_directory_timeout[0] == '\0')
ftpfs_directory_timeout = 0;
else
ftpfs_directory_timeout = atoi (ret_directory_timeout);
g_free (ret_directory_timeout);
#endif
}

View File

@ -622,7 +622,7 @@ view_filtered_cmd (const WPanel * panel)
if (input_is_empty (cmdline))
initial_command = selection (panel)->fname->str;
else
initial_command = cmdline->buffer->str;
initial_command = input_get_ctext (cmdline);
command =
input_dialog (_("Filtered view"),

View File

@ -81,11 +81,13 @@ static input_colors_t command_colors;
static cb_ret_t
enter (WInput * lc_cmdline)
{
const char *cmd = lc_cmdline->buffer->str;
const char *cmd;
if (!command_prompt)
return MSG_HANDLED;
cmd = input_get_ctext (lc_cmdline);
/* Any initial whitespace should be removed at this point */
while (whiteness (*cmd))
cmd++;

View File

@ -1405,7 +1405,7 @@ file_mask_dialog (file_op_context_t * ctx, FileOperation operation, gboolean onl
ctx->umask_kill = i2 ^ 0777777;
}
if (dest_dir == NULL || *dest_dir == '\0')
if (*dest_dir == '\0')
{
g_free (def_text_secure);
g_free (source_mask);

View File

@ -1460,12 +1460,12 @@ is_cmdline_mute (void)
static gboolean
handle_cmdline_enter (void)
{
size_t i;
const char *s;
for (i = 0; i < cmdline->buffer->len && whitespace (cmdline->buffer->str[i]); i++)
for (s = input_get_ctext (cmdline); *s != '\0' && whitespace (*s); s++)
;
if (i != cmdline->buffer->len)
if (*s != '\0')
{
send_message (cmdline, NULL, MSG_KEY, '\n', NULL);
return TRUE;

View File

@ -501,7 +501,7 @@ find_parm_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, voi
/* check filename regexp */
if (!file_pattern_cbox->state && !input_is_empty (in_name)
&& !find_check_regexp (in_name->buffer->str))
&& !find_check_regexp (input_get_ctext (in_name)))
{
/* Don't stop the dialog */
widget_set_state (w, WST_ACTIVE, TRUE);
@ -512,7 +512,7 @@ find_parm_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, voi
/* check content regexp */
if (content_regexp_cbox->state && !content_is_empty
&& !find_check_regexp (in_with->buffer->str))
&& !find_check_regexp (input_get_ctext (in_with)))
{
/* Don't stop the dialog */
widget_set_state (w, WST_ACTIVE, TRUE);
@ -783,12 +783,15 @@ find_parameters (WPanel * panel, char **start_dir, ssize_t * start_dir_len,
case B_TREE:
{
const char *start_cstr;
const char *temp_dir;
if (input_is_empty (in_start) || DIR_IS_DOT (in_start->buffer->str))
start_cstr = input_get_ctext (in_start);
if (input_is_empty (in_start) || DIR_IS_DOT (start_cstr))
temp_dir = vfs_path_as_str (panel->cwd_vpath);
else
temp_dir = in_start->buffer->str;
temp_dir = start_cstr;
if (in_start_dir != INPUT_LAST_TEXT)
g_free (in_start_dir);
@ -823,11 +826,12 @@ find_parameters (WPanel * panel, char **start_dir, ssize_t * start_dir_len,
g_free (options.ignore_dirs);
options.ignore_dirs = input_get_text (in_ignore);
*content = input_get_text (in_with);
*pattern = input_get_text (in_name);
if (*pattern == NULL)
*content = !input_is_empty (in_with) ? input_get_text (in_with) : NULL;
if (input_is_empty (in_name))
*pattern = g_strdup (options.file_pattern ? "*" : ".*");
*start_dir = !input_is_empty (in_start) ? in_start->buffer->str : (char *) ".";
else
*pattern = input_get_text (in_name);
*start_dir = (char *) (!input_is_empty (in_start) ? input_get_ctext (in_start) : ".");
if (in_start_dir != INPUT_LAST_TEXT)
g_free (in_start_dir);
in_start_dir = g_strdup (*start_dir);
@ -858,7 +862,7 @@ find_parameters (WPanel * panel, char **start_dir, ssize_t * start_dir_len,
}
if (!options.ignore_dirs_enable || input_is_empty (in_ignore)
|| DIR_IS_DOT (in_ignore->buffer->str))
|| DIR_IS_DOT (input_get_ctext (in_ignore)))
*ignore_dirs = NULL;
else
*ignore_dirs = input_get_text (in_ignore);

View File

@ -2570,7 +2570,7 @@ panel_select_unselect_files_dialog (select_flags_t * flags, const char *title,
if (quick_dialog (&qdlg) == B_CANCEL)
return NULL;
if (reg_exp == NULL || *reg_exp == '\0')
if (*reg_exp == '\0')
{
g_free (reg_exp);
if (str != NULL)

View File

@ -1462,21 +1462,23 @@ invoke_subshell (const char *command, int how, vfs_path_t ** new_dir_vpath)
if (use_persistent_buffer)
{
const char *s;
size_t i;
int pos;
s = input_get_ctext (cmdline);
/* Check to make sure there are no non text characters in the command buffer,
* such as tab, or newline, as this could cause problems. */
for (i = 0; i < cmdline->buffer->len; i++)
if ((unsigned char) cmdline->buffer->str[i] < 32
|| (unsigned char) cmdline->buffer->str[i] == 127)
if ((unsigned char) s[i] < 32 || (unsigned char) s[i] == 127)
g_string_overwrite_len (cmdline->buffer, i, " ", 1);
/* Write the command buffer to the subshell. */
write_all (mc_global.tty.subshell_pty, cmdline->buffer->str, cmdline->buffer->len);
write_all (mc_global.tty.subshell_pty, s, cmdline->buffer->len);
/* Put the cursor in the correct place in the subshell. */
pos = str_length (cmdline->buffer->str) - cmdline->point;
pos = str_length (s) - cmdline->point;
for (i = 0; i < (size_t) pos; i++)
write_all (mc_global.tty.subshell_pty, ESC_STR "[D", 3);
}

View File

@ -110,7 +110,7 @@ mcview_dialog_search (WView * view)
g_strfreev (list_of_types);
if ((qd_result == B_CANCEL) || (exp == NULL) || (exp[0] == '\0'))
if (qd_result == B_CANCEL || exp[0] == '\0')
{
g_free (exp);
return FALSE;
@ -196,7 +196,7 @@ mcview_dialog_goto (WView * view, off_t * offset)
*offset = -1;
/* check input line value */
res = (qd_result != B_CANCEL && exp != NULL && exp[0] != '\0');
res = (qd_result != B_CANCEL && exp[0] != '\0');
if (res)
{
int base = (current_goto_type == MC_VIEW_GOTO_OFFSET_HEX) ? 16 : 10;