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
[] = {
97 "version", 'V', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
99 N_("Displays the current version"),
103 /* options for wrappers */
105 "datadir", 'f', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
106 &mc_args__show_datadirs
,
107 N_("Print data directory"),
112 "printwd", 'P', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
113 &mc_args__last_wd_file
,
114 N_("Print last working directory to specified file"),
118 #ifdef HAVE_SUBSHELL_SUPPORT
120 "subshell", 'U', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
122 N_("Enables subshell support (default)"),
127 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_NONE
,
128 &mc_args__nouse_subshell
,
129 N_("Disables subshell support"),
137 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
138 &mc_args__netfs_logfile
,
139 N_("Log ftp dialog to specified file"),
142 #ifdef ENABLE_VFS_SMB
144 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_INT
,
145 &mc_args__debug_level
,
146 N_("Set debug level"),
149 #endif /* ENABLE_VFS_SMB */
152 /* single file operations */
154 "view", 'v', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
156 N_("Launches the file viewer on a file"),
161 "edit", 'e', G_OPTION_FLAG_IN_MAIN
, G_OPTION_ARG_STRING
,
163 N_("Edits one file"),
170 GOptionGroup
*terminal_group
;
171 #define ARGS_TERM_OPTIONS 0
172 static const GOptionEntry argument_terminal_table
[] = {
174 /* terminal options */
176 "xterm", 'x', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
177 &mc_args__force_xterm
,
178 N_("Forces xterm features"),
183 "nomouse", 'd', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
185 N_("Disable mouse support in text version"),
191 "termcap", 't', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
193 N_("Tries to use termcap instead of terminfo"),
199 "slow", 's', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
200 &mc_args__slow_terminal
,
201 N_("To run on slow terminals"),
206 "stickchars", 'a', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
207 &mc_args__ugly_line_drawing
,
208 N_("Use stickchars to draw"),
213 "resetsoft", 'k', ARGS_TERM_OPTIONS
, G_OPTION_ARG_NONE
,
215 N_("Resets soft keys on HP terminals"),
220 "keymap", 'K', ARGS_TERM_OPTIONS
, G_OPTION_ARG_STRING
,
221 &mc_args__keymap_file
,
222 N_("Load definitions of key bindings from specified file"),
228 #undef ARGS_TERM_OPTIONS
230 GOptionGroup
*color_group
;
231 #define ARGS_COLOR_OPTIONS 0
232 // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
233 static const GOptionEntry argument_color_table
[] = {
236 "nocolor", 'b', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
237 &mc_args__disable_colors
,
238 N_("Requests to run in black and white"),
243 "color", 'c', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_NONE
,
244 &mc_args__force_colors
,
245 N_("Request to run in color mode"),
250 "colors", 'C', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
251 &command_line_colors
,
252 N_("Specifies a color configuration"),
257 "skin", 'S', ARGS_COLOR_OPTIONS
, G_OPTION_ARG_STRING
,
259 N_("Show mc with specified skin"),
265 #undef ARGS_COLOR_OPTIONS
267 static gchar
*mc_args__loc__colors_string
= NULL
;
268 static gchar
*mc_args__loc__footer_string
= NULL
;
269 static gchar
*mc_args__loc__header_string
= NULL
;
270 static gchar
*mc_args__loc__usage_string
= NULL
;
272 /*** file scope functions ************************************************************************/
274 /* --------------------------------------------------------------------------------------------- */
276 mc_args_clean_temp_help_strings(void)
278 g_free(mc_args__loc__colors_string
);
279 mc_args__loc__colors_string
= NULL
;
281 g_free(mc_args__loc__footer_string
);
282 mc_args__loc__footer_string
= NULL
;
284 g_free(mc_args__loc__header_string
);
285 mc_args__loc__header_string
= NULL
;
287 g_free(mc_args__loc__usage_string
);
288 mc_args__loc__usage_string
= NULL
;
291 /* --------------------------------------------------------------------------------------------- */
293 static GOptionGroup
*
294 mc_args_new_color_group(void)
297 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
298 * and editmarked. To preserve translations, lines should be split.
301 mc_args__loc__colors_string
= g_strdup_printf("%s%s",
302 /* TRANSLATORS: don't translate keywords and names of colors */
303 _( "--colors KEYWORD={FORE},{BACK}\n\n"
304 "{FORE} and {BACK} can be omitted, and the default will be used\n"
306 " Global: errors, reverse, gauge, input, viewunderline\n"
307 " File display: normal, selected, marked, markselect\n"
308 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
310 " Menus: menu, menuhot, menusel, menuhotsel, menuinactive\n"
311 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
313 /* TRANSLATORS: don't translate keywords and names of colors */
314 _( " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
316 " black, gray, red, brightred, green, brightgreen, brown,\n"
317 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
318 " brightcyan, lightgray and white\n\n")
321 return g_option_group_new ("color", mc_args__loc__colors_string
,
322 _("Color options"),NULL
, NULL
);
326 /* --------------------------------------------------------------------------------------------- */
329 mc_args_add_usage_info(void)
331 mc_args__loc__usage_string
= g_strdup_printf("[%s] %s\n %s - %s\n",
333 _("[this_dir] [other_panel_dir]"),
335 _("Set initial line number for the internal editor")
337 return mc_args__loc__usage_string
;
340 /* --------------------------------------------------------------------------------------------- */
343 mc_args_add_extended_info_to_help(void)
345 mc_args__loc__footer_string
= g_strdup_printf("%s",
348 "Please send any bug reports (including the output of `mc -V')\n"
349 "to mc-devel@gnome.org\n")
351 mc_args__loc__header_string
= g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION
);
353 #if GLIB_CHECK_VERSION(2,12,0)
354 g_option_context_set_description (context
, mc_args__loc__footer_string
);
355 g_option_context_set_summary (context
, mc_args__loc__header_string
);
360 /* --------------------------------------------------------------------------------------------- */
363 mc_args_process(void)
365 if (mc_args__version
){
369 if (mc_args__show_datadirs
){
370 printf ("%s (%s)\n", mc_home
, mc_home_alt
);
374 if (mc_args__force_colors
)
375 mc_args__disable_colors
= FALSE
;
377 #ifdef HAVE_SUBSHELL_SUPPORT
378 if (mc_args__nouse_subshell
)
381 if (mc_args__nouse_subshell
)
383 #endif /* HAVE_SUBSHELL_SUPPORT */
388 /* --------------------------------------------------------------------------------------------- */
391 mc_args__convert_help_to_syscharset(const gchar
*charset
, const gchar
*error_message
, const gchar
*help_str
)
393 GString
*buffer
= g_string_new("");
394 GIConv conv
= g_iconv_open ( charset
, "UTF-8");
395 gchar
*full_help_str
= g_strdup_printf("%s\n\n%s\n",error_message
,help_str
);
397 str_convert (conv
, full_help_str
, buffer
);
399 g_free(full_help_str
);
400 g_iconv_close (conv
);
402 return g_string_free(buffer
, FALSE
);
405 /* --------------------------------------------------------------------------------------------- */
407 /*** public functions ****************************************************************************/
409 /* --------------------------------------------------------------------------------------------- */
412 mc_args_handle(int *argc
, char ***argv
, const gchar
*translation_domain
)
414 GError
*error
= NULL
;
415 const gchar
*_system_codepage
= str_detect_termencoding();
418 if (!str_isutf8 (_system_codepage
))
419 bind_textdomain_codeset ("mc", "UTF-8");
422 context
= g_option_context_new (mc_args_add_usage_info());
424 g_option_context_set_ignore_unknown_options (context
, FALSE
);
426 mc_args_add_extended_info_to_help();
428 main_group
= g_option_group_new ("main", _("Main options"),
429 _("Main options"),NULL
, NULL
);
431 g_option_group_add_entries (main_group
, argument_main_table
);
432 g_option_context_set_main_group (context
, main_group
);
433 g_option_group_set_translation_domain(main_group
, translation_domain
);
436 terminal_group
= g_option_group_new ("terminal", _("Terminal options"),
437 _("Terminal options"),NULL
, NULL
);
439 g_option_group_add_entries (terminal_group
, argument_terminal_table
);
440 g_option_context_add_group (context
, terminal_group
);
441 g_option_group_set_translation_domain(terminal_group
, translation_domain
);
444 color_group
= mc_args_new_color_group();
446 g_option_group_add_entries (color_group
, argument_color_table
);
447 g_option_context_add_group (context
, color_group
);
448 g_option_group_set_translation_domain(color_group
, translation_domain
);
450 if (! g_option_context_parse (context
, argc
, argv
, &error
)) {
453 gchar
*full_help_str
;
456 #if GLIB_CHECK_VERSION(2,14,0)
457 help_str
= g_option_context_get_help (context
, TRUE
, NULL
);
459 help_str
= g_strdup("");
461 if ( !str_isutf8 (_system_codepage
))
462 full_help_str
= mc_args__convert_help_to_syscharset(_system_codepage
,error
->message
, help_str
);
464 full_help_str
= g_strdup_printf("%s\n\n%s\n",error
->message
,help_str
);
466 fprintf(stderr
, "%s",full_help_str
);
469 g_free(full_help_str
);
470 g_error_free (error
);
472 g_option_context_free (context
);
473 mc_args_clean_temp_help_strings();
477 g_option_context_free (context
);
478 mc_args_clean_temp_help_strings();
481 if (!str_isutf8 (_system_codepage
))
482 bind_textdomain_codeset ("mc", _system_codepage
);
485 return mc_args_process();
488 /* --------------------------------------------------------------------------------------------- */