Merge branch '2605_fish_get_fix'
[midnight-commander.git] / src / args.c
blob15bd5063b6de2eecb186da6bc009ce0f80e09bd9
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/strutil.h"
34 #include "lib/vfs/vfs.h"
35 #include "lib/util.h" /* x_basename() */
37 #ifdef ENABLE_VFS_SMB
38 #include "src/vfs/smbfs/smbfs.h" /* smbfs_set_debugf() */
39 #endif
41 #include "src/main.h"
42 #include "src/textconf.h"
44 #include "src/args.h"
46 /*** external variables **************************************************************************/
48 /*** global variables ****************************************************************************/
50 /* If true, show version info and exit */
51 gboolean mc_args__show_version = FALSE;
53 /* If true, assume we are running on an xterm terminal */
54 gboolean mc_args__force_xterm = FALSE;
56 gboolean mc_args__nomouse = FALSE;
58 /* Force colors, only used by Slang */
59 gboolean mc_args__force_colors = FALSE;
61 /* Don't load keymap form file and use default one */
62 gboolean mc_args__nokeymap = FALSE;
64 /* Line to start the editor on */
65 int mc_args__edit_start_line = 0;
67 char *mc_args__last_wd_file = NULL;
69 /* when enabled NETCODE, use folowing file as logfile */
70 char *mc_args__netfs_logfile = NULL;
72 /* keymap file */
73 char *mc_args__keymap_file = NULL;
75 /* Debug level */
76 int mc_args__debug_level = 0;
78 /*** file scope macro definitions ****************************************************************/
80 /*** file scope type declarations ****************************************************************/
82 /*** file scope variables ************************************************************************/
84 /* forward declarations */
85 static gboolean parse_mc_e_argument (const gchar * option_name, const gchar * value,
86 gpointer data, GError ** error);
87 static gboolean parse_mc_v_argument (const gchar * option_name, const gchar * value,
88 gpointer data, GError ** error);
90 static GOptionContext *context;
92 static gboolean mc_args__nouse_subshell = FALSE;
93 static gboolean mc_args__show_datadirs = FALSE;
94 static gboolean mc_args__show_datadirs_extended = FALSE;
95 static gboolean mc_args__show_configure_opts = FALSE;
97 static GOptionGroup *main_group;
99 static const GOptionEntry argument_main_table[] = {
100 /* *INDENT-OFF* */
101 /* generic options */
103 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
104 &mc_args__show_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
117 /* show extended information about used data directories */
119 "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
120 &mc_args__show_datadirs_extended,
121 N_("Print extended info about used data directories"),
122 NULL
125 /* show configure options */
127 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
128 &mc_args__show_configure_opts,
129 N_("Print configure options"),
130 NULL
134 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
135 &mc_args__last_wd_file,
136 N_("Print last working directory to specified file"),
137 "<file>"
140 #ifdef HAVE_SUBSHELL_SUPPORT
142 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
143 &mc_global.tty.use_subshell,
144 N_("Enables subshell support (default)"),
145 NULL
149 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
150 &mc_args__nouse_subshell,
151 N_("Disables subshell support"),
152 NULL
154 #endif
156 /* debug options */
157 #ifdef ENABLE_VFS_FTP
159 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
160 &mc_args__netfs_logfile,
161 N_("Log ftp dialog to specified file"),
162 "<file>"
164 #endif /* ENABLE_VFS_FTP */
165 #ifdef ENABLE_VFS_SMB
167 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
168 &mc_args__debug_level,
169 N_("Set debug level"),
170 "<integer>"
172 #endif /* ENABLE_VFS_SMB */
174 /* single file operations */
176 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
177 parse_mc_v_argument,
178 N_("Launches the file viewer on a file"),
179 "<file>"
183 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
184 parse_mc_e_argument,
185 N_("Edits one file"),
186 "<file>"},
189 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
191 /* *INDENT-ON* */
194 GOptionGroup *terminal_group;
195 #define ARGS_TERM_OPTIONS 0
196 static const GOptionEntry argument_terminal_table[] = {
197 /* *INDENT-OFF* */
198 /* terminal options */
200 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
201 &mc_args__force_xterm,
202 N_("Forces xterm features"),
203 NULL
207 "oldmouse", 'g', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
208 &mc_global.tty.old_mouse,
209 N_("Tries to use an old highlight mouse tracking"),
210 NULL
214 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
215 &mc_args__nomouse,
216 N_("Disable mouse support in text version"),
217 NULL
220 #ifdef HAVE_SLANG
222 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
223 &SLtt_Try_Termcap,
224 N_("Tries to use termcap instead of terminfo"),
225 NULL
227 #endif
230 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
231 &mc_global.tty.slow_terminal,
232 N_("To run on slow terminals"),
233 NULL
237 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
238 &mc_global.tty.ugly_line_drawing,
239 N_("Use stickchars to draw"),
240 NULL
244 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
245 &reset_hp_softkeys,
246 N_("Resets soft keys on HP terminals"),
247 NULL
251 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
252 &mc_args__keymap_file,
253 N_("Load definitions of key bindings from specified file"),
254 "<file>"
258 "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
259 &mc_args__nokeymap,
260 N_("Don't load definitions of key bindings from file, use defaults"),
261 NULL
265 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
267 /* *INDENT-ON* */
270 #undef ARGS_TERM_OPTIONS
272 GOptionGroup *color_group;
273 #define ARGS_COLOR_OPTIONS 0
274 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
275 static const GOptionEntry argument_color_table[] = {
276 /* *INDENT-OFF* */
277 /* color options */
279 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
280 &mc_global.tty.disable_colors,
281 N_("Requests to run in black and white"),
282 NULL
286 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
287 &mc_args__force_colors,
288 N_("Request to run in color mode"),
289 NULL
293 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
294 &mc_global.tty.command_line_colors,
295 N_("Specifies a color configuration"),
296 "<string>"
300 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
301 &mc_global.skin,
302 N_("Show mc with specified skin"),
303 "<string>"
307 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
309 /* *INDENT-ON* */
312 #undef ARGS_COLOR_OPTIONS
314 static gchar *mc_args__loc__colors_string = NULL;
315 static gchar *mc_args__loc__footer_string = NULL;
316 static gchar *mc_args__loc__header_string = NULL;
317 static gchar *mc_args__loc__usage_string = NULL;
319 /*** file scope functions ************************************************************************/
321 /* --------------------------------------------------------------------------------------------- */
322 static void
323 mc_args_clean_temp_help_strings (void)
325 g_free (mc_args__loc__colors_string);
326 mc_args__loc__colors_string = NULL;
328 g_free (mc_args__loc__footer_string);
329 mc_args__loc__footer_string = NULL;
331 g_free (mc_args__loc__header_string);
332 mc_args__loc__header_string = NULL;
334 g_free (mc_args__loc__usage_string);
335 mc_args__loc__usage_string = NULL;
338 /* --------------------------------------------------------------------------------------------- */
340 static GOptionGroup *
341 mc_args_new_color_group (void)
343 /* *INDENT-OFF* */
344 /* FIXME: to preserve translations, lines should be split. */
345 mc_args__loc__colors_string = g_strdup_printf ("%s\n%s",
346 /* TRANSLATORS: don't translate keywords */
347 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
348 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
349 "\n Keywords:\n"
350 " Global: errors, disabled, reverse, gauge, header\n"
351 " input, inputmark, inputunchanged, commandlinemark\n"
352 " bbarhotkey, bbarbutton, statusbar\n"
353 " File display: normal, selected, marked, markselect\n"
354 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
355 " errdhotfocus\n"
356 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
357 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
358 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
359 " editlinestate\n"
360 " Viewer: viewbold, viewunderline, viewselected\n"
361 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
362 /* TRANSLATORS: don't translate color names and attributes */
363 _("Standard Colors:\n"
364 " black, gray, red, brightred, green, brightgreen, brown,\n"
365 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
366 " brightcyan, lightgray and white\n\n"
367 "Extended colors, when 256 colors are available:\n"
368 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
369 "Attributes:\n"
370 " bold, underline, reverse, blink; append more with '+'\n")
372 /* *INDENT-ON* */
374 return g_option_group_new ("color", mc_args__loc__colors_string,
375 _("Color options"), NULL, NULL);
379 /* --------------------------------------------------------------------------------------------- */
381 static gchar *
382 mc_args_add_usage_info (void)
384 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
385 _("+number"),
386 _("[this_dir] [other_panel_dir]"),
387 _("+number"),
389 ("Set initial line number for the internal editor"));
390 return mc_args__loc__usage_string;
393 /* --------------------------------------------------------------------------------------------- */
395 static void
396 mc_args_add_extended_info_to_help (void)
398 mc_args__loc__footer_string = g_strdup_printf ("%s",
400 ("\n"
401 "Please send any bug reports (including the output of `mc -V')\n"
402 "as tickets at www.midnight-commander.org\n"));
403 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
405 #if GLIB_CHECK_VERSION(2,12,0)
406 g_option_context_set_description (context, mc_args__loc__footer_string);
407 g_option_context_set_summary (context, mc_args__loc__header_string);
408 #endif
411 /* --------------------------------------------------------------------------------------------- */
413 static void
414 mc_setup_by_args (int argc, char *argv[])
416 const char *base;
417 char *tmp;
419 #ifdef ENABLE_VFS_SMB
420 if (mc_args__debug_level != 0)
421 smbfs_set_debug (mc_args__debug_level);
422 #endif /* ENABLE_VFS_SMB */
424 if (mc_args__netfs_logfile != NULL)
426 #ifdef ENABLE_VFS_FTP
427 mc_setctl ("ftp://", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
428 #endif /* ENABLE_VFS_FTP */
429 #ifdef ENABLE_VFS_SMB
430 mc_setctl ("smb://", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
431 #endif /* ENABLE_VFS_SMB */
434 base = x_basename (argv[0]);
435 tmp = (argc > 0) ? argv[1] : NULL;
437 if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
439 /* mce* or vi is link to mc */
441 mc_run_param0 = g_strdup ("");
442 if (tmp != NULL)
445 * Check for filename:lineno, followed by an optional colon.
446 * This format is used by many programs (especially compilers)
447 * in error messages and warnings. It is supported so that
448 * users can quickly copy and paste file locations.
450 char *end, *p;
452 end = tmp + strlen (tmp);
453 p = end;
455 if (p > tmp && p[-1] == ':')
456 p--;
457 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
458 p--;
459 if (tmp < p && p < end && p[-1] == ':')
461 char *fname;
462 struct stat st;
464 fname = g_strndup (tmp, p - 1 - tmp);
466 * Check that the file before the colon actually exists.
467 * If it doesn't exist, revert to the old behavior.
469 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1)
471 mc_run_param0 = fname;
472 mc_args__edit_start_line = atoi (p);
474 else
476 g_free (fname);
477 goto try_plus_filename;
480 else
482 try_plus_filename:
483 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
485 int start_line;
487 start_line = atoi (tmp);
490 * If start_line is zero, position the cursor at the
491 * beginning of the file as other editors (vi, nano)
493 if (start_line == 0)
494 start_line++;
496 if (start_line > 0)
498 char *file;
500 file = (argc > 1) ? argv[2] : NULL;
501 if (file != NULL)
503 tmp = file;
504 mc_args__edit_start_line = start_line;
508 mc_run_param0 = g_strdup (tmp);
511 mc_global.mc_run_mode = MC_RUN_EDITOR;
513 else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
515 /* mcv* or view is link to mc */
517 if (tmp != NULL)
518 mc_run_param0 = g_strdup (tmp);
519 else
521 fprintf (stderr, "%s\n", _("No arguments given to the viewer."));
522 exit (EXIT_FAILURE);
524 mc_global.mc_run_mode = MC_RUN_VIEWER;
526 #ifdef USE_DIFF_VIEW
527 else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
529 /* mcd* or diff is link to mc */
531 if (argc < 3)
533 fprintf (stderr, "%s\n", _("Two files are required to evoke the diffviewer."));
534 exit (EXIT_FAILURE);
537 if (tmp != NULL)
539 mc_run_param0 = g_strdup (tmp);
540 tmp = (argc > 1) ? argv[2] : NULL;
541 if (tmp != NULL)
542 mc_run_param1 = g_strdup (tmp);
543 mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
546 #endif /* USE_DIFF_VIEW */
547 else
549 /* MC is run as mc */
551 switch (mc_global.mc_run_mode)
553 case MC_RUN_EDITOR:
554 case MC_RUN_VIEWER:
555 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
556 break;
558 case MC_RUN_DIFFVIEWER:
559 /* not implemented yet */
560 break;
562 case MC_RUN_FULL:
563 default:
564 /* sets the current dir and the other dir */
565 if (tmp != NULL)
567 mc_run_param0 = g_strdup (tmp);
568 tmp = (argc > 1) ? argv[2] : NULL;
569 if (tmp != NULL)
570 mc_run_param1 = g_strdup (tmp);
572 mc_global.mc_run_mode = MC_RUN_FULL;
573 break;
578 /* --------------------------------------------------------------------------------------------- */
580 static gboolean
581 mc_args_process (int argc, char *argv[])
583 if (mc_args__show_version)
585 show_version ();
586 return FALSE;
588 if (mc_args__show_datadirs)
590 printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
591 return FALSE;
594 if (mc_args__show_datadirs_extended)
596 show_datadirs_extended ();
597 return FALSE;
600 if (mc_args__show_configure_opts)
602 show_configure_options ();
603 return FALSE;
606 if (mc_args__force_colors)
607 mc_global.tty.disable_colors = FALSE;
609 #ifdef HAVE_SUBSHELL_SUPPORT
610 if (mc_args__nouse_subshell)
611 mc_global.tty.use_subshell = FALSE;
612 #endif /* HAVE_SUBSHELL_SUPPORT */
614 mc_setup_by_args (argc, argv);
616 return TRUE;
619 /* --------------------------------------------------------------------------------------------- */
621 static gchar *
622 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
623 const gchar * help_str)
625 GString *buffer = g_string_new ("");
626 GIConv conv = g_iconv_open (charset, "UTF-8");
627 gchar *full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
629 str_convert (conv, full_help_str, buffer);
631 g_free (full_help_str);
632 g_iconv_close (conv);
634 return g_string_free (buffer, FALSE);
637 /* --------------------------------------------------------------------------------------------- */
639 static gboolean
640 parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
642 (void) option_name;
643 (void) data;
644 (void) error;
646 mc_global.mc_run_mode = MC_RUN_EDITOR;
647 mc_run_param0 = g_strdup (value);
649 return TRUE;
652 /* --------------------------------------------------------------------------------------------- */
654 static gboolean
655 parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
657 (void) option_name;
658 (void) data;
659 (void) error;
661 mc_global.mc_run_mode = MC_RUN_VIEWER;
662 mc_run_param0 = g_strdup (value);
664 return TRUE;
667 /* --------------------------------------------------------------------------------------------- */
669 /*** public functions ****************************************************************************/
671 /* --------------------------------------------------------------------------------------------- */
673 gboolean
674 mc_args_handle (int argc, char **argv, const char *translation_domain)
676 GError *error = NULL;
677 const gchar *_system_codepage = str_detect_termencoding ();
679 #ifdef ENABLE_NLS
680 if (!str_isutf8 (_system_codepage))
681 bind_textdomain_codeset ("mc", "UTF-8");
682 #endif
684 context = g_option_context_new (mc_args_add_usage_info ());
686 g_option_context_set_ignore_unknown_options (context, FALSE);
688 mc_args_add_extended_info_to_help ();
690 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
692 g_option_group_add_entries (main_group, argument_main_table);
693 g_option_context_set_main_group (context, main_group);
694 g_option_group_set_translation_domain (main_group, translation_domain);
696 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
697 _("Terminal options"), NULL, NULL);
699 g_option_group_add_entries (terminal_group, argument_terminal_table);
700 g_option_context_add_group (context, terminal_group);
701 g_option_group_set_translation_domain (terminal_group, translation_domain);
703 color_group = mc_args_new_color_group ();
705 g_option_group_add_entries (color_group, argument_color_table);
706 g_option_context_add_group (context, color_group);
707 g_option_group_set_translation_domain (color_group, translation_domain);
709 if (!g_option_context_parse (context, &argc, &argv, &error))
711 if (error != NULL)
713 gchar *full_help_str;
714 gchar *help_str;
716 #if GLIB_CHECK_VERSION(2,14,0)
717 help_str = g_option_context_get_help (context, TRUE, NULL);
718 #else
719 help_str = g_strdup ("");
720 #endif
721 if (!str_isutf8 (_system_codepage))
722 full_help_str =
723 mc_args__convert_help_to_syscharset (_system_codepage, error->message,
724 help_str);
725 else
726 full_help_str = g_strdup_printf ("%s\n\n%s\n", error->message, help_str);
728 fprintf (stderr, "%s", full_help_str);
730 g_free (help_str);
731 g_free (full_help_str);
732 g_error_free (error);
734 g_option_context_free (context);
735 mc_args_clean_temp_help_strings ();
736 return FALSE;
739 g_option_context_free (context);
740 mc_args_clean_temp_help_strings ();
742 #ifdef ENABLE_NLS
743 if (!str_isutf8 (_system_codepage))
744 bind_textdomain_codeset ("mc", _system_codepage);
745 #endif
747 return mc_args_process (argc, argv);
750 /* --------------------------------------------------------------------------------------------- */