Ticket #1935: Incorrect lock files check when hostname contains wide characters.
[midnight-commander.git] / src / args.c
blob60aa6d8c952643dc0f696aa246474fa4508febf8
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 "lib/global.h"
32 #include "lib/tty/tty.h"
33 #include "lib/strutil.h"
35 #include "src/args.h"
36 #include "src/textconf.h"
38 /*** external variables **************************************************************************/
40 extern int reset_hp_softkeys;
41 extern int use_subshell;
43 extern char *mc_home;
44 extern char *mc_home_alt;
46 /* colors specified on the command line: they override any other setting */
47 extern char *command_line_colors;
49 extern const char *edit_one_file;
50 extern const char *view_one_file;
51 /*** global variables ****************************************************************************/
53 /* If true, show version info and exit */
54 gboolean mc_args__version = FALSE;
56 /* If true, assume we are running on an xterm terminal */
57 gboolean mc_args__force_xterm = FALSE;
59 gboolean mc_args__nomouse = FALSE;
61 /* For slow terminals */
62 gboolean mc_args__slow_terminal = FALSE;
64 /* If true use +, -, | for line drawing */
65 gboolean mc_args__ugly_line_drawing = FALSE;
67 /* Set to force black and white display at program startup */
68 gboolean mc_args__disable_colors = FALSE;
70 /* Force colors, only used by Slang */
71 gboolean mc_args__force_colors = FALSE;
73 /* Show in specified skin */
74 char *mc_args__skin = NULL;
76 char *mc_args__last_wd_file = NULL;
78 /* when enabled NETCODE, use folowing file as logfile */
79 char *mc_args__netfs_logfile = NULL;
81 /* keymap file */
82 char *mc_args__keymap_file = NULL;
84 /* Debug level*/
85 int mc_args__debug_level = 0;
88 /*** file scope macro definitions ****************************************************************/
90 /*** file scope type declarations ****************************************************************/
92 /*** file scope variables ************************************************************************/
94 static GOptionContext *context;
97 static gboolean mc_args__nouse_subshell = FALSE;
98 static gboolean mc_args__show_datadirs = FALSE;
100 GOptionGroup *main_group;
101 static const GOptionEntry argument_main_table[] = {
102 /* generic options */
104 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
105 &mc_args__version,
106 N_("Displays the current version"),
107 NULL
110 /* options for wrappers */
112 "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
113 &mc_args__show_datadirs,
114 N_("Print data directory"),
115 NULL
119 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
120 &mc_args__last_wd_file,
121 N_("Print last working directory to specified file"),
122 "<file>"
125 #ifdef HAVE_SUBSHELL_SUPPORT
127 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
128 &use_subshell,
129 N_("Enables subshell support (default)"),
130 NULL
134 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
135 &mc_args__nouse_subshell,
136 N_("Disables subshell support"),
137 NULL
139 #endif
141 /* debug options */
142 #ifdef USE_NETCODE
144 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
145 &mc_args__netfs_logfile,
146 N_("Log ftp dialog to specified file"),
147 "<file>"
149 #ifdef ENABLE_VFS_SMB
151 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
152 &mc_args__debug_level,
153 N_("Set debug level"),
154 "<integer>"
156 #endif /* ENABLE_VFS_SMB */
157 #endif
159 /* single file operations */
161 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
162 &view_one_file,
163 N_("Launches the file viewer on a file"),
164 "<file>"
168 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
169 &edit_one_file,
170 N_("Edits one file"),
171 "<file>"
174 {NULL}
177 GOptionGroup *terminal_group;
178 #define ARGS_TERM_OPTIONS 0
179 static const GOptionEntry argument_terminal_table[] = {
181 /* terminal options */
183 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
184 &mc_args__force_xterm,
185 N_("Forces xterm features"),
186 NULL
190 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
191 &mc_args__nomouse,
192 N_("Disable mouse support in text version"),
193 NULL
196 #ifdef HAVE_SLANG
198 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
199 &SLtt_Try_Termcap,
200 N_("Tries to use termcap instead of terminfo"),
201 NULL
203 #endif
206 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
207 &mc_args__slow_terminal,
208 N_("To run on slow terminals"),
209 NULL
213 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
214 &mc_args__ugly_line_drawing,
215 N_("Use stickchars to draw"),
216 NULL
220 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
221 &reset_hp_softkeys,
222 N_("Resets soft keys on HP terminals"),
223 NULL
227 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
228 &mc_args__keymap_file,
229 N_("Load definitions of key bindings from specified file"),
230 "<file>"
233 {NULL}
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 /* color options */
243 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
244 &mc_args__disable_colors,
245 N_("Requests to run in black and white"),
246 NULL
250 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
251 &mc_args__force_colors,
252 N_("Request to run in color mode"),
253 NULL
257 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
258 &command_line_colors,
259 N_("Specifies a color configuration"),
260 "<string>"
264 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
265 &mc_args__skin,
266 N_("Show mc with specified skin"),
267 "<string>"
270 {NULL}
272 #undef ARGS_COLOR_OPTIONS
274 static gchar *mc_args__loc__colors_string = NULL;
275 static gchar *mc_args__loc__footer_string = NULL;
276 static gchar *mc_args__loc__header_string = NULL;
277 static gchar *mc_args__loc__usage_string = NULL;
279 /*** file scope functions ************************************************************************/
281 /* --------------------------------------------------------------------------------------------- */
282 static void
283 mc_args_clean_temp_help_strings(void)
285 g_free(mc_args__loc__colors_string);
286 mc_args__loc__colors_string = NULL;
288 g_free(mc_args__loc__footer_string);
289 mc_args__loc__footer_string = NULL;
291 g_free(mc_args__loc__header_string);
292 mc_args__loc__header_string = NULL;
294 g_free(mc_args__loc__usage_string);
295 mc_args__loc__usage_string = NULL;
298 /* --------------------------------------------------------------------------------------------- */
300 static GOptionGroup *
301 mc_args_new_color_group(void)
304 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
305 * and editmarked. To preserve translations, lines should be split.
308 mc_args__loc__colors_string = g_strdup_printf("%s%s",
309 /* TRANSLATORS: don't translate keywords and names of colors */
310 _( "--colors KEYWORD={FORE},{BACK}\n\n"
311 "{FORE} and {BACK} can be omitted, and the default will be used\n"
312 "\n" "Keywords:\n"
313 " Global: errors, reverse, gauge, input, viewunderline\n"
314 " File display: normal, selected, marked, markselect\n"
315 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
316 " errdhotfocus\n"
317 " Menus: menu, menuhot, menusel, menuhotsel\n"
318 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
319 " editlinestate\n"),
320 /* TRANSLATORS: don't translate keywords and names of colors */
321 _( " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
322 "\n" "Colors:\n"
323 " black, gray, red, brightred, green, brightgreen, brown,\n"
324 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
325 " brightcyan, lightgray and white\n\n")
328 return g_option_group_new ("color", mc_args__loc__colors_string,
329 _("Color options"),NULL, NULL);
333 /* --------------------------------------------------------------------------------------------- */
335 static gchar *
336 mc_args_add_usage_info(void)
338 mc_args__loc__usage_string = g_strdup_printf("[%s] %s\n %s - %s\n",
339 _("+number"),
340 _("[this_dir] [other_panel_dir]"),
341 _("+number"),
342 _("Set initial line number for the internal editor")
344 return mc_args__loc__usage_string;
347 /* --------------------------------------------------------------------------------------------- */
349 static void
350 mc_args_add_extended_info_to_help(void)
352 mc_args__loc__footer_string = g_strdup_printf("%s",
354 ("\n"
355 "Please send any bug reports (including the output of `mc -V')\n"
356 "to mc-devel@gnome.org\n")
358 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
360 #if GLIB_CHECK_VERSION(2,12,0)
361 g_option_context_set_description (context, mc_args__loc__footer_string);
362 g_option_context_set_summary (context, mc_args__loc__header_string);
363 #endif
367 /* --------------------------------------------------------------------------------------------- */
369 static gboolean
370 mc_args_process(void)
372 if (mc_args__version){
373 show_version ();
374 return FALSE;
376 if (mc_args__show_datadirs){
377 printf ("%s (%s)\n", mc_home, mc_home_alt);
378 return FALSE;
381 if (mc_args__force_colors)
382 mc_args__disable_colors = FALSE;
384 #ifdef HAVE_SUBSHELL_SUPPORT
385 if (mc_args__nouse_subshell)
386 use_subshell = 0;
388 if (mc_args__nouse_subshell)
389 use_subshell = 0;
390 #endif /* HAVE_SUBSHELL_SUPPORT */
392 return TRUE;
395 /* --------------------------------------------------------------------------------------------- */
397 static gchar *
398 mc_args__convert_help_to_syscharset(const gchar *charset, const gchar *error_message, const gchar *help_str)
400 GString *buffer = g_string_new("");
401 GIConv conv = g_iconv_open ( charset, "UTF-8");
402 gchar *full_help_str = g_strdup_printf("%s\n\n%s\n",error_message,help_str);
404 str_convert (conv, full_help_str, buffer);
406 g_free(full_help_str);
407 g_iconv_close (conv);
409 return g_string_free(buffer, FALSE);
412 /* --------------------------------------------------------------------------------------------- */
414 /*** public functions ****************************************************************************/
416 /* --------------------------------------------------------------------------------------------- */
418 gboolean
419 mc_args_handle(int *argc, char ***argv, const gchar *translation_domain)
421 GError *error = NULL;
422 const gchar *_system_codepage = str_detect_termencoding();
424 #ifdef ENABLE_NLS
425 if (!str_isutf8 (_system_codepage))
426 bind_textdomain_codeset ("mc", "UTF-8");
427 #endif
429 context = g_option_context_new (mc_args_add_usage_info());
431 g_option_context_set_ignore_unknown_options (context, FALSE);
433 mc_args_add_extended_info_to_help();
435 main_group = g_option_group_new ("main", _("Main options"),
436 _("Main options"),NULL, NULL);
438 g_option_group_add_entries (main_group, argument_main_table);
439 g_option_context_set_main_group (context, main_group);
440 g_option_group_set_translation_domain(main_group, translation_domain);
443 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
444 _("Terminal options"),NULL, NULL);
446 g_option_group_add_entries (terminal_group, argument_terminal_table);
447 g_option_context_add_group (context, terminal_group);
448 g_option_group_set_translation_domain(terminal_group, translation_domain);
451 color_group = mc_args_new_color_group();
453 g_option_group_add_entries (color_group, argument_color_table);
454 g_option_context_add_group (context, color_group);
455 g_option_group_set_translation_domain(color_group, translation_domain);
457 if (! g_option_context_parse (context, argc, argv, &error)) {
458 if (error != NULL)
460 gchar *full_help_str;
461 gchar *help_str;
463 #if GLIB_CHECK_VERSION(2,14,0)
464 help_str = g_option_context_get_help (context, TRUE, NULL);
465 #else
466 help_str = g_strdup("");
467 #endif
468 if ( !str_isutf8 (_system_codepage))
469 full_help_str = mc_args__convert_help_to_syscharset(_system_codepage,error->message, help_str);
470 else
471 full_help_str = g_strdup_printf("%s\n\n%s\n",error->message,help_str);
473 fprintf(stderr, "%s",full_help_str);
475 g_free(help_str);
476 g_free(full_help_str);
477 g_error_free (error);
479 g_option_context_free (context);
480 mc_args_clean_temp_help_strings();
481 return FALSE;
484 g_option_context_free (context);
485 mc_args_clean_temp_help_strings();
487 #ifdef ENABLE_NLS
488 if (!str_isutf8 (_system_codepage))
489 bind_textdomain_codeset ("mc", _system_codepage);
490 #endif
492 return mc_args_process();
495 /* --------------------------------------------------------------------------------------------- */