Update NEWS file for #2135
[midnight-commander.git] / src / args.c
blobb9b481e1c70fd1dc30a2534d6b0fe07c9205cd36
1 /*
2 Handle command line arguments.
4 Copyright (C) 2009 The Free Software Foundation, Inc.
6 Written by:
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,
24 MA 02110-1301, USA.
27 #include <config.h>
28 #include <stdlib.h>
29 #include <stdio.h>
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"
36 #include "src/main.h"
37 #include "src/textconf.h"
38 #include "subshell.h" /* use_subshell */
40 #include "src/args.h"
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;
74 /* keymap file */
75 char *mc_args__keymap_file = NULL;
77 /* Debug level */
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[] = {
95 /* *INDENT-OFF* */
96 /* generic options */
98 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
99 &mc_args__version,
100 N_("Displays the current version"),
101 NULL
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"),
109 NULL
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"),
116 "<file>"
119 #ifdef HAVE_SUBSHELL_SUPPORT
121 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
122 &use_subshell,
123 N_("Enables subshell support (default)"),
124 NULL
128 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
129 &mc_args__nouse_subshell,
130 N_("Disables subshell support"),
131 NULL
133 #endif
135 /* debug options */
136 #ifdef USE_NETCODE
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"),
141 "<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"),
148 "<integer>"
150 #endif /* ENABLE_VFS_SMB */
151 #endif
153 /* single file operations */
155 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
156 &mc_run_param0,
157 N_("Launches the file viewer on a file"),
158 "<file>"
162 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
163 &mc_run_param0,
164 N_("Edits one file"),
165 "<file>"},
168 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
170 /* *INDENT-ON* */
173 GOptionGroup *terminal_group;
174 #define ARGS_TERM_OPTIONS 0
175 static const GOptionEntry argument_terminal_table[] = {
176 /* *INDENT-OFF* */
177 /* terminal options */
179 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
180 &mc_args__force_xterm,
181 N_("Forces xterm features"),
182 NULL
186 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
187 &mc_args__nomouse,
188 N_("Disable mouse support in text version"),
189 NULL
192 #ifdef HAVE_SLANG
194 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
195 &SLtt_Try_Termcap,
196 N_("Tries to use termcap instead of terminfo"),
197 NULL
199 #endif
202 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
203 &mc_args__slow_terminal,
204 N_("To run on slow terminals"),
205 NULL
209 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
210 &mc_args__ugly_line_drawing,
211 N_("Use stickchars to draw"),
212 NULL
216 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
217 &reset_hp_softkeys,
218 N_("Resets soft keys on HP terminals"),
219 NULL
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"),
226 "<file>"
230 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
232 /* *INDENT-ON* */
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[] = {
241 /* *INDENT-OFF* */
242 /* color options */
244 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
245 &mc_args__disable_colors,
246 N_("Requests to run in black and white"),
247 NULL
251 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
252 &mc_args__force_colors,
253 N_("Request to run in color mode"),
254 NULL
258 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
259 &command_line_colors,
260 N_("Specifies a color configuration"),
261 "<string>"
265 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
266 &mc_args__skin,
267 N_("Show mc with specified skin"),
268 "<string>"
272 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
274 /* *INDENT-ON* */
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 /* --------------------------------------------------------------------------------------------- */
287 static void
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"
317 "\n" "Keywords:\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"
321 " errdhotfocus\n"
322 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
323 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
324 " editlinestate\n"),
325 /* TRANSLATORS: don't translate keywords and names of colors */
327 (" Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
328 "\n" "Colors:\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 /* --------------------------------------------------------------------------------------------- */
340 static gchar *
341 mc_args_add_usage_info (void)
343 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
344 _("+number"),
345 _("[this_dir] [other_panel_dir]"),
346 _("+number"),
348 ("Set initial line number for the internal editor"));
349 return mc_args__loc__usage_string;
352 /* --------------------------------------------------------------------------------------------- */
354 static void
355 mc_args_add_extended_info_to_help (void)
357 mc_args__loc__footer_string = g_strdup_printf ("%s",
359 ("\n"
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);
367 #endif
371 /* --------------------------------------------------------------------------------------------- */
373 static gboolean
374 mc_args_process (void)
376 if (mc_args__version)
378 show_version ();
379 return FALSE;
381 if (mc_args__show_datadirs)
383 printf ("%s (%s)\n", mc_home, mc_home_alt);
384 return FALSE;
387 if (mc_args__force_colors)
388 mc_args__disable_colors = FALSE;
390 #ifdef HAVE_SUBSHELL_SUPPORT
391 if (mc_args__nouse_subshell)
392 use_subshell = 0;
394 if (mc_args__nouse_subshell)
395 use_subshell = 0;
396 #endif /* HAVE_SUBSHELL_SUPPORT */
398 return TRUE;
401 /* --------------------------------------------------------------------------------------------- */
403 static gchar *
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 /* --------------------------------------------------------------------------------------------- */
425 gboolean
426 mc_args_handle (int *argc, char ***argv, const gchar * translation_domain)
428 GError *error = NULL;
429 const gchar *_system_codepage = str_detect_termencoding ();
431 #ifdef ENABLE_NLS
432 if (!str_isutf8 (_system_codepage))
433 bind_textdomain_codeset ("mc", "UTF-8");
434 #endif
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))
465 if (error != NULL)
467 gchar *full_help_str;
468 gchar *help_str;
470 #if GLIB_CHECK_VERSION(2,14,0)
471 help_str = g_option_context_get_help (context, TRUE, NULL);
472 #else
473 help_str = g_strdup ("");
474 #endif
475 if (!str_isutf8 (_system_codepage))
476 full_help_str =
477 mc_args__convert_help_to_syscharset (_system_codepage, error->message,
478 help_str);
479 else
480 full_help_str = g_strdup_printf ("%s\n\n%s\n", error->message, help_str);
482 fprintf (stderr, "%s", full_help_str);
484 g_free (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 ();
490 return FALSE;
493 g_option_context_free (context);
494 mc_args_clean_temp_help_strings ();
496 #ifdef ENABLE_NLS
497 if (!str_isutf8 (_system_codepage))
498 bind_textdomain_codeset ("mc", _system_codepage);
499 #endif
501 return mc_args_process ();
504 /* --------------------------------------------------------------------------------------------- */