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/strutil.h"
37 #include "src/textconf.h"
38 #include "subshell.h" /* use_subshell */
42 /*** external variables **************************************************************************/
44 /*** global variables ****************************************************************************/
46 /* If true, show version info and exit */
47 gboolean mc_args__version
= FALSE
;
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 /* For slow terminals */
55 gboolean mc_args__slow_terminal
= FALSE
;
57 /* If true use +, -, | for line drawing */
58 gboolean mc_args__ugly_line_drawing
= FALSE
;
60 /* Set to force black and white display at program startup */
61 gboolean mc_args__disable_colors
= FALSE
;
63 /* Force colors, only used by Slang */
64 gboolean mc_args__force_colors
= FALSE
;
66 /* Show in specified skin */
67 char *mc_args__skin
= NULL
;
69 char *mc_args__last_wd_file
= NULL
;
71 /* when enabled NETCODE, use folowing file as logfile */
72 char *mc_args__netfs_logfile
= NULL
;
75 char *mc_args__keymap_file
= NULL
;
78 int mc_args__debug_level
= 0;
81 /*** file scope macro definitions ****************************************************************/
83 /*** file scope type declarations ****************************************************************/
85 /*** file scope variables ************************************************************************/
87 static GOptionContext
*context
;
89 static gboolean mc_args__nouse_subshell
= FALSE
;
90 static gboolean mc_args__show_datadirs
= FALSE
;
92 static GOptionGroup
*main_group
;
94 static const GOptionEntry argument_main_table
[] = {
98 "version", 'V', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
100 N_("Displays the current version"),
104 /* options for wrappers */
106 "datadir", 'f', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
107 &mc_args__show_datadirs
,
108 N_("Print data directory"),
113 "printwd", 'P', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
114 &mc_args__last_wd_file
,
115 N_("Print last working directory to specified file"),
119 #ifdef HAVE_SUBSHELL_SUPPORT
121 "subshell", 'U', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
123 N_("Enables subshell support (default)"),
128 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
129 &mc_args__nouse_subshell
,
130 N_("Disables subshell support"),
138 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
139 &mc_args__netfs_logfile
,
140 N_("Log ftp dialog to specified file"),
143 #ifdef ENABLE_VFS_SMB
145 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_INT
,
146 &mc_args__debug_level
,
147 N_("Set debug level"),
150 #endif /* ENABLE_VFS_SMB */
153 /* single file operations */
155 "view", 'v', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
157 N_("Launches the file viewer on a file"),
162 "edit", 'e', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
164 N_("Edits one file"),
168 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
173 GOptionGroup
*terminal_group
;
174 #define ARGS_TERM_OPTIONS 0
175 static const GOptionEntry argument_terminal_table
[] = {
177 /* terminal options */
179 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
180 &mc_args__force_xterm
,
181 N_("Forces xterm features"),
186 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
188 N_("Disable mouse support in text version"),
194 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
196 N_("Tries to use termcap instead of terminfo"),
202 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
203 &mc_args__slow_terminal
,
204 N_("To run on slow terminals"),
209 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
210 &mc_args__ugly_line_drawing
,
211 N_("Use stickchars to draw"),
216 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
218 N_("Resets soft keys on HP terminals"),
223 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
224 &mc_args__keymap_file
,
225 N_("Load definitions of key bindings from specified file"),
230 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
235 #undef ARGS_TERM_OPTIONS
237 GOptionGroup
*color_group
;
238 #define ARGS_COLOR_OPTIONS 0
239 // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
240 static const GOptionEntry argument_color_table
[] = {
244 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
245 &mc_args__disable_colors
,
246 N_("Requests to run in black and white"),
251 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
252 &mc_args__force_colors
,
253 N_("Request to run in color mode"),
258 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
259 &command_line_colors
,
260 N_("Specifies a color configuration"),
265 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
267 N_("Show mc with specified skin"),
272 NULL
, '\0', 0, 0, NULL
, NULL
, NULL
/* Complete struct initialization */
277 #undef ARGS_COLOR_OPTIONS
279 static gchar
*mc_args__loc__colors_string
= NULL
;
280 static gchar
*mc_args__loc__footer_string
= NULL
;
281 static gchar
*mc_args__loc__header_string
= NULL
;
282 static gchar
*mc_args__loc__usage_string
= NULL
;
284 /*** file scope functions ************************************************************************/
286 /* --------------------------------------------------------------------------------------------- */
288 mc_args_clean_temp_help_strings (void)
290 g_free (mc_args__loc__colors_string
);
291 mc_args__loc__colors_string
= NULL
;
293 g_free (mc_args__loc__footer_string
);
294 mc_args__loc__footer_string
= NULL
;
296 g_free (mc_args__loc__header_string
);
297 mc_args__loc__header_string
= NULL
;
299 g_free (mc_args__loc__usage_string
);
300 mc_args__loc__usage_string
= NULL
;
303 /* --------------------------------------------------------------------------------------------- */
305 static GOptionGroup
*
306 mc_args_new_color_group (void)
309 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
310 * and editmarked. To preserve translations, lines should be split.
313 mc_args__loc__colors_string
= g_strdup_printf ("%s%s",
314 /* TRANSLATORS: don't translate keywords and names of colors */
315 _("--colors KEYWORD={FORE},{BACK}\n\n"
316 "{FORE} and {BACK} can be omitted, and the default will be used\n"
318 " Global: errors, reverse, gauge, input, viewunderline\n"
319 " File display: normal, selected, marked, markselect\n"
320 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
322 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
323 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
325 /* TRANSLATORS: don't translate keywords and names of colors */
327 (" Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
329 " black, gray, red, brightred, green, brightgreen, brown,\n"
330 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
331 " brightcyan, lightgray and white\n\n"));
333 return g_option_group_new ("color", mc_args__loc__colors_string
,
334 _("Color options"), NULL
, NULL
);
338 /* --------------------------------------------------------------------------------------------- */
341 mc_args_add_usage_info (void)
343 mc_args__loc__usage_string
= g_strdup_printf ("[%s] %s\n %s - %s\n",
345 _("[this_dir] [other_panel_dir]"),
348 ("Set initial line number for the internal editor"));
349 return mc_args__loc__usage_string
;
352 /* --------------------------------------------------------------------------------------------- */
355 mc_args_add_extended_info_to_help (void)
357 mc_args__loc__footer_string
= g_strdup_printf ("%s",
360 "Please send any bug reports (including the output of `mc -V')\n"
361 "to mc-devel@gnome.org\n"));
362 mc_args__loc__header_string
= g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION
);
364 #if GLIB_CHECK_VERSION(2,12,0)
365 g_option_context_set_description (context
, mc_args__loc__footer_string
);
366 g_option_context_set_summary (context
, mc_args__loc__header_string
);
371 /* --------------------------------------------------------------------------------------------- */
374 mc_args_process (void)
376 if (mc_args__version
)
381 if (mc_args__show_datadirs
)
383 printf ("%s (%s)\n", mc_home
, mc_home_alt
);
387 if (mc_args__force_colors
)
388 mc_args__disable_colors
= FALSE
;
390 #ifdef HAVE_SUBSHELL_SUPPORT
391 if (mc_args__nouse_subshell
)
394 if (mc_args__nouse_subshell
)
396 #endif /* HAVE_SUBSHELL_SUPPORT */
401 /* --------------------------------------------------------------------------------------------- */
404 mc_args__convert_help_to_syscharset (const gchar
* charset
, const gchar
* error_message
,
405 const gchar
* help_str
)
407 GString
*buffer
= g_string_new ("");
408 GIConv conv
= g_iconv_open (charset
, "UTF-8");
409 gchar
*full_help_str
= g_strdup_printf ("%s\n\n%s\n", error_message
, help_str
);
411 str_convert (conv
, full_help_str
, buffer
);
413 g_free (full_help_str
);
414 g_iconv_close (conv
);
416 return g_string_free (buffer
, FALSE
);
419 /* --------------------------------------------------------------------------------------------- */
421 /*** public functions ****************************************************************************/
423 /* --------------------------------------------------------------------------------------------- */
426 mc_args_handle (int *argc
, char ***argv
, const gchar
* translation_domain
)
428 GError
*error
= NULL
;
429 const gchar
*_system_codepage
= str_detect_termencoding ();
432 if (!str_isutf8 (_system_codepage
))
433 bind_textdomain_codeset ("mc", "UTF-8");
436 context
= g_option_context_new (mc_args_add_usage_info ());
438 g_option_context_set_ignore_unknown_options (context
, FALSE
);
440 mc_args_add_extended_info_to_help ();
442 main_group
= g_option_group_new ("main", _("Main options"), _("Main options"), NULL
, NULL
);
444 g_option_group_add_entries (main_group
, argument_main_table
);
445 g_option_context_set_main_group (context
, main_group
);
446 g_option_group_set_translation_domain (main_group
, translation_domain
);
449 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
450 _("Terminal options"), NULL
, NULL
);
452 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
453 g_option_context_add_group (context
, terminal_group
);
454 g_option_group_set_translation_domain (terminal_group
, translation_domain
);
457 color_group
= mc_args_new_color_group ();
459 g_option_group_add_entries (color_group
, argument_color_table
);
460 g_option_context_add_group (context
, color_group
);
461 g_option_group_set_translation_domain (color_group
, translation_domain
);
463 if (!g_option_context_parse (context
, argc
, argv
, &error
))
467 gchar
*full_help_str
;
470 #if GLIB_CHECK_VERSION(2,14,0)
471 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
473 help_str
= g_strdup ("");
475 if (!str_isutf8 (_system_codepage
))
477 mc_args__convert_help_to_syscharset (_system_codepage
, error
->message
,
480 full_help_str
= g_strdup_printf ("%s\n\n%s\n", error
->message
, help_str
);
482 fprintf (stderr
, "%s", full_help_str
);
485 g_free (full_help_str
);
486 g_error_free (error
);
488 g_option_context_free (context
);
489 mc_args_clean_temp_help_strings ();
493 g_option_context_free (context
);
494 mc_args_clean_temp_help_strings ();
497 if (!str_isutf8 (_system_codepage
))
498 bind_textdomain_codeset ("mc", _system_codepage
);
501 return mc_args_process ();
504 /* --------------------------------------------------------------------------------------------- */