2 Handle command line arguments.
4 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Slava Zanko <slavazanko@gmail.com>, 2009.
9 This file is part of the Midnight Commander.
11 The Midnight Commander is free software; you can redistribute it
12 and/or modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 The Midnight Commander is distributed in the hope that it will be
17 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
18 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31 #include "lib/global.h"
32 #include "lib/tty/tty.h"
33 #include "lib/tty/color.h" /* command_line_colors */
34 #include "lib/tty/mouse.h"
35 #include "lib/strutil.h"
36 #include "lib/vfs/mc-vfs/vfs.h"
38 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debugf() */
40 #include "lib/util.h" /* x_basename() */
43 #include "src/textconf.h"
44 #include "src/subshell.h" /* use_subshell */
48 /*** external variables **************************************************************************/
50 /*** global variables ****************************************************************************/
52 /* If true, show version info and exit */
53 gboolean mc_args__show_version
= FALSE
;
55 /* If true, assume we are running on an xterm terminal */
56 gboolean mc_args__force_xterm
= FALSE
;
58 gboolean mc_args__nomouse
= FALSE
;
60 /* For slow terminals */
61 gboolean mc_args__slow_terminal
= FALSE
;
63 /* If true use +, -, | for line drawing */
64 gboolean mc_args__ugly_line_drawing
= FALSE
;
66 /* Set to force black and white display at program startup */
67 gboolean mc_args__disable_colors
= FALSE
;
69 /* Force colors, only used by Slang */
70 gboolean mc_args__force_colors
= FALSE
;
72 /* Line to start the editor on */
73 int mc_args__edit_start_line
= 0;
75 /* Show in specified skin */
76 char *mc_args__skin
= NULL
;
78 char *mc_args__last_wd_file
= NULL
;
80 /* when enabled NETCODE, use folowing file as logfile */
81 char *mc_args__netfs_logfile
= NULL
;
84 char *mc_args__keymap_file
= NULL
;
87 int mc_args__debug_level
= 0;
89 /*** file scope macro definitions ****************************************************************/
91 /*** file scope type declarations ****************************************************************/
93 /*** file scope variables ************************************************************************/
95 /* forward declarations */
96 static gboolean
parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
,
97 gpointer data
, GError
** error
);
98 static gboolean
parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
,
99 gpointer data
, GError
** error
);
101 static GOptionContext
*context
;
103 static gboolean mc_args__nouse_subshell
= FALSE
;
104 static gboolean mc_args__show_datadirs
= FALSE
;
106 static GOptionGroup
*main_group
;
108 static const GOptionEntry argument_main_table
[] = {
110 /* generic options */
112 "version", 'V', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
113 &mc_args__show_version
,
114 N_("Displays the current version"),
118 /* options for wrappers */
120 "datadir", 'f', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
121 &mc_args__show_datadirs
,
122 N_("Print data directory"),
127 "printwd", 'P', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
128 &mc_args__last_wd_file
,
129 N_("Print last working directory to specified file"),
133 #ifdef HAVE_SUBSHELL_SUPPORT
135 "subshell", 'U', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
137 N_("Enables subshell support (default)"),
142 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
143 &mc_args__nouse_subshell
,
144 N_("Disables subshell support"),
150 #ifdef ENABLE_VFS_FTP
152 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
153 &mc_args__netfs_logfile
,
154 N_("Log ftp dialog to specified file"),
157 #endif /* ENABLE_VFS_FTP */
158 #ifdef ENABLE_VFS_SMB
160 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_INT
,
161 &mc_args__debug_level
,
162 N_("Set debug level"),
165 #endif /* ENABLE_VFS_SMB */
167 /* single file operations */
169 "view", 'v', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_CALLBACK
,
171 N_("Launches the file viewer on a file"),
176 "edit", 'e', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_CALLBACK
,
178 N_("Edits one file"),
182 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
187 GOptionGroup
*terminal_group
;
188 #define ARGS_TERM_OPTIONS 0
189 static const GOptionEntry argument_terminal_table
[] = {
191 /* terminal options */
193 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
194 &mc_args__force_xterm
,
195 N_("Forces xterm features"),
200 "oldmouse", 'g', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
202 N_("Tries to use an old highlight mouse tracking"),
207 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
209 N_("Disable mouse support in text version"),
215 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
217 N_("Tries to use termcap instead of terminfo"),
223 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
224 &mc_args__slow_terminal
,
225 N_("To run on slow terminals"),
230 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
231 &mc_args__ugly_line_drawing
,
232 N_("Use stickchars to draw"),
237 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
239 N_("Resets soft keys on HP terminals"),
244 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
245 &mc_args__keymap_file
,
246 N_("Load definitions of key bindings from specified file"),
251 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
256 #undef ARGS_TERM_OPTIONS
258 GOptionGroup
*color_group
;
259 #define ARGS_COLOR_OPTIONS 0
260 // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
261 static const GOptionEntry argument_color_table
[] = {
265 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
266 &mc_args__disable_colors
,
267 N_("Requests to run in black and white"),
272 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
273 &mc_args__force_colors
,
274 N_("Request to run in color mode"),
279 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
280 &command_line_colors
,
281 N_("Specifies a color configuration"),
286 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
288 N_("Show mc with specified skin"),
293 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
298 #undef ARGS_COLOR_OPTIONS
300 static gchar
*mc_args__loc__colors_string
= NULL
;
301 static gchar
*mc_args__loc__footer_string
= NULL
;
302 static gchar
*mc_args__loc__header_string
= NULL
;
303 static gchar
*mc_args__loc__usage_string
= NULL
;
305 /*** file scope functions ************************************************************************/
307 /* --------------------------------------------------------------------------------------------- */
309 mc_args_clean_temp_help_strings (void)
311 g_free (mc_args__loc__colors_string
);
312 mc_args__loc__colors_string
= NULL
;
314 g_free (mc_args__loc__footer_string
);
315 mc_args__loc__footer_string
= NULL
;
317 g_free (mc_args__loc__header_string
);
318 mc_args__loc__header_string
= NULL
;
320 g_free (mc_args__loc__usage_string
);
321 mc_args__loc__usage_string
= NULL
;
324 /* --------------------------------------------------------------------------------------------- */
326 static GOptionGroup
*
327 mc_args_new_color_group (void)
329 /* FIXME: to preserve translations, lines should be split. */
330 mc_args__loc__colors_string
= g_strdup_printf ("%s\n%s",
331 /* TRANSLATORS: don't translate keywords */
332 _("--colors KEYWORD={FORE},{BACK}\n\n"
333 "{FORE} and {BACK} can be omitted, and the default will be used\n"
335 " Global: errors, disabled, reverse, gauge, header\n"
336 " input, inputmark, inputunchanged, commandlinemark\n"
337 " bbarhotkey, bbarbutton, statusbar\n"
338 " File display: normal, selected, marked, markselect\n"
339 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
341 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
342 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
343 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
345 " Viewer: viewbold, viewunderline, viewselected\n"
346 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
347 /* TRANSLATORS: don't translate color names */
349 " black, gray, red, brightred, green, brightgreen, brown,\n"
350 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
351 " brightcyan, lightgray and white\n\n"));
353 return g_option_group_new ("color", mc_args__loc__colors_string
,
354 _("Color options"), NULL
, NULL
);
358 /* --------------------------------------------------------------------------------------------- */
361 mc_args_add_usage_info (void)
363 mc_args__loc__usage_string
= g_strdup_printf ("[%s] %s\n %s - %s\n",
365 _("[this_dir] [other_panel_dir]"),
368 ("Set initial line number for the internal editor"));
369 return mc_args__loc__usage_string
;
372 /* --------------------------------------------------------------------------------------------- */
375 mc_args_add_extended_info_to_help (void)
377 mc_args__loc__footer_string
= g_strdup_printf ("%s",
380 "Please send any bug reports (including the output of `mc -V')\n"
381 "as tickets at www.midnight-commander.org\n"));
382 mc_args__loc__header_string
= g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION
);
384 #if GLIB_CHECK_VERSION(2,12,0)
385 g_option_context_set_description (context
, mc_args__loc__footer_string
);
386 g_option_context_set_summary (context
, mc_args__loc__header_string
);
390 /* --------------------------------------------------------------------------------------------- */
393 mc_setup_by_args (int argc
, char *argv
[])
398 #ifdef ENABLE_VFS_SMB
399 if (mc_args__debug_level
!= 0)
400 smbfs_set_debug (mc_args__debug_level
);
401 #endif /* ENABLE_VFS_SMB */
403 if (mc_args__netfs_logfile
!= NULL
)
405 #ifdef ENABLE_VFS_FTP
406 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
407 #endif /* ENABLE_VFS_FTP */
408 #ifdef ENABLE_VFS_SMB
409 mc_setctl ("/#smb:", VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
410 #endif /* ENABLE_VFS_SMB */
413 base
= x_basename (argv
[0]);
414 tmp
= (argc
> 0) ? argv
[1] : NULL
;
416 if (strncmp (base
, "mce", 3) == 0 || strcmp (base
, "vi") == 0)
418 /* mce* or vi is link to mc */
420 mc_run_param0
= g_strdup ("");
424 * Check for filename:lineno, followed by an optional colon.
425 * This format is used by many programs (especially compilers)
426 * in error messages and warnings. It is supported so that
427 * users can quickly copy and paste file locations.
431 end
= tmp
+ strlen (tmp
);
434 if (p
> tmp
&& p
[-1] == ':')
436 while (p
> tmp
&& g_ascii_isdigit ((gchar
) p
[-1]))
438 if (tmp
< p
&& p
< end
&& p
[-1] == ':')
443 fname
= g_strndup (tmp
, p
- 1 - tmp
);
445 * Check that the file before the colon actually exists.
446 * If it doesn't exist, revert to the old behavior.
448 if (mc_stat (tmp
, &st
) == -1 && mc_stat (fname
, &st
) != -1)
450 mc_run_param0
= fname
;
451 mc_args__edit_start_line
= atoi (p
);
456 goto try_plus_filename
;
462 if (*tmp
== '+' && g_ascii_isdigit ((gchar
) tmp
[1]))
466 start_line
= atoi (tmp
);
469 * If start_line is zero, position the cursor at the
470 * beginning of the file as other editors (vi, nano)
479 file
= (argc
> 1) ? argv
[2] : NULL
;
483 mc_args__edit_start_line
= start_line
;
487 mc_run_param0
= g_strdup (tmp
);
490 mc_run_mode
= MC_RUN_EDITOR
;
492 else if (strncmp (base
, "mcv", 3) == 0 || strcmp (base
, "view") == 0)
494 /* mcv* or view is link to mc */
497 mc_run_param0
= g_strdup (tmp
);
500 fprintf (stderr
, "%s\n", _("No arguments given to the viewer."));
503 mc_run_mode
= MC_RUN_VIEWER
;
506 else if (strncmp (base
, "mcd", 3) == 0 || strcmp (base
, "diff") == 0)
508 /* mcd* or diff is link to mc */
512 fprintf (stderr
, "%s\n", _("Two files are required to evoke the diffviewer."));
518 mc_run_param0
= g_strdup (tmp
);
519 tmp
= (argc
> 1) ? argv
[2] : NULL
;
521 mc_run_param1
= g_strdup (tmp
);
522 mc_run_mode
= MC_RUN_DIFFVIEWER
;
525 #endif /* USE_DIFF_VIEW */
528 /* MC is run as mc */
534 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
537 case MC_RUN_DIFFVIEWER
:
538 /* not implemented yet */
543 /* sets the current dir and the other dir */
546 mc_run_param0
= g_strdup (tmp
);
547 tmp
= (argc
> 1) ? argv
[2] : NULL
;
549 mc_run_param1
= g_strdup (tmp
);
551 mc_run_mode
= MC_RUN_FULL
;
557 /* --------------------------------------------------------------------------------------------- */
560 mc_args_process (int argc
, char *argv
[])
562 if (mc_args__show_version
)
567 if (mc_args__show_datadirs
)
569 printf ("%s (%s)\n", mc_home
, mc_home_alt
);
573 if (mc_args__force_colors
)
574 mc_args__disable_colors
= FALSE
;
576 #ifdef HAVE_SUBSHELL_SUPPORT
577 if (mc_args__nouse_subshell
)
579 #endif /* HAVE_SUBSHELL_SUPPORT */
581 mc_setup_by_args (argc
, argv
);
586 /* --------------------------------------------------------------------------------------------- */
589 mc_args__convert_help_to_syscharset (const gchar
* charset
, const gchar
* error_message
,
590 const gchar
* help_str
)
592 GString
*buffer
= g_string_new ("");
593 GIConv conv
= g_iconv_open (charset
, "UTF-8");
594 gchar
*full_help_str
= g_strdup_printf ("%s\n\n%s\n", error_message
, help_str
);
596 str_convert (conv
, full_help_str
, buffer
);
598 g_free (full_help_str
);
599 g_iconv_close (conv
);
601 return g_string_free (buffer
, FALSE
);
604 /* --------------------------------------------------------------------------------------------- */
607 parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
, GError
** error
)
613 mc_run_mode
= MC_RUN_EDITOR
;
614 mc_run_param0
= g_strdup (value
);
619 /* --------------------------------------------------------------------------------------------- */
622 parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
, GError
** error
)
628 mc_run_mode
= MC_RUN_VIEWER
;
629 mc_run_param0
= g_strdup (value
);
634 /* --------------------------------------------------------------------------------------------- */
636 /*** public functions ****************************************************************************/
638 /* --------------------------------------------------------------------------------------------- */
641 mc_args_handle (int argc
, char **argv
, const char *translation_domain
)
643 GError
*error
= NULL
;
644 const gchar
*_system_codepage
= str_detect_termencoding ();
647 if (!str_isutf8 (_system_codepage
))
648 bind_textdomain_codeset ("mc", "UTF-8");
651 context
= g_option_context_new (mc_args_add_usage_info ());
653 g_option_context_set_ignore_unknown_options (context
, FALSE
);
655 mc_args_add_extended_info_to_help ();
657 main_group
= g_option_group_new ("main", _("Main options"), _("Main options"), NULL
, NULL
);
659 g_option_group_add_entries (main_group
, argument_main_table
);
660 g_option_context_set_main_group (context
, main_group
);
661 g_option_group_set_translation_domain (main_group
, translation_domain
);
663 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
664 _("Terminal options"), NULL
, NULL
);
666 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
667 g_option_context_add_group (context
, terminal_group
);
668 g_option_group_set_translation_domain (terminal_group
, translation_domain
);
670 color_group
= mc_args_new_color_group ();
672 g_option_group_add_entries (color_group
, argument_color_table
);
673 g_option_context_add_group (context
, color_group
);
674 g_option_group_set_translation_domain (color_group
, translation_domain
);
676 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
680 gchar
*full_help_str
;
683 #if GLIB_CHECK_VERSION(2,14,0)
684 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
686 help_str
= g_strdup ("");
688 if (!str_isutf8 (_system_codepage
))
690 mc_args__convert_help_to_syscharset (_system_codepage
, error
->message
,
693 full_help_str
= g_strdup_printf ("%s\n\n%s\n", error
->message
, help_str
);
695 fprintf (stderr
, "%s", full_help_str
);
698 g_free (full_help_str
);
699 g_error_free (error
);
701 g_option_context_free (context
);
702 mc_args_clean_temp_help_strings ();
706 g_option_context_free (context
);
707 mc_args_clean_temp_help_strings ();
710 if (!str_isutf8 (_system_codepage
))
711 bind_textdomain_codeset ("mc", _system_codepage
);
714 return mc_args_process (argc
, argv
);
717 /* --------------------------------------------------------------------------------------------- */