2 Handle command line arguments.
4 Copyright (C) 2009-2024
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"
39 #ifdef USE_INTERNAL_EDIT
40 #include "editor/edit.h" /* edit_arg_t */
45 /*** external variables **************************************************************************/
47 /*** global variables ****************************************************************************/
49 /* If true, assume we are running on an xterm terminal */
50 gboolean mc_args__force_xterm
= FALSE
;
52 gboolean mc_args__nomouse
= FALSE
;
54 /* Force colors, only used by Slang */
55 gboolean mc_args__force_colors
= FALSE
;
57 /* Don't load keymap from file and use default one */
58 gboolean mc_args__nokeymap
= FALSE
;
60 char *mc_args__last_wd_file
= NULL
;
62 /* when enabled NETCODE, use following file as logfile */
63 char *mc_args__netfs_logfile
= NULL
;
66 char *mc_args__keymap_file
= NULL
;
68 void *mc_run_param0
= NULL
;
69 char *mc_run_param1
= NULL
;
71 /*** file scope macro definitions ****************************************************************/
73 /*** file scope type declarations ****************************************************************/
75 /*** forward declarations (file scope functions) *************************************************/
77 static gboolean
parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
,
78 gpointer data
, GError
** mcerror
);
79 static gboolean
parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
,
80 gpointer data
, GError
** mcerror
);
82 /*** file scope variables ************************************************************************/
84 /* If true, show version info and exit */
85 static gboolean mc_args__show_version
= FALSE
;
87 static GOptionContext
*context
;
89 #ifdef ENABLE_SUBSHELL
90 static gboolean mc_args__nouse_subshell
= FALSE
;
91 #endif /* ENABLE_SUBSHELL */
92 static gboolean mc_args__show_datadirs
= FALSE
;
93 static gboolean mc_args__show_datadirs_extended
= FALSE
;
94 #ifdef ENABLE_CONFIGURE_ARGS
95 static gboolean mc_args__show_configure_opts
= FALSE
;
98 static GOptionGroup
*main_group
;
100 static const GOptionEntry argument_main_table
[] = {
102 /* generic options */
104 "version", 'V', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
105 &mc_args__show_version
,
106 N_("Displays the current version"),
110 /* options for wrappers */
112 "datadir", 'f', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
113 &mc_args__show_datadirs
,
114 N_("Print data directory"),
118 /* show extended information about used data directories */
120 "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
121 &mc_args__show_datadirs_extended
,
122 N_("Print extended info about used data directories"),
126 #ifdef ENABLE_CONFIGURE_ARGS
127 /* show configure options */
129 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
130 &mc_args__show_configure_opts
,
131 N_("Print configure options"),
137 "printwd", 'P', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
138 &mc_args__last_wd_file
,
139 N_("Print last working directory to specified file"),
143 #ifdef ENABLE_SUBSHELL
145 "subshell", 'U', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
146 &mc_global
.tty
.use_subshell
,
147 N_("Enables subshell support (default)"),
152 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
153 &mc_args__nouse_subshell
,
154 N_("Disables subshell support"),
160 #ifdef ENABLE_VFS_FTP
162 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
163 &mc_args__netfs_logfile
,
164 N_("Log ftp dialog to specified file"),
167 #endif /* ENABLE_VFS_FTP */
170 /* handle arguments manually */
171 "view", 'v', G_OPTION_FLAG_IN_MAIN
| G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
172 (gpointer
) parse_mc_v_argument
,
173 N_("Launches the file viewer on a file"),
178 /* handle arguments manually */
179 "edit", 'e', G_OPTION_FLAG_IN_MAIN
| G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
180 (gpointer
) parse_mc_e_argument
,
189 static GOptionGroup
*terminal_group
;
190 #define ARGS_TERM_OPTIONS 0
191 static const GOptionEntry argument_terminal_table
[] = {
193 /* terminal options */
195 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
196 &mc_args__force_xterm
,
197 N_("Forces xterm features"),
202 "no-x11", 'X', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
203 &mc_global
.tty
.disable_x11
,
204 N_("Disable X11 support"),
209 "oldmouse", 'g', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
210 &mc_global
.tty
.old_mouse
,
211 N_("Tries to use an old highlight mouse tracking"),
216 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
218 N_("Disable mouse support in text version"),
224 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
226 N_("Tries to use termcap instead of terminfo"),
232 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
233 &mc_global
.tty
.slow_terminal
,
234 N_("To run on slow terminals"),
239 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
240 &mc_global
.tty
.ugly_line_drawing
,
241 N_("Use stickchars to draw"),
247 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
249 N_("Resets soft keys on HP terminals"),
255 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
256 &mc_args__keymap_file
,
257 N_("Load definitions of key bindings from specified file"),
262 "nokeymap", '\0', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
264 N_("Don't load definitions of key bindings from file, use defaults"),
272 #undef ARGS_TERM_OPTIONS
274 static GOptionGroup
*color_group
;
275 #define ARGS_COLOR_OPTIONS 0
276 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
277 static const GOptionEntry argument_color_table
[] = {
281 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
282 &mc_global
.tty
.disable_colors
,
283 N_("Requests to run in black and white"),
288 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
289 &mc_args__force_colors
,
290 N_("Request to run in color mode"),
295 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
296 &mc_global
.tty
.command_line_colors
,
297 N_("Specifies a color configuration"),
302 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
304 N_("Show mc with specified skin"),
312 #undef ARGS_COLOR_OPTIONS
314 static gchar
*mc_args__loc__colors_string
= NULL
;
315 static gchar
*mc_args__loc__footer_string
= NULL
;
316 static gchar
*mc_args__loc__header_string
= NULL
;
317 static gchar
*mc_args__loc__usage_string
= NULL
;
319 /* --------------------------------------------------------------------------------------------- */
320 /*** file scope functions ************************************************************************/
321 /* --------------------------------------------------------------------------------------------- */
324 mc_args_clean_temp_help_strings (void)
326 MC_PTR_FREE (mc_args__loc__colors_string
);
327 MC_PTR_FREE (mc_args__loc__footer_string
);
328 MC_PTR_FREE (mc_args__loc__header_string
);
329 MC_PTR_FREE (mc_args__loc__usage_string
);
332 /* --------------------------------------------------------------------------------------------- */
334 static GOptionGroup
*
335 mc_args_new_color_group (void)
338 /* FIXME: to preserve translations, lines should be split. */
339 mc_args__loc__colors_string
= g_strdup_printf ("%s\n%s",
340 /* TRANSLATORS: don't translate keywords */
341 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
342 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
344 " Global: errors, disabled, reverse, gauge, header\n"
345 " input, inputmark, inputunchanged, commandlinemark\n"
346 " bbarhotkey, bbarbutton, statusbar\n"
347 " File display: normal, selected, marked, markselect\n"
348 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
350 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
351 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
352 " Editor: editnormal, editbold, editmarked, editwhitespace, editnonprintable,\n"
353 " editlinestate, editbg, editframe, editframeactive\n"
355 " Viewer: viewnormal,viewbold, viewunderline, viewselected\n"
356 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
357 /* TRANSLATORS: don't translate color names and attributes */
358 _("Standard Colors:\n"
359 " black, gray, red, brightred, green, brightgreen, brown,\n"
360 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
361 " brightcyan, lightgray and white\n\n"
362 "Extended colors, when 256 colors are available:\n"
363 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
365 " bold, italic, underline, reverse, blink; append more with '+'\n")
369 return g_option_group_new ("color", mc_args__loc__colors_string
,
370 _("Color options"), NULL
, NULL
);
374 /* --------------------------------------------------------------------------------------------- */
377 mc_args_add_usage_info (void)
381 switch (mc_global
.mc_run_mode
)
383 #ifdef USE_INTERNAL_EDIT
385 s
= g_strdup_printf ("%s\n", _("[+lineno] file1[:lineno] [file2[:lineno]...]"));
387 #endif /* USE_INTERNAL_EDIT */
389 s
= g_strdup_printf ("%s\n", _("file"));
392 case MC_RUN_DIFFVIEWER
:
393 s
= g_strdup_printf ("%s\n", _("file1 file2"));
395 #endif /* USE_DIFF_VIEW */
398 s
= g_strdup_printf ("%s\n", _("[this_dir] [other_panel_dir]"));
401 mc_args__loc__usage_string
= s
;
403 return mc_args__loc__usage_string
;
406 /* --------------------------------------------------------------------------------------------- */
409 mc_args_add_extended_info_to_help (void)
411 mc_args__loc__footer_string
= g_strdup_printf ("%s",
414 "Please send any bug reports (including the output of 'mc -V')\n"
415 "as tickets at www.midnight-commander.org\n"));
416 mc_args__loc__header_string
=
417 g_strdup_printf (_("GNU Midnight Commander %s\n"), mc_global
.mc_version
);
419 g_option_context_set_description (context
, mc_args__loc__footer_string
);
420 g_option_context_set_summary (context
, mc_args__loc__header_string
);
423 /* --------------------------------------------------------------------------------------------- */
426 mc_args__convert_help_to_syscharset (const gchar
*charset
, const gchar
*error_message_str
,
427 const gchar
*help_str
)
431 gchar
*full_help_str
;
433 buffer
= g_string_new ("");
434 conv
= g_iconv_open (charset
, "UTF-8");
435 full_help_str
= g_strdup_printf ("%s\n\n%s\n", error_message_str
, help_str
);
437 str_convert (conv
, full_help_str
, buffer
);
439 g_free (full_help_str
);
440 g_iconv_close (conv
);
445 /* --------------------------------------------------------------------------------------------- */
448 parse_mc_e_argument (const gchar
*option_name
, const gchar
*value
, gpointer data
, GError
**mcerror
)
454 mc_return_val_if_error (mcerror
, FALSE
);
456 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
461 /* --------------------------------------------------------------------------------------------- */
464 parse_mc_v_argument (const gchar
*option_name
, const gchar
*value
, gpointer data
, GError
**mcerror
)
470 mc_return_val_if_error (mcerror
, FALSE
);
472 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
477 /* --------------------------------------------------------------------------------------------- */
479 #ifdef USE_INTERNAL_EDIT
481 * Get list of filenames (and line numbers) from command line, when mc called as editor
483 * @param argc count of all arguments
484 * @param argv array of strings, contains arguments
485 * @return list of edit_arg_t objects
489 parse_mcedit_arguments (int argc
, char **argv
)
493 long first_line_number
= -1;
495 for (i
= 0; i
< argc
; i
++)
504 * First, try to get line number as +lineno.
511 lineno
= strtol (tmp
+ 1, &error
, 10);
515 /* this is line number */
516 first_line_number
= lineno
;
519 /* this is file name */
523 * Check for filename:lineno, followed by an optional colon.
524 * This format is used by many programs (especially compilers)
525 * in error messages and warnings. It is supported so that
526 * users can quickly copy and paste file locations.
528 end
= tmp
+ strlen (tmp
);
531 if (p
> tmp
&& p
[-1] == ':')
533 while (p
> tmp
&& g_ascii_isdigit ((gchar
) p
[-1]))
536 if (tmp
< p
&& p
< end
&& p
[-1] == ':')
539 vfs_path_t
*tmp_vpath
, *fname_vpath
;
542 fname
= g_strndup (tmp
, p
- 1 - tmp
);
543 tmp_vpath
= vfs_path_from_str (tmp
);
544 fname_vpath
= vfs_path_from_str (fname
);
547 * Check that the file before the colon actually exists.
548 * If it doesn't exist, create new file.
550 if (mc_stat (tmp_vpath
, &st
) == -1 && mc_stat (fname_vpath
, &st
) != -1)
552 arg
= edit_arg_vpath_new (fname_vpath
, atoi (p
));
553 vfs_path_free (tmp_vpath
, TRUE
);
557 arg
= edit_arg_vpath_new (tmp_vpath
, 0);
558 vfs_path_free (fname_vpath
, TRUE
);
564 arg
= edit_arg_new (tmp
, 0);
566 flist
= g_list_prepend (flist
, arg
);
570 flist
= g_list_prepend (flist
, edit_arg_new (NULL
, 0));
571 else if (first_line_number
!= -1)
573 /* overwrite line number for first file */
576 l
= g_list_last (flist
);
577 ((edit_arg_t
*) l
->data
)->line_number
= first_line_number
;
582 #endif /* USE_INTERNAL_EDIT */
584 /* --------------------------------------------------------------------------------------------- */
585 /*** public functions ****************************************************************************/
586 /* --------------------------------------------------------------------------------------------- */
589 mc_setup_run_mode (char **argv
)
593 base
= x_basename (argv
[0]);
595 if (strncmp (base
, "mcv", 3) == 0 || strcmp (base
, "view") == 0)
597 /* mcv* or view is link to mc */
598 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
600 #ifdef USE_INTERNAL_EDIT
601 else if (strncmp (base
, "mce", 3) == 0 || strcmp (base
, "vi") == 0)
603 /* mce* or vi is link to mc */
604 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
608 else if (strncmp (base
, "mcd", 3) == 0 || strcmp (base
, "diff") == 0)
610 /* mcd* or diff is link to mc */
611 mc_global
.mc_run_mode
= MC_RUN_DIFFVIEWER
;
613 #endif /* USE_DIFF_VIEW */
616 /* --------------------------------------------------------------------------------------------- */
619 mc_args_parse (int *argc
, char ***argv
, const char *translation_domain
, GError
**mcerror
)
621 const gchar
*_system_codepage
;
624 mc_return_val_if_error (mcerror
, FALSE
);
626 _system_codepage
= str_detect_termencoding ();
629 if (!str_isutf8 (_system_codepage
))
630 bind_textdomain_codeset ("mc", "UTF-8");
633 context
= g_option_context_new (mc_args_add_usage_info ());
635 g_option_context_set_ignore_unknown_options (context
, FALSE
);
637 mc_args_add_extended_info_to_help ();
639 main_group
= g_option_group_new ("main", _("Main options"), _("Main options"), NULL
, NULL
);
641 g_option_group_add_entries (main_group
, argument_main_table
);
642 g_option_context_set_main_group (context
, main_group
);
643 g_option_group_set_translation_domain (main_group
, translation_domain
);
645 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
646 _("Terminal options"), NULL
, NULL
);
648 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
649 g_option_context_add_group (context
, terminal_group
);
650 g_option_group_set_translation_domain (terminal_group
, translation_domain
);
652 color_group
= mc_args_new_color_group ();
654 g_option_group_add_entries (color_group
, argument_color_table
);
655 g_option_context_add_group (context
, color_group
);
656 g_option_group_set_translation_domain (color_group
, translation_domain
);
658 if (!g_option_context_parse (context
, argc
, argv
, mcerror
))
660 if (*mcerror
== NULL
)
661 mc_propagate_error (mcerror
, 0, "%s\n", _("Arguments parse error!"));
666 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
668 if (str_isutf8 (_system_codepage
))
669 mc_replace_error (mcerror
, (*mcerror
)->code
, "%s\n\n%s\n", (*mcerror
)->message
,
673 GString
*full_help_str
;
676 mc_args__convert_help_to_syscharset (_system_codepage
, (*mcerror
)->message
,
678 mc_replace_error (mcerror
, (*mcerror
)->code
, "%s", full_help_str
->str
);
679 g_string_free (full_help_str
, TRUE
);
687 g_option_context_free (context
);
688 mc_args_clean_temp_help_strings ();
691 if (!str_isutf8 (_system_codepage
))
692 bind_textdomain_codeset ("mc", _system_codepage
);
698 /* --------------------------------------------------------------------------------------------- */
701 mc_args_show_info (void)
703 if (mc_args__show_version
)
709 if (mc_args__show_datadirs
)
711 printf ("%s (%s)\n", mc_global
.sysconfig_dir
, mc_global
.share_data_dir
);
715 if (mc_args__show_datadirs_extended
)
717 show_datadirs_extended ();
721 #ifdef ENABLE_CONFIGURE_ARGS
722 if (mc_args__show_configure_opts
)
724 show_configure_options ();
732 /* --------------------------------------------------------------------------------------------- */
735 mc_setup_by_args (int argc
, char **argv
, GError
**mcerror
)
739 mc_return_val_if_error (mcerror
, FALSE
);
741 if (mc_args__force_colors
)
742 mc_global
.tty
.disable_colors
= FALSE
;
744 #ifdef ENABLE_SUBSHELL
745 if (mc_args__nouse_subshell
)
746 mc_global
.tty
.use_subshell
= FALSE
;
747 #endif /* ENABLE_SUBSHELL */
749 #ifdef ENABLE_VFS_FTP
750 if (mc_args__netfs_logfile
!= NULL
)
754 vpath
= vfs_path_from_str ("ftp://");
755 mc_setctl (vpath
, VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
756 vfs_path_free (vpath
, TRUE
);
758 #endif /* ENABLE_VFS_FTP */
760 tmp
= (argc
> 0) ? argv
[1] : NULL
;
762 switch (mc_global
.mc_run_mode
)
765 #ifdef USE_INTERNAL_EDIT
766 mc_run_param0
= parse_mcedit_arguments (argc
- 1, &argv
[1]);
769 mc_propagate_error (mcerror
, 0, "%s\n", _("MC is built without builtin editor."));
776 mc_propagate_error (mcerror
, 0, "%s\n", _("No arguments given to the viewer."));
780 mc_run_param0
= g_strdup (tmp
);
784 case MC_RUN_DIFFVIEWER
:
787 mc_propagate_error (mcerror
, 0, "%s\n",
788 _("Two files are required to invoke the diffviewer."));
792 #endif /* USE_DIFF_VIEW */
796 /* set the current dir and the other dir for filemanager,
797 or two files for diff viewer */
800 mc_run_param0
= g_strdup (tmp
);
801 tmp
= (argc
> 1) ? argv
[2] : NULL
;
803 mc_run_param1
= g_strdup (tmp
);
811 /* --------------------------------------------------------------------------------------------- */