2 Handle command line arguments.
4 Copyright (C) 2009-2017
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() */
38 #include "src/vfs/smbfs/smbfs.h" /* smbfs_set_debugf() */
41 #include "src/textconf.h"
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 folowing file as logfile */
63 char *mc_args__netfs_logfile
= NULL
;
66 char *mc_args__keymap_file
= NULL
;
70 int mc_args__debug_level
= 0;
73 void *mc_run_param0
= NULL
;
74 char *mc_run_param1
= NULL
;
76 /*** file scope macro definitions ****************************************************************/
78 /*** file scope type declarations ****************************************************************/
80 /*** file scope variables ************************************************************************/
82 /* If true, show version info and exit */
83 static gboolean mc_args__show_version
= FALSE
;
85 /* forward declarations */
86 static gboolean
parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
,
87 gpointer data
, GError
** mcerror
);
88 static gboolean
parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
,
89 gpointer data
, GError
** mcerror
);
91 static GOptionContext
*context
;
93 #ifdef ENABLE_SUBSHELL
94 static gboolean mc_args__nouse_subshell
= FALSE
;
95 #endif /* ENABLE_SUBSHELL */
96 static gboolean mc_args__show_datadirs
= FALSE
;
97 static gboolean mc_args__show_datadirs_extended
= FALSE
;
98 static gboolean mc_args__show_configure_opts
= FALSE
;
100 static GOptionGroup
*main_group
;
102 static const GOptionEntry argument_main_table
[] = {
104 /* generic options */
106 "version", 'V', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
107 &mc_args__show_version
,
108 N_("Displays the current version"),
112 /* options for wrappers */
114 "datadir", 'f', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
115 &mc_args__show_datadirs
,
116 N_("Print data directory"),
120 /* show extended information about used data directories */
122 "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
123 &mc_args__show_datadirs_extended
,
124 N_("Print extended info about used data directories"),
128 /* show configure options */
130 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
131 &mc_args__show_configure_opts
,
132 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 */
168 #ifdef ENABLE_VFS_SMB
170 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_INT
,
171 &mc_args__debug_level
,
172 N_("Set debug level"),
175 #endif /* ENABLE_VFS_SMB */
178 /* handle arguments manually */
179 "view", 'v', G_OPTION_FLAG_IN_MAIN
| G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
180 (gpointer
) parse_mc_v_argument
,
181 N_("Launches the file viewer on a file"),
186 /* handle arguments manually */
187 "edit", 'e', G_OPTION_FLAG_IN_MAIN
| G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
188 (gpointer
) parse_mc_e_argument
,
193 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
198 static GOptionGroup
*terminal_group
;
199 #define ARGS_TERM_OPTIONS 0
200 static const GOptionEntry argument_terminal_table
[] = {
202 /* terminal options */
204 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
205 &mc_args__force_xterm
,
206 N_("Forces xterm features"),
211 "no-x11", 'X', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
212 &mc_global
.tty
.disable_x11
,
213 N_("Disable X11 support"),
218 "oldmouse", 'g', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
219 &mc_global
.tty
.old_mouse
,
220 N_("Tries to use an old highlight mouse tracking"),
225 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
227 N_("Disable mouse support in text version"),
233 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
235 N_("Tries to use termcap instead of terminfo"),
241 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
242 &mc_global
.tty
.slow_terminal
,
243 N_("To run on slow terminals"),
248 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
249 &mc_global
.tty
.ugly_line_drawing
,
250 N_("Use stickchars to draw"),
256 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
258 N_("Resets soft keys on HP terminals"),
264 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
265 &mc_args__keymap_file
,
266 N_("Load definitions of key bindings from specified file"),
271 "nokeymap", '\0', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
273 N_("Don't load definitions of key bindings from file, use defaults"),
278 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
283 #undef ARGS_TERM_OPTIONS
285 static GOptionGroup
*color_group
;
286 #define ARGS_COLOR_OPTIONS 0
287 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
288 static const GOptionEntry argument_color_table
[] = {
292 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
293 &mc_global
.tty
.disable_colors
,
294 N_("Requests to run in black and white"),
299 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
300 &mc_args__force_colors
,
301 N_("Request to run in color mode"),
306 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
307 &mc_global
.tty
.command_line_colors
,
308 N_("Specifies a color configuration"),
313 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
315 N_("Show mc with specified skin"),
320 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
325 #undef ARGS_COLOR_OPTIONS
327 static gchar
*mc_args__loc__colors_string
= NULL
;
328 static gchar
*mc_args__loc__footer_string
= NULL
;
329 static gchar
*mc_args__loc__header_string
= NULL
;
330 static gchar
*mc_args__loc__usage_string
= NULL
;
332 /*** file scope functions ************************************************************************/
334 /* --------------------------------------------------------------------------------------------- */
336 mc_args_clean_temp_help_strings (void)
338 MC_PTR_FREE (mc_args__loc__colors_string
);
339 MC_PTR_FREE (mc_args__loc__footer_string
);
340 MC_PTR_FREE (mc_args__loc__header_string
);
341 MC_PTR_FREE (mc_args__loc__usage_string
);
344 /* --------------------------------------------------------------------------------------------- */
346 static GOptionGroup
*
347 mc_args_new_color_group (void)
350 /* FIXME: to preserve translations, lines should be split. */
351 mc_args__loc__colors_string
= g_strdup_printf ("%s\n%s",
352 /* TRANSLATORS: don't translate keywords */
353 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
354 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
356 " Global: errors, disabled, reverse, gauge, header\n"
357 " input, inputmark, inputunchanged, commandlinemark\n"
358 " bbarhotkey, bbarbutton, statusbar\n"
359 " File display: normal, selected, marked, markselect\n"
360 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
362 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
363 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
364 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
365 " editlinestate, editbg, editframe, editframeactive\n"
367 " Viewer: viewnormal,viewbold, viewunderline, viewselected\n"
368 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
369 /* TRANSLATORS: don't translate color names and attributes */
370 _("Standard Colors:\n"
371 " black, gray, red, brightred, green, brightgreen, brown,\n"
372 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
373 " brightcyan, lightgray and white\n\n"
374 "Extended colors, when 256 colors are available:\n"
375 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
377 " bold, italic, underline, reverse, blink; append more with '+'\n")
381 return g_option_group_new ("color", mc_args__loc__colors_string
,
382 _("Color options"), NULL
, NULL
);
386 /* --------------------------------------------------------------------------------------------- */
389 mc_args_add_usage_info (void)
391 mc_args__loc__usage_string
= g_strdup_printf ("[%s] %s\n %s - %s\n",
393 _("[this_dir] [other_panel_dir]"),
396 ("Set initial line number for the internal editor"));
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
= g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION
);
412 g_option_context_set_description (context
, mc_args__loc__footer_string
);
413 g_option_context_set_summary (context
, mc_args__loc__header_string
);
416 /* --------------------------------------------------------------------------------------------- */
419 mc_args__convert_help_to_syscharset (const gchar
* charset
, const gchar
* error_message_str
,
420 const gchar
* help_str
)
424 gchar
*full_help_str
;
426 buffer
= g_string_new ("");
427 conv
= g_iconv_open (charset
, "UTF-8");
428 full_help_str
= g_strdup_printf ("%s\n\n%s\n", error_message_str
, help_str
);
430 str_convert (conv
, full_help_str
, buffer
);
432 g_free (full_help_str
);
433 g_iconv_close (conv
);
435 return g_string_free (buffer
, FALSE
);
438 /* --------------------------------------------------------------------------------------------- */
441 parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
,
448 mc_return_val_if_error (mcerror
, FALSE
);
450 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
455 /* --------------------------------------------------------------------------------------------- */
458 parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
,
465 mc_return_val_if_error (mcerror
, FALSE
);
467 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
472 /* --------------------------------------------------------------------------------------------- */
474 * Create mcedit_arg_t object from vfs_path_t object and the line number.
476 * @param file_vpath file path object
477 * @param line_number line number. If value is 0, try to restore saved position.
478 * @return mcedit_arg_t object
481 static mcedit_arg_t
*
482 mcedit_arg_vpath_new (vfs_path_t
* file_vpath
, long line_number
)
486 arg
= g_new (mcedit_arg_t
, 1);
487 arg
->file_vpath
= file_vpath
;
488 arg
->line_number
= line_number
;
493 /* --------------------------------------------------------------------------------------------- */
495 * Create mcedit_arg_t object from file name and the line number.
497 * @param file_name file name
498 * @param line_number line number. If value is 0, try to restore saved position.
499 * @return mcedit_arg_t object
502 static mcedit_arg_t
*
503 mcedit_arg_new (const char *file_name
, long line_number
)
505 return mcedit_arg_vpath_new (vfs_path_from_str (file_name
), line_number
);
508 /* --------------------------------------------------------------------------------------------- */
510 * Get list of filenames (and line numbers) from command line, when mc called as editor
512 * @param argc count of all arguments
513 * @param argv array of strings, contains arguments
514 * @return list of mcedit_arg_t objects
518 parse_mcedit_arguments (int argc
, char **argv
)
522 long first_line_number
= -1;
524 for (i
= 0; i
< argc
; i
++)
533 * First, try to get line number as +lineno.
540 lineno
= strtol (tmp
+ 1, &error
, 10);
544 /* this is line number */
545 first_line_number
= lineno
;
548 /* this is file name */
552 * Check for filename:lineno, followed by an optional colon.
553 * This format is used by many programs (especially compilers)
554 * in error messages and warnings. It is supported so that
555 * users can quickly copy and paste file locations.
557 end
= tmp
+ strlen (tmp
);
560 if (p
> tmp
&& p
[-1] == ':')
562 while (p
> tmp
&& g_ascii_isdigit ((gchar
) p
[-1]))
565 if (tmp
< p
&& p
< end
&& p
[-1] == ':')
568 vfs_path_t
*tmp_vpath
, *fname_vpath
;
571 fname
= g_strndup (tmp
, p
- 1 - tmp
);
572 tmp_vpath
= vfs_path_from_str (tmp
);
573 fname_vpath
= vfs_path_from_str (fname
);
576 * Check that the file before the colon actually exists.
577 * If it doesn't exist, create new file.
579 if (mc_stat (tmp_vpath
, &st
) == -1 && mc_stat (fname_vpath
, &st
) != -1)
581 arg
= mcedit_arg_vpath_new (fname_vpath
, atoi (p
));
582 vfs_path_free (tmp_vpath
);
586 arg
= mcedit_arg_vpath_new (tmp_vpath
, 0);
587 vfs_path_free (fname_vpath
);
593 arg
= mcedit_arg_new (tmp
, 0);
595 flist
= g_list_prepend (flist
, arg
);
599 flist
= g_list_prepend (flist
, mcedit_arg_new (NULL
, 0));
600 else if (first_line_number
!= -1)
602 /* overwrite line number for first file */
605 l
= g_list_last (flist
);
606 ((mcedit_arg_t
*) l
->data
)->line_number
= first_line_number
;
612 /* --------------------------------------------------------------------------------------------- */
613 /*** public functions ****************************************************************************/
614 /* --------------------------------------------------------------------------------------------- */
617 mc_args_parse (int *argc
, char ***argv
, const char *translation_domain
, GError
** mcerror
)
619 const gchar
*_system_codepage
;
622 mc_return_val_if_error (mcerror
, FALSE
);
624 _system_codepage
= str_detect_termencoding ();
627 if (!str_isutf8 (_system_codepage
))
628 bind_textdomain_codeset ("mc", "UTF-8");
631 context
= g_option_context_new (mc_args_add_usage_info ());
633 g_option_context_set_ignore_unknown_options (context
, FALSE
);
635 mc_args_add_extended_info_to_help ();
637 main_group
= g_option_group_new ("main", _("Main options"), _("Main options"), NULL
, NULL
);
639 g_option_group_add_entries (main_group
, argument_main_table
);
640 g_option_context_set_main_group (context
, main_group
);
641 g_option_group_set_translation_domain (main_group
, translation_domain
);
643 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
644 _("Terminal options"), NULL
, NULL
);
646 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
647 g_option_context_add_group (context
, terminal_group
);
648 g_option_group_set_translation_domain (terminal_group
, translation_domain
);
650 color_group
= mc_args_new_color_group ();
652 g_option_group_add_entries (color_group
, argument_color_table
);
653 g_option_context_add_group (context
, color_group
);
654 g_option_group_set_translation_domain (color_group
, translation_domain
);
656 if (!g_option_context_parse (context
, argc
, argv
, mcerror
))
658 if (*mcerror
== NULL
)
659 mc_propagate_error (mcerror
, 0, "%s\n", _("Arguments parse error!"));
664 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
666 if (str_isutf8 (_system_codepage
))
667 mc_replace_error (mcerror
, (*mcerror
)->code
, "%s\n\n%s\n", (*mcerror
)->message
,
671 gchar
*full_help_str
;
674 mc_args__convert_help_to_syscharset (_system_codepage
, (*mcerror
)->message
,
676 mc_replace_error (mcerror
, (*mcerror
)->code
, "%s", full_help_str
);
677 g_free (full_help_str
);
685 g_option_context_free (context
);
686 mc_args_clean_temp_help_strings ();
689 if (!str_isutf8 (_system_codepage
))
690 bind_textdomain_codeset ("mc", _system_codepage
);
696 /* --------------------------------------------------------------------------------------------- */
699 mc_args_show_info (void)
701 if (mc_args__show_version
)
707 if (mc_args__show_datadirs
)
709 printf ("%s (%s)\n", mc_global
.sysconfig_dir
, mc_global
.share_data_dir
);
713 if (mc_args__show_datadirs_extended
)
715 show_datadirs_extended ();
719 if (mc_args__show_configure_opts
)
721 show_configure_options ();
728 /* --------------------------------------------------------------------------------------------- */
731 mc_setup_by_args (int argc
, char **argv
, GError
** mcerror
)
736 mc_return_val_if_error (mcerror
, FALSE
);
738 if (mc_args__force_colors
)
739 mc_global
.tty
.disable_colors
= FALSE
;
741 #ifdef ENABLE_SUBSHELL
742 if (mc_args__nouse_subshell
)
743 mc_global
.tty
.use_subshell
= FALSE
;
744 #endif /* ENABLE_SUBSHELL */
746 #ifdef ENABLE_VFS_SMB
747 if (mc_args__debug_level
!= 0)
748 smbfs_set_debug (mc_args__debug_level
);
749 #endif /* ENABLE_VFS_SMB */
751 if (mc_args__netfs_logfile
!= NULL
)
754 #ifdef ENABLE_VFS_FTP
755 vpath
= vfs_path_from_str ("ftp://");
756 mc_setctl (vpath
, VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
757 vfs_path_free (vpath
);
758 #endif /* ENABLE_VFS_FTP */
759 #ifdef ENABLE_VFS_SMB
760 vpath
= vfs_path_from_str ("smb://");
761 mc_setctl (vpath
, VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
762 vfs_path_free (vpath
);
763 #endif /* ENABLE_VFS_SMB */
767 base
= x_basename (argv
[0]);
768 tmp
= (argc
> 0) ? argv
[1] : NULL
;
770 if (strncmp (base
, "mce", 3) == 0 || strcmp (base
, "vi") == 0)
772 /* mce* or vi is link to mc */
773 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
775 else if (strncmp (base
, "mcv", 3) == 0 || strcmp (base
, "view") == 0)
777 /* mcv* or view is link to mc */
778 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
781 else if (strncmp (base
, "mcd", 3) == 0 || strcmp (base
, "diff") == 0)
783 /* mcd* or diff is link to mc */
784 mc_global
.mc_run_mode
= MC_RUN_DIFFVIEWER
;
786 #endif /* USE_DIFF_VIEW */
788 switch (mc_global
.mc_run_mode
)
791 mc_run_param0
= parse_mcedit_arguments (argc
- 1, &argv
[1]);
797 mc_propagate_error (mcerror
, 0, "%s\n", _("No arguments given to the viewer."));
801 mc_run_param0
= g_strdup (tmp
);
805 case MC_RUN_DIFFVIEWER
:
808 mc_propagate_error (mcerror
, 0, "%s\n",
809 _("Two files are required to envoke the diffviewer."));
813 #endif /* USE_DIFF_VIEW */
817 /* set the current dir and the other dir for filemanager,
818 or two files for diff viewer */
821 mc_run_param0
= g_strdup (tmp
);
822 tmp
= (argc
> 1) ? argv
[2] : NULL
;
824 mc_run_param1
= g_strdup (tmp
);
832 /* --------------------------------------------------------------------------------------------- */
834 * Free the mcedit_arg_t object.
836 * @param arg mcedit_arg_t object
840 mcedit_arg_free (mcedit_arg_t
* arg
)
842 vfs_path_free (arg
->file_vpath
);
846 /* --------------------------------------------------------------------------------------------- */