Merge branch '2121_dir_symlink'
[kaloumi3.git] / src / args.c
blob12846952209e0227b63d034b306304ce7c8afb0a
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
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"),
108 NULL
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"),
115 "<file>"
118 #ifdef HAVE_SUBSHELL_SUPPORT
120 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
121 &use_subshell,
122 N_("Enables subshell support (default)"),
123 NULL
127 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
128 &mc_args__nouse_subshell,
129 N_("Disables subshell support"),
130 NULL
132 #endif
134 /* debug options */
135 #ifdef USE_NETCODE
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"),
140 "<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"),
147 "<integer>"
149 #endif /* ENABLE_VFS_SMB */
150 #endif
152 /* single file operations */
154 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
155 &view_one_file,
156 N_("Launches the file viewer on a file"),
157 "<file>"
161 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
162 &edit_one_file,
163 N_("Edits one file"),
164 "<file>"
167 {NULL}
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"),
179 NULL
183 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
184 &mc_args__nomouse,
185 N_("Disable mouse support in text version"),
186 NULL
189 #ifdef HAVE_SLANG
191 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
192 &SLtt_Try_Termcap,
193 N_("Tries to use termcap instead of terminfo"),
194 NULL
196 #endif
199 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
200 &mc_args__slow_terminal,
201 N_("To run on slow terminals"),
202 NULL
206 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
207 &mc_args__ugly_line_drawing,
208 N_("Use stickchars to draw"),
209 NULL
213 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
214 &reset_hp_softkeys,
215 N_("Resets soft keys on HP terminals"),
216 NULL
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"),
223 "<file>"
226 {NULL}
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[] = {
234 /* color options */
236 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
237 &mc_args__disable_colors,
238 N_("Requests to run in black and white"),
239 NULL
243 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
244 &mc_args__force_colors,
245 N_("Request to run in color mode"),
246 NULL
250 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
251 &command_line_colors,
252 N_("Specifies a color configuration"),
253 "<string>"
257 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
258 &mc_args__skin,
259 N_("Show mc with specified skin"),
260 "<string>"
263 {NULL}
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 /* --------------------------------------------------------------------------------------------- */
275 static void
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"
305 "\n" "Keywords:\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"
309 " errdhotfocus\n"
310 " Menus: menu, menuhot, menusel, menuhotsel, menuinactive\n"
311 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
312 " editlinestate\n"),
313 /* TRANSLATORS: don't translate keywords and names of colors */
314 _( " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
315 "\n" "Colors:\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 /* --------------------------------------------------------------------------------------------- */
328 static gchar *
329 mc_args_add_usage_info(void)
331 mc_args__loc__usage_string = g_strdup_printf("[%s] %s\n %s - %s\n",
332 _("+number"),
333 _("[this_dir] [other_panel_dir]"),
334 _("+number"),
335 _("Set initial line number for the internal editor")
337 return mc_args__loc__usage_string;
340 /* --------------------------------------------------------------------------------------------- */
342 static void
343 mc_args_add_extended_info_to_help(void)
345 mc_args__loc__footer_string = g_strdup_printf("%s",
347 ("\n"
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);
356 #endif
360 /* --------------------------------------------------------------------------------------------- */
362 static gboolean
363 mc_args_process(void)
365 if (mc_args__version){
366 show_version ();
367 return FALSE;
369 if (mc_args__show_datadirs){
370 printf ("%s (%s)\n", mc_home, mc_home_alt);
371 return FALSE;
374 if (mc_args__force_colors)
375 mc_args__disable_colors = FALSE;
377 #ifdef HAVE_SUBSHELL_SUPPORT
378 if (mc_args__nouse_subshell)
379 use_subshell = 0;
381 if (mc_args__nouse_subshell)
382 use_subshell = 0;
383 #endif /* HAVE_SUBSHELL_SUPPORT */
385 return TRUE;
388 /* --------------------------------------------------------------------------------------------- */
390 static gchar *
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 /* --------------------------------------------------------------------------------------------- */
411 gboolean
412 mc_args_handle(int *argc, char ***argv, const gchar *translation_domain)
414 GError *error = NULL;
415 const gchar *_system_codepage = str_detect_termencoding();
417 #ifdef ENABLE_NLS
418 if (!str_isutf8 (_system_codepage))
419 bind_textdomain_codeset ("mc", "UTF-8");
420 #endif
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)) {
451 if (error != NULL)
453 gchar *full_help_str;
454 gchar *help_str;
456 #if GLIB_CHECK_VERSION(2,14,0)
457 help_str = g_option_context_get_help (context, TRUE, NULL);
458 #else
459 help_str = g_strdup("");
460 #endif
461 if ( !str_isutf8 (_system_codepage))
462 full_help_str = mc_args__convert_help_to_syscharset(_system_codepage,error->message, help_str);
463 else
464 full_help_str = g_strdup_printf("%s\n\n%s\n",error->message,help_str);
466 fprintf(stderr, "%s",full_help_str);
468 g_free(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();
474 return FALSE;
477 g_option_context_free (context);
478 mc_args_clean_temp_help_strings();
480 #ifdef ENABLE_NLS
481 if (!str_isutf8 (_system_codepage))
482 bind_textdomain_codeset ("mc", _system_codepage);
483 #endif
485 return mc_args_process();
488 /* --------------------------------------------------------------------------------------------- */