Changes into src directory:
[midnight-commander.git] / src / args.c
blob15e3da2035801c2dd8241716aa4175ce6d9e6d3e
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 /* generic options */
97 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
98 &mc_args__version,
99 N_("Displays the current version"),
100 NULL},
102 /* options for wrappers */
104 "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
105 &mc_args__show_datadirs,
106 N_("Print data directory"),
107 NULL},
110 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
111 &mc_args__last_wd_file,
112 N_("Print last working directory to specified file"),
113 "<file>"},
115 #ifdef HAVE_SUBSHELL_SUPPORT
117 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
118 &use_subshell,
119 N_("Enables subshell support (default)"),
120 NULL},
123 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
124 &mc_args__nouse_subshell,
125 N_("Disables subshell support"),
126 NULL},
127 #endif
129 /* debug options */
130 #ifdef USE_NETCODE
132 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
133 &mc_args__netfs_logfile,
134 N_("Log ftp dialog to specified file"),
135 "<file>"},
136 #ifdef ENABLE_VFS_SMB
138 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
139 &mc_args__debug_level,
140 N_("Set debug level"),
141 "<integer>"},
142 #endif /* ENABLE_VFS_SMB */
143 #endif
145 /* single file operations */
147 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
148 &mc_run_param0,
149 N_("Launches the file viewer on a file"),
150 "<file>"},
153 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
154 &mc_run_param0,
155 N_("Edits one file"),
156 "<file>"},
158 {NULL}
161 GOptionGroup *terminal_group;
162 #define ARGS_TERM_OPTIONS 0
163 static const GOptionEntry argument_terminal_table[] = {
165 /* terminal options */
167 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
168 &mc_args__force_xterm,
169 N_("Forces xterm features"),
170 NULL},
173 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
174 &mc_args__nomouse,
175 N_("Disable mouse support in text version"),
176 NULL},
178 #ifdef HAVE_SLANG
180 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
181 &SLtt_Try_Termcap,
182 N_("Tries to use termcap instead of terminfo"),
183 NULL},
184 #endif
187 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
188 &mc_args__slow_terminal,
189 N_("To run on slow terminals"),
190 NULL},
193 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
194 &mc_args__ugly_line_drawing,
195 N_("Use stickchars to draw"),
196 NULL},
199 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
200 &reset_hp_softkeys,
201 N_("Resets soft keys on HP terminals"),
202 NULL},
205 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
206 &mc_args__keymap_file,
207 N_("Load definitions of key bindings from specified file"),
208 "<file>"},
210 {NULL}
213 #undef ARGS_TERM_OPTIONS
215 GOptionGroup *color_group;
216 #define ARGS_COLOR_OPTIONS 0
217 // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
218 static const GOptionEntry argument_color_table[] = {
219 /* color options */
221 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
222 &mc_args__disable_colors,
223 N_("Requests to run in black and white"),
224 NULL},
227 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
228 &mc_args__force_colors,
229 N_("Request to run in color mode"),
230 NULL},
233 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
234 &command_line_colors,
235 N_("Specifies a color configuration"),
236 "<string>"},
239 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
240 &mc_args__skin,
241 N_("Show mc with specified skin"),
242 "<string>"},
244 { NULL }
247 #undef ARGS_COLOR_OPTIONS
249 static gchar *mc_args__loc__colors_string = NULL;
250 static gchar *mc_args__loc__footer_string = NULL;
251 static gchar *mc_args__loc__header_string = NULL;
252 static gchar *mc_args__loc__usage_string = NULL;
254 /*** file scope functions ************************************************************************/
256 /* --------------------------------------------------------------------------------------------- */
257 static void
258 mc_args_clean_temp_help_strings (void)
260 g_free (mc_args__loc__colors_string);
261 mc_args__loc__colors_string = NULL;
263 g_free (mc_args__loc__footer_string);
264 mc_args__loc__footer_string = NULL;
266 g_free (mc_args__loc__header_string);
267 mc_args__loc__header_string = NULL;
269 g_free (mc_args__loc__usage_string);
270 mc_args__loc__usage_string = NULL;
273 /* --------------------------------------------------------------------------------------------- */
275 static GOptionGroup *
276 mc_args_new_color_group (void)
279 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
280 * and editmarked. To preserve translations, lines should be split.
283 mc_args__loc__colors_string = g_strdup_printf ("%s%s",
284 /* TRANSLATORS: don't translate keywords and names of colors */
285 _("--colors KEYWORD={FORE},{BACK}\n\n"
286 "{FORE} and {BACK} can be omitted, and the default will be used\n"
287 "\n" "Keywords:\n"
288 " Global: errors, reverse, gauge, input, viewunderline\n"
289 " File display: normal, selected, marked, markselect\n"
290 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
291 " errdhotfocus\n"
292 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
293 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
294 " editlinestate\n"),
295 /* TRANSLATORS: don't translate keywords and names of colors */
297 (" Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
298 "\n" "Colors:\n"
299 " black, gray, red, brightred, green, brightgreen, brown,\n"
300 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
301 " brightcyan, lightgray and white\n\n"));
303 return g_option_group_new ("color", mc_args__loc__colors_string,
304 _("Color options"), NULL, NULL);
308 /* --------------------------------------------------------------------------------------------- */
310 static gchar *
311 mc_args_add_usage_info (void)
313 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
314 _("+number"),
315 _("[this_dir] [other_panel_dir]"),
316 _("+number"),
318 ("Set initial line number for the internal editor"));
319 return mc_args__loc__usage_string;
322 /* --------------------------------------------------------------------------------------------- */
324 static void
325 mc_args_add_extended_info_to_help (void)
327 mc_args__loc__footer_string = g_strdup_printf ("%s",
329 ("\n"
330 "Please send any bug reports (including the output of `mc -V')\n"
331 "to mc-devel@gnome.org\n"));
332 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
334 #if GLIB_CHECK_VERSION(2,12,0)
335 g_option_context_set_description (context, mc_args__loc__footer_string);
336 g_option_context_set_summary (context, mc_args__loc__header_string);
337 #endif
341 /* --------------------------------------------------------------------------------------------- */
343 static gboolean
344 mc_args_process (void)
346 if (mc_args__version)
348 show_version ();
349 return FALSE;
351 if (mc_args__show_datadirs)
353 printf ("%s (%s)\n", mc_home, mc_home_alt);
354 return FALSE;
357 if (mc_args__force_colors)
358 mc_args__disable_colors = FALSE;
360 #ifdef HAVE_SUBSHELL_SUPPORT
361 if (mc_args__nouse_subshell)
362 use_subshell = 0;
364 if (mc_args__nouse_subshell)
365 use_subshell = 0;
366 #endif /* HAVE_SUBSHELL_SUPPORT */
368 return TRUE;
371 /* --------------------------------------------------------------------------------------------- */
373 static gchar *
374 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
375 const gchar * help_str)
377 GString *buffer = g_string_new ("");
378 GIConv conv = g_iconv_open (charset, "UTF-8");
379 gchar *full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
381 str_convert (conv, full_help_str, buffer);
383 g_free (full_help_str);
384 g_iconv_close (conv);
386 return g_string_free (buffer, FALSE);
389 /* --------------------------------------------------------------------------------------------- */
391 /*** public functions ****************************************************************************/
393 /* --------------------------------------------------------------------------------------------- */
395 gboolean
396 mc_args_handle (int *argc, char ***argv, const gchar * translation_domain)
398 GError *error = NULL;
399 const gchar *_system_codepage = str_detect_termencoding ();
401 #ifdef ENABLE_NLS
402 if (!str_isutf8 (_system_codepage))
403 bind_textdomain_codeset ("mc", "UTF-8");
404 #endif
406 context = g_option_context_new (mc_args_add_usage_info ());
408 g_option_context_set_ignore_unknown_options (context, FALSE);
410 mc_args_add_extended_info_to_help ();
412 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
414 g_option_group_add_entries (main_group, argument_main_table);
415 g_option_context_set_main_group (context, main_group);
416 g_option_group_set_translation_domain (main_group, translation_domain);
419 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
420 _("Terminal options"), NULL, NULL);
422 g_option_group_add_entries (terminal_group, argument_terminal_table);
423 g_option_context_add_group (context, terminal_group);
424 g_option_group_set_translation_domain (terminal_group, translation_domain);
427 color_group = mc_args_new_color_group ();
429 g_option_group_add_entries (color_group, argument_color_table);
430 g_option_context_add_group (context, color_group);
431 g_option_group_set_translation_domain (color_group, translation_domain);
433 if (!g_option_context_parse (context, argc, argv, &error))
435 if (error != NULL)
437 gchar *full_help_str;
438 gchar *help_str;
440 #if GLIB_CHECK_VERSION(2,14,0)
441 help_str = g_option_context_get_help (context, TRUE, NULL);
442 #else
443 help_str = g_strdup ("");
444 #endif
445 if (!str_isutf8 (_system_codepage))
446 full_help_str =
447 mc_args__convert_help_to_syscharset (_system_codepage, error->message,
448 help_str);
449 else
450 full_help_str = g_strdup_printf ("%s\n\n%s\n", error->message, help_str);
452 fprintf (stderr, "%s", full_help_str);
454 g_free (help_str);
455 g_free (full_help_str);
456 g_error_free (error);
458 g_option_context_free (context);
459 mc_args_clean_temp_help_strings ();
460 return FALSE;
463 g_option_context_free (context);
464 mc_args_clean_temp_help_strings ();
466 #ifdef ENABLE_NLS
467 if (!str_isutf8 (_system_codepage))
468 bind_textdomain_codeset ("mc", _system_codepage);
469 #endif
471 return mc_args_process ();
474 /* --------------------------------------------------------------------------------------------- */