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/mouse.h"
34 #include "lib/strutil.h"
35 #include "lib/vfs/vfs.h"
36 #include "lib/util.h" /* x_basename() */
39 #include "src/vfs/smbfs/smbfs.h" /* smbfs_set_debugf() */
43 #include "src/textconf.h"
47 /*** external variables **************************************************************************/
49 /*** global variables ****************************************************************************/
51 /* If true, show version info and exit */
52 gboolean mc_args__show_version
= FALSE
;
54 /* If true, assume we are running on an xterm terminal */
55 gboolean mc_args__force_xterm
= FALSE
;
57 gboolean mc_args__nomouse
= FALSE
;
59 /* Force colors, only used by Slang */
60 gboolean mc_args__force_colors
= FALSE
;
62 /* Don't load keymap form file and use default one */
63 gboolean mc_args__nokeymap
= FALSE
;
65 /* Line to start the editor on */
66 int mc_args__edit_start_line
= 0;
68 char *mc_args__last_wd_file
= NULL
;
70 /* when enabled NETCODE, use folowing file as logfile */
71 char *mc_args__netfs_logfile
= NULL
;
74 char *mc_args__keymap_file
= NULL
;
77 int mc_args__debug_level
= 0;
79 /*** file scope macro definitions ****************************************************************/
81 /*** file scope type declarations ****************************************************************/
83 /*** file scope variables ************************************************************************/
85 /* forward declarations */
86 static gboolean
parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
,
87 gpointer data
, GError
** error
);
88 static gboolean
parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
,
89 gpointer data
, GError
** error
);
91 static GOptionContext
*context
;
93 static gboolean mc_args__nouse_subshell
= FALSE
;
94 static gboolean mc_args__show_datadirs
= FALSE
;
95 static gboolean mc_args__show_datadirs_extended
= FALSE
;
96 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 /* show configure options */
128 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
129 &mc_args__show_configure_opts
,
130 N_("Print configure options"),
135 "printwd", 'P', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
136 &mc_args__last_wd_file
,
137 N_("Print last working directory to specified file"),
141 #ifdef HAVE_SUBSHELL_SUPPORT
143 "subshell", 'U', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
144 &mc_global
.tty
.use_subshell
,
145 N_("Enables subshell support (default)"),
150 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
151 &mc_args__nouse_subshell
,
152 N_("Disables subshell support"),
158 #ifdef ENABLE_VFS_FTP
160 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
161 &mc_args__netfs_logfile
,
162 N_("Log ftp dialog to specified file"),
165 #endif /* ENABLE_VFS_FTP */
166 #ifdef ENABLE_VFS_SMB
168 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_INT
,
169 &mc_args__debug_level
,
170 N_("Set debug level"),
173 #endif /* ENABLE_VFS_SMB */
175 /* single file operations */
177 "view", 'v', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_CALLBACK
,
179 N_("Launches the file viewer on a file"),
184 "edit", 'e', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_CALLBACK
,
186 N_("Edits one file"),
190 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
195 GOptionGroup
*terminal_group
;
196 #define ARGS_TERM_OPTIONS 0
197 static const GOptionEntry argument_terminal_table
[] = {
199 /* terminal options */
201 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
202 &mc_args__force_xterm
,
203 N_("Forces xterm features"),
208 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
210 N_("Disable mouse support in text version"),
216 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
218 N_("Tries to use termcap instead of terminfo"),
224 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
225 &mc_global
.args
.slow_terminal
,
226 N_("To run on slow terminals"),
231 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
232 &mc_global
.args
.ugly_line_drawing
,
233 N_("Use stickchars to draw"),
238 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
240 N_("Resets soft keys on HP terminals"),
245 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
246 &mc_args__keymap_file
,
247 N_("Load definitions of key bindings from specified file"),
252 "nokeymap", '\0', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
254 N_("Don't load definitions of key bindings from file, use defaults"),
259 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
264 #undef ARGS_TERM_OPTIONS
266 GOptionGroup
*color_group
;
267 #define ARGS_COLOR_OPTIONS 0
268 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
269 static const GOptionEntry argument_color_table
[] = {
273 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
274 &mc_global
.args
.disable_colors
,
275 N_("Requests to run in black and white"),
280 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
281 &mc_args__force_colors
,
282 N_("Request to run in color mode"),
287 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
288 &mc_global
.tty
.command_line_colors
,
289 N_("Specifies a color configuration"),
294 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
295 &mc_global
.args
.skin
,
296 N_("Show mc with specified skin"),
301 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
306 #undef ARGS_COLOR_OPTIONS
308 static gchar
*mc_args__loc__colors_string
= NULL
;
309 static gchar
*mc_args__loc__footer_string
= NULL
;
310 static gchar
*mc_args__loc__header_string
= NULL
;
311 static gchar
*mc_args__loc__usage_string
= NULL
;
313 /*** file scope functions ************************************************************************/
315 /* --------------------------------------------------------------------------------------------- */
317 mc_args_clean_temp_help_strings (void)
319 g_free (mc_args__loc__colors_string
);
320 mc_args__loc__colors_string
= NULL
;
322 g_free (mc_args__loc__footer_string
);
323 mc_args__loc__footer_string
= NULL
;
325 g_free (mc_args__loc__header_string
);
326 mc_args__loc__header_string
= NULL
;
328 g_free (mc_args__loc__usage_string
);
329 mc_args__loc__usage_string
= NULL
;
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,\n"
354 " Viewer: viewbold, viewunderline, viewselected\n"
355 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
356 /* TRANSLATORS: don't translate color names and attributes */
357 _("Standard Colors:\n"
358 " black, gray, red, brightred, green, brightgreen, brown,\n"
359 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
360 " brightcyan, lightgray and white\n\n"
361 "Extended colors, when 256 colors are available:\n"
362 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
364 " bold, underline, reverse, blink; append more with '+'\n")
368 return g_option_group_new ("color", mc_args__loc__colors_string
,
369 _("Color options"), NULL
, NULL
);
373 /* --------------------------------------------------------------------------------------------- */
376 mc_args_add_usage_info (void)
378 mc_args__loc__usage_string
= g_strdup_printf ("[%s] %s\n %s - %s\n",
380 _("[this_dir] [other_panel_dir]"),
383 ("Set initial line number for the internal editor"));
384 return mc_args__loc__usage_string
;
387 /* --------------------------------------------------------------------------------------------- */
390 mc_args_add_extended_info_to_help (void)
392 mc_args__loc__footer_string
= g_strdup_printf ("%s",
395 "Please send any bug reports (including the output of `mc -V')\n"
396 "as tickets at www.midnight-commander.org\n"));
397 mc_args__loc__header_string
= g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION
);
399 #if GLIB_CHECK_VERSION(2,12,0)
400 g_option_context_set_description (context
, mc_args__loc__footer_string
);
401 g_option_context_set_summary (context
, mc_args__loc__header_string
);
405 /* --------------------------------------------------------------------------------------------- */
408 mc_setup_by_args (int argc
, char *argv
[])
413 if (mc_args__nomouse
)
414 use_mouse_p
= MOUSE_DISABLED
;
416 if (mc_args__netfs_logfile
!= NULL
)
418 #ifdef ENABLE_VFS_FTP
419 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
420 #endif /* ENABLE_VFS_FTP */
421 #ifdef ENABLE_VFS_SMB
422 smbfs_set_debugf (mc_args__netfs_logfile
);
423 #endif /* ENABLE_VFS_SMB */
426 #ifdef ENABLE_VFS_SMB
427 if (mc_args__debug_level
!= 0)
428 smbfs_set_debug (mc_args__debug_level
);
429 #endif /* ENABLE_VFS_SMB */
431 base
= x_basename (argv
[0]);
432 tmp
= (argc
> 0) ? argv
[1] : NULL
;
434 if (strncmp (base
, "mce", 3) == 0 || strcmp (base
, "vi") == 0)
436 /* mce* or vi is link to mc */
438 mc_run_param0
= g_strdup ("");
442 * Check for filename:lineno, followed by an optional colon.
443 * This format is used by many programs (especially compilers)
444 * in error messages and warnings. It is supported so that
445 * users can quickly copy and paste file locations.
449 end
= tmp
+ strlen (tmp
);
452 if (p
> tmp
&& p
[-1] == ':')
454 while (p
> tmp
&& g_ascii_isdigit ((gchar
) p
[-1]))
456 if (tmp
< p
&& p
< end
&& p
[-1] == ':')
461 fname
= g_strndup (tmp
, p
- 1 - tmp
);
463 * Check that the file before the colon actually exists.
464 * If it doesn't exist, revert to the old behavior.
466 if (mc_stat (tmp
, &st
) == -1 && mc_stat (fname
, &st
) != -1)
468 mc_run_param0
= fname
;
469 mc_args__edit_start_line
= atoi (p
);
474 goto try_plus_filename
;
480 if (*tmp
== '+' && g_ascii_isdigit ((gchar
) tmp
[1]))
484 start_line
= atoi (tmp
);
487 * If start_line is zero, position the cursor at the
488 * beginning of the file as other editors (vi, nano)
497 file
= (argc
> 1) ? argv
[2] : NULL
;
501 mc_args__edit_start_line
= start_line
;
505 mc_run_param0
= g_strdup (tmp
);
508 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
510 else if (strncmp (base
, "mcv", 3) == 0 || strcmp (base
, "view") == 0)
512 /* mcv* or view is link to mc */
515 mc_run_param0
= g_strdup (tmp
);
518 fprintf (stderr
, "%s\n", _("No arguments given to the viewer."));
521 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
524 else if (strncmp (base
, "mcd", 3) == 0 || strcmp (base
, "diff") == 0)
526 /* mcd* or diff is link to mc */
530 fprintf (stderr
, "%s\n", _("Two files are required to evoke the diffviewer."));
536 mc_run_param0
= g_strdup (tmp
);
537 tmp
= (argc
> 1) ? argv
[2] : NULL
;
539 mc_run_param1
= g_strdup (tmp
);
540 mc_global
.mc_run_mode
= MC_RUN_DIFFVIEWER
;
543 #endif /* USE_DIFF_VIEW */
546 /* MC is run as mc */
548 switch (mc_global
.mc_run_mode
)
552 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
555 case MC_RUN_DIFFVIEWER
:
556 /* not implemented yet */
561 /* sets the current dir and the other dir */
564 mc_run_param0
= g_strdup (tmp
);
565 tmp
= (argc
> 1) ? argv
[2] : NULL
;
567 mc_run_param1
= g_strdup (tmp
);
569 mc_global
.mc_run_mode
= MC_RUN_FULL
;
575 /* --------------------------------------------------------------------------------------------- */
578 mc_args_process (int argc
, char *argv
[])
580 if (mc_args__show_version
)
585 if (mc_args__show_datadirs
)
587 printf ("%s (%s)\n", mc_global
.sysconfig_dir
, mc_global
.share_data_dir
);
591 if (mc_args__show_datadirs_extended
)
593 show_datadirs_extended ();
597 if (mc_args__show_configure_opts
)
599 show_configure_options ();
603 if (mc_args__force_colors
)
604 mc_global
.args
.disable_colors
= FALSE
;
606 #ifdef HAVE_SUBSHELL_SUPPORT
607 if (mc_args__nouse_subshell
)
608 mc_global
.tty
.use_subshell
= FALSE
;
609 #endif /* HAVE_SUBSHELL_SUPPORT */
611 mc_setup_by_args (argc
, argv
);
616 /* --------------------------------------------------------------------------------------------- */
619 mc_args__convert_help_to_syscharset (const gchar
* charset
, const gchar
* error_message
,
620 const gchar
* help_str
)
622 GString
*buffer
= g_string_new ("");
623 GIConv conv
= g_iconv_open (charset
, "UTF-8");
624 gchar
*full_help_str
= g_strdup_printf ("%s\n\n%s\n", error_message
, help_str
);
626 str_convert (conv
, full_help_str
, buffer
);
628 g_free (full_help_str
);
629 g_iconv_close (conv
);
631 return g_string_free (buffer
, FALSE
);
634 /* --------------------------------------------------------------------------------------------- */
637 parse_mc_e_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
, GError
** error
)
643 mc_global
.mc_run_mode
= MC_RUN_EDITOR
;
644 mc_run_param0
= g_strdup (value
);
649 /* --------------------------------------------------------------------------------------------- */
652 parse_mc_v_argument (const gchar
* option_name
, const gchar
* value
, gpointer data
, GError
** error
)
658 mc_global
.mc_run_mode
= MC_RUN_VIEWER
;
659 mc_run_param0
= g_strdup (value
);
664 /* --------------------------------------------------------------------------------------------- */
666 /*** public functions ****************************************************************************/
668 /* --------------------------------------------------------------------------------------------- */
671 mc_args_handle (int argc
, char **argv
, const char *translation_domain
)
673 GError
*error
= NULL
;
674 const gchar
*_system_codepage
= str_detect_termencoding ();
677 if (!str_isutf8 (_system_codepage
))
678 bind_textdomain_codeset ("mc", "UTF-8");
681 context
= g_option_context_new (mc_args_add_usage_info ());
683 g_option_context_set_ignore_unknown_options (context
, FALSE
);
685 mc_args_add_extended_info_to_help ();
687 main_group
= g_option_group_new ("main", _("Main options"), _("Main options"), NULL
, NULL
);
689 g_option_group_add_entries (main_group
, argument_main_table
);
690 g_option_context_set_main_group (context
, main_group
);
691 g_option_group_set_translation_domain (main_group
, translation_domain
);
693 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
694 _("Terminal options"), NULL
, NULL
);
696 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
697 g_option_context_add_group (context
, terminal_group
);
698 g_option_group_set_translation_domain (terminal_group
, translation_domain
);
700 color_group
= mc_args_new_color_group ();
702 g_option_group_add_entries (color_group
, argument_color_table
);
703 g_option_context_add_group (context
, color_group
);
704 g_option_group_set_translation_domain (color_group
, translation_domain
);
706 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
710 gchar
*full_help_str
;
713 #if GLIB_CHECK_VERSION(2,14,0)
714 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
716 help_str
= g_strdup ("");
718 if (!str_isutf8 (_system_codepage
))
720 mc_args__convert_help_to_syscharset (_system_codepage
, error
->message
,
723 full_help_str
= g_strdup_printf ("%s\n\n%s\n", error
->message
, help_str
);
725 fprintf (stderr
, "%s", full_help_str
);
728 g_free (full_help_str
);
729 g_error_free (error
);
731 g_option_context_free (context
);
732 mc_args_clean_temp_help_strings ();
736 g_option_context_free (context
);
737 mc_args_clean_temp_help_strings ();
740 if (!str_isutf8 (_system_codepage
))
741 bind_textdomain_codeset ("mc", _system_codepage
);
744 return mc_args_process (argc
, argv
);
747 /* --------------------------------------------------------------------------------------------- */