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"
37 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debugf() */
40 #include "src/textconf.h"
41 #include "src/subshell.h" /* use_subshell */
45 /*** external variables **************************************************************************/
47 /*** global variables ****************************************************************************/
49 /* If true, show version info and exit */
50 gboolean mc_args__show_version
= FALSE
;
52 /* If true, assume we are running on an xterm terminal */
53 gboolean mc_args__force_xterm
= FALSE
;
55 gboolean mc_args__nomouse
= FALSE
;
57 /* For slow terminals */
58 gboolean mc_args__slow_terminal
= FALSE
;
60 /* If true use +, -, | for line drawing */
61 gboolean mc_args__ugly_line_drawing
= FALSE
;
63 /* Set to force black and white display at program startup */
64 gboolean mc_args__disable_colors
= FALSE
;
66 /* Force colors, only used by Slang */
67 gboolean mc_args__force_colors
= FALSE
;
69 /* Line to start the editor on */
70 int mc_args__edit_start_line
= 0;
72 /* Show in specified skin */
73 char *mc_args__skin
= NULL
;
75 char *mc_args__last_wd_file
= NULL
;
77 /* when enabled NETCODE, use folowing file as logfile */
78 char *mc_args__netfs_logfile
= NULL
;
81 char *mc_args__keymap_file
= NULL
;
84 int mc_args__debug_level
= 0;
86 /*** file scope macro definitions ****************************************************************/
88 /*** file scope type declarations ****************************************************************/
90 /*** file scope variables ************************************************************************/
92 /* forward declarations */
93 static gboolean
parse_mc_e_argument (const gchar
*option_name
, const gchar
*value
,
94 gpointer data
, GError
**error
);
95 static gboolean
parse_mc_v_argument (const gchar
*option_name
, const gchar
*value
,
96 gpointer data
, GError
**error
);
98 static GOptionContext
*context
;
100 static gboolean mc_args__nouse_subshell
= FALSE
;
101 static gboolean mc_args__show_datadirs
= FALSE
;
103 static GOptionGroup
*main_group
;
105 static const GOptionEntry argument_main_table
[] = {
107 /* generic options */
109 "version", 'V', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
110 &mc_args__show_version
,
111 N_("Displays the current version"),
115 /* options for wrappers */
117 "datadir", 'f', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
118 &mc_args__show_datadirs
,
119 N_("Print data directory"),
124 "printwd", 'P', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
125 &mc_args__last_wd_file
,
126 N_("Print last working directory to specified file"),
130 #ifdef HAVE_SUBSHELL_SUPPORT
132 "subshell", 'U', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
134 N_("Enables subshell support (default)"),
139 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
140 &mc_args__nouse_subshell
,
141 N_("Disables subshell support"),
149 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
150 &mc_args__netfs_logfile
,
151 N_("Log ftp dialog to specified file"),
154 #ifdef ENABLE_VFS_SMB
156 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_INT
,
157 &mc_args__debug_level
,
158 N_("Set debug level"),
161 #endif /* ENABLE_VFS_SMB */
164 /* single file operations */
166 "view", 'v', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_CALLBACK
,
168 N_("Launches the file viewer on a file"),
173 "edit", 'e', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_CALLBACK
,
175 N_("Edits one file"),
179 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
184 GOptionGroup
*terminal_group
;
185 #define ARGS_TERM_OPTIONS 0
186 static const GOptionEntry argument_terminal_table
[] = {
188 /* terminal options */
190 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
191 &mc_args__force_xterm
,
192 N_("Forces xterm features"),
197 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
199 N_("Disable mouse support in text version"),
205 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
207 N_("Tries to use termcap instead of terminfo"),
213 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
214 &mc_args__slow_terminal
,
215 N_("To run on slow terminals"),
220 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
221 &mc_args__ugly_line_drawing
,
222 N_("Use stickchars to draw"),
227 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
229 N_("Resets soft keys on HP terminals"),
234 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
235 &mc_args__keymap_file
,
236 N_("Load definitions of key bindings from specified file"),
241 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
246 #undef ARGS_TERM_OPTIONS
248 GOptionGroup
*color_group
;
249 #define ARGS_COLOR_OPTIONS 0
250 // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
251 static const GOptionEntry argument_color_table
[] = {
255 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
256 &mc_args__disable_colors
,
257 N_("Requests to run in black and white"),
262 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
263 &mc_args__force_colors
,
264 N_("Request to run in color mode"),
269 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
270 &command_line_colors
,
271 N_("Specifies a color configuration"),
276 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
278 N_("Show mc with specified skin"),
283 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
288 #undef ARGS_COLOR_OPTIONS
290 static gchar
*mc_args__loc__colors_string
= NULL
;
291 static gchar
*mc_args__loc__footer_string
= NULL
;
292 static gchar
*mc_args__loc__header_string
= NULL
;
293 static gchar
*mc_args__loc__usage_string
= NULL
;
295 /*** file scope functions ************************************************************************/
297 /* --------------------------------------------------------------------------------------------- */
299 mc_args_clean_temp_help_strings (void)
301 g_free (mc_args__loc__colors_string
);
302 mc_args__loc__colors_string
= NULL
;
304 g_free (mc_args__loc__footer_string
);
305 mc_args__loc__footer_string
= NULL
;
307 g_free (mc_args__loc__header_string
);
308 mc_args__loc__header_string
= NULL
;
310 g_free (mc_args__loc__usage_string
);
311 mc_args__loc__usage_string
= NULL
;
314 /* --------------------------------------------------------------------------------------------- */
316 static GOptionGroup
*
317 mc_args_new_color_group (void)
320 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
321 * and editmarked. To preserve translations, lines should be split.
323 mc_args__loc__colors_string
= g_strdup_printf ("%s%s",
324 /* TRANSLATORS: don't translate keywords and names of colors */
325 _("--colors KEYWORD={FORE},{BACK}\n\n"
326 "{FORE} and {BACK} can be omitted, and the default will be used\n"
328 " Global: errors, reverse, gauge, viewunderline\n"
329 " input, inputmark, inputunchanged, commandlinemark\n"
330 " File display: normal, selected, marked, markselect\n"
331 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
333 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
334 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
336 /* TRANSLATORS: don't translate keywords and names of colors */
338 (" Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
340 " black, gray, red, brightred, green, brightgreen, brown,\n"
341 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
342 " brightcyan, lightgray and white\n\n"));
344 return g_option_group_new ("color", mc_args__loc__colors_string
,
345 _("Color options"), NULL
, NULL
);
349 /* --------------------------------------------------------------------------------------------- */
352 mc_args_add_usage_info (void)
354 mc_args__loc__usage_string
= g_strdup_printf ("[%s] %s\n %s - %s\n",
356 _("[this_dir] [other_panel_dir]"),
359 ("Set initial line number for the internal editor"));
360 return mc_args__loc__usage_string
;
363 /* --------------------------------------------------------------------------------------------- */
366 mc_args_add_extended_info_to_help (void)
368 mc_args__loc__footer_string
= g_strdup_printf ("%s",
371 "Please send any bug reports (including the output of `mc -V')\n"
372 "as tickets at www.midnight-commander.org\n"));
373 mc_args__loc__header_string
= g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION
);
375 #if GLIB_CHECK_VERSION(2,12,0)
376 g_option_context_set_description (context
, mc_args__loc__footer_string
);
377 g_option_context_set_summary (context
, mc_args__loc__header_string
);
381 /* --------------------------------------------------------------------------------------------- */
384 mc_setup_by_args (int argc
, char *argv
[])
389 if (mc_args__nomouse
)
390 use_mouse_p
= MOUSE_DISABLED
;
393 if (mc_args__netfs_logfile
!= NULL
)
395 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE
, (void *) mc_args__netfs_logfile
);
396 #ifdef ENABLE_VFS_SMB
397 smbfs_set_debugf (mc_args__netfs_logfile
);
398 #endif /* ENABLE_VFS_SMB */
401 #ifdef ENABLE_VFS_SMB
402 if (mc_args__debug_level
!= 0)
403 smbfs_set_debug (mc_args__debug_level
);
404 #endif /* ENABLE_VFS_SMB */
405 #endif /* USE_NETCODE */
407 base
= x_basename (argv
[0]);
408 tmp
= (argc
> 0) ? argv
[1] : NULL
;
410 if (strncmp (base
, "mce", 3) == 0 || strcmp (base
, "vi") == 0)
412 /* mce* or vi is link to mc */
414 mc_run_param0
= g_strdup ("");
418 * Check for filename:lineno, followed by an optional colon.
419 * This format is used by many programs (especially compilers)
420 * in error messages and warnings. It is supported so that
421 * users can quickly copy and paste file locations.
425 end
= tmp
+ strlen (tmp
);\
428 if (p
> tmp
&& p
[-1] == ':')
430 while (p
> tmp
&& g_ascii_isdigit ((gchar
) p
[-1]))
432 if (tmp
< p
&& p
< end
&& p
[-1] == ':')
437 fname
= g_strndup (tmp
, p
- 1 - tmp
);
439 * Check that the file before the colon actually exists.
440 * If it doesn't exist, revert to the old behavior.
442 if (mc_stat (tmp
, &st
) == -1 && mc_stat (fname
, &st
) != -1)
444 mc_run_param0
= fname
;
445 mc_args__edit_start_line
= atoi (p
);
450 goto try_plus_filename
;
456 if (*tmp
== '+' && g_ascii_isdigit ((gchar
) tmp
[1]))
460 start_line
= atoi (tmp
);
465 file
= (argc
> 1) ? argv
[2] : NULL
;
469 mc_args__edit_start_line
= start_line
;
473 mc_run_param0
= g_strdup (tmp
);
476 mc_run_mode
= MC_RUN_EDITOR
;
478 else if (strncmp (base
, "mcv", 3) == 0 || strcmp (base
, "view") == 0)
480 /* mcv* or view is link to mc */
483 mc_run_param0
= g_strdup (tmp
);
486 fprintf (stderr
, "%s\n", _("No arguments given to the viewer."));
489 mc_run_mode
= MC_RUN_VIEWER
;
492 else if (strncmp (base
, "mcd", 3) == 0 || strcmp (base
, "diff") == 0)
494 /* mcd* or diff is link to mc */
498 fprintf (stderr
, "%s\n", _("There 2 files are required to diffviewer."));
504 mc_run_param0
= g_strdup (tmp
);
505 tmp
= (argc
> 1) ? argv
[2] : NULL
;
507 mc_run_param1
= g_strdup (tmp
);
508 mc_run_mode
= MC_RUN_DIFFVIEWER
;
511 #endif /* USE_DIFF_VIEW */
514 /* MC is run as mc */
520 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
523 case MC_RUN_DIFFVIEWER
:
524 /* not implemented yet */
529 /* sets the current dir and the other dir */
532 mc_run_param0
= g_strdup (tmp
);
533 tmp
= (argc
> 1) ? argv
[2] : NULL
;
535 mc_run_param1
= g_strdup (tmp
);
537 mc_run_mode
= MC_RUN_FULL
;
543 /* --------------------------------------------------------------------------------------------- */
546 mc_args_process (int argc
, char *argv
[])
548 if (mc_args__show_version
)
553 if (mc_args__show_datadirs
)
555 printf ("%s (%s)\n", mc_home
, mc_home_alt
);
559 if (mc_args__force_colors
)
560 mc_args__disable_colors
= FALSE
;
562 #ifdef HAVE_SUBSHELL_SUPPORT
563 if (mc_args__nouse_subshell
)
566 if (mc_args__nouse_subshell
)
568 #endif /* HAVE_SUBSHELL_SUPPORT */
570 mc_setup_by_args (argc
, argv
);
575 /* --------------------------------------------------------------------------------------------- */
578 mc_args__convert_help_to_syscharset (const gchar
* charset
, const gchar
* error_message
,
579 const gchar
* help_str
)
581 GString
*buffer
= g_string_new ("");
582 GIConv conv
= g_iconv_open (charset
, "UTF-8");
583 gchar
*full_help_str
= g_strdup_printf ("%s\n\n%s\n", error_message
, help_str
);
585 str_convert (conv
, full_help_str
, buffer
);
587 g_free (full_help_str
);
588 g_iconv_close (conv
);
590 return g_string_free (buffer
, FALSE
);
593 /* --------------------------------------------------------------------------------------------- */
596 parse_mc_e_argument (const gchar
*option_name
, const gchar
*value
,
597 gpointer data
, GError
**error
)
603 mc_run_mode
= MC_RUN_EDITOR
;
604 mc_run_param0
= g_strdup (value
);
609 /* --------------------------------------------------------------------------------------------- */
612 parse_mc_v_argument (const gchar
*option_name
, const gchar
*value
,
613 gpointer data
, GError
**error
)
619 mc_run_mode
= MC_RUN_VIEWER
;
620 mc_run_param0
= g_strdup (value
);
625 /* --------------------------------------------------------------------------------------------- */
627 /*** public functions ****************************************************************************/
629 /* --------------------------------------------------------------------------------------------- */
632 mc_args_handle (int argc
, char **argv
, const char *translation_domain
)
634 GError
*error
= NULL
;
635 const gchar
*_system_codepage
= str_detect_termencoding ();
638 if (!str_isutf8 (_system_codepage
))
639 bind_textdomain_codeset ("mc", "UTF-8");
642 context
= g_option_context_new (mc_args_add_usage_info ());
644 g_option_context_set_ignore_unknown_options (context
, FALSE
);
646 mc_args_add_extended_info_to_help ();
648 main_group
= g_option_group_new ("main", _("Main options"), _("Main options"), NULL
, NULL
);
650 g_option_group_add_entries (main_group
, argument_main_table
);
651 g_option_context_set_main_group (context
, main_group
);
652 g_option_group_set_translation_domain (main_group
, translation_domain
);
654 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
655 _("Terminal options"), NULL
, NULL
);
657 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
658 g_option_context_add_group (context
, terminal_group
);
659 g_option_group_set_translation_domain (terminal_group
, translation_domain
);
661 color_group
= mc_args_new_color_group ();
663 g_option_group_add_entries (color_group
, argument_color_table
);
664 g_option_context_add_group (context
, color_group
);
665 g_option_group_set_translation_domain (color_group
, translation_domain
);
667 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
671 gchar
*full_help_str
;
674 #if GLIB_CHECK_VERSION(2,14,0)
675 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
677 help_str
= g_strdup ("");
679 if (!str_isutf8 (_system_codepage
))
681 mc_args__convert_help_to_syscharset (_system_codepage
, error
->message
,
684 full_help_str
= g_strdup_printf ("%s\n\n%s\n", error
->message
, help_str
);
686 fprintf (stderr
, "%s", full_help_str
);
689 g_free (full_help_str
);
690 g_error_free (error
);
692 g_option_context_free (context
);
693 mc_args_clean_temp_help_strings ();
697 g_option_context_free (context
);
698 mc_args_clean_temp_help_strings ();
701 if (!str_isutf8 (_system_codepage
))
702 bind_textdomain_codeset ("mc", _system_codepage
);
705 return mc_args_process (argc
, argv
);
708 /* --------------------------------------------------------------------------------------------- */