mc.keymap.default, mc.keymap.emacs: comment out unassigned commands.
[midnight-commander.git] / src / args.c
blob99b2584eb71feb175d6b657a8400cac94098e3ec
1 /*
2 Handle command line arguments.
4 Copyright (C) 2009, 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
27 #include <stdlib.h>
28 #include <stdio.h>
30 #include "lib/global.h"
31 #include "lib/tty/tty.h"
32 #include "lib/strutil.h"
33 #include "lib/vfs/vfs.h"
34 #include "lib/util.h" /* x_basename() */
36 #ifdef ENABLE_VFS_SMB
37 #include "src/vfs/smbfs/smbfs.h" /* smbfs_set_debugf() */
38 #endif
40 #include "src/main.h"
41 #include "src/textconf.h"
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 /* Force colors, only used by Slang */
58 gboolean mc_args__force_colors = FALSE;
60 /* Don't load keymap form file and use default one */
61 gboolean mc_args__nokeymap = FALSE;
63 /* Line to start the editor on */
64 int mc_args__edit_start_line = 0;
66 char *mc_args__last_wd_file = NULL;
68 /* when enabled NETCODE, use folowing file as logfile */
69 char *mc_args__netfs_logfile = NULL;
71 /* keymap file */
72 char *mc_args__keymap_file = NULL;
74 /* Debug level */
75 int mc_args__debug_level = 0;
77 /*** file scope macro definitions ****************************************************************/
79 /*** file scope type declarations ****************************************************************/
81 /*** file scope variables ************************************************************************/
83 /* forward declarations */
84 static gboolean parse_mc_e_argument (const gchar * option_name, const gchar * value,
85 gpointer data, GError ** error);
86 static gboolean parse_mc_v_argument (const gchar * option_name, const gchar * value,
87 gpointer data, GError ** error);
89 static GOptionContext *context;
91 #ifdef HAVE_SUBSHELL_SUPPORT
92 static gboolean mc_args__nouse_subshell = FALSE;
93 #endif /* HAVE_SUBSHELL_SUPPORT */
94 static gboolean mc_args__show_datadirs = FALSE;
95 static gboolean mc_args__show_datadirs_extended = FALSE;
96 static gboolean mc_args__show_configure_opts = FALSE;
98 static GOptionGroup *main_group;
100 static const GOptionEntry argument_main_table[] = {
101 /* *INDENT-OFF* */
102 /* generic options */
104 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
105 &mc_args__show_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
118 /* show extended information about used data directories */
120 "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
121 &mc_args__show_datadirs_extended,
122 N_("Print extended info about used data directories"),
123 NULL
126 /* show configure options */
128 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
129 &mc_args__show_configure_opts,
130 N_("Print configure options"),
131 NULL
135 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
136 &mc_args__last_wd_file,
137 N_("Print last working directory to specified file"),
138 "<file>"
141 #ifdef HAVE_SUBSHELL_SUPPORT
143 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
144 &mc_global.tty.use_subshell,
145 N_("Enables subshell support (default)"),
146 NULL
150 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
151 &mc_args__nouse_subshell,
152 N_("Disables subshell support"),
153 NULL
155 #endif
157 /* debug options */
158 #ifdef ENABLE_VFS_FTP
160 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
161 &mc_args__netfs_logfile,
162 N_("Log ftp dialog to specified file"),
163 "<file>"
165 #endif /* ENABLE_VFS_FTP */
166 #ifdef ENABLE_VFS_SMB
168 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
169 &mc_args__debug_level,
170 N_("Set debug level"),
171 "<integer>"
173 #endif /* ENABLE_VFS_SMB */
175 /* single file operations */
177 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
178 parse_mc_v_argument,
179 N_("Launches the file viewer on a file"),
180 "<file>"
184 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
185 parse_mc_e_argument,
186 N_("Edits one file"),
187 "<file>"},
190 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
192 /* *INDENT-ON* */
195 GOptionGroup *terminal_group;
196 #define ARGS_TERM_OPTIONS 0
197 static const GOptionEntry argument_terminal_table[] = {
198 /* *INDENT-OFF* */
199 /* terminal options */
201 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
202 &mc_args__force_xterm,
203 N_("Forces xterm features"),
204 NULL
208 "no-x11", 'X', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
209 &mc_global.tty.disable_x11,
210 N_("Disable X11 support"),
211 NULL
215 "oldmouse", 'g', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
216 &mc_global.tty.old_mouse,
217 N_("Tries to use an old highlight mouse tracking"),
218 NULL
222 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
223 &mc_args__nomouse,
224 N_("Disable mouse support in text version"),
225 NULL
228 #ifdef HAVE_SLANG
230 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
231 &SLtt_Try_Termcap,
232 N_("Tries to use termcap instead of terminfo"),
233 NULL
235 #endif
238 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
239 &mc_global.tty.slow_terminal,
240 N_("To run on slow terminals"),
241 NULL
245 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
246 &mc_global.tty.ugly_line_drawing,
247 N_("Use stickchars to draw"),
248 NULL
252 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
253 &reset_hp_softkeys,
254 N_("Resets soft keys on HP terminals"),
255 NULL
259 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
260 &mc_args__keymap_file,
261 N_("Load definitions of key bindings from specified file"),
262 "<file>"
266 "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
267 &mc_args__nokeymap,
268 N_("Don't load definitions of key bindings from file, use defaults"),
269 NULL
273 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
275 /* *INDENT-ON* */
278 #undef ARGS_TERM_OPTIONS
280 GOptionGroup *color_group;
281 #define ARGS_COLOR_OPTIONS 0
282 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
283 static const GOptionEntry argument_color_table[] = {
284 /* *INDENT-OFF* */
285 /* color options */
287 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
288 &mc_global.tty.disable_colors,
289 N_("Requests to run in black and white"),
290 NULL
294 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
295 &mc_args__force_colors,
296 N_("Request to run in color mode"),
297 NULL
301 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
302 &mc_global.tty.command_line_colors,
303 N_("Specifies a color configuration"),
304 "<string>"
308 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
309 &mc_global.tty.skin,
310 N_("Show mc with specified skin"),
311 "<string>"
315 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
317 /* *INDENT-ON* */
320 #undef ARGS_COLOR_OPTIONS
322 static gchar *mc_args__loc__colors_string = NULL;
323 static gchar *mc_args__loc__footer_string = NULL;
324 static gchar *mc_args__loc__header_string = NULL;
325 static gchar *mc_args__loc__usage_string = NULL;
327 /*** file scope functions ************************************************************************/
329 /* --------------------------------------------------------------------------------------------- */
330 static void
331 mc_args_clean_temp_help_strings (void)
333 g_free (mc_args__loc__colors_string);
334 mc_args__loc__colors_string = NULL;
336 g_free (mc_args__loc__footer_string);
337 mc_args__loc__footer_string = NULL;
339 g_free (mc_args__loc__header_string);
340 mc_args__loc__header_string = NULL;
342 g_free (mc_args__loc__usage_string);
343 mc_args__loc__usage_string = NULL;
346 /* --------------------------------------------------------------------------------------------- */
348 static GOptionGroup *
349 mc_args_new_color_group (void)
351 /* *INDENT-OFF* */
352 /* FIXME: to preserve translations, lines should be split. */
353 mc_args__loc__colors_string = g_strdup_printf ("%s\n%s",
354 /* TRANSLATORS: don't translate keywords */
355 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
356 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
357 "\n Keywords:\n"
358 " Global: errors, disabled, reverse, gauge, header\n"
359 " input, inputmark, inputunchanged, commandlinemark\n"
360 " bbarhotkey, bbarbutton, statusbar\n"
361 " File display: normal, selected, marked, markselect\n"
362 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
363 " errdhotfocus\n"
364 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
365 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
366 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
367 " editlinestate\n"
368 " Viewer: viewbold, viewunderline, viewselected\n"
369 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
370 /* TRANSLATORS: don't translate color names and attributes */
371 _("Standard Colors:\n"
372 " black, gray, red, brightred, green, brightgreen, brown,\n"
373 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
374 " brightcyan, lightgray and white\n\n"
375 "Extended colors, when 256 colors are available:\n"
376 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
377 "Attributes:\n"
378 " bold, underline, reverse, blink; append more with '+'\n")
380 /* *INDENT-ON* */
382 return g_option_group_new ("color", mc_args__loc__colors_string,
383 _("Color options"), NULL, NULL);
387 /* --------------------------------------------------------------------------------------------- */
389 static gchar *
390 mc_args_add_usage_info (void)
392 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
393 _("+number"),
394 _("[this_dir] [other_panel_dir]"),
395 _("+number"),
397 ("Set initial line number for the internal editor"));
398 return mc_args__loc__usage_string;
401 /* --------------------------------------------------------------------------------------------- */
403 static void
404 mc_args_add_extended_info_to_help (void)
406 mc_args__loc__footer_string = g_strdup_printf ("%s",
408 ("\n"
409 "Please send any bug reports (including the output of `mc -V')\n"
410 "as tickets at www.midnight-commander.org\n"));
411 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
413 #if GLIB_CHECK_VERSION(2,12,0)
414 g_option_context_set_description (context, mc_args__loc__footer_string);
415 g_option_context_set_summary (context, mc_args__loc__header_string);
416 #endif
419 /* --------------------------------------------------------------------------------------------- */
421 static gchar *
422 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
423 const gchar * help_str)
425 GString *buffer;
426 GIConv conv;
427 gchar *full_help_str;
429 buffer = g_string_new ("");
430 conv = g_iconv_open (charset, "UTF-8");
431 full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
433 str_convert (conv, full_help_str, buffer);
435 g_free (full_help_str);
436 g_iconv_close (conv);
438 return g_string_free (buffer, FALSE);
441 /* --------------------------------------------------------------------------------------------- */
443 static gboolean
444 parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
446 (void) option_name;
447 (void) data;
448 (void) error;
450 mc_global.mc_run_mode = MC_RUN_EDITOR;
451 mc_run_param0 = g_strdup (value);
453 return TRUE;
456 /* --------------------------------------------------------------------------------------------- */
458 static gboolean
459 parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
461 (void) option_name;
462 (void) data;
463 (void) error;
465 mc_global.mc_run_mode = MC_RUN_VIEWER;
466 mc_run_param0 = g_strdup (value);
468 return TRUE;
471 /* --------------------------------------------------------------------------------------------- */
472 /*** public functions ****************************************************************************/
473 /* --------------------------------------------------------------------------------------------- */
475 gboolean
476 mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError ** error)
478 const gchar *_system_codepage;
479 gboolean ok = TRUE;
481 _system_codepage = str_detect_termencoding ();
483 #ifdef ENABLE_NLS
484 if (!str_isutf8 (_system_codepage))
485 bind_textdomain_codeset ("mc", "UTF-8");
486 #endif
488 context = g_option_context_new (mc_args_add_usage_info ());
490 g_option_context_set_ignore_unknown_options (context, FALSE);
492 mc_args_add_extended_info_to_help ();
494 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
496 g_option_group_add_entries (main_group, argument_main_table);
497 g_option_context_set_main_group (context, main_group);
498 g_option_group_set_translation_domain (main_group, translation_domain);
500 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
501 _("Terminal options"), NULL, NULL);
503 g_option_group_add_entries (terminal_group, argument_terminal_table);
504 g_option_context_add_group (context, terminal_group);
505 g_option_group_set_translation_domain (terminal_group, translation_domain);
507 color_group = mc_args_new_color_group ();
509 g_option_group_add_entries (color_group, argument_color_table);
510 g_option_context_add_group (context, color_group);
511 g_option_group_set_translation_domain (color_group, translation_domain);
513 if (!g_option_context_parse (context, argc, argv, error))
515 GError *error2 = NULL;
517 if (*error == NULL)
518 *error = g_error_new (MC_ERROR, 0, "%s\n", _("Arguments parse error!"));
519 else
521 gchar *help_str;
523 #if GLIB_CHECK_VERSION(2,14,0)
524 help_str = g_option_context_get_help (context, TRUE, NULL);
525 #else
526 help_str = g_strdup ("");
527 #endif
528 if (str_isutf8 (_system_codepage))
529 error2 = g_error_new ((*error)->domain, (*error)->code, "%s\n\n%s\n",
530 (*error)->message, help_str);
531 else
533 gchar *full_help_str;
535 full_help_str =
536 mc_args__convert_help_to_syscharset (_system_codepage, (*error)->message,
537 help_str);
538 error2 = g_error_new ((*error)->domain, (*error)->code, "%s", full_help_str);
539 g_free (full_help_str);
542 g_free (help_str);
543 g_error_free (*error);
544 *error = error2;
547 ok = FALSE;
550 g_option_context_free (context);
551 mc_args_clean_temp_help_strings ();
553 #ifdef ENABLE_NLS
554 if (!str_isutf8 (_system_codepage))
555 bind_textdomain_codeset ("mc", _system_codepage);
556 #endif
558 return ok;
561 /* --------------------------------------------------------------------------------------------- */
563 gboolean
564 mc_args_show_info (void)
566 if (mc_args__show_version)
568 show_version ();
569 return FALSE;
572 if (mc_args__show_datadirs)
574 printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
575 return FALSE;
578 if (mc_args__show_datadirs_extended)
580 show_datadirs_extended ();
581 return FALSE;
584 if (mc_args__show_configure_opts)
586 show_configure_options ();
587 return FALSE;
590 return TRUE;
593 /* --------------------------------------------------------------------------------------------- */
595 gboolean
596 mc_setup_by_args (int argc, char **argv, GError ** error)
598 const char *base;
599 char *tmp;
601 if (mc_args__force_colors)
602 mc_global.tty.disable_colors = FALSE;
604 #ifdef HAVE_SUBSHELL_SUPPORT
605 if (mc_args__nouse_subshell)
606 mc_global.tty.use_subshell = FALSE;
607 #endif /* HAVE_SUBSHELL_SUPPORT */
609 #ifdef ENABLE_VFS_SMB
610 if (mc_args__debug_level != 0)
611 smbfs_set_debug (mc_args__debug_level);
612 #endif /* ENABLE_VFS_SMB */
614 #if defined ENABLE_VFS_FTP || defined ENABLE_VFS_FTP
615 if (mc_args__netfs_logfile != NULL)
617 vfs_path_t *vpath;
619 #ifdef ENABLE_VFS_FTP
620 vpath = vfs_path_from_str ("ftp://");
621 mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
622 vfs_path_free (vpath);
623 #endif /* ENABLE_VFS_FTP */
624 #ifdef ENABLE_VFS_SMB
625 vpath = vfs_path_from_str ("smb://");
626 mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
627 vfs_path_free (vpath);
628 #endif /* ENABLE_VFS_SMB */
630 #endif /* ENABLE_VFS_FTP || ENABLE_VFS_SMB */
632 base = x_basename (argv[0]);
633 tmp = (argc > 0) ? argv[1] : NULL;
635 if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
637 /* mce* or vi is link to mc */
639 mc_run_param0 = g_strdup ("");
640 if (tmp != NULL)
643 * Check for filename:lineno, followed by an optional colon.
644 * This format is used by many programs (especially compilers)
645 * in error messages and warnings. It is supported so that
646 * users can quickly copy and paste file locations.
648 char *end, *p;
650 end = tmp + strlen (tmp);
651 p = end;
653 if (p > tmp && p[-1] == ':')
654 p--;
655 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
656 p--;
657 if (tmp < p && p < end && p[-1] == ':')
659 char *fname;
660 struct stat st;
661 vfs_path_t *tmp_vpath, *fname_vpath;
662 gboolean ok;
664 fname = g_strndup (tmp, p - 1 - tmp);
665 tmp_vpath = vfs_path_from_str (tmp);
666 fname_vpath = vfs_path_from_str (fname);
668 * Check that the file before the colon actually exists.
669 * If it doesn't exist, revert to the old behavior.
671 ok = mc_stat (tmp_vpath, &st) == -1 && mc_stat (fname_vpath, &st) != -1;
672 vfs_path_free (tmp_vpath);
673 vfs_path_free (fname_vpath);
675 if (ok)
677 mc_run_param0 = fname;
678 mc_args__edit_start_line = atoi (p);
680 else
682 g_free (fname);
683 goto try_plus_filename;
686 else
688 try_plus_filename:
689 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
691 int start_line;
693 start_line = atoi (tmp);
696 * If start_line is zero, position the cursor at the
697 * beginning of the file as other editors (vi, nano)
699 if (start_line == 0)
700 start_line++;
702 if (start_line > 0)
704 char *file;
706 file = (argc > 1) ? argv[2] : NULL;
707 if (file != NULL)
709 tmp = file;
710 mc_args__edit_start_line = start_line;
714 mc_run_param0 = g_strdup (tmp);
717 mc_global.mc_run_mode = MC_RUN_EDITOR;
719 else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
721 /* mcv* or view is link to mc */
723 if (tmp != NULL)
724 mc_run_param0 = g_strdup (tmp);
725 else
727 *error = g_error_new (MC_ERROR, 0, "%s\n", _("No arguments given to the viewer."));
728 return FALSE;
730 mc_global.mc_run_mode = MC_RUN_VIEWER;
732 #ifdef USE_DIFF_VIEW
733 else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
735 /* mcd* or diff is link to mc */
737 if (argc < 3)
739 *error = g_error_new (MC_ERROR, 0, "%s\n",
740 _("Two files are required to evoke the diffviewer."));
741 return FALSE;
744 if (tmp != NULL)
746 mc_run_param0 = g_strdup (tmp);
747 tmp = (argc > 1) ? argv[2] : NULL;
748 if (tmp != NULL)
749 mc_run_param1 = g_strdup (tmp);
750 mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
753 #endif /* USE_DIFF_VIEW */
754 else
756 /* MC is run as mc */
758 switch (mc_global.mc_run_mode)
760 case MC_RUN_EDITOR:
761 case MC_RUN_VIEWER:
762 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
763 break;
765 case MC_RUN_DIFFVIEWER:
766 /* not implemented yet */
767 break;
769 case MC_RUN_FULL:
770 default:
771 /* sets the current dir and the other dir */
772 if (tmp != NULL)
774 mc_run_param0 = g_strdup (tmp);
775 tmp = (argc > 1) ? argv[2] : NULL;
776 if (tmp != NULL)
777 mc_run_param1 = g_strdup (tmp);
779 mc_global.mc_run_mode = MC_RUN_FULL;
780 break;
784 return TRUE;
787 /* --------------------------------------------------------------------------------------------- */