Skip to content

Commit

Permalink
fix: GUI help program name no more shown as '(null)'
Browse files Browse the repository at this point in the history
GUI help message was shown too early, before command arg parsing happens and g_set_prgname() is called. Now it is treated as if '--help-all' arg is appended when no arg exists.
  • Loading branch information
abelcheung committed Nov 25, 2023
1 parent 1c95d33 commit cb0e998
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/rifiuti-vista.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ main (int argc,
"Parse index files in C:\\$Recycle.bin style folder "
"and dump recycle bin data. Can also dump a single index file."));
rifiuti_setup_opt_ctx (&context, RECYCLE_BIN_TYPE_DIR);
exit_status = rifiuti_parse_opt_ctx (&context, &argc, &argv);
exit_status = rifiuti_parse_opt_ctx (&context, &argv, &error);
if (exit_status != R2_OK)
goto cleanup;

Expand Down
2 changes: 1 addition & 1 deletion src/rifiuti.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ main (int argc,
g_option_context_set_summary (context, N_(
"Parse INFO2 file and dump recycle bin data."));
rifiuti_setup_opt_ctx (&context, RECYCLE_BIN_TYPE_FILE);
exit_status = rifiuti_parse_opt_ctx (&context, &argc, &argv);
exit_status = rifiuti_parse_opt_ctx (&context, &argv, &error);
if (exit_status != R2_OK)
goto cleanup;

Expand Down
61 changes: 32 additions & 29 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,50 +780,53 @@ rifiuti_setup_opt_ctx (GOptionContext **context,
g_option_context_set_help_enabled (*context, TRUE);
}

/**
* @brief Process command line arguments
* @param context option context pointer
* @param argv reference to command line `argv`
* @param error reference of `GError` pointer to store errors
* @return `R2_OK` upon parse success, `R2_ERR_ARG` on failure.
* `error` is set upon error as well.
*/
r2status
rifiuti_parse_opt_ctx (GOptionContext **context,
int *argc,
char ***argv)
char ***argv,
GError **error)
{
GError *err = NULL;
gboolean parse_ok;
int argc;
char **args;
GArray *arg_array; // TODO GStrvBuilder since 2.68

argc = g_strv_length (*argv);

/* Must be done before parsing, since argc might be modified later */
if (*argc <= 1) {
#ifdef G_OS_WIN32
g_set_print_handler (gui_message);
args = g_win32_get_command_line ();
#else
args = g_strdupv (*argv);
#endif
char *help_msg = g_option_context_get_help (
*context, FALSE, NULL);
g_print ("%s", help_msg);
g_free (help_msg);
g_option_context_free (*context);

return (R2_ERR_GUI_HELP);
arg_array = g_array_new (TRUE, TRUE, sizeof(gpointer));
g_array_append_vals (arg_array, args, argc);
if (argc == 1) {
char *help_opt = g_strdup ("--help-all");
g_array_append_val (arg_array, help_opt);
#ifdef G_OS_WIN32
g_set_print_handler (gui_message);
#endif
}

{
#ifdef G_OS_WIN32
char **args = g_win32_get_command_line ();
#else
char **args = g_strdupv (*argv);
#endif
char *args_str = g_strjoinv("|", args);
char *args_str = g_strjoinv("|", (char **) arg_array->data);
g_debug("Calling args: %s", args_str);
g_free(args_str);

parse_ok = g_option_context_parse_strv (*context, &args, &err);
g_option_context_free (*context);
g_strfreev (args);
}

if (parse_ok)
return R2_OK;
g_option_context_parse_strv (*context,
(char ***) &(arg_array->data), error);
g_option_context_free (*context);
g_array_unref (arg_array);

g_printerr (_("Error parsing options: %s"), err->message);
g_printerr ("\n");
g_clear_error (&err);
return R2_ERR_ARG;
return (*error != NULL) ? R2_ERR_ARG : R2_OK;
}


Expand Down
4 changes: 2 additions & 2 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ void rifiuti_setup_opt_ctx (GOptionContext **context,
rbin_type type);

r2status rifiuti_parse_opt_ctx (GOptionContext **context,
int *argc,
char ***argv);
char ***argv,
GError **error);

GDateTime * win_filetime_to_gdatetime (int64_t win_filetime);

Expand Down

0 comments on commit cb0e998

Please sign in to comment.