Fixup update of command line after window resize.
[midnight-commander.git] / src / args.c
blob6e63eac09f3f8abb12835c7677d62ffa8a749261
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 "oldmouse", 'g', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
207 &mc_global.tty.old_mouse,
208 N_("Tries to use an old highlight mouse tracking"),
209 NULL
213 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
214 &mc_args__nomouse,
215 N_("Disable mouse support in text version"),
216 NULL
219 #ifdef HAVE_SLANG
221 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
222 &SLtt_Try_Termcap,
223 N_("Tries to use termcap instead of terminfo"),
224 NULL
226 #endif
229 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
230 &mc_global.tty.slow_terminal,
231 N_("To run on slow terminals"),
232 NULL
236 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
237 &mc_global.tty.ugly_line_drawing,
238 N_("Use stickchars to draw"),
239 NULL
243 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
244 &reset_hp_softkeys,
245 N_("Resets soft keys on HP terminals"),
246 NULL
250 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
251 &mc_args__keymap_file,
252 N_("Load definitions of key bindings from specified file"),
253 "<file>"
257 "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
258 &mc_args__nokeymap,
259 N_("Don't load definitions of key bindings from file, use defaults"),
260 NULL
264 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
266 /* *INDENT-ON* */
269 #undef ARGS_TERM_OPTIONS
271 GOptionGroup *color_group;
272 #define ARGS_COLOR_OPTIONS 0
273 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
274 static const GOptionEntry argument_color_table[] = {
275 /* *INDENT-OFF* */
276 /* color options */
278 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
279 &mc_global.tty.disable_colors,
280 N_("Requests to run in black and white"),
281 NULL
285 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
286 &mc_args__force_colors,
287 N_("Request to run in color mode"),
288 NULL
292 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
293 &mc_global.tty.command_line_colors,
294 N_("Specifies a color configuration"),
295 "<string>"
299 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
300 &mc_global.skin,
301 N_("Show mc with specified skin"),
302 "<string>"
306 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
308 /* *INDENT-ON* */
311 #undef ARGS_COLOR_OPTIONS
313 static gchar *mc_args__loc__colors_string = NULL;
314 static gchar *mc_args__loc__footer_string = NULL;
315 static gchar *mc_args__loc__header_string = NULL;
316 static gchar *mc_args__loc__usage_string = NULL;
318 /*** file scope functions ************************************************************************/
320 /* --------------------------------------------------------------------------------------------- */
321 static void
322 mc_args_clean_temp_help_strings (void)
324 g_free (mc_args__loc__colors_string);
325 mc_args__loc__colors_string = NULL;
327 g_free (mc_args__loc__footer_string);
328 mc_args__loc__footer_string = NULL;
330 g_free (mc_args__loc__header_string);
331 mc_args__loc__header_string = NULL;
333 g_free (mc_args__loc__usage_string);
334 mc_args__loc__usage_string = NULL;
337 /* --------------------------------------------------------------------------------------------- */
339 static GOptionGroup *
340 mc_args_new_color_group (void)
342 /* *INDENT-OFF* */
343 /* FIXME: to preserve translations, lines should be split. */
344 mc_args__loc__colors_string = g_strdup_printf ("%s\n%s",
345 /* TRANSLATORS: don't translate keywords */
346 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
347 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
348 "\n Keywords:\n"
349 " Global: errors, disabled, reverse, gauge, header\n"
350 " input, inputmark, inputunchanged, commandlinemark\n"
351 " bbarhotkey, bbarbutton, statusbar\n"
352 " File display: normal, selected, marked, markselect\n"
353 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
354 " errdhotfocus\n"
355 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
356 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
357 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
358 " editlinestate\n"
359 " Viewer: viewbold, viewunderline, viewselected\n"
360 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
361 /* TRANSLATORS: don't translate color names and attributes */
362 _("Standard Colors:\n"
363 " black, gray, red, brightred, green, brightgreen, brown,\n"
364 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
365 " brightcyan, lightgray and white\n\n"
366 "Extended colors, when 256 colors are available:\n"
367 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
368 "Attributes:\n"
369 " bold, underline, reverse, blink; append more with '+'\n")
371 /* *INDENT-ON* */
373 return g_option_group_new ("color", mc_args__loc__colors_string,
374 _("Color options"), NULL, NULL);
378 /* --------------------------------------------------------------------------------------------- */
380 static gchar *
381 mc_args_add_usage_info (void)
383 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
384 _("+number"),
385 _("[this_dir] [other_panel_dir]"),
386 _("+number"),
388 ("Set initial line number for the internal editor"));
389 return mc_args__loc__usage_string;
392 /* --------------------------------------------------------------------------------------------- */
394 static void
395 mc_args_add_extended_info_to_help (void)
397 mc_args__loc__footer_string = g_strdup_printf ("%s",
399 ("\n"
400 "Please send any bug reports (including the output of `mc -V')\n"
401 "as tickets at www.midnight-commander.org\n"));
402 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
404 #if GLIB_CHECK_VERSION(2,12,0)
405 g_option_context_set_description (context, mc_args__loc__footer_string);
406 g_option_context_set_summary (context, mc_args__loc__header_string);
407 #endif
410 /* --------------------------------------------------------------------------------------------- */
412 static void
413 mc_setup_by_args (int argc, char *argv[])
415 const char *base;
416 char *tmp;
418 #ifdef ENABLE_VFS_SMB
419 if (mc_args__debug_level != 0)
420 smbfs_set_debug (mc_args__debug_level);
421 #endif /* ENABLE_VFS_SMB */
423 if (mc_args__netfs_logfile != NULL)
425 #ifdef ENABLE_VFS_FTP
426 mc_setctl ("ftp://", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
427 #endif /* ENABLE_VFS_FTP */
428 #ifdef ENABLE_VFS_SMB
429 mc_setctl ("smb://", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
430 #endif /* ENABLE_VFS_SMB */
433 base = x_basename (argv[0]);
434 tmp = (argc > 0) ? argv[1] : NULL;
436 if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
438 /* mce* or vi is link to mc */
440 mc_run_param0 = g_strdup ("");
441 if (tmp != NULL)
444 * Check for filename:lineno, followed by an optional colon.
445 * This format is used by many programs (especially compilers)
446 * in error messages and warnings. It is supported so that
447 * users can quickly copy and paste file locations.
449 char *end, *p;
451 end = tmp + strlen (tmp);
452 p = end;
454 if (p > tmp && p[-1] == ':')
455 p--;
456 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
457 p--;
458 if (tmp < p && p < end && p[-1] == ':')
460 char *fname;
461 struct stat st;
463 fname = g_strndup (tmp, p - 1 - tmp);
465 * Check that the file before the colon actually exists.
466 * If it doesn't exist, revert to the old behavior.
468 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1)
470 mc_run_param0 = fname;
471 mc_args__edit_start_line = atoi (p);
473 else
475 g_free (fname);
476 goto try_plus_filename;
479 else
481 try_plus_filename:
482 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
484 int start_line;
486 start_line = atoi (tmp);
489 * If start_line is zero, position the cursor at the
490 * beginning of the file as other editors (vi, nano)
492 if (start_line == 0)
493 start_line++;
495 if (start_line > 0)
497 char *file;
499 file = (argc > 1) ? argv[2] : NULL;
500 if (file != NULL)
502 tmp = file;
503 mc_args__edit_start_line = start_line;
507 mc_run_param0 = g_strdup (tmp);
510 mc_global.mc_run_mode = MC_RUN_EDITOR;
512 else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
514 /* mcv* or view is link to mc */
516 if (tmp != NULL)
517 mc_run_param0 = g_strdup (tmp);
518 else
520 fprintf (stderr, "%s\n", _("No arguments given to the viewer."));
521 exit (EXIT_FAILURE);
523 mc_global.mc_run_mode = MC_RUN_VIEWER;
525 #ifdef USE_DIFF_VIEW
526 else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
528 /* mcd* or diff is link to mc */
530 if (argc < 3)
532 fprintf (stderr, "%s\n", _("Two files are required to evoke the diffviewer."));
533 exit (EXIT_FAILURE);
536 if (tmp != NULL)
538 mc_run_param0 = g_strdup (tmp);
539 tmp = (argc > 1) ? argv[2] : NULL;
540 if (tmp != NULL)
541 mc_run_param1 = g_strdup (tmp);
542 mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
545 #endif /* USE_DIFF_VIEW */
546 else
548 /* MC is run as mc */
550 switch (mc_global.mc_run_mode)
552 case MC_RUN_EDITOR:
553 case MC_RUN_VIEWER:
554 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
555 break;
557 case MC_RUN_DIFFVIEWER:
558 /* not implemented yet */
559 break;
561 case MC_RUN_FULL:
562 default:
563 /* sets the current dir and the other dir */
564 if (tmp != NULL)
566 mc_run_param0 = g_strdup (tmp);
567 tmp = (argc > 1) ? argv[2] : NULL;
568 if (tmp != NULL)
569 mc_run_param1 = g_strdup (tmp);
571 mc_global.mc_run_mode = MC_RUN_FULL;
572 break;
577 /* --------------------------------------------------------------------------------------------- */
579 static gboolean
580 mc_args_process (int argc, char *argv[])
582 if (mc_args__show_version)
584 show_version ();
585 return FALSE;
587 if (mc_args__show_datadirs)
589 printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
590 return FALSE;
593 if (mc_args__show_datadirs_extended)
595 show_datadirs_extended ();
596 return FALSE;
599 if (mc_args__show_configure_opts)
601 show_configure_options ();
602 return FALSE;
605 if (mc_args__force_colors)
606 mc_global.tty.disable_colors = FALSE;
608 #ifdef HAVE_SUBSHELL_SUPPORT
609 if (mc_args__nouse_subshell)
610 mc_global.tty.use_subshell = FALSE;
611 #endif /* HAVE_SUBSHELL_SUPPORT */
613 mc_setup_by_args (argc, argv);
615 return TRUE;
618 /* --------------------------------------------------------------------------------------------- */
620 static gchar *
621 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
622 const gchar * help_str)
624 GString *buffer = g_string_new ("");
625 GIConv conv = g_iconv_open (charset, "UTF-8");
626 gchar *full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
628 str_convert (conv, full_help_str, buffer);
630 g_free (full_help_str);
631 g_iconv_close (conv);
633 return g_string_free (buffer, FALSE);
636 /* --------------------------------------------------------------------------------------------- */
638 static gboolean
639 parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
641 (void) option_name;
642 (void) data;
643 (void) error;
645 mc_global.mc_run_mode = MC_RUN_EDITOR;
646 mc_run_param0 = g_strdup (value);
648 return TRUE;
651 /* --------------------------------------------------------------------------------------------- */
653 static gboolean
654 parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
656 (void) option_name;
657 (void) data;
658 (void) error;
660 mc_global.mc_run_mode = MC_RUN_VIEWER;
661 mc_run_param0 = g_strdup (value);
663 return TRUE;
666 /* --------------------------------------------------------------------------------------------- */
668 /*** public functions ****************************************************************************/
670 /* --------------------------------------------------------------------------------------------- */
672 gboolean
673 mc_args_handle (int argc, char **argv, const char *translation_domain)
675 GError *error = NULL;
676 const gchar *_system_codepage = str_detect_termencoding ();
678 #ifdef ENABLE_NLS
679 if (!str_isutf8 (_system_codepage))
680 bind_textdomain_codeset ("mc", "UTF-8");
681 #endif
683 context = g_option_context_new (mc_args_add_usage_info ());
685 g_option_context_set_ignore_unknown_options (context, FALSE);
687 mc_args_add_extended_info_to_help ();
689 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
691 g_option_group_add_entries (main_group, argument_main_table);
692 g_option_context_set_main_group (context, main_group);
693 g_option_group_set_translation_domain (main_group, translation_domain);
695 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
696 _("Terminal options"), NULL, NULL);
698 g_option_group_add_entries (terminal_group, argument_terminal_table);
699 g_option_context_add_group (context, terminal_group);
700 g_option_group_set_translation_domain (terminal_group, translation_domain);
702 color_group = mc_args_new_color_group ();
704 g_option_group_add_entries (color_group, argument_color_table);
705 g_option_context_add_group (context, color_group);
706 g_option_group_set_translation_domain (color_group, translation_domain);
708 if (!g_option_context_parse (context, &argc, &argv, &error))
710 if (error != NULL)
712 gchar *full_help_str;
713 gchar *help_str;
715 #if GLIB_CHECK_VERSION(2,14,0)
716 help_str = g_option_context_get_help (context, TRUE, NULL);
717 #else
718 help_str = g_strdup ("");
719 #endif
720 if (!str_isutf8 (_system_codepage))
721 full_help_str =
722 mc_args__convert_help_to_syscharset (_system_codepage, error->message,
723 help_str);
724 else
725 full_help_str = g_strdup_printf ("%s\n\n%s\n", error->message, help_str);
727 fprintf (stderr, "%s", full_help_str);
729 g_free (help_str);
730 g_free (full_help_str);
731 g_error_free (error);
733 g_option_context_free (context);
734 mc_args_clean_temp_help_strings ();
735 return FALSE;
738 g_option_context_free (context);
739 mc_args_clean_temp_help_strings ();
741 #ifdef ENABLE_NLS
742 if (!str_isutf8 (_system_codepage))
743 bind_textdomain_codeset ("mc", _system_codepage);
744 #endif
746 return mc_args_process (argc, argv);
749 /* --------------------------------------------------------------------------------------------- */