2 Handle command line arguments.
4 Copyright (C) 2009-2023
5 Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2009.
9 Andrew Borodin <aborodin@vmail.ru>, 2011, 2012.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "lib/global.h"
32 #include "lib/tty/tty.h"
33 #include "lib/strutil.h"
34 #include "lib/vfs/vfs.h"
35 #include "lib/util.h" /* x_basename() */
37 #include "src/textconf.h"
41 /*** external variables **************************************************************************/
43 /*** global variables ****************************************************************************/
45 /* If true, assume we are running on an xterm terminal */
46 gboolean mc_args__force_xterm
= FALSE
;
48 gboolean mc_args__nomouse
= FALSE
;
50 /* Force colors, only used by Slang */
51 gboolean mc_args__force_colors
= FALSE
;
53 /* Don't load keymap from file and use default one */
54 gboolean mc_args__nokeymap
= FALSE
;
56 char *mc_args__last_wd_file
= NULL
;
58 /* when enabled NETCODE, use following file as logfile */
59 char *mc_args__netfs_logfile
= NULL
;
62 char *mc_args__keymap_file
= NULL
;
64 void *mc_run_param0
= NULL
;
65 char *mc_run_param1
= NULL
;
67 /*** file scope macro definitions ****************************************************************/
69 /*** file scope type declarations ****************************************************************/
71 /*** forward declarations (file scope functions) *************************************************/
73 static gboolean
parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
,
74 gpointer data
, GError
** mcerror
);
75 static gboolean
parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
,
76 gpointer data
, GError
** mcerror
);
78 /*** file scope variables ************************************************************************/
80 /* If true, show version info and exit */
81 static gboolean mc_args__show_version
= FALSE
;
83 static GOptionContext
*context
;
85 #ifdef ENABLE_SUBSHELL
86 static gboolean mc_args__nouse_subshell
= FALSE
;
87 #endif /* ENABLE_SUBSHELL */
88 static gboolean mc_args__show_datadirs
= FALSE
;
89 static gboolean mc_args__show_datadirs_extended
= FALSE
;
90 #ifdef ENABLE_CONFIGURE_ARGS
91 static gboolean mc_args__show_configure_opts
= FALSE
;
94 static GOptionGroup
*main_group
;
96 static const GOptionEntry argument_main_table
[] = {
100 "version", 'V', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
101 &mc_args__show_version
,
102 N_("Displays the current version"),
106 /* options for wrappers */
108 "datadir", 'f', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
109 &mc_args__show_datadirs
,
110 N_("Print data directory"),
114 /* show extended information about used data directories */
116 "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
117 &mc_args__show_datadirs_extended
,
118 N_("Print extended info about used data directories"),
122 #ifdef ENABLE_CONFIGURE_ARGS
123 /* show configure options */
125 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
126 &mc_args__show_configure_opts
,
127 N_("Print configure options"),
133 "printwd", 'P', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
134 &mc_args__last_wd_file
,
135 N_("Print last working directory to specified file"),
139 #ifdef ENABLE_SUBSHELL
141 "subshell", 'U', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
142 &mc_global
.tty
.use_subshell
,
143 N_("Enables subshell support (default)"),
148 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
149 &mc_args__nouse_subshell
,
150 N_("Disables subshell support"),
156 #ifdef ENABLE_VFS_FTP
158 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
159 &mc_args__netfs_logfile
,
160 N_("Log ftp dialog to specified file"),
163 #endif /* ENABLE_VFS_FTP */
166 /* handle arguments manually */
167 "view", 'v', G_OPTION_FLAG_IN_MAIN
| G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
168 (gpointer
) parse_mc_v_argument
,
169 N_("Launches the file viewer on a file"),
174 /* handle arguments manually */
175 "edit", 'e', G_OPTION_FLAG_IN_MAIN
| G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
176 (gpointer
) parse_mc_e_argument
,
185 static GOptionGroup
*terminal_group
;
186 #define ARGS_TERM_OPTIONS 0
187 static const GOptionEntry argument_terminal_table
[] = {
189 /* terminal options */
191 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
192 &mc_args__force_xterm
,
193 N_("Forces xterm features"),
198 "no-x11", 'X', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
199 &mc_global
.tty
.disable_x11
,
200 N_("Disable X11 support"),
205 "oldmouse", 'g', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
206 &mc_global
.tty
.old_mouse
,
207 N_("Tries to use an old highlight mouse tracking"),
212 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
214 N_("Disable mouse support in text version"),
220 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
222 N_("Tries to use termcap instead of terminfo"),
228 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
229 &mc_global
.tty
.slow_terminal
,
230 N_("To run on slow terminals"),
235 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
236 &mc_global
.tty
.ugly_line_drawing
,
237 N_("Use stickchars to draw"),
243 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
245 N_("Resets soft keys on HP terminals"),
251 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
252 &mc_args__keymap_file
,
253 N_("Load definitions of key bindings from specified file"),
258 "nokeymap", '\0', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
260 N_("Don't load definitions of key bindings from file, use defaults"),
268 #undef ARGS_TERM_OPTIONS
270 static GOptionGroup
*color_group
;
271 #define ARGS_COLOR_OPTIONS 0
272 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
273 static const GOptionEntry argument_color_table
[] = {
277 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
278 &mc_global
.tty
.disable_colors
,
279 N_("Requests to run in black and white"),
284 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
285 &mc_args__force_colors
,
286 N_("Request to run in color mode"),
291 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
292 &mc_global
.tty
.command_line_colors
,
293 N_("Specifies a color configuration"),
298 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
300 N_("Show mc with specified skin"),
308 #undef ARGS_COLOR_OPTIONS
310 static gchar
*mc_args__loc__colors_string
= NULL
;
311 static gchar
*mc_args__loc__footer_string
= NULL
;
312 static gchar
*mc_args__loc__header_string
= NULL
;
313 static gchar
*mc_args__loc__usage_string
= NULL
;
315 /* --------------------------------------------------------------------------------------------- */
316 /*** file scope functions ************************************************************************/
317 /* --------------------------------------------------------------------------------------------- */
320 mc_args_clean_temp_help_strings (void)
322 MC_PTR_FREE (mc_args__loc__colors_string
);
323 MC_PTR_FREE (mc_args__loc__footer_string
);
324 MC_PTR_FREE (mc_args__loc__header_string
);
325 MC_PTR_FREE (mc_args__loc__usage_string
);
328 /* --------------------------------------------------------------------------------------------- */
330 static GOptionGroup
*
331 mc_args_new_color_group (void)
334 /* FIXME: to preserve translations, lines should be split. */
335 mc_args__loc__colors_string
= g_strdup_printf ("%s\n%s",
336 /* TRANSLATORS: don't translate keywords */
337 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
338 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
340 " Global: errors, disabled, reverse, gauge, header\n"
341 " input, inputmark, inputunchanged, commandlinemark\n"
342 " bbarhotkey, bbarbutton, statusbar\n"
343 " File display: normal, selected, marked, markselect\n"
344 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
346 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
347 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
348 " Editor: editnormal, editbold, editmarked, editwhitespace, editnonprintable,\n"
349 " editlinestate, editbg, editframe, editframeactive\n"
351 " Viewer: viewnormal,viewbold, viewunderline, viewselected\n"
352 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
353 /* TRANSLATORS: don't translate color names and attributes */
354 _("Standard Colors:\n"
355 " black, gray, red, brightred, green, brightgreen, brown,\n"
356 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
357 " brightcyan, lightgray and white\n\n"
358 "Extended colors, when 256 colors are available:\n"
359 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
361 " bold, italic, underline, reverse, blink; append more with '+'\n")
365 return g_option_group_new ("color", mc_args__loc__colors_string
,
366 _("Color options"), NULL
, NULL
);
370 /* --------------------------------------------------------------------------------------------- */
373 mc_args_add_usage_info (void)
377 switch (mc_global
.mc_run_mode
)
380 s
= g_strdup_printf ("%s\n", _("[+lineno] file1[:lineno] [file2[:lineno]...]"));
383 s
= g_strdup_printf ("%s\n", _("file"));
386 case MC_RUN_DIFFVIEWER
:
387 s
= g_strdup_printf ("%s\n", _("file1 file2"));
389 #endif /* USE_DIFF_VIEW */
392 s
= g_strdup_printf ("%s\n", _("[this_dir] [other_panel_dir]"));
395 mc_args__loc__usage_string
= s
;
397 return mc_args__loc__usage_string
;
400 /* --------------------------------------------------------------------------------------------- */
403 mc_args_add_extended_info_to_help (void)
405 mc_args__loc__footer_string
= g_strdup_printf ("%s",
408 "Please send any bug reports (including the output of 'mc -V')\n"
409 "as tickets at www.midnight-commander.org\n"));
410 mc_args__loc__header_string
=
411 g_strdup_printf (_("GNU Midnight Commander %s\n"), mc_global
.mc_version
);
413 g_option_context_set_description (context
, mc_args__loc__footer_string
);
414 g_option_context_set_summary (context
, mc_args__loc__header_string
);
417 /* --------------------------------------------------------------------------------------------- */
420 mc_args__convert_help_to_syscharset (const gchar
* charset
, const gchar
* error_message_str
,
421 const gchar
* help_str
)
425 gchar
*full_help_str
;
427 buffer
= g_string_new ("");
428 conv
= g_iconv_open (charset
, "UTF-8");
429 full_help_str
= g_strdup_printf ("%s\n\n%s\n", error_message_str
, help_str
);
431 str_convert (conv
, full_help_str
, buffer
);
433 g_free (full_help_str
);
434 g_iconv_close (conv
);
439 /* --------------------------------------------------------------------------------------------- */
442 parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
,
449 mc_return_val_if_error (mcerror
, FALSE
);
451 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
456 /* --------------------------------------------------------------------------------------------- */
459 parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
,
466 mc_return_val_if_error (mcerror
, FALSE
);
468 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
473 /* --------------------------------------------------------------------------------------------- */
475 * Create mcedit_arg_t object from vfs_path_t object and the line number.
477 * @param file_vpath file path object
478 * @param line_number line number. If value is 0, try to restore saved position.
479 * @return mcedit_arg_t object
482 static mcedit_arg_t
*
483 mcedit_arg_vpath_new (vfs_path_t
* file_vpath
, long line_number
)
487 arg
= g_new (mcedit_arg_t
, 1);
488 arg
->file_vpath
= file_vpath
;
489 arg
->line_number
= line_number
;
494 /* --------------------------------------------------------------------------------------------- */
496 * Create mcedit_arg_t object from file name and the line number.
498 * @param file_name file name
499 * @param line_number line number. If value is 0, try to restore saved position.
500 * @return mcedit_arg_t object
503 static mcedit_arg_t
*
504 mcedit_arg_new (const char *file_name
, long line_number
)
506 return mcedit_arg_vpath_new (vfs_path_from_str (file_name
), line_number
);
509 /* --------------------------------------------------------------------------------------------- */
511 * Get list of filenames (and line numbers) from command line, when mc called as editor
513 * @param argc count of all arguments
514 * @param argv array of strings, contains arguments
515 * @return list of mcedit_arg_t objects
519 parse_mcedit_arguments (int argc
, char **argv
)
523 long first_line_number
= -1;
525 for (i
= 0; i
< argc
; i
++)
534 * First, try to get line number as +lineno.
541 lineno
= strtol (tmp
+ 1, &error
, 10);
545 /* this is line number */
546 first_line_number
= lineno
;
549 /* this is file name */
553 * Check for filename:lineno, followed by an optional colon.
554 * This format is used by many programs (especially compilers)
555 * in error messages and warnings. It is supported so that
556 * users can quickly copy and paste file locations.
558 end
= tmp
+ strlen (tmp
);
561 if (p
> tmp
&& p
[-1] == ':')
563 while (p
> tmp
&& g_ascii_isdigit ((gchar
) p
[-1]))
566 if (tmp
< p
&& p
< end
&& p
[-1] == ':')
569 vfs_path_t
*tmp_vpath
, *fname_vpath
;
572 fname
= g_strndup (tmp
, p
- 1 - tmp
);
573 tmp_vpath
= vfs_path_from_str (tmp
);
574 fname_vpath
= vfs_path_from_str (fname
);
577 * Check that the file before the colon actually exists.
578 * If it doesn't exist, create new file.
580 if (mc_stat (tmp_vpath
, &st
) == -1 && mc_stat (fname_vpath
, &st
) != -1)
582 arg
= mcedit_arg_vpath_new (fname_vpath
, atoi (p
));
583 vfs_path_free (tmp_vpath
, TRUE
);
587 arg
= mcedit_arg_vpath_new (tmp_vpath
, 0);
588 vfs_path_free (fname_vpath
, TRUE
);
594 arg
= mcedit_arg_new (tmp
, 0);
596 flist
= g_list_prepend (flist
, arg
);
600 flist
= g_list_prepend (flist
, mcedit_arg_new (NULL
, 0));
601 else if (first_line_number
!= -1)
603 /* overwrite line number for first file */
606 l
= g_list_last (flist
);
607 ((mcedit_arg_t
*) l
->data
)->line_number
= first_line_number
;
613 /* --------------------------------------------------------------------------------------------- */
614 /*** public functions ****************************************************************************/
615 /* --------------------------------------------------------------------------------------------- */
618 mc_setup_run_mode (char **argv
)
622 base
= x_basename (argv
[0]);
624 if (strncmp (base
, "mce", 3) == 0 || strcmp (base
, "vi") == 0)
626 /* mce* or vi is link to mc */
627 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
629 else if (strncmp (base
, "mcv", 3) == 0 || strcmp (base
, "view") == 0)
631 /* mcv* or view is link to mc */
632 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
635 else if (strncmp (base
, "mcd", 3) == 0 || strcmp (base
, "diff") == 0)
637 /* mcd* or diff is link to mc */
638 mc_global
.mc_run_mode
= MC_RUN_DIFFVIEWER
;
640 #endif /* USE_DIFF_VIEW */
644 mc_args_parse (int *argc
, char ***argv
, const char *translation_domain
, GError
** mcerror
)
646 const gchar
*_system_codepage
;
649 mc_return_val_if_error (mcerror
, FALSE
);
651 _system_codepage
= str_detect_termencoding ();
654 if (!str_isutf8 (_system_codepage
))
655 bind_textdomain_codeset ("mc", "UTF-8");
658 context
= g_option_context_new (mc_args_add_usage_info ());
660 g_option_context_set_ignore_unknown_options (context
, FALSE
);
662 mc_args_add_extended_info_to_help ();
664 main_group
= g_option_group_new ("main", _("Main options"), _("Main options"), NULL
, NULL
);
666 g_option_group_add_entries (main_group
, argument_main_table
);
667 g_option_context_set_main_group (context
, main_group
);
668 g_option_group_set_translation_domain (main_group
, translation_domain
);
670 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
671 _("Terminal options"), NULL
, NULL
);
673 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
674 g_option_context_add_group (context
, terminal_group
);
675 g_option_group_set_translation_domain (terminal_group
, translation_domain
);
677 color_group
= mc_args_new_color_group ();
679 g_option_group_add_entries (color_group
, argument_color_table
);
680 g_option_context_add_group (context
, color_group
);
681 g_option_group_set_translation_domain (color_group
, translation_domain
);
683 if (!g_option_context_parse (context
, argc
, argv
, mcerror
))
685 if (*mcerror
== NULL
)
686 mc_propagate_error (mcerror
, 0, "%s\n", _("Arguments parse error!"));
691 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
693 if (str_isutf8 (_system_codepage
))
694 mc_replace_error (mcerror
, (*mcerror
)->code
, "%s\n\n%s\n", (*mcerror
)->message
,
698 GString
*full_help_str
;
701 mc_args__convert_help_to_syscharset (_system_codepage
, (*mcerror
)->message
,
703 mc_replace_error (mcerror
, (*mcerror
)->code
, "%s", full_help_str
->str
);
704 g_string_free (full_help_str
, TRUE
);
712 g_option_context_free (context
);
713 mc_args_clean_temp_help_strings ();
716 if (!str_isutf8 (_system_codepage
))
717 bind_textdomain_codeset ("mc", _system_codepage
);
723 /* --------------------------------------------------------------------------------------------- */
726 mc_args_show_info (void)
728 if (mc_args__show_version
)
734 if (mc_args__show_datadirs
)
736 printf ("%s (%s)\n", mc_global
.sysconfig_dir
, mc_global
.share_data_dir
);
740 if (mc_args__show_datadirs_extended
)
742 show_datadirs_extended ();
746 #ifdef ENABLE_CONFIGURE_ARGS
747 if (mc_args__show_configure_opts
)
749 show_configure_options ();
757 /* --------------------------------------------------------------------------------------------- */
760 mc_setup_by_args (int argc
, char **argv
, GError
** mcerror
)
764 mc_return_val_if_error (mcerror
, FALSE
);
766 if (mc_args__force_colors
)
767 mc_global
.tty
.disable_colors
= FALSE
;
769 #ifdef ENABLE_SUBSHELL
770 if (mc_args__nouse_subshell
)
771 mc_global
.tty
.use_subshell
= FALSE
;
772 #endif /* ENABLE_SUBSHELL */
774 #ifdef ENABLE_VFS_FTP
775 if (mc_args__netfs_logfile
!= NULL
)
779 vpath
= vfs_path_from_str ("ftp://");
780 mc_setctl (vpath
, VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
781 vfs_path_free (vpath
, TRUE
);
783 #endif /* ENABLE_VFS_FTP */
785 tmp
= (argc
> 0) ? argv
[1] : NULL
;
787 switch (mc_global
.mc_run_mode
)
790 mc_run_param0
= parse_mcedit_arguments (argc
- 1, &argv
[1]);
796 mc_propagate_error (mcerror
, 0, "%s\n", _("No arguments given to the viewer."));
800 mc_run_param0
= g_strdup (tmp
);
804 case MC_RUN_DIFFVIEWER
:
807 mc_propagate_error (mcerror
, 0, "%s\n",
808 _("Two files are required to invoke the diffviewer."));
812 #endif /* USE_DIFF_VIEW */
816 /* set the current dir and the other dir for filemanager,
817 or two files for diff viewer */
820 mc_run_param0
= g_strdup (tmp
);
821 tmp
= (argc
> 1) ? argv
[2] : NULL
;
823 mc_run_param1
= g_strdup (tmp
);
831 /* --------------------------------------------------------------------------------------------- */
833 * Free the mcedit_arg_t object.
835 * @param arg mcedit_arg_t object
839 mcedit_arg_free (mcedit_arg_t
* arg
)
841 vfs_path_free (arg
->file_vpath
, TRUE
);
845 /* --------------------------------------------------------------------------------------------- */