Changes to handle vfs_path_t object:
[midnight-commander.git] / src / args.c
blob2e19e237bfe7627c7cba212bb5b670174a3010dd
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 static gboolean mc_args__nouse_subshell = FALSE;
92 static gboolean mc_args__show_datadirs = FALSE;
93 static gboolean mc_args__show_datadirs_extended = FALSE;
94 static gboolean mc_args__show_configure_opts = FALSE;
96 static GOptionGroup *main_group;
98 static const GOptionEntry argument_main_table[] = {
99 /* *INDENT-OFF* */
100 /* generic options */
102 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
103 &mc_args__show_version,
104 N_("Displays the current version"),
105 NULL
108 /* options for wrappers */
110 "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
111 &mc_args__show_datadirs,
112 N_("Print data directory"),
113 NULL
116 /* show extended information about used data directories */
118 "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
119 &mc_args__show_datadirs_extended,
120 N_("Print extended info about used data directories"),
121 NULL
124 /* show configure options */
126 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
127 &mc_args__show_configure_opts,
128 N_("Print configure options"),
129 NULL
133 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
134 &mc_args__last_wd_file,
135 N_("Print last working directory to specified file"),
136 "<file>"
139 #ifdef HAVE_SUBSHELL_SUPPORT
141 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
142 &mc_global.tty.use_subshell,
143 N_("Enables subshell support (default)"),
144 NULL
148 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
149 &mc_args__nouse_subshell,
150 N_("Disables subshell support"),
151 NULL
153 #endif
155 /* debug options */
156 #ifdef ENABLE_VFS_FTP
158 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
159 &mc_args__netfs_logfile,
160 N_("Log ftp dialog to specified file"),
161 "<file>"
163 #endif /* ENABLE_VFS_FTP */
164 #ifdef ENABLE_VFS_SMB
166 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
167 &mc_args__debug_level,
168 N_("Set debug level"),
169 "<integer>"
171 #endif /* ENABLE_VFS_SMB */
173 /* single file operations */
175 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
176 parse_mc_v_argument,
177 N_("Launches the file viewer on a file"),
178 "<file>"
182 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
183 parse_mc_e_argument,
184 N_("Edits one file"),
185 "<file>"},
188 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
190 /* *INDENT-ON* */
193 GOptionGroup *terminal_group;
194 #define ARGS_TERM_OPTIONS 0
195 static const GOptionEntry argument_terminal_table[] = {
196 /* *INDENT-OFF* */
197 /* terminal options */
199 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
200 &mc_args__force_xterm,
201 N_("Forces xterm features"),
202 NULL
206 "no-x11", 'X', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
207 &mc_global.tty.disable_x11,
208 N_("Disable X11 support"),
209 NULL
213 "oldmouse", 'g', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
214 &mc_global.tty.old_mouse,
215 N_("Tries to use an old highlight mouse tracking"),
216 NULL
220 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
221 &mc_args__nomouse,
222 N_("Disable mouse support in text version"),
223 NULL
226 #ifdef HAVE_SLANG
228 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
229 &SLtt_Try_Termcap,
230 N_("Tries to use termcap instead of terminfo"),
231 NULL
233 #endif
236 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
237 &mc_global.tty.slow_terminal,
238 N_("To run on slow terminals"),
239 NULL
243 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
244 &mc_global.tty.ugly_line_drawing,
245 N_("Use stickchars to draw"),
246 NULL
250 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
251 &reset_hp_softkeys,
252 N_("Resets soft keys on HP terminals"),
253 NULL
257 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
258 &mc_args__keymap_file,
259 N_("Load definitions of key bindings from specified file"),
260 "<file>"
264 "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
265 &mc_args__nokeymap,
266 N_("Don't load definitions of key bindings from file, use defaults"),
267 NULL
271 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
273 /* *INDENT-ON* */
276 #undef ARGS_TERM_OPTIONS
278 GOptionGroup *color_group;
279 #define ARGS_COLOR_OPTIONS 0
280 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
281 static const GOptionEntry argument_color_table[] = {
282 /* *INDENT-OFF* */
283 /* color options */
285 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
286 &mc_global.tty.disable_colors,
287 N_("Requests to run in black and white"),
288 NULL
292 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
293 &mc_args__force_colors,
294 N_("Request to run in color mode"),
295 NULL
299 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
300 &mc_global.tty.command_line_colors,
301 N_("Specifies a color configuration"),
302 "<string>"
306 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
307 &mc_global.skin,
308 N_("Show mc with specified skin"),
309 "<string>"
313 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
315 /* *INDENT-ON* */
318 #undef ARGS_COLOR_OPTIONS
320 static gchar *mc_args__loc__colors_string = NULL;
321 static gchar *mc_args__loc__footer_string = NULL;
322 static gchar *mc_args__loc__header_string = NULL;
323 static gchar *mc_args__loc__usage_string = NULL;
325 /*** file scope functions ************************************************************************/
327 /* --------------------------------------------------------------------------------------------- */
328 static void
329 mc_args_clean_temp_help_strings (void)
331 g_free (mc_args__loc__colors_string);
332 mc_args__loc__colors_string = NULL;
334 g_free (mc_args__loc__footer_string);
335 mc_args__loc__footer_string = NULL;
337 g_free (mc_args__loc__header_string);
338 mc_args__loc__header_string = NULL;
340 g_free (mc_args__loc__usage_string);
341 mc_args__loc__usage_string = NULL;
344 /* --------------------------------------------------------------------------------------------- */
346 static GOptionGroup *
347 mc_args_new_color_group (void)
349 /* *INDENT-OFF* */
350 /* FIXME: to preserve translations, lines should be split. */
351 mc_args__loc__colors_string = g_strdup_printf ("%s\n%s",
352 /* TRANSLATORS: don't translate keywords */
353 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
354 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
355 "\n Keywords:\n"
356 " Global: errors, disabled, reverse, gauge, header\n"
357 " input, inputmark, inputunchanged, commandlinemark\n"
358 " bbarhotkey, bbarbutton, statusbar\n"
359 " File display: normal, selected, marked, markselect\n"
360 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
361 " errdhotfocus\n"
362 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
363 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
364 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
365 " editlinestate\n"
366 " Viewer: viewbold, viewunderline, viewselected\n"
367 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
368 /* TRANSLATORS: don't translate color names and attributes */
369 _("Standard Colors:\n"
370 " black, gray, red, brightred, green, brightgreen, brown,\n"
371 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
372 " brightcyan, lightgray and white\n\n"
373 "Extended colors, when 256 colors are available:\n"
374 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
375 "Attributes:\n"
376 " bold, underline, reverse, blink; append more with '+'\n")
378 /* *INDENT-ON* */
380 return g_option_group_new ("color", mc_args__loc__colors_string,
381 _("Color options"), NULL, NULL);
385 /* --------------------------------------------------------------------------------------------- */
387 static gchar *
388 mc_args_add_usage_info (void)
390 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
391 _("+number"),
392 _("[this_dir] [other_panel_dir]"),
393 _("+number"),
395 ("Set initial line number for the internal editor"));
396 return mc_args__loc__usage_string;
399 /* --------------------------------------------------------------------------------------------- */
401 static void
402 mc_args_add_extended_info_to_help (void)
404 mc_args__loc__footer_string = g_strdup_printf ("%s",
406 ("\n"
407 "Please send any bug reports (including the output of `mc -V')\n"
408 "as tickets at www.midnight-commander.org\n"));
409 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
411 #if GLIB_CHECK_VERSION(2,12,0)
412 g_option_context_set_description (context, mc_args__loc__footer_string);
413 g_option_context_set_summary (context, mc_args__loc__header_string);
414 #endif
417 /* --------------------------------------------------------------------------------------------- */
419 static void
420 mc_setup_by_args (int argc, char *argv[])
422 const char *base;
423 char *tmp;
425 #ifdef ENABLE_VFS_SMB
426 if (mc_args__debug_level != 0)
427 smbfs_set_debug (mc_args__debug_level);
428 #endif /* ENABLE_VFS_SMB */
430 if (mc_args__netfs_logfile != NULL)
432 vfs_path_t *vpath;
433 #ifdef ENABLE_VFS_FTP
434 vpath = vfs_path_from_str ("ftp://");
435 mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
436 vfs_path_free (vpath);
437 #endif /* ENABLE_VFS_FTP */
438 #ifdef ENABLE_VFS_SMB
439 vpath = vfs_path_from_str ("smb://");
440 mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
441 vfs_path_free (vpath);
442 #endif /* ENABLE_VFS_SMB */
445 base = x_basename (argv[0]);
446 tmp = (argc > 0) ? argv[1] : NULL;
448 if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
450 /* mce* or vi is link to mc */
452 mc_run_param0 = g_strdup ("");
453 if (tmp != NULL)
456 * Check for filename:lineno, followed by an optional colon.
457 * This format is used by many programs (especially compilers)
458 * in error messages and warnings. It is supported so that
459 * users can quickly copy and paste file locations.
461 char *end, *p;
463 end = tmp + strlen (tmp);
464 p = end;
466 if (p > tmp && p[-1] == ':')
467 p--;
468 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
469 p--;
470 if (tmp < p && p < end && p[-1] == ':')
472 char *fname;
473 struct stat st;
474 vfs_path_t *tmp_vpath, *fname_vpath;
476 fname = g_strndup (tmp, p - 1 - tmp);
477 tmp_vpath = vfs_path_from_str (tmp);
478 fname_vpath = vfs_path_from_str (fname);
480 * Check that the file before the colon actually exists.
481 * If it doesn't exist, revert to the old behavior.
483 if (mc_stat (tmp_vpath, &st) == -1 && mc_stat (fname_vpath, &st) != -1)
485 mc_run_param0 = fname;
486 mc_args__edit_start_line = atoi (p);
488 else
490 g_free (fname);
491 goto try_plus_filename;
493 vfs_path_free (tmp_vpath);
494 vfs_path_free (fname_vpath);
496 else
498 try_plus_filename:
499 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
501 int start_line;
503 start_line = atoi (tmp);
506 * If start_line is zero, position the cursor at the
507 * beginning of the file as other editors (vi, nano)
509 if (start_line == 0)
510 start_line++;
512 if (start_line > 0)
514 char *file;
516 file = (argc > 1) ? argv[2] : NULL;
517 if (file != NULL)
519 tmp = file;
520 mc_args__edit_start_line = start_line;
524 mc_run_param0 = g_strdup (tmp);
527 mc_global.mc_run_mode = MC_RUN_EDITOR;
529 else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
531 /* mcv* or view is link to mc */
533 if (tmp != NULL)
534 mc_run_param0 = g_strdup (tmp);
535 else
537 fprintf (stderr, "%s\n", _("No arguments given to the viewer."));
538 exit (EXIT_FAILURE);
540 mc_global.mc_run_mode = MC_RUN_VIEWER;
542 #ifdef USE_DIFF_VIEW
543 else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
545 /* mcd* or diff is link to mc */
547 if (argc < 3)
549 fprintf (stderr, "%s\n", _("Two files are required to evoke the diffviewer."));
550 exit (EXIT_FAILURE);
553 if (tmp != NULL)
555 mc_run_param0 = g_strdup (tmp);
556 tmp = (argc > 1) ? argv[2] : NULL;
557 if (tmp != NULL)
558 mc_run_param1 = g_strdup (tmp);
559 mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
562 #endif /* USE_DIFF_VIEW */
563 else
565 /* MC is run as mc */
567 switch (mc_global.mc_run_mode)
569 case MC_RUN_EDITOR:
570 case MC_RUN_VIEWER:
571 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
572 break;
574 case MC_RUN_DIFFVIEWER:
575 /* not implemented yet */
576 break;
578 case MC_RUN_FULL:
579 default:
580 /* sets the current dir and the other dir */
581 if (tmp != NULL)
583 mc_run_param0 = g_strdup (tmp);
584 tmp = (argc > 1) ? argv[2] : NULL;
585 if (tmp != NULL)
586 mc_run_param1 = g_strdup (tmp);
588 mc_global.mc_run_mode = MC_RUN_FULL;
589 break;
594 /* --------------------------------------------------------------------------------------------- */
596 static gboolean
597 mc_args_process (int argc, char *argv[])
599 if (mc_args__show_version)
601 show_version ();
602 return FALSE;
604 if (mc_args__show_datadirs)
606 printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
607 return FALSE;
610 if (mc_args__show_datadirs_extended)
612 show_datadirs_extended ();
613 return FALSE;
616 if (mc_args__show_configure_opts)
618 show_configure_options ();
619 return FALSE;
622 if (mc_args__force_colors)
623 mc_global.tty.disable_colors = FALSE;
625 #ifdef HAVE_SUBSHELL_SUPPORT
626 if (mc_args__nouse_subshell)
627 mc_global.tty.use_subshell = FALSE;
628 #endif /* HAVE_SUBSHELL_SUPPORT */
630 mc_setup_by_args (argc, argv);
632 return TRUE;
635 /* --------------------------------------------------------------------------------------------- */
637 static gchar *
638 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
639 const gchar * help_str)
641 GString *buffer = g_string_new ("");
642 GIConv conv = g_iconv_open (charset, "UTF-8");
643 gchar *full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
645 str_convert (conv, full_help_str, buffer);
647 g_free (full_help_str);
648 g_iconv_close (conv);
650 return g_string_free (buffer, FALSE);
653 /* --------------------------------------------------------------------------------------------- */
655 static gboolean
656 parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
658 (void) option_name;
659 (void) data;
660 (void) error;
662 mc_global.mc_run_mode = MC_RUN_EDITOR;
663 mc_run_param0 = g_strdup (value);
665 return TRUE;
668 /* --------------------------------------------------------------------------------------------- */
670 static gboolean
671 parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
673 (void) option_name;
674 (void) data;
675 (void) error;
677 mc_global.mc_run_mode = MC_RUN_VIEWER;
678 mc_run_param0 = g_strdup (value);
680 return TRUE;
683 /* --------------------------------------------------------------------------------------------- */
685 /*** public functions ****************************************************************************/
687 /* --------------------------------------------------------------------------------------------- */
689 gboolean
690 mc_args_handle (int argc, char **argv, const char *translation_domain)
692 GError *error = NULL;
693 const gchar *_system_codepage = str_detect_termencoding ();
695 #ifdef ENABLE_NLS
696 if (!str_isutf8 (_system_codepage))
697 bind_textdomain_codeset ("mc", "UTF-8");
698 #endif
700 context = g_option_context_new (mc_args_add_usage_info ());
702 g_option_context_set_ignore_unknown_options (context, FALSE);
704 mc_args_add_extended_info_to_help ();
706 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
708 g_option_group_add_entries (main_group, argument_main_table);
709 g_option_context_set_main_group (context, main_group);
710 g_option_group_set_translation_domain (main_group, translation_domain);
712 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
713 _("Terminal options"), NULL, NULL);
715 g_option_group_add_entries (terminal_group, argument_terminal_table);
716 g_option_context_add_group (context, terminal_group);
717 g_option_group_set_translation_domain (terminal_group, translation_domain);
719 color_group = mc_args_new_color_group ();
721 g_option_group_add_entries (color_group, argument_color_table);
722 g_option_context_add_group (context, color_group);
723 g_option_group_set_translation_domain (color_group, translation_domain);
725 if (!g_option_context_parse (context, &argc, &argv, &error))
727 if (error != NULL)
729 gchar *full_help_str;
730 gchar *help_str;
732 #if GLIB_CHECK_VERSION(2,14,0)
733 help_str = g_option_context_get_help (context, TRUE, NULL);
734 #else
735 help_str = g_strdup ("");
736 #endif
737 if (!str_isutf8 (_system_codepage))
738 full_help_str =
739 mc_args__convert_help_to_syscharset (_system_codepage, error->message,
740 help_str);
741 else
742 full_help_str = g_strdup_printf ("%s\n\n%s\n", error->message, help_str);
744 fprintf (stderr, "%s", full_help_str);
746 g_free (help_str);
747 g_free (full_help_str);
748 g_error_free (error);
750 g_option_context_free (context);
751 mc_args_clean_temp_help_strings ();
752 return FALSE;
755 g_option_context_free (context);
756 mc_args_clean_temp_help_strings ();
758 #ifdef ENABLE_NLS
759 if (!str_isutf8 (_system_codepage))
760 bind_textdomain_codeset ("mc", _system_codepage);
761 #endif
763 return mc_args_process (argc, argv);
766 /* --------------------------------------------------------------------------------------------- */