fixed man and help
[midnight-commander.git] / src / args.c
blobd4742760af9a340d5bacb527f6f426260d95fada
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/tty/mouse.h"
35 #include "lib/strutil.h"
36 #include "lib/vfs/mc-vfs/vfs.h"
37 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debugf() */
39 #include "src/main.h"
40 #include "src/textconf.h"
41 #include "src/subshell.h" /* use_subshell */
43 #include "src/args.h"
45 /*** external variables **************************************************************************/
47 /*** global variables ****************************************************************************/
49 /* If true, show version info and exit */
50 gboolean mc_args__show_version = FALSE;
52 /* If true, assume we are running on an xterm terminal */
53 gboolean mc_args__force_xterm = FALSE;
55 gboolean mc_args__nomouse = FALSE;
57 /* For slow terminals */
58 gboolean mc_args__slow_terminal = FALSE;
60 /* If true use +, -, | for line drawing */
61 gboolean mc_args__ugly_line_drawing = FALSE;
63 /* Set to force black and white display at program startup */
64 gboolean mc_args__disable_colors = FALSE;
66 /* Force colors, only used by Slang */
67 gboolean mc_args__force_colors = FALSE;
69 /* Line to start the editor on */
70 int mc_args__edit_start_line = 0;
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;
86 /*** file scope macro definitions ****************************************************************/
88 /*** file scope type declarations ****************************************************************/
90 /*** file scope variables ************************************************************************/
92 /* forward declarations */
93 static gboolean parse_mc_e_argument (const gchar *option_name, const gchar *value,
94 gpointer data, GError **error);
95 static gboolean parse_mc_v_argument (const gchar *option_name, const gchar *value,
96 gpointer data, GError **error);
98 static GOptionContext *context;
100 static gboolean mc_args__nouse_subshell = FALSE;
101 static gboolean mc_args__show_datadirs = FALSE;
103 static GOptionGroup *main_group;
105 static const GOptionEntry argument_main_table[] = {
106 /* *INDENT-OFF* */
107 /* generic options */
109 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
110 &mc_args__show_version,
111 N_("Displays the current version"),
112 NULL
115 /* options for wrappers */
117 "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
118 &mc_args__show_datadirs,
119 N_("Print data directory"),
120 NULL
124 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
125 &mc_args__last_wd_file,
126 N_("Print last working directory to specified file"),
127 "<file>"
130 #ifdef HAVE_SUBSHELL_SUPPORT
132 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
133 &use_subshell,
134 N_("Enables subshell support (default)"),
135 NULL
139 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
140 &mc_args__nouse_subshell,
141 N_("Disables subshell support"),
142 NULL
144 #endif
146 /* debug options */
147 #ifdef USE_NETCODE
149 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
150 &mc_args__netfs_logfile,
151 N_("Log ftp dialog to specified file"),
152 "<file>"
154 #ifdef ENABLE_VFS_SMB
156 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
157 &mc_args__debug_level,
158 N_("Set debug level"),
159 "<integer>"
161 #endif /* ENABLE_VFS_SMB */
162 #endif
164 /* single file operations */
166 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
167 parse_mc_v_argument,
168 N_("Launches the file viewer on a file"),
169 "<file>"
173 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
174 parse_mc_e_argument,
175 N_("Edits one file"),
176 "<file>"},
179 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
181 /* *INDENT-ON* */
184 GOptionGroup *terminal_group;
185 #define ARGS_TERM_OPTIONS 0
186 static const GOptionEntry argument_terminal_table[] = {
187 /* *INDENT-OFF* */
188 /* terminal options */
190 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
191 &mc_args__force_xterm,
192 N_("Forces xterm features"),
193 NULL
197 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
198 &mc_args__nomouse,
199 N_("Disable mouse support in text version"),
200 NULL
203 #ifdef HAVE_SLANG
205 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
206 &SLtt_Try_Termcap,
207 N_("Tries to use termcap instead of terminfo"),
208 NULL
210 #endif
213 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
214 &mc_args__slow_terminal,
215 N_("To run on slow terminals"),
216 NULL
220 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
221 &mc_args__ugly_line_drawing,
222 N_("Use stickchars to draw"),
223 NULL
227 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
228 &reset_hp_softkeys,
229 N_("Resets soft keys on HP terminals"),
230 NULL
234 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
235 &mc_args__keymap_file,
236 N_("Load definitions of key bindings from specified file"),
237 "<file>"
241 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
243 /* *INDENT-ON* */
246 #undef ARGS_TERM_OPTIONS
248 GOptionGroup *color_group;
249 #define ARGS_COLOR_OPTIONS 0
250 // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
251 static const GOptionEntry argument_color_table[] = {
252 /* *INDENT-OFF* */
253 /* color options */
255 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
256 &mc_args__disable_colors,
257 N_("Requests to run in black and white"),
258 NULL
262 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
263 &mc_args__force_colors,
264 N_("Request to run in color mode"),
265 NULL
269 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
270 &command_line_colors,
271 N_("Specifies a color configuration"),
272 "<string>"
276 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
277 &mc_args__skin,
278 N_("Show mc with specified skin"),
279 "<string>"
283 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
285 /* *INDENT-ON* */
288 #undef ARGS_COLOR_OPTIONS
290 static gchar *mc_args__loc__colors_string = NULL;
291 static gchar *mc_args__loc__footer_string = NULL;
292 static gchar *mc_args__loc__header_string = NULL;
293 static gchar *mc_args__loc__usage_string = NULL;
295 /*** file scope functions ************************************************************************/
297 /* --------------------------------------------------------------------------------------------- */
298 static void
299 mc_args_clean_temp_help_strings (void)
301 g_free (mc_args__loc__colors_string);
302 mc_args__loc__colors_string = NULL;
304 g_free (mc_args__loc__footer_string);
305 mc_args__loc__footer_string = NULL;
307 g_free (mc_args__loc__header_string);
308 mc_args__loc__header_string = NULL;
310 g_free (mc_args__loc__usage_string);
311 mc_args__loc__usage_string = NULL;
314 /* --------------------------------------------------------------------------------------------- */
316 static GOptionGroup *
317 mc_args_new_color_group (void)
320 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
321 * and editmarked. To preserve translations, lines should be split.
323 mc_args__loc__colors_string = g_strdup_printf ("%s%s",
324 /* TRANSLATORS: don't translate keywords and names of colors */
325 _("--colors KEYWORD={FORE},{BACK}\n\n"
326 "{FORE} and {BACK} can be omitted, and the default will be used\n"
327 "\n" "Keywords:\n"
328 " Global: errors, reverse, gauge, viewunderline\n"
329 " input, inputmark, inputunchanged, commandlinemark\n"
330 " File display: normal, selected, marked, markselect\n"
331 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
332 " errdhotfocus\n"
333 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
334 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
335 " editlinestate\n"),
336 /* TRANSLATORS: don't translate keywords and names of colors */
338 (" Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
339 "\n" "Colors:\n"
340 " black, gray, red, brightred, green, brightgreen, brown,\n"
341 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
342 " brightcyan, lightgray and white\n\n"));
344 return g_option_group_new ("color", mc_args__loc__colors_string,
345 _("Color options"), NULL, NULL);
349 /* --------------------------------------------------------------------------------------------- */
351 static gchar *
352 mc_args_add_usage_info (void)
354 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
355 _("+number"),
356 _("[this_dir] [other_panel_dir]"),
357 _("+number"),
359 ("Set initial line number for the internal editor"));
360 return mc_args__loc__usage_string;
363 /* --------------------------------------------------------------------------------------------- */
365 static void
366 mc_args_add_extended_info_to_help (void)
368 mc_args__loc__footer_string = g_strdup_printf ("%s",
370 ("\n"
371 "Please send any bug reports (including the output of `mc -V')\n"
372 "as tickets at www.midnight-commander.org\n"));
373 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
375 #if GLIB_CHECK_VERSION(2,12,0)
376 g_option_context_set_description (context, mc_args__loc__footer_string);
377 g_option_context_set_summary (context, mc_args__loc__header_string);
378 #endif
381 /* --------------------------------------------------------------------------------------------- */
383 static void
384 mc_setup_by_args (int argc, char *argv[])
386 const char *base;
387 char *tmp;
389 if (mc_args__nomouse)
390 use_mouse_p = MOUSE_DISABLED;
392 #ifdef USE_NETCODE
393 if (mc_args__netfs_logfile != NULL)
395 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
396 #ifdef ENABLE_VFS_SMB
397 smbfs_set_debugf (mc_args__netfs_logfile);
398 #endif /* ENABLE_VFS_SMB */
401 #ifdef ENABLE_VFS_SMB
402 if (mc_args__debug_level != 0)
403 smbfs_set_debug (mc_args__debug_level);
404 #endif /* ENABLE_VFS_SMB */
405 #endif /* USE_NETCODE */
407 base = x_basename (argv[0]);
408 tmp = (argc > 0) ? argv[1] : NULL;
410 if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
412 /* mce* or vi is link to mc */
414 mc_run_param0 = g_strdup ("");
415 if (tmp != NULL)
418 * Check for filename:lineno, followed by an optional colon.
419 * This format is used by many programs (especially compilers)
420 * in error messages and warnings. It is supported so that
421 * users can quickly copy and paste file locations.
423 char *end, *p;
425 end = tmp + strlen (tmp);\
426 p = end;
428 if (p > tmp && p[-1] == ':')
429 p--;
430 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
431 p--;
432 if (tmp < p && p < end && p[-1] == ':')
434 char *fname;
435 struct stat st;
437 fname = g_strndup (tmp, p - 1 - tmp);
439 * Check that the file before the colon actually exists.
440 * If it doesn't exist, revert to the old behavior.
442 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1)
444 mc_run_param0 = fname;
445 mc_args__edit_start_line = atoi (p);
447 else
449 g_free (fname);
450 goto try_plus_filename;
453 else
455 try_plus_filename:
456 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
458 int start_line;;
460 start_line = atoi (tmp);
461 if (start_line > 0)
463 char *file;
465 file = (argc > 1) ? argv[2] : NULL;
466 if (file != NULL)
468 tmp = file;
469 mc_args__edit_start_line = start_line;
473 mc_run_param0 = g_strdup (tmp);
476 mc_run_mode = MC_RUN_EDITOR;
478 else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
480 /* mcv* or view is link to mc */
482 if (tmp != NULL)
483 mc_run_param0 = g_strdup (tmp);
484 else
486 fprintf (stderr, "%s\n", _("No arguments given to the viewer."));
487 exit (EXIT_FAILURE);
489 mc_run_mode = MC_RUN_VIEWER;
491 #ifdef USE_DIFF_VIEW
492 else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
494 /* mcd* or diff is link to mc */
496 if (argc < 3)
498 fprintf (stderr, "%s\n", _("There 2 files are required to diffviewer."));
499 exit (EXIT_FAILURE);
502 if (tmp != NULL)
504 mc_run_param0 = g_strdup (tmp);
505 tmp = (argc > 1) ? argv[2] : NULL;
506 if (tmp != NULL)
507 mc_run_param1 = g_strdup (tmp);
508 mc_run_mode = MC_RUN_DIFFVIEWER;
511 #endif /* USE_DIFF_VIEW */
512 else
514 /* MC is run as mc */
516 switch (mc_run_mode)
518 case MC_RUN_EDITOR:
519 case MC_RUN_VIEWER:
520 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
521 break;
523 case MC_RUN_DIFFVIEWER:
524 /* not implemented yet */
525 break;
527 case MC_RUN_FULL:
528 default:
529 /* sets the current dir and the other dir */
530 if (tmp != NULL)
532 mc_run_param0 = g_strdup (tmp);
533 tmp = (argc > 1) ? argv[2] : NULL;
534 if (tmp != NULL)
535 mc_run_param1 = g_strdup (tmp);
537 mc_run_mode = MC_RUN_FULL;
538 break;
543 /* --------------------------------------------------------------------------------------------- */
545 static gboolean
546 mc_args_process (int argc, char *argv[])
548 if (mc_args__show_version)
550 show_version ();
551 return FALSE;
553 if (mc_args__show_datadirs)
555 printf ("%s (%s)\n", mc_home, mc_home_alt);
556 return FALSE;
559 if (mc_args__force_colors)
560 mc_args__disable_colors = FALSE;
562 #ifdef HAVE_SUBSHELL_SUPPORT
563 if (mc_args__nouse_subshell)
564 use_subshell = 0;
566 if (mc_args__nouse_subshell)
567 use_subshell = 0;
568 #endif /* HAVE_SUBSHELL_SUPPORT */
570 mc_setup_by_args (argc, argv);
572 return TRUE;
575 /* --------------------------------------------------------------------------------------------- */
577 static gchar *
578 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
579 const gchar * help_str)
581 GString *buffer = g_string_new ("");
582 GIConv conv = g_iconv_open (charset, "UTF-8");
583 gchar *full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
585 str_convert (conv, full_help_str, buffer);
587 g_free (full_help_str);
588 g_iconv_close (conv);
590 return g_string_free (buffer, FALSE);
593 /* --------------------------------------------------------------------------------------------- */
595 static gboolean
596 parse_mc_e_argument (const gchar *option_name, const gchar *value,
597 gpointer data, GError **error)
599 (void) option_name;
600 (void) data;
601 (void) error;
603 mc_run_mode = MC_RUN_EDITOR;
604 mc_run_param0 = g_strdup (value);
606 return TRUE;
609 /* --------------------------------------------------------------------------------------------- */
611 static gboolean
612 parse_mc_v_argument (const gchar *option_name, const gchar *value,
613 gpointer data, GError **error)
615 (void) option_name;
616 (void) data;
617 (void) error;
619 mc_run_mode = MC_RUN_VIEWER;
620 mc_run_param0 = g_strdup (value);
622 return TRUE;
625 /* --------------------------------------------------------------------------------------------- */
627 /*** public functions ****************************************************************************/
629 /* --------------------------------------------------------------------------------------------- */
631 gboolean
632 mc_args_handle (int argc, char **argv, const char *translation_domain)
634 GError *error = NULL;
635 const gchar *_system_codepage = str_detect_termencoding ();
637 #ifdef ENABLE_NLS
638 if (!str_isutf8 (_system_codepage))
639 bind_textdomain_codeset ("mc", "UTF-8");
640 #endif
642 context = g_option_context_new (mc_args_add_usage_info ());
644 g_option_context_set_ignore_unknown_options (context, FALSE);
646 mc_args_add_extended_info_to_help ();
648 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
650 g_option_group_add_entries (main_group, argument_main_table);
651 g_option_context_set_main_group (context, main_group);
652 g_option_group_set_translation_domain (main_group, translation_domain);
654 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
655 _("Terminal options"), NULL, NULL);
657 g_option_group_add_entries (terminal_group, argument_terminal_table);
658 g_option_context_add_group (context, terminal_group);
659 g_option_group_set_translation_domain (terminal_group, translation_domain);
661 color_group = mc_args_new_color_group ();
663 g_option_group_add_entries (color_group, argument_color_table);
664 g_option_context_add_group (context, color_group);
665 g_option_group_set_translation_domain (color_group, translation_domain);
667 if (!g_option_context_parse (context, &argc, &argv, &error))
669 if (error != NULL)
671 gchar *full_help_str;
672 gchar *help_str;
674 #if GLIB_CHECK_VERSION(2,14,0)
675 help_str = g_option_context_get_help (context, TRUE, NULL);
676 #else
677 help_str = g_strdup ("");
678 #endif
679 if (!str_isutf8 (_system_codepage))
680 full_help_str =
681 mc_args__convert_help_to_syscharset (_system_codepage, error->message,
682 help_str);
683 else
684 full_help_str = g_strdup_printf ("%s\n\n%s\n", error->message, help_str);
686 fprintf (stderr, "%s", full_help_str);
688 g_free (help_str);
689 g_free (full_help_str);
690 g_error_free (error);
692 g_option_context_free (context);
693 mc_args_clean_temp_help_strings ();
694 return FALSE;
697 g_option_context_free (context);
698 mc_args_clean_temp_help_strings ();
700 #ifdef ENABLE_NLS
701 if (!str_isutf8 (_system_codepage))
702 bind_textdomain_codeset ("mc", _system_codepage);
703 #endif
705 return mc_args_process (argc, argv);
708 /* --------------------------------------------------------------------------------------------- */