Ticket #1396: Fixed logic of processing '--with-search-engine' configure option.
[midnight-commander.git] / src / args.c
blob5b864f4c881d01c45eadb7ec344021d554f05368
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 <stdio.h>
31 #include "../src/global.h"
32 #include "../src/tty/tty.h"
33 #include "../src/args.h"
34 #include "../src/strutil.h"
35 #include "../src/textconf.h"
37 /*** external variables **************************************************************************/
39 extern int reset_hp_softkeys;
40 extern int use_subshell;
42 extern char *mc_home;
43 extern char *mc_home_alt;
45 /* colors specified on the command line: they override any other setting */
46 extern char *command_line_colors;
48 extern const char *edit_one_file;
49 extern const char *view_one_file;
50 /*** global variables ****************************************************************************/
52 /* If true, show version info and exit */
53 gboolean mc_args__version = FALSE;
55 /* If true, assume we are running on an xterm terminal */
56 gboolean mc_args__force_xterm = FALSE;
58 gboolean mc_args__nomouse = FALSE;
60 /* For slow terminals */
61 gboolean mc_args__slow_terminal = FALSE;
63 /* If true use +, -, | for line drawing */
64 gboolean mc_args__ugly_line_drawing = FALSE;
66 /* Set to force black and white display at program startup */
67 gboolean mc_args__disable_colors = FALSE;
69 /* Force colors, only used by Slang */
70 gboolean mc_args__force_colors = FALSE;
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;
80 /* keymap file */
81 char *mc_args__keymap_file = NULL;
83 /* Debug level*/
84 int mc_args__debug_level = 0;
87 /*** file scope macro definitions ****************************************************************/
89 /*** file scope type declarations ****************************************************************/
91 /*** file scope variables ************************************************************************/
93 static GOptionContext *context;
96 static gboolean mc_args__nouse_subshell = FALSE;
97 static gboolean mc_args__show_datadirs = FALSE;
99 GOptionGroup *main_group;
100 static const GOptionEntry argument_main_table[] = {
101 /* generic options */
103 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
104 &mc_args__version,
105 N_("Displays the current version"),
106 NULL
109 /* options for wrappers */
111 "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
112 &mc_args__show_datadirs,
113 N_("Print data directory"),
114 NULL
118 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
119 &mc_args__last_wd_file,
120 N_("Print last working directory to specified file"),
121 "<file>"
124 #ifdef HAVE_SUBSHELL_SUPPORT
126 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
127 &use_subshell,
128 N_("Enables subshell support (default)"),
129 NULL
133 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
134 &mc_args__nouse_subshell,
135 N_("Disables subshell support"),
136 NULL
138 #endif
140 /* debug options */
141 #ifdef USE_NETCODE
143 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
144 &mc_args__netfs_logfile,
145 N_("Log ftp dialog to specified file"),
146 "<file>"
148 #ifdef WITH_SMBFS
150 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
151 &mc_args__debug_level,
152 N_("Set debug level"),
153 "<integer>"
155 #endif
156 #endif
158 /* single file operations */
160 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
161 &view_one_file,
162 N_("Launches the file viewer on a file"),
163 "<file>"
167 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
168 &edit_one_file,
169 N_("Edits one file"),
170 "<file>"
173 {NULL}
176 GOptionGroup *terminal_group;
177 #define ARGS_TERM_OPTIONS 0
178 static const GOptionEntry argument_terminal_table[] = {
180 /* terminal options */
182 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
183 &mc_args__force_xterm,
184 N_("Forces xterm features"),
185 NULL
189 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
190 &mc_args__nomouse,
191 N_("Disable mouse support in text version"),
192 NULL
195 #ifdef HAVE_SLANG
197 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
198 &SLtt_Try_Termcap,
199 N_("Tries to use termcap instead of terminfo"),
200 NULL
202 #endif
205 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
206 &mc_args__slow_terminal,
207 N_("To run on slow terminals"),
208 NULL
212 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
213 &mc_args__ugly_line_drawing,
214 N_("Use stickchars to draw"),
215 NULL
219 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
220 &reset_hp_softkeys,
221 N_("Resets soft keys on HP terminals"),
222 NULL
226 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
227 &mc_args__keymap_file,
228 N_("Load definitions of key bindings from specified file"),
229 "<file>"
232 {NULL}
234 #undef ARGS_TERM_OPTIONS
236 GOptionGroup *color_group;
237 #define ARGS_COLOR_OPTIONS 0
238 // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
239 static const GOptionEntry argument_color_table[] = {
240 /* color options */
242 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
243 &mc_args__disable_colors,
244 N_("Requests to run in black and white"),
245 NULL
249 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
250 &mc_args__force_colors,
251 N_("Request to run in color mode"),
252 NULL
256 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
257 &command_line_colors,
258 N_("Specifies a color configuration"),
259 "<string>"
263 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
264 &mc_args__skin,
265 N_("Show mc with specified skin"),
266 "<string>"
269 {NULL}
271 #undef ARGS_COLOR_OPTIONS
273 static gchar *mc_args__loc__colors_string = NULL;
274 static gchar *mc_args__loc__footer_string = NULL;
275 static gchar *mc_args__loc__header_string = NULL;
276 static gchar *mc_args__loc__usage_string = NULL;
278 /*** file scope functions ************************************************************************/
280 /* --------------------------------------------------------------------------------------------- */
281 static void
282 mc_args_clean_temp_help_strings(void)
284 g_free(mc_args__loc__colors_string);
285 mc_args__loc__colors_string = NULL;
287 g_free(mc_args__loc__footer_string);
288 mc_args__loc__footer_string = NULL;
290 g_free(mc_args__loc__header_string);
291 mc_args__loc__header_string = NULL;
293 g_free(mc_args__loc__usage_string);
294 mc_args__loc__usage_string = NULL;
297 /* --------------------------------------------------------------------------------------------- */
299 static GOptionGroup *
300 mc_args_new_color_group(void)
303 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
304 * and editmarked. To preserve translations, lines should be split.
307 mc_args__loc__colors_string = g_strdup_printf("%s%s",
308 /* TRANSLATORS: don't translate keywords and names of colors */
309 _( "--colors KEYWORD={FORE},{BACK}\n\n"
310 "{FORE} and {BACK} can be omitted, and the default will be used\n"
311 "\n" "Keywords:\n"
312 " Global: errors, reverse, gauge, input, viewunderline\n"
313 " File display: normal, selected, marked, markselect\n"
314 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
315 " errdhotfocus\n"
316 " Menus: menu, menuhot, menusel, menuhotsel\n"
317 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
318 " editlinestate\n"),
319 /* TRANSLATORS: don't translate keywords and names of colors */
320 _( " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
321 "\n" "Colors:\n"
322 " black, gray, red, brightred, green, brightgreen, brown,\n"
323 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
324 " brightcyan, lightgray and white\n\n")
327 return g_option_group_new ("color", mc_args__loc__colors_string,
328 _("Color options"),NULL, NULL);
332 /* --------------------------------------------------------------------------------------------- */
334 static gchar *
335 mc_args_add_usage_info(void)
337 mc_args__loc__usage_string = g_strdup_printf("[%s] %s\n %s - %s\n",
338 _("+number"),
339 _("[this_dir] [other_panel_dir]"),
340 _("+number"),
341 _("Set initial line number for the internal editor")
343 return mc_args__loc__usage_string;
346 /* --------------------------------------------------------------------------------------------- */
348 static void
349 mc_args_add_extended_info_to_help(void)
351 mc_args__loc__footer_string = g_strdup_printf("%s",
353 ("\n"
354 "Please send any bug reports (including the output of `mc -V')\n"
355 "to mc-devel@gnome.org\n")
357 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
359 #if GLIB_CHECK_VERSION(2,12,0)
360 g_option_context_set_description (context, mc_args__loc__footer_string);
361 g_option_context_set_summary (context, mc_args__loc__header_string);
362 #endif
366 /* --------------------------------------------------------------------------------------------- */
368 static gboolean
369 mc_args_process(void)
371 if (mc_args__version){
372 show_version ();
373 return FALSE;
375 if (mc_args__show_datadirs){
376 printf ("%s (%s)\n", mc_home, mc_home_alt);
377 return FALSE;
380 if (mc_args__force_colors)
381 mc_args__disable_colors = FALSE;
383 #ifdef HAVE_SUBSHELL_SUPPORT
384 if (mc_args__nouse_subshell)
385 use_subshell = 0;
387 if (mc_args__nouse_subshell)
388 use_subshell = 0;
389 #endif /* HAVE_SUBSHELL_SUPPORT */
391 return TRUE;
394 /* --------------------------------------------------------------------------------------------- */
396 static gchar *
397 mc_args__convert_help_to_syscharset(const gchar *charset, const gchar *error_message, const gchar *help_str)
399 GString *buffer = g_string_new("");
400 GIConv conv = g_iconv_open ( charset, "UTF-8");
401 gchar *full_help_str = g_strdup_printf("%s\n\n%s\n",error_message,help_str);
403 str_convert (conv, full_help_str, buffer);
405 g_free(full_help_str);
406 g_iconv_close (conv);
408 return g_string_free(buffer, FALSE);
411 /* --------------------------------------------------------------------------------------------- */
413 /*** public functions ****************************************************************************/
415 /* --------------------------------------------------------------------------------------------- */
417 gboolean
418 mc_args_handle(int *argc, char ***argv, const gchar *translation_domain)
420 GError *error = NULL;
421 const gchar *_system_codepage = str_detect_termencoding();
423 #ifdef ENABLE_NLS
424 if (!str_isutf8 (_system_codepage))
425 bind_textdomain_codeset ("mc", "UTF-8");
426 #endif
428 context = g_option_context_new (mc_args_add_usage_info());
430 g_option_context_set_ignore_unknown_options (context, FALSE);
432 mc_args_add_extended_info_to_help();
434 main_group = g_option_group_new ("main", _("Main options"),
435 _("Main options"),NULL, NULL);
437 g_option_group_add_entries (main_group, argument_main_table);
438 g_option_context_set_main_group (context, main_group);
439 g_option_group_set_translation_domain(main_group, translation_domain);
442 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
443 _("Terminal options"),NULL, NULL);
445 g_option_group_add_entries (terminal_group, argument_terminal_table);
446 g_option_context_add_group (context, terminal_group);
447 g_option_group_set_translation_domain(terminal_group, translation_domain);
450 color_group = mc_args_new_color_group();
452 g_option_group_add_entries (color_group, argument_color_table);
453 g_option_context_add_group (context, color_group);
454 g_option_group_set_translation_domain(color_group, translation_domain);
456 if (! g_option_context_parse (context, argc, argv, &error)) {
457 if (error != NULL)
459 gchar *full_help_str;
460 gchar *help_str;
462 #if GLIB_CHECK_VERSION(2,14,0)
463 help_str = g_option_context_get_help (context, TRUE, NULL);
464 #else
465 help_str = g_strdup("");
466 #endif
467 if ( !str_isutf8 (_system_codepage))
468 full_help_str = mc_args__convert_help_to_syscharset(_system_codepage,error->message, help_str);
469 else
470 full_help_str = g_strdup_printf("%s\n\n%s\n",error->message,help_str);
472 fprintf(stderr, "%s",full_help_str);
474 g_free(help_str);
475 g_free(full_help_str);
476 g_error_free (error);
478 g_option_context_free (context);
479 mc_args_clean_temp_help_strings();
480 return FALSE;
483 g_option_context_free (context);
484 mc_args_clean_temp_help_strings();
486 #ifdef ENABLE_NLS
487 if (!str_isutf8 (_system_codepage))
488 bind_textdomain_codeset ("mc", _system_codepage);
489 #endif
491 return mc_args_process();
494 /* --------------------------------------------------------------------------------------------- */