Tweak arguments parsing

...to allow to show info about mc options regardless of the fact that
configuration directories cannot be created.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2012-03-16 10:06:17 +03:00
parent dcba554a6d
commit cce7ccf48a
3 changed files with 207 additions and 203 deletions

View File

@ -416,12 +416,194 @@ mc_args_add_extended_info_to_help (void)
/* --------------------------------------------------------------------------------------------- */
static gchar *
mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
const gchar * help_str)
{
GString *buffer;
GIConv conv;
gchar *full_help_str;
buffer = g_string_new ("");
conv = g_iconv_open (charset, "UTF-8");
full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
str_convert (conv, full_help_str, buffer);
g_free (full_help_str);
g_iconv_close (conv);
return g_string_free (buffer, FALSE);
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
mc_setup_by_args (int argc, char *argv[], GError ** error)
parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
{
(void) option_name;
(void) data;
(void) error;
mc_global.mc_run_mode = MC_RUN_EDITOR;
mc_run_param0 = g_strdup (value);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
{
(void) option_name;
(void) data;
(void) error;
mc_global.mc_run_mode = MC_RUN_VIEWER;
mc_run_param0 = g_strdup (value);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
gboolean
mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError ** error)
{
const gchar *_system_codepage;
gboolean ok = TRUE;
_system_codepage = str_detect_termencoding ();
#ifdef ENABLE_NLS
if (!str_isutf8 (_system_codepage))
bind_textdomain_codeset ("mc", "UTF-8");
#endif
context = g_option_context_new (mc_args_add_usage_info ());
g_option_context_set_ignore_unknown_options (context, FALSE);
mc_args_add_extended_info_to_help ();
main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
g_option_group_add_entries (main_group, argument_main_table);
g_option_context_set_main_group (context, main_group);
g_option_group_set_translation_domain (main_group, translation_domain);
terminal_group = g_option_group_new ("terminal", _("Terminal options"),
_("Terminal options"), NULL, NULL);
g_option_group_add_entries (terminal_group, argument_terminal_table);
g_option_context_add_group (context, terminal_group);
g_option_group_set_translation_domain (terminal_group, translation_domain);
color_group = mc_args_new_color_group ();
g_option_group_add_entries (color_group, argument_color_table);
g_option_context_add_group (context, color_group);
g_option_group_set_translation_domain (color_group, translation_domain);
if (!g_option_context_parse (context, argc, argv, error))
{
GError *error2 = NULL;
if (*error == NULL)
*error = g_error_new (MC_ERROR, 0, "%s\n", _("Arguments parse error!"));
else
{
gchar *help_str;
#if GLIB_CHECK_VERSION(2,14,0)
help_str = g_option_context_get_help (context, TRUE, NULL);
#else
help_str = g_strdup ("");
#endif
if (str_isutf8 (_system_codepage))
error2 = g_error_new ((*error)->domain, (*error)->code, "%s\n\n%s\n",
(*error)->message, help_str);
else
{
gchar *full_help_str;
full_help_str =
mc_args__convert_help_to_syscharset (_system_codepage, (*error)->message,
help_str);
error2 = g_error_new ((*error)->domain, (*error)->code, "%s", full_help_str);
g_free (full_help_str);
}
g_free (help_str);
g_error_free (*error);
*error = error2;
}
ok = FALSE;
}
g_option_context_free (context);
mc_args_clean_temp_help_strings ();
#ifdef ENABLE_NLS
if (!str_isutf8 (_system_codepage))
bind_textdomain_codeset ("mc", _system_codepage);
#endif
return ok;
}
/* --------------------------------------------------------------------------------------------- */
gboolean
mc_args_show_info (void)
{
if (mc_args__show_version)
{
show_version ();
return FALSE;
}
if (mc_args__show_datadirs)
{
printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
return FALSE;
}
if (mc_args__show_datadirs_extended)
{
show_datadirs_extended ();
return FALSE;
}
if (mc_args__show_configure_opts)
{
show_configure_options ();
return FALSE;
}
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
gboolean
mc_setup_by_args (int argc, char **argv, GError ** error)
{
const char *base;
char *tmp;
if (mc_args__force_colors)
mc_global.tty.disable_colors = FALSE;
#ifdef HAVE_SUBSHELL_SUPPORT
if (mc_args__nouse_subshell)
mc_global.tty.use_subshell = FALSE;
#endif /* HAVE_SUBSHELL_SUPPORT */
#ifdef ENABLE_VFS_SMB
if (mc_args__debug_level != 0)
smbfs_set_debug (mc_args__debug_level);
@ -440,6 +622,7 @@ mc_setup_by_args (int argc, char *argv[], GError ** error)
mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
vfs_path_free (vpath);
#endif /* ENABLE_VFS_SMB */
(void) vpath;
}
base = x_basename (argv[0]);
@ -595,185 +778,3 @@ mc_setup_by_args (int argc, char *argv[], GError ** error)
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
mc_args_process (int argc, char *argv[], GError ** error)
{
if (mc_args__show_version)
{
show_version ();
return FALSE;
}
if (mc_args__show_datadirs)
{
printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
return FALSE;
}
if (mc_args__show_datadirs_extended)
{
show_datadirs_extended ();
return FALSE;
}
if (mc_args__show_configure_opts)
{
show_configure_options ();
return FALSE;
}
if (mc_args__force_colors)
mc_global.tty.disable_colors = FALSE;
#ifdef HAVE_SUBSHELL_SUPPORT
if (mc_args__nouse_subshell)
mc_global.tty.use_subshell = FALSE;
#endif /* HAVE_SUBSHELL_SUPPORT */
return mc_setup_by_args (argc, argv, error);
}
/* --------------------------------------------------------------------------------------------- */
static gchar *
mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
const gchar * help_str)
{
GString *buffer;
GIConv conv;
gchar *full_help_str;
buffer = g_string_new ("");
conv = g_iconv_open (charset, "UTF-8");
full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
str_convert (conv, full_help_str, buffer);
g_free (full_help_str);
g_iconv_close (conv);
return g_string_free (buffer, FALSE);
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
{
(void) option_name;
(void) data;
(void) error;
mc_global.mc_run_mode = MC_RUN_EDITOR;
mc_run_param0 = g_strdup (value);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
{
(void) option_name;
(void) data;
(void) error;
mc_global.mc_run_mode = MC_RUN_VIEWER;
mc_run_param0 = g_strdup (value);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
gboolean
mc_args_handle (int argc, char **argv, const char *translation_domain, GError ** error)
{
const gchar *_system_codepage = str_detect_termencoding ();
#ifdef ENABLE_NLS
if (!str_isutf8 (_system_codepage))
bind_textdomain_codeset ("mc", "UTF-8");
#endif
context = g_option_context_new (mc_args_add_usage_info ());
g_option_context_set_ignore_unknown_options (context, FALSE);
mc_args_add_extended_info_to_help ();
main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
g_option_group_add_entries (main_group, argument_main_table);
g_option_context_set_main_group (context, main_group);
g_option_group_set_translation_domain (main_group, translation_domain);
terminal_group = g_option_group_new ("terminal", _("Terminal options"),
_("Terminal options"), NULL, NULL);
g_option_group_add_entries (terminal_group, argument_terminal_table);
g_option_context_add_group (context, terminal_group);
g_option_group_set_translation_domain (terminal_group, translation_domain);
color_group = mc_args_new_color_group ();
g_option_group_add_entries (color_group, argument_color_table);
g_option_context_add_group (context, color_group);
g_option_group_set_translation_domain (color_group, translation_domain);
if (!g_option_context_parse (context, &argc, &argv, error))
{
GError *error2 = NULL;
if (*error != NULL)
{
gchar *help_str;
#if GLIB_CHECK_VERSION(2,14,0)
help_str = g_option_context_get_help (context, TRUE, NULL);
#else
help_str = g_strdup ("");
#endif
if (str_isutf8 (_system_codepage))
error2 = g_error_new ((*error)->domain, (*error)->code, "%s\n\n%s\n",
(*error)->message, help_str);
else
{
gchar *full_help_str;
full_help_str =
mc_args__convert_help_to_syscharset (_system_codepage, (*error)->message,
help_str);
error2 = g_error_new ((*error)->domain, (*error)->code, "%s", full_help_str);
g_free (full_help_str);
}
g_free (help_str);
g_error_free (*error);
*error = error2;
}
else
*error = g_error_new (MC_ERROR, 0, "%s\n", _("Arguments parse error!"));
g_option_context_free (context);
mc_args_clean_temp_help_strings ();
return FALSE;
}
g_option_context_free (context);
mc_args_clean_temp_help_strings ();
#ifdef ENABLE_NLS
if (!str_isutf8 (_system_codepage))
bind_textdomain_codeset ("mc", _system_codepage);
#endif
return mc_args_process (argc, argv, error);
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -15,7 +15,6 @@ extern gboolean mc_args__force_xterm;
extern gboolean mc_args__nomouse;
extern gboolean mc_args__force_colors;
extern gboolean mc_args__nokeymap;
extern gboolean mc_args__version;
extern int mc_args__edit_start_line;
extern char *mc_args__last_wd_file;
extern char *mc_args__netfs_logfile;
@ -24,7 +23,9 @@ extern int mc_args__debug_level;
/*** declarations of public functions ************************************************************/
gboolean mc_args_handle (int argc, char **argv, const char *translation_domain, GError ** error);
gboolean mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError ** error);
gboolean mc_args_show_info (void);
gboolean mc_setup_by_args (int argc, char **argv, GError ** error);
/*** inline functions ****************************************************************************/
#endif /* MC__ARGS_H */

View File

@ -399,37 +399,45 @@ main (int argc, char *argv[])
{
GError *error = NULL;
int exit_code = EXIT_FAILURE;
gboolean isInitialized;
/* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
(void) setlocale (LC_ALL, "");
(void) bindtextdomain ("mc", LOCALEDIR);
(void) textdomain ("mc");
if (!events_init (&error))
/* do this before args parsing */
str_init_strings (NULL);
if (!mc_args_parse (&argc, &argv, "mc", &error))
{
startup_exit_falure:
fprintf (stderr, _("Failed to run:\n%s\n"), error->message);
g_error_free (error);
g_free (shell);
startup_exit_ok:
(void) mc_event_deinit (NULL);
str_uninit_strings ();
return exit_code;
}
str_init_strings (NULL);
if (!mc_args_show_info ())
{
exit_code = EXIT_SUCCESS;
goto startup_exit_ok;
}
if (!events_init (&error))
goto startup_exit_falure;
/* Set up temporary directory */
(void) mc_tmpdir ();
OS_Setup ();
/* Initialize and create home directories */
/* do it after the screen library initialization to show the error message */
mc_config_init_config_paths (&error);
if (error == NULL && mc_config_deprecated_dir_present ())
mc_config_migrate_from_old_place (&error);
if (error != NULL)
{
g_free (shell);
mc_event_deinit (NULL);
goto startup_exit_falure;
}
@ -437,18 +445,12 @@ main (int argc, char *argv[])
vfs_plugins_init ();
vfs_setup_work_dir ();
/* do this after vfs initialization due to mc_setctl() call in mc_args_handle() */
if (!mc_args_handle (argc, argv, "mc", &error))
/* do this after vfs initialization due to mc_setctl() call in mc_setup_by_args() */
if (!mc_setup_by_args (argc, argv, &error))
{
if (error != NULL)
{
vfs_shut ();
goto startup_exit_falure;
}
/* for help messages */
exit_code = EXIT_SUCCESS;
goto startup_exit_ok;
vfs_shut ();
mc_event_deinit (NULL);
goto startup_exit_falure;
}
/* check terminal type
@ -498,7 +500,7 @@ main (int argc, char *argv[])
tty_init_colors (mc_global.tty.disable_colors, mc_args__force_colors);
isInitialized = mc_skin_init (&error);
mc_skin_init (&error);
if (error != NULL)
{
message (D_ERROR, _("Warning"), "%s", error->message);