(name_quote): refactoring

...and related modifications.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2014-09-03 10:59:37 +04:00
parent 21fba120d0
commit ce568de9b9
10 changed files with 52 additions and 52 deletions

View File

@ -245,28 +245,26 @@ is_printable (int c)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/** /**
* Quote the filename for the purpose of inserting it into the command * Quote the filename for the purpose of inserting it into the command
* line. If quote_percent is 1, replace "%" with "%%" - the percent is * line. If quote_percent is TRUE, replace "%" with "%%" - the percent is
* processed by the mc command line. * processed by the mc command line.
*/ */
char * char *
name_quote (const char *s, int quote_percent) name_quote (const char *s, gboolean quote_percent)
{ {
char *ret, *d; GString *ret;
ret = g_string_sized_new (64);
d = ret = g_malloc (strlen (s) * 2 + 2 + 1);
if (*s == '-') if (*s == '-')
{ g_string_append (ret, "." PATH_SEP_STR);
*d++ = '.';
*d++ = '/';
}
for (; *s; s++, d++) for (; *s != '\0'; s++)
{ {
switch (*s) switch (*s)
{ {
case '%': case '%':
if (quote_percent) if (quote_percent)
*d++ = '%'; g_string_append_c (ret, '%');
break; break;
case '\'': case '\'':
case '\\': case '\\':
@ -291,24 +289,24 @@ name_quote (const char *s, int quote_percent)
case '*': case '*':
case '(': case '(':
case ')': case ')':
*d++ = '\\'; g_string_append_c (ret, '\\');
break; break;
case '~': case '~':
case '#': case '#':
if (d == ret) if (ret->len == 0)
*d++ = '\\'; g_string_append_c (ret, '\\');
break; break;
} }
*d = *s; g_string_append_c (ret, *s);
} }
*d = '\0';
return ret; return g_string_free (ret, FALSE);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
char * char *
fake_name_quote (const char *s, int quote_percent) fake_name_quote (const char *s, gboolean quote_percent)
{ {
(void) quote_percent; (void) quote_percent;
return g_strdup (s); return g_strdup (s);

View File

@ -124,10 +124,10 @@ int is_printable (int c);
/* Quote the filename for the purpose of inserting it into the command /* Quote the filename for the purpose of inserting it into the command
* line. If quote_percent is 1, replace "%" with "%%" - the percent is * line. If quote_percent is 1, replace "%" with "%%" - the percent is
* processed by the mc command line. */ * processed by the mc command line. */
char *name_quote (const char *c, int quote_percent); char *name_quote (const char *c, gboolean quote_percent);
/* returns a duplicate of c. */ /* returns a duplicate of c. */
char *fake_name_quote (const char *c, int quote_percent); char *fake_name_quote (const char *c, gboolean quote_percent);
/* path_trunc() is the same as str_trunc() but /* path_trunc() is the same as str_trunc() but
* it deletes possible password from path for security * it deletes possible password from path for security

View File

@ -256,7 +256,7 @@ edit_get_filter (const vfs_path_t * filename_vpath)
if (i < 0) if (i < 0)
return NULL; return NULL;
quoted_name = name_quote (vfs_path_as_str (filename_vpath), 0); quoted_name = name_quote (vfs_path_as_str (filename_vpath), FALSE);
p = g_strdup_printf (all_filters[i].read, quoted_name); p = g_strdup_printf (all_filters[i].read, quoted_name);
g_free (quoted_name); g_free (quoted_name);
return p; return p;
@ -1810,7 +1810,7 @@ edit_get_write_filter (const vfs_path_t * write_name_vpath, const vfs_path_t * f
return NULL; return NULL;
path_element = vfs_path_get_by_index (write_name_vpath, -1); path_element = vfs_path_get_by_index (write_name_vpath, -1);
writename = name_quote (path_element->path, 0); writename = name_quote (path_element->path, FALSE);
p = g_strdup_printf (all_filters[i].write, writename); p = g_strdup_printf (all_filters[i].write, writename);
g_free (writename); g_free (writename);
return p; return p;

View File

@ -1109,9 +1109,9 @@ pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
FILE *p = 0; FILE *p = 0;
char *s; char *s;
to = name_quote (to, 0); to = name_quote (to, FALSE);
subject = name_quote (subject, 0); subject = name_quote (subject, FALSE);
cc = name_quote (cc, 0); cc = name_quote (cc, FALSE);
s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL); s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
g_free (to); g_free (to);
g_free (subject); g_free (subject);

View File

@ -508,7 +508,7 @@ command_insert (WInput * in, const char *text, gboolean insert_extra_space)
{ {
char *quoted_text; char *quoted_text;
quoted_text = name_quote (text, 1); quoted_text = name_quote (text, TRUE);
input_insert (in, quoted_text, insert_extra_space); input_insert (in, quoted_text, insert_extra_space);
g_free (quoted_text); g_free (quoted_text);
} }

View File

@ -78,7 +78,7 @@
/*** file scope type declarations ****************************************************************/ /*** file scope type declarations ****************************************************************/
typedef char *(*quote_func_t) (const char *name, int quote_percent); typedef char *(*quote_func_t) (const char *name, gboolean quote_percent);
/*** file scope variables ************************************************************************/ /*** file scope variables ************************************************************************/
@ -138,7 +138,7 @@ static char *
exec_get_file_name (const vfs_path_t * filename_vpath) exec_get_file_name (const vfs_path_t * filename_vpath)
{ {
if (!do_local_copy) if (!do_local_copy)
return quote_func (vfs_path_get_last_path_str (filename_vpath), 0); return quote_func (vfs_path_get_last_path_str (filename_vpath), FALSE);
if (localfilecopy_vpath == NULL) if (localfilecopy_vpath == NULL)
{ {
@ -151,7 +151,7 @@ exec_get_file_name (const vfs_path_t * filename_vpath)
localmtime = mystat.st_mtime; localmtime = mystat.st_mtime;
} }
return quote_func (vfs_path_get_last_path_str (localfilecopy_vpath), 0); return quote_func (vfs_path_get_last_path_str (localfilecopy_vpath), FALSE);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -574,7 +574,7 @@ get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
char *tmp; char *tmp;
int ret; int ret;
tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0); tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
ret = get_popen_information (FILE_CMD, tmp, buf, buflen); ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
g_free (tmp); g_free (tmp);
@ -594,8 +594,8 @@ get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int bufle
char *tmp, *lang, *args; char *tmp, *lang, *args;
int ret; int ret;
tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0); tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
lang = name_quote (autodetect_codeset, 0); lang = name_quote (autodetect_codeset, FALSE);
args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL); args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
ret = get_popen_information ("enca", args, buf, buflen); ret = get_popen_information ("enca", args, buf, buflen);

View File

@ -2784,8 +2784,10 @@ do_enter_on_file_entry (file_entry_t * fe)
} }
{ {
char *tmp = name_quote (fe->fname, 0); char *tmp, *cmd;
char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
tmp = name_quote (fe->fname, FALSE);
cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
g_free (tmp); g_free (tmp);
shell_execute (cmd, 0); shell_execute (cmd, 0);
g_free (cmd); g_free (cmd);

View File

@ -491,7 +491,7 @@ execute_menu_command (WEdit * edit_widget, const char *commands, gboolean show_p
{ {
char *tmp; char *tmp;
tmp = name_quote (parameter, 0); tmp = name_quote (parameter, FALSE);
fputs (tmp, cmd_file); fputs (tmp, cmd_file);
g_free (tmp); g_free (tmp);
} }
@ -737,7 +737,7 @@ char *
expand_format (struct WEdit *edit_widget, char c, gboolean do_quote) expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
{ {
WPanel *panel = NULL; WPanel *panel = NULL;
char *(*quote_func) (const char *, int); char *(*quote_func) (const char *, gboolean);
char *fname = NULL; char *fname = NULL;
char *result; char *result;
char c_lc; char c_lc;
@ -785,10 +785,10 @@ expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
{ {
case 'f': case 'f':
case 'p': case 'p':
result = (*quote_func) (fname, 0); result = quote_func (fname, FALSE);
goto ret; goto ret;
case 'x': case 'x':
result = (*quote_func) (extension (fname), 0); result = quote_func (extension (fname), FALSE);
goto ret; goto ret;
case 'd': case 'd':
{ {
@ -800,7 +800,7 @@ expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
else else
cwd = vfs_get_current_dir (); cwd = vfs_get_current_dir ();
qstr = (*quote_func) (cwd, 0); qstr = quote_func (cwd, FALSE);
g_free (cwd); g_free (cwd);
@ -838,14 +838,14 @@ expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
char *file; char *file;
file = mc_config_get_full_path (EDIT_BLOCK_FILE); file = mc_config_get_full_path (EDIT_BLOCK_FILE);
result = (*quote_func) (file, 0); result = quote_func (file, FALSE);
g_free (file); g_free (file);
goto ret; goto ret;
} }
#endif #endif
if (c_lc == 'b') if (c_lc == 'b')
{ {
result = strip_ext ((*quote_func) (fname, 0)); result = strip_ext (quote_func (fname, FALSE));
goto ret; goto ret;
} }
break; break;
@ -854,7 +854,7 @@ expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
#ifdef USE_INTERNAL_EDIT #ifdef USE_INTERNAL_EDIT
if (edit_widget) if (edit_widget)
{ {
result = strip_ext ((*quote_func) (fname, 0)); result = strip_ext (quote_func (fname, FALSE));
goto ret; goto ret;
} }
#endif #endif
@ -862,14 +862,14 @@ expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
case 'm': /* menu file name */ case 'm': /* menu file name */
if (menu) if (menu)
{ {
result = (*quote_func) (menu, 0); result = quote_func (menu, FALSE);
goto ret; goto ret;
} }
break; break;
case 's': case 's':
if (!panel || !panel->marked) if (!panel || !panel->marked)
{ {
result = (*quote_func) (fname, 0); result = quote_func (fname, FALSE);
goto ret; goto ret;
} }
@ -894,7 +894,7 @@ expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
{ {
char *tmp; char *tmp;
tmp = (*quote_func) (panel->dir.list[i].fname, 0); tmp = quote_func (panel->dir.list[i].fname, FALSE);
g_string_append (block, tmp); g_string_append (block, tmp);
g_string_append_c (block, ' '); g_string_append_c (block, ' ');
g_free (tmp); g_free (tmp);

View File

@ -420,7 +420,7 @@ extfs_open_archive (int fstype, const char *name, struct archive **pparc)
goto ret; goto ret;
} }
tmp = name_quote (vfs_path_get_last_path_str (name_vpath), 0); tmp = name_quote (vfs_path_get_last_path_str (name_vpath), FALSE);
} }
cmd = g_strconcat (info->path, info->prefix, " list ", cmd = g_strconcat (info->path, info->prefix, " list ",
@ -834,13 +834,13 @@ extfs_cmd (const char *str_extfs_cmd, struct archive *archive,
int retval; int retval;
file = extfs_get_path_from_entry (entry); file = extfs_get_path_from_entry (entry);
quoted_file = name_quote (file, 0); quoted_file = name_quote (file, FALSE);
g_free (file); g_free (file);
archive_name = extfs_get_archive_name (archive); archive_name = extfs_get_archive_name (archive);
quoted_archive_name = name_quote (archive_name, 0); quoted_archive_name = name_quote (archive_name, FALSE);
g_free (archive_name); g_free (archive_name);
quoted_localname = name_quote (localname, 0); quoted_localname = name_quote (localname, FALSE);
info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype); info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
cmd = g_strconcat (info->path, info->prefix, str_extfs_cmd, cmd = g_strconcat (info->path, info->prefix, str_extfs_cmd,
quoted_archive_name, " ", quoted_file, " ", quoted_localname, (char *) NULL); quoted_archive_name, " ", quoted_file, " ", quoted_localname, (char *) NULL);
@ -868,11 +868,11 @@ extfs_run (const vfs_path_t * vpath)
p = extfs_get_path (vpath, &archive, FALSE); p = extfs_get_path (vpath, &archive, FALSE);
if (p == NULL) if (p == NULL)
return; return;
q = name_quote (p, 0); q = name_quote (p, FALSE);
g_free (p); g_free (p);
archive_name = extfs_get_archive_name (archive); archive_name = extfs_get_archive_name (archive);
quoted_archive_name = name_quote (archive_name, 0); quoted_archive_name = name_quote (archive_name, FALSE);
g_free (archive_name); g_free (archive_name);
info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype); info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
cmd = cmd =

View File

@ -157,11 +157,11 @@ sfs_vfmake (const vfs_path_t * vpath, vfs_path_t * cache_vpath)
vfs_path_free (pname); vfs_path_free (pname);
return -1; return -1;
} }
pqname = name_quote (vfs_path_get_last_path_str (s), 0); pqname = name_quote (vfs_path_get_last_path_str (s), FALSE);
vfs_path_free (s); vfs_path_free (s);
} }
else else
pqname = name_quote (vfs_path_as_str (pname), 0); pqname = name_quote (vfs_path_as_str (pname), FALSE);
vfs_path_free (pname); vfs_path_free (pname);