2 Handle command line arguments.
4 Copyright (C) 2009-2019
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 #ifdef ENABLE_CONFIGURE_ARGS
99 static gboolean mc_args__show_configure_opts
= FALSE
;
102 static GOptionGroup
*main_group
;
104 static const GOptionEntry argument_main_table
[] = {
106 /* generic options */
108 "version", 'V', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
109 &mc_args__show_version
,
110 N_("Displays the current version"),
114 /* options for wrappers */
116 "datadir", 'f', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
117 &mc_args__show_datadirs
,
118 N_("Print data directory"),
122 /* show extended information about used data directories */
124 "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
125 &mc_args__show_datadirs_extended
,
126 N_("Print extended info about used data directories"),
130 #ifdef ENABLE_CONFIGURE_ARGS
131 /* show configure options */
133 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
134 &mc_args__show_configure_opts
,
135 N_("Print configure options"),
141 "printwd", 'P', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
142 &mc_args__last_wd_file
,
143 N_("Print last working directory to specified file"),
147 #ifdef ENABLE_SUBSHELL
149 "subshell", 'U', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
150 &mc_global
.tty
.use_subshell
,
151 N_("Enables subshell support (default)"),
156 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
157 &mc_args__nouse_subshell
,
158 N_("Disables subshell support"),
164 #ifdef ENABLE_VFS_FTP
166 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
167 &mc_args__netfs_logfile
,
168 N_("Log ftp dialog to specified file"),
171 #endif /* ENABLE_VFS_FTP */
172 #ifdef ENABLE_VFS_SMB
174 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_INT
,
175 &mc_args__debug_level
,
176 N_("Set debug level"),
179 #endif /* ENABLE_VFS_SMB */
182 /* handle arguments manually */
183 "view", 'v', G_OPTION_FLAG_IN_MAIN
| G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
184 (gpointer
) parse_mc_v_argument
,
185 N_("Launches the file viewer on a file"),
190 /* handle arguments manually */
191 "edit", 'e', G_OPTION_FLAG_IN_MAIN
| G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
,
192 (gpointer
) parse_mc_e_argument
,
197 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
202 static GOptionGroup
*terminal_group
;
203 #define ARGS_TERM_OPTIONS 0
204 static const GOptionEntry argument_terminal_table
[] = {
206 /* terminal options */
208 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
209 &mc_args__force_xterm
,
210 N_("Forces xterm features"),
215 "no-x11", 'X', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
216 &mc_global
.tty
.disable_x11
,
217 N_("Disable X11 support"),
222 "oldmouse", 'g', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
223 &mc_global
.tty
.old_mouse
,
224 N_("Tries to use an old highlight mouse tracking"),
229 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
231 N_("Disable mouse support in text version"),
237 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
239 N_("Tries to use termcap instead of terminfo"),
245 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
246 &mc_global
.tty
.slow_terminal
,
247 N_("To run on slow terminals"),
252 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
253 &mc_global
.tty
.ugly_line_drawing
,
254 N_("Use stickchars to draw"),
260 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
262 N_("Resets soft keys on HP terminals"),
268 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
269 &mc_args__keymap_file
,
270 N_("Load definitions of key bindings from specified file"),
275 "nokeymap", '\0', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
277 N_("Don't load definitions of key bindings from file, use defaults"),
282 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
287 #undef ARGS_TERM_OPTIONS
289 static GOptionGroup
*color_group
;
290 #define ARGS_COLOR_OPTIONS 0
291 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
292 static const GOptionEntry argument_color_table
[] = {
296 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
297 &mc_global
.tty
.disable_colors
,
298 N_("Requests to run in black and white"),
303 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
304 &mc_args__force_colors
,
305 N_("Request to run in color mode"),
310 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
311 &mc_global
.tty
.command_line_colors
,
312 N_("Specifies a color configuration"),
317 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
319 N_("Show mc with specified skin"),
324 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
329 #undef ARGS_COLOR_OPTIONS
331 static gchar
*mc_args__loc__colors_string
= NULL
;
332 static gchar
*mc_args__loc__footer_string
= NULL
;
333 static gchar
*mc_args__loc__header_string
= NULL
;
334 static gchar
*mc_args__loc__usage_string
= NULL
;
336 /*** file scope functions ************************************************************************/
338 /* --------------------------------------------------------------------------------------------- */
340 mc_args_clean_temp_help_strings (void)
342 MC_PTR_FREE (mc_args__loc__colors_string
);
343 MC_PTR_FREE (mc_args__loc__footer_string
);
344 MC_PTR_FREE (mc_args__loc__header_string
);
345 MC_PTR_FREE (mc_args__loc__usage_string
);
348 /* --------------------------------------------------------------------------------------------- */
350 static GOptionGroup
*
351 mc_args_new_color_group (void)
354 /* FIXME: to preserve translations, lines should be split. */
355 mc_args__loc__colors_string
= g_strdup_printf ("%s\n%s",
356 /* TRANSLATORS: don't translate keywords */
357 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
358 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
360 " Global: errors, disabled, reverse, gauge, header\n"
361 " input, inputmark, inputunchanged, commandlinemark\n"
362 " bbarhotkey, bbarbutton, statusbar\n"
363 " File display: normal, selected, marked, markselect\n"
364 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
366 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
367 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
368 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
369 " editlinestate, editbg, editframe, editframeactive\n"
371 " Viewer: viewnormal,viewbold, viewunderline, viewselected\n"
372 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
373 /* TRANSLATORS: don't translate color names and attributes */
374 _("Standard Colors:\n"
375 " black, gray, red, brightred, green, brightgreen, brown,\n"
376 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
377 " brightcyan, lightgray and white\n\n"
378 "Extended colors, when 256 colors are available:\n"
379 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
381 " bold, italic, underline, reverse, blink; append more with '+'\n")
385 return g_option_group_new ("color", mc_args__loc__colors_string
,
386 _("Color options"), NULL
, NULL
);
390 /* --------------------------------------------------------------------------------------------- */
393 mc_args_add_usage_info (void)
397 switch (mc_global
.mc_run_mode
)
400 s
= g_strdup_printf ("%s\n", _("[+lineno] file1[:lineno] [file2[:lineno]...]"));
403 s
= g_strdup_printf ("%s\n", _("file"));
406 case MC_RUN_DIFFVIEWER
:
407 s
= g_strdup_printf ("%s\n", _("file1 file2"));
409 #endif /* USE_DIFF_VIEW */
412 s
= g_strdup_printf ("%s\n", _("[this_dir] [other_panel_dir]"));
415 mc_args__loc__usage_string
= s
;
417 return mc_args__loc__usage_string
;
420 /* --------------------------------------------------------------------------------------------- */
423 mc_args_add_extended_info_to_help (void)
425 mc_args__loc__footer_string
= g_strdup_printf ("%s",
428 "Please send any bug reports (including the output of 'mc -V')\n"
429 "as tickets at www.midnight-commander.org\n"));
430 mc_args__loc__header_string
= g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION
);
432 g_option_context_set_description (context
, mc_args__loc__footer_string
);
433 g_option_context_set_summary (context
, mc_args__loc__header_string
);
436 /* --------------------------------------------------------------------------------------------- */
439 mc_args__convert_help_to_syscharset (const gchar
* charset
, const gchar
* error_message_str
,
440 const gchar
* help_str
)
444 gchar
*full_help_str
;
446 buffer
= g_string_new ("");
447 conv
= g_iconv_open (charset
, "UTF-8");
448 full_help_str
= g_strdup_printf ("%s\n\n%s\n", error_message_str
, help_str
);
450 str_convert (conv
, full_help_str
, buffer
);
452 g_free (full_help_str
);
453 g_iconv_close (conv
);
455 return g_string_free (buffer
, FALSE
);
458 /* --------------------------------------------------------------------------------------------- */
461 parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
,
468 mc_return_val_if_error (mcerror
, FALSE
);
470 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
475 /* --------------------------------------------------------------------------------------------- */
478 parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
,
485 mc_return_val_if_error (mcerror
, FALSE
);
487 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
492 /* --------------------------------------------------------------------------------------------- */
494 * Create mcedit_arg_t object from vfs_path_t object and the line number.
496 * @param file_vpath file path object
497 * @param line_number line number. If value is 0, try to restore saved position.
498 * @return mcedit_arg_t object
501 static mcedit_arg_t
*
502 mcedit_arg_vpath_new (vfs_path_t
* file_vpath
, long line_number
)
506 arg
= g_new (mcedit_arg_t
, 1);
507 arg
->file_vpath
= file_vpath
;
508 arg
->line_number
= line_number
;
513 /* --------------------------------------------------------------------------------------------- */
515 * Create mcedit_arg_t object from file name and the line number.
517 * @param file_name file name
518 * @param line_number line number. If value is 0, try to restore saved position.
519 * @return mcedit_arg_t object
522 static mcedit_arg_t
*
523 mcedit_arg_new (const char *file_name
, long line_number
)
525 return mcedit_arg_vpath_new (vfs_path_from_str (file_name
), line_number
);
528 /* --------------------------------------------------------------------------------------------- */
530 * Get list of filenames (and line numbers) from command line, when mc called as editor
532 * @param argc count of all arguments
533 * @param argv array of strings, contains arguments
534 * @return list of mcedit_arg_t objects
538 parse_mcedit_arguments (int argc
, char **argv
)
542 long first_line_number
= -1;
544 for (i
= 0; i
< argc
; i
++)
553 * First, try to get line number as +lineno.
560 lineno
= strtol (tmp
+ 1, &error
, 10);
564 /* this is line number */
565 first_line_number
= lineno
;
568 /* this is file name */
572 * Check for filename:lineno, followed by an optional colon.
573 * This format is used by many programs (especially compilers)
574 * in error messages and warnings. It is supported so that
575 * users can quickly copy and paste file locations.
577 end
= tmp
+ strlen (tmp
);
580 if (p
> tmp
&& p
[-1] == ':')
582 while (p
> tmp
&& g_ascii_isdigit ((gchar
) p
[-1]))
585 if (tmp
< p
&& p
< end
&& p
[-1] == ':')
588 vfs_path_t
*tmp_vpath
, *fname_vpath
;
591 fname
= g_strndup (tmp
, p
- 1 - tmp
);
592 tmp_vpath
= vfs_path_from_str (tmp
);
593 fname_vpath
= vfs_path_from_str (fname
);
596 * Check that the file before the colon actually exists.
597 * If it doesn't exist, create new file.
599 if (mc_stat (tmp_vpath
, &st
) == -1 && mc_stat (fname_vpath
, &st
) != -1)
601 arg
= mcedit_arg_vpath_new (fname_vpath
, atoi (p
));
602 vfs_path_free (tmp_vpath
);
606 arg
= mcedit_arg_vpath_new (tmp_vpath
, 0);
607 vfs_path_free (fname_vpath
);
613 arg
= mcedit_arg_new (tmp
, 0);
615 flist
= g_list_prepend (flist
, arg
);
619 flist
= g_list_prepend (flist
, mcedit_arg_new (NULL
, 0));
620 else if (first_line_number
!= -1)
622 /* overwrite line number for first file */
625 l
= g_list_last (flist
);
626 ((mcedit_arg_t
*) l
->data
)->line_number
= first_line_number
;
632 /* --------------------------------------------------------------------------------------------- */
633 /*** public functions ****************************************************************************/
634 /* --------------------------------------------------------------------------------------------- */
637 mc_setup_run_mode (char **argv
)
641 base
= x_basename (argv
[0]);
643 if (strncmp (base
, "mce", 3) == 0 || strcmp (base
, "vi") == 0)
645 /* mce* or vi is link to mc */
646 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
648 else if (strncmp (base
, "mcv", 3) == 0 || strcmp (base
, "view") == 0)
650 /* mcv* or view is link to mc */
651 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
654 else if (strncmp (base
, "mcd", 3) == 0 || strcmp (base
, "diff") == 0)
656 /* mcd* or diff is link to mc */
657 mc_global
.mc_run_mode
= MC_RUN_DIFFVIEWER
;
659 #endif /* USE_DIFF_VIEW */
663 mc_args_parse (int *argc
, char ***argv
, const char *translation_domain
, GError
** mcerror
)
665 const gchar
*_system_codepage
;
668 mc_return_val_if_error (mcerror
, FALSE
);
670 _system_codepage
= str_detect_termencoding ();
673 if (!str_isutf8 (_system_codepage
))
674 bind_textdomain_codeset ("mc", "UTF-8");
677 context
= g_option_context_new (mc_args_add_usage_info ());
679 g_option_context_set_ignore_unknown_options (context
, FALSE
);
681 mc_args_add_extended_info_to_help ();
683 main_group
= g_option_group_new ("main", _("Main options"), _("Main options"), NULL
, NULL
);
685 g_option_group_add_entries (main_group
, argument_main_table
);
686 g_option_context_set_main_group (context
, main_group
);
687 g_option_group_set_translation_domain (main_group
, translation_domain
);
689 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
690 _("Terminal options"), NULL
, NULL
);
692 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
693 g_option_context_add_group (context
, terminal_group
);
694 g_option_group_set_translation_domain (terminal_group
, translation_domain
);
696 color_group
= mc_args_new_color_group ();
698 g_option_group_add_entries (color_group
, argument_color_table
);
699 g_option_context_add_group (context
, color_group
);
700 g_option_group_set_translation_domain (color_group
, translation_domain
);
702 if (!g_option_context_parse (context
, argc
, argv
, mcerror
))
704 if (*mcerror
== NULL
)
705 mc_propagate_error (mcerror
, 0, "%s\n", _("Arguments parse error!"));
710 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
712 if (str_isutf8 (_system_codepage
))
713 mc_replace_error (mcerror
, (*mcerror
)->code
, "%s\n\n%s\n", (*mcerror
)->message
,
717 gchar
*full_help_str
;
720 mc_args__convert_help_to_syscharset (_system_codepage
, (*mcerror
)->message
,
722 mc_replace_error (mcerror
, (*mcerror
)->code
, "%s", full_help_str
);
723 g_free (full_help_str
);
731 g_option_context_free (context
);
732 mc_args_clean_temp_help_strings ();
735 if (!str_isutf8 (_system_codepage
))
736 bind_textdomain_codeset ("mc", _system_codepage
);
742 /* --------------------------------------------------------------------------------------------- */
745 mc_args_show_info (void)
747 if (mc_args__show_version
)
753 if (mc_args__show_datadirs
)
755 printf ("%s (%s)\n", mc_global
.sysconfig_dir
, mc_global
.share_data_dir
);
759 if (mc_args__show_datadirs_extended
)
761 show_datadirs_extended ();
765 #ifdef ENABLE_CONFIGURE_ARGS
766 if (mc_args__show_configure_opts
)
768 show_configure_options ();
776 /* --------------------------------------------------------------------------------------------- */
779 mc_setup_by_args (int argc
, char **argv
, GError
** mcerror
)
783 mc_return_val_if_error (mcerror
, FALSE
);
785 if (mc_args__force_colors
)
786 mc_global
.tty
.disable_colors
= FALSE
;
788 #ifdef ENABLE_SUBSHELL
789 if (mc_args__nouse_subshell
)
790 mc_global
.tty
.use_subshell
= FALSE
;
791 #endif /* ENABLE_SUBSHELL */
793 #ifdef ENABLE_VFS_SMB
794 if (mc_args__debug_level
!= 0)
795 smbfs_set_debug (mc_args__debug_level
);
796 #endif /* ENABLE_VFS_SMB */
798 if (mc_args__netfs_logfile
!= NULL
)
801 #ifdef ENABLE_VFS_FTP
802 vpath
= vfs_path_from_str ("ftp://");
803 mc_setctl (vpath
, VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
804 vfs_path_free (vpath
);
805 #endif /* ENABLE_VFS_FTP */
806 #ifdef ENABLE_VFS_SMB
807 vpath
= vfs_path_from_str ("smb://");
808 mc_setctl (vpath
, VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
809 vfs_path_free (vpath
);
810 #endif /* ENABLE_VFS_SMB */
814 tmp
= (argc
> 0) ? argv
[1] : NULL
;
816 switch (mc_global
.mc_run_mode
)
819 mc_run_param0
= parse_mcedit_arguments (argc
- 1, &argv
[1]);
825 mc_propagate_error (mcerror
, 0, "%s\n", _("No arguments given to the viewer."));
829 mc_run_param0
= g_strdup (tmp
);
833 case MC_RUN_DIFFVIEWER
:
836 mc_propagate_error (mcerror
, 0, "%s\n",
837 _("Two files are required to envoke the diffviewer."));
841 #endif /* USE_DIFF_VIEW */
845 /* set the current dir and the other dir for filemanager,
846 or two files for diff viewer */
849 mc_run_param0
= g_strdup (tmp
);
850 tmp
= (argc
> 1) ? argv
[2] : NULL
;
852 mc_run_param1
= g_strdup (tmp
);
860 /* --------------------------------------------------------------------------------------------- */
862 * Free the mcedit_arg_t object.
864 * @param arg mcedit_arg_t object
868 mcedit_arg_free (mcedit_arg_t
* arg
)
870 vfs_path_free (arg
->file_vpath
);
874 /* --------------------------------------------------------------------------------------------- */