Merge branch '2499_mcedit_select_cur_word'
[midnight-commander/borarpet.git] / src / args.c
blob3f47e8e8c3f90172276ca083f909f0e6e410fd5d
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 #ifdef ENABLE_VFS_SMB
38 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debugf() */
39 #endif
40 #include "lib/util.h" /* x_basename() */
42 #include "src/main.h"
43 #include "src/textconf.h"
44 #include "src/subshell.h" /* use_subshell */
46 #include "src/args.h"
48 /*** external variables **************************************************************************/
50 /*** global variables ****************************************************************************/
52 /* If true, show version info and exit */
53 gboolean mc_args__show_version = FALSE;
55 /* If true, assume we are running on an xterm terminal */
56 gboolean mc_args__force_xterm = FALSE;
58 gboolean mc_args__nomouse = FALSE;
60 /* For slow terminals */
61 gboolean mc_args__slow_terminal = FALSE;
63 /* If true use +, -, | for line drawing */
64 gboolean mc_args__ugly_line_drawing = FALSE;
66 /* Set to force black and white display at program startup */
67 gboolean mc_args__disable_colors = FALSE;
69 /* Force colors, only used by Slang */
70 gboolean mc_args__force_colors = FALSE;
72 /* Line to start the editor on */
73 int mc_args__edit_start_line = 0;
75 /* Show in specified skin */
76 char *mc_args__skin = NULL;
78 char *mc_args__last_wd_file = NULL;
80 /* when enabled NETCODE, use folowing file as logfile */
81 char *mc_args__netfs_logfile = NULL;
83 /* keymap file */
84 char *mc_args__keymap_file = NULL;
86 /* Debug level */
87 int mc_args__debug_level = 0;
89 /*** file scope macro definitions ****************************************************************/
91 /*** file scope type declarations ****************************************************************/
93 /*** file scope variables ************************************************************************/
95 /* forward declarations */
96 static gboolean parse_mc_e_argument (const gchar * option_name, const gchar * value,
97 gpointer data, GError ** error);
98 static gboolean parse_mc_v_argument (const gchar * option_name, const gchar * value,
99 gpointer data, GError ** error);
101 static GOptionContext *context;
103 static gboolean mc_args__nouse_subshell = FALSE;
104 static gboolean mc_args__show_datadirs = FALSE;
106 static GOptionGroup *main_group;
108 static const GOptionEntry argument_main_table[] = {
109 /* *INDENT-OFF* */
110 /* generic options */
112 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
113 &mc_args__show_version,
114 N_("Displays the current version"),
115 NULL
118 /* options for wrappers */
120 "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
121 &mc_args__show_datadirs,
122 N_("Print data directory"),
123 NULL
127 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
128 &mc_args__last_wd_file,
129 N_("Print last working directory to specified file"),
130 "<file>"
133 #ifdef HAVE_SUBSHELL_SUPPORT
135 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
136 &use_subshell,
137 N_("Enables subshell support (default)"),
138 NULL
142 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
143 &mc_args__nouse_subshell,
144 N_("Disables subshell support"),
145 NULL
147 #endif
149 /* debug options */
150 #ifdef ENABLE_VFS_FTP
152 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
153 &mc_args__netfs_logfile,
154 N_("Log ftp dialog to specified file"),
155 "<file>"
157 #endif /* ENABLE_VFS_FTP */
158 #ifdef ENABLE_VFS_SMB
160 "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
161 &mc_args__debug_level,
162 N_("Set debug level"),
163 "<integer>"
165 #endif /* ENABLE_VFS_SMB */
167 /* single file operations */
169 "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
170 parse_mc_v_argument,
171 N_("Launches the file viewer on a file"),
172 "<file>"
176 "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_CALLBACK,
177 parse_mc_e_argument,
178 N_("Edits one file"),
179 "<file>"},
182 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
184 /* *INDENT-ON* */
187 GOptionGroup *terminal_group;
188 #define ARGS_TERM_OPTIONS 0
189 static const GOptionEntry argument_terminal_table[] = {
190 /* *INDENT-OFF* */
191 /* terminal options */
193 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
194 &mc_args__force_xterm,
195 N_("Forces xterm features"),
196 NULL
200 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
201 &mc_args__nomouse,
202 N_("Disable mouse support in text version"),
203 NULL
206 #ifdef HAVE_SLANG
208 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
209 &SLtt_Try_Termcap,
210 N_("Tries to use termcap instead of terminfo"),
211 NULL
213 #endif
216 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
217 &mc_args__slow_terminal,
218 N_("To run on slow terminals"),
219 NULL
223 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
224 &mc_args__ugly_line_drawing,
225 N_("Use stickchars to draw"),
226 NULL
230 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
231 &reset_hp_softkeys,
232 N_("Resets soft keys on HP terminals"),
233 NULL
237 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
238 &mc_args__keymap_file,
239 N_("Load definitions of key bindings from specified file"),
240 "<file>"
244 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
246 /* *INDENT-ON* */
249 #undef ARGS_TERM_OPTIONS
251 GOptionGroup *color_group;
252 #define ARGS_COLOR_OPTIONS 0
253 // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
254 static const GOptionEntry argument_color_table[] = {
255 /* *INDENT-OFF* */
256 /* color options */
258 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
259 &mc_args__disable_colors,
260 N_("Requests to run in black and white"),
261 NULL
265 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
266 &mc_args__force_colors,
267 N_("Request to run in color mode"),
268 NULL
272 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
273 &command_line_colors,
274 N_("Specifies a color configuration"),
275 "<string>"
279 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
280 &mc_args__skin,
281 N_("Show mc with specified skin"),
282 "<string>"
286 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
288 /* *INDENT-ON* */
291 #undef ARGS_COLOR_OPTIONS
293 static gchar *mc_args__loc__colors_string = NULL;
294 static gchar *mc_args__loc__footer_string = NULL;
295 static gchar *mc_args__loc__header_string = NULL;
296 static gchar *mc_args__loc__usage_string = NULL;
298 /*** file scope functions ************************************************************************/
300 /* --------------------------------------------------------------------------------------------- */
301 static void
302 mc_args_clean_temp_help_strings (void)
304 g_free (mc_args__loc__colors_string);
305 mc_args__loc__colors_string = NULL;
307 g_free (mc_args__loc__footer_string);
308 mc_args__loc__footer_string = NULL;
310 g_free (mc_args__loc__header_string);
311 mc_args__loc__header_string = NULL;
313 g_free (mc_args__loc__usage_string);
314 mc_args__loc__usage_string = NULL;
317 /* --------------------------------------------------------------------------------------------- */
319 static GOptionGroup *
320 mc_args_new_color_group (void)
322 /* FIXME: to preserve translations, lines should be split. */
323 mc_args__loc__colors_string = g_strdup_printf ("%s\n%s",
324 /* TRANSLATORS: don't translate keywords */
325 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
326 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
327 "\n Keywords:\n"
328 " Global: errors, disabled, reverse, gauge, header\n"
329 " input, inputmark, inputunchanged, commandlinemark\n"
330 " bbarhotkey, bbarbutton, statusbar\n"
331 " File display: normal, selected, marked, markselect\n"
332 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
333 " errdhotfocus\n"
334 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
335 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
336 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
337 " editlinestate\n"
338 " Viewer: viewbold, viewunderline, viewselected\n"
339 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
340 /* TRANSLATORS: don't translate color names and attributes */
341 _("Standard Colors:\n"
342 " black, gray, red, brightred, green, brightgreen, brown,\n"
343 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
344 " brightcyan, lightgray and white\n\n"
345 "Extended colors, when 256 colors are available:\n"
346 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
347 "Attributes:\n"
348 " bold, underline, reverse, blink; append more with '+'\n")
351 return g_option_group_new ("color", mc_args__loc__colors_string,
352 _("Color options"), NULL, NULL);
356 /* --------------------------------------------------------------------------------------------- */
358 static gchar *
359 mc_args_add_usage_info (void)
361 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
362 _("+number"),
363 _("[this_dir] [other_panel_dir]"),
364 _("+number"),
366 ("Set initial line number for the internal editor"));
367 return mc_args__loc__usage_string;
370 /* --------------------------------------------------------------------------------------------- */
372 static void
373 mc_args_add_extended_info_to_help (void)
375 mc_args__loc__footer_string = g_strdup_printf ("%s",
377 ("\n"
378 "Please send any bug reports (including the output of `mc -V')\n"
379 "as tickets at www.midnight-commander.org\n"));
380 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
382 #if GLIB_CHECK_VERSION(2,12,0)
383 g_option_context_set_description (context, mc_args__loc__footer_string);
384 g_option_context_set_summary (context, mc_args__loc__header_string);
385 #endif
388 /* --------------------------------------------------------------------------------------------- */
390 static void
391 mc_setup_by_args (int argc, char *argv[])
393 const char *base;
394 char *tmp;
396 if (mc_args__nomouse)
397 use_mouse_p = MOUSE_DISABLED;
399 if (mc_args__netfs_logfile != NULL)
401 #ifdef ENABLE_VFS_FTP
402 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
403 #endif /* ENABLE_VFS_FTP */
404 #ifdef ENABLE_VFS_SMB
405 smbfs_set_debugf (mc_args__netfs_logfile);
406 #endif /* ENABLE_VFS_SMB */
409 #ifdef ENABLE_VFS_SMB
410 if (mc_args__debug_level != 0)
411 smbfs_set_debug (mc_args__debug_level);
412 #endif /* ENABLE_VFS_SMB */
414 base = x_basename (argv[0]);
415 tmp = (argc > 0) ? argv[1] : NULL;
417 if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
419 /* mce* or vi is link to mc */
421 mc_run_param0 = g_strdup ("");
422 if (tmp != NULL)
425 * Check for filename:lineno, followed by an optional colon.
426 * This format is used by many programs (especially compilers)
427 * in error messages and warnings. It is supported so that
428 * users can quickly copy and paste file locations.
430 char *end, *p;
432 end = tmp + strlen (tmp);
433 p = end;
435 if (p > tmp && p[-1] == ':')
436 p--;
437 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
438 p--;
439 if (tmp < p && p < end && p[-1] == ':')
441 char *fname;
442 struct stat st;
444 fname = g_strndup (tmp, p - 1 - tmp);
446 * Check that the file before the colon actually exists.
447 * If it doesn't exist, revert to the old behavior.
449 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1)
451 mc_run_param0 = fname;
452 mc_args__edit_start_line = atoi (p);
454 else
456 g_free (fname);
457 goto try_plus_filename;
460 else
462 try_plus_filename:
463 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
465 int start_line;
467 start_line = atoi (tmp);
470 * If start_line is zero, position the cursor at the
471 * beginning of the file as other editors (vi, nano)
473 if (start_line == 0)
474 start_line++;
476 if (start_line > 0)
478 char *file;
480 file = (argc > 1) ? argv[2] : NULL;
481 if (file != NULL)
483 tmp = file;
484 mc_args__edit_start_line = start_line;
488 mc_run_param0 = g_strdup (tmp);
491 mc_run_mode = MC_RUN_EDITOR;
493 else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
495 /* mcv* or view is link to mc */
497 if (tmp != NULL)
498 mc_run_param0 = g_strdup (tmp);
499 else
501 fprintf (stderr, "%s\n", _("No arguments given to the viewer."));
502 exit (EXIT_FAILURE);
504 mc_run_mode = MC_RUN_VIEWER;
506 #ifdef USE_DIFF_VIEW
507 else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
509 /* mcd* or diff is link to mc */
511 if (argc < 3)
513 fprintf (stderr, "%s\n", _("Two files are required to evoke the diffviewer."));
514 exit (EXIT_FAILURE);
517 if (tmp != NULL)
519 mc_run_param0 = g_strdup (tmp);
520 tmp = (argc > 1) ? argv[2] : NULL;
521 if (tmp != NULL)
522 mc_run_param1 = g_strdup (tmp);
523 mc_run_mode = MC_RUN_DIFFVIEWER;
526 #endif /* USE_DIFF_VIEW */
527 else
529 /* MC is run as mc */
531 switch (mc_run_mode)
533 case MC_RUN_EDITOR:
534 case MC_RUN_VIEWER:
535 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
536 break;
538 case MC_RUN_DIFFVIEWER:
539 /* not implemented yet */
540 break;
542 case MC_RUN_FULL:
543 default:
544 /* sets the current dir and the other dir */
545 if (tmp != NULL)
547 mc_run_param0 = g_strdup (tmp);
548 tmp = (argc > 1) ? argv[2] : NULL;
549 if (tmp != NULL)
550 mc_run_param1 = g_strdup (tmp);
552 mc_run_mode = MC_RUN_FULL;
553 break;
558 /* --------------------------------------------------------------------------------------------- */
560 static gboolean
561 mc_args_process (int argc, char *argv[])
563 if (mc_args__show_version)
565 show_version ();
566 return FALSE;
568 if (mc_args__show_datadirs)
570 printf ("%s (%s)\n", mc_sysconfig_dir, mc_share_data_dir);
571 return FALSE;
574 if (mc_args__force_colors)
575 mc_args__disable_colors = FALSE;
577 #ifdef HAVE_SUBSHELL_SUPPORT
578 if (mc_args__nouse_subshell)
579 use_subshell = 0;
580 #endif /* HAVE_SUBSHELL_SUPPORT */
582 mc_setup_by_args (argc, argv);
584 return TRUE;
587 /* --------------------------------------------------------------------------------------------- */
589 static gchar *
590 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
591 const gchar * help_str)
593 GString *buffer = g_string_new ("");
594 GIConv conv = g_iconv_open (charset, "UTF-8");
595 gchar *full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
597 str_convert (conv, full_help_str, buffer);
599 g_free (full_help_str);
600 g_iconv_close (conv);
602 return g_string_free (buffer, FALSE);
605 /* --------------------------------------------------------------------------------------------- */
607 static gboolean
608 parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
610 (void) option_name;
611 (void) data;
612 (void) error;
614 mc_run_mode = MC_RUN_EDITOR;
615 mc_run_param0 = g_strdup (value);
617 return TRUE;
620 /* --------------------------------------------------------------------------------------------- */
622 static gboolean
623 parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
625 (void) option_name;
626 (void) data;
627 (void) error;
629 mc_run_mode = MC_RUN_VIEWER;
630 mc_run_param0 = g_strdup (value);
632 return TRUE;
635 /* --------------------------------------------------------------------------------------------- */
637 /*** public functions ****************************************************************************/
639 /* --------------------------------------------------------------------------------------------- */
641 gboolean
642 mc_args_handle (int argc, char **argv, const char *translation_domain)
644 GError *error = NULL;
645 const gchar *_system_codepage = str_detect_termencoding ();
647 #ifdef ENABLE_NLS
648 if (!str_isutf8 (_system_codepage))
649 bind_textdomain_codeset ("mc", "UTF-8");
650 #endif
652 context = g_option_context_new (mc_args_add_usage_info ());
654 g_option_context_set_ignore_unknown_options (context, FALSE);
656 mc_args_add_extended_info_to_help ();
658 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
660 g_option_group_add_entries (main_group, argument_main_table);
661 g_option_context_set_main_group (context, main_group);
662 g_option_group_set_translation_domain (main_group, translation_domain);
664 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
665 _("Terminal options"), NULL, NULL);
667 g_option_group_add_entries (terminal_group, argument_terminal_table);
668 g_option_context_add_group (context, terminal_group);
669 g_option_group_set_translation_domain (terminal_group, translation_domain);
671 color_group = mc_args_new_color_group ();
673 g_option_group_add_entries (color_group, argument_color_table);
674 g_option_context_add_group (context, color_group);
675 g_option_group_set_translation_domain (color_group, translation_domain);
677 if (!g_option_context_parse (context, &argc, &argv, &error))
679 if (error != NULL)
681 gchar *full_help_str;
682 gchar *help_str;
684 #if GLIB_CHECK_VERSION(2,14,0)
685 help_str = g_option_context_get_help (context, TRUE, NULL);
686 #else
687 help_str = g_strdup ("");
688 #endif
689 if (!str_isutf8 (_system_codepage))
690 full_help_str =
691 mc_args__convert_help_to_syscharset (_system_codepage, error->message,
692 help_str);
693 else
694 full_help_str = g_strdup_printf ("%s\n\n%s\n", error->message, help_str);
696 fprintf (stderr, "%s", full_help_str);
698 g_free (help_str);
699 g_free (full_help_str);
700 g_error_free (error);
702 g_option_context_free (context);
703 mc_args_clean_temp_help_strings ();
704 return FALSE;
707 g_option_context_free (context);
708 mc_args_clean_temp_help_strings ();
710 #ifdef ENABLE_NLS
711 if (!str_isutf8 (_system_codepage))
712 bind_textdomain_codeset ("mc", _system_codepage);
713 #endif
715 return mc_args_process (argc, argv);
718 /* --------------------------------------------------------------------------------------------- */