Fixed search for first symbol in string.
[midnight-commander.git] / src / args.c
blobf6ad04eb15552bc65f1e1ef609e153616089da94
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/mouse.h"
34 #include "lib/strutil.h"
35 #include "lib/vfs/vfs.h"
36 #include "lib/util.h" /* x_basename() */
38 #ifdef ENABLE_VFS_SMB
39 #include "src/vfs/smbfs/smbfs.h" /* smbfs_set_debugf() */
40 #endif
42 #include "src/main.h"
43 #include "src/textconf.h"
45 #include "src/args.h"
47 /*** external variables **************************************************************************/
49 /*** global variables ****************************************************************************/
51 /* If true, show version info and exit */
52 gboolean mc_args__show_version = FALSE;
54 /* If true, assume we are running on an xterm terminal */
55 gboolean mc_args__force_xterm = FALSE;
57 gboolean mc_args__nomouse = FALSE;
59 /* Force colors, only used by Slang */
60 gboolean mc_args__force_colors = FALSE;
62 /* Don't load keymap form file and use default one */
63 gboolean mc_args__nokeymap = FALSE;
65 /* Line to start the editor on */
66 int mc_args__edit_start_line = 0;
68 char *mc_args__last_wd_file = NULL;
70 /* when enabled NETCODE, use folowing file as logfile */
71 char *mc_args__netfs_logfile = NULL;
73 /* keymap file */
74 char *mc_args__keymap_file = NULL;
76 /* Debug level */
77 int mc_args__debug_level = 0;
79 /*** file scope macro definitions ****************************************************************/
81 /*** file scope type declarations ****************************************************************/
83 /*** file scope variables ************************************************************************/
85 /* forward declarations */
86 static gboolean parse_mc_e_argument (const gchar * option_name, const gchar * value,
87 gpointer data, GError ** error);
88 static gboolean parse_mc_v_argument (const gchar * option_name, const gchar * value,
89 gpointer data, GError ** error);
91 static GOptionContext *context;
93 static gboolean mc_args__nouse_subshell = FALSE;
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 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
209 &mc_args__nomouse,
210 N_("Disable mouse support in text version"),
211 NULL
214 #ifdef HAVE_SLANG
216 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
217 &SLtt_Try_Termcap,
218 N_("Tries to use termcap instead of terminfo"),
219 NULL
221 #endif
224 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
225 &mc_global.args.slow_terminal,
226 N_("To run on slow terminals"),
227 NULL
231 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
232 &mc_global.args.ugly_line_drawing,
233 N_("Use stickchars to draw"),
234 NULL
238 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
239 &reset_hp_softkeys,
240 N_("Resets soft keys on HP terminals"),
241 NULL
245 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
246 &mc_args__keymap_file,
247 N_("Load definitions of key bindings from specified file"),
248 "<file>"
252 "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
253 &mc_args__nokeymap,
254 N_("Don't load definitions of key bindings from file, use defaults"),
255 NULL
259 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
261 /* *INDENT-ON* */
264 #undef ARGS_TERM_OPTIONS
266 GOptionGroup *color_group;
267 #define ARGS_COLOR_OPTIONS 0
268 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
269 static const GOptionEntry argument_color_table[] = {
270 /* *INDENT-OFF* */
271 /* color options */
273 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
274 &mc_global.args.disable_colors,
275 N_("Requests to run in black and white"),
276 NULL
280 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
281 &mc_args__force_colors,
282 N_("Request to run in color mode"),
283 NULL
287 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
288 &mc_global.tty.command_line_colors,
289 N_("Specifies a color configuration"),
290 "<string>"
294 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
295 &mc_global.args.skin,
296 N_("Show mc with specified skin"),
297 "<string>"
301 NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
303 /* *INDENT-ON* */
306 #undef ARGS_COLOR_OPTIONS
308 static gchar *mc_args__loc__colors_string = NULL;
309 static gchar *mc_args__loc__footer_string = NULL;
310 static gchar *mc_args__loc__header_string = NULL;
311 static gchar *mc_args__loc__usage_string = NULL;
313 /*** file scope functions ************************************************************************/
315 /* --------------------------------------------------------------------------------------------- */
316 static void
317 mc_args_clean_temp_help_strings (void)
319 g_free (mc_args__loc__colors_string);
320 mc_args__loc__colors_string = NULL;
322 g_free (mc_args__loc__footer_string);
323 mc_args__loc__footer_string = NULL;
325 g_free (mc_args__loc__header_string);
326 mc_args__loc__header_string = NULL;
328 g_free (mc_args__loc__usage_string);
329 mc_args__loc__usage_string = NULL;
332 /* --------------------------------------------------------------------------------------------- */
334 static GOptionGroup *
335 mc_args_new_color_group (void)
337 /* *INDENT-OFF* */
338 /* FIXME: to preserve translations, lines should be split. */
339 mc_args__loc__colors_string = g_strdup_printf ("%s\n%s",
340 /* TRANSLATORS: don't translate keywords */
341 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
342 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
343 "\n Keywords:\n"
344 " Global: errors, disabled, reverse, gauge, header\n"
345 " input, inputmark, inputunchanged, commandlinemark\n"
346 " bbarhotkey, bbarbutton, statusbar\n"
347 " File display: normal, selected, marked, markselect\n"
348 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
349 " errdhotfocus\n"
350 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
351 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
352 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
353 " editlinestate\n"
354 " Viewer: viewbold, viewunderline, viewselected\n"
355 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
356 /* TRANSLATORS: don't translate color names and attributes */
357 _("Standard Colors:\n"
358 " black, gray, red, brightred, green, brightgreen, brown,\n"
359 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
360 " brightcyan, lightgray and white\n\n"
361 "Extended colors, when 256 colors are available:\n"
362 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
363 "Attributes:\n"
364 " bold, underline, reverse, blink; append more with '+'\n")
366 /* *INDENT-ON* */
368 return g_option_group_new ("color", mc_args__loc__colors_string,
369 _("Color options"), NULL, NULL);
373 /* --------------------------------------------------------------------------------------------- */
375 static gchar *
376 mc_args_add_usage_info (void)
378 mc_args__loc__usage_string = g_strdup_printf ("[%s] %s\n %s - %s\n",
379 _("+number"),
380 _("[this_dir] [other_panel_dir]"),
381 _("+number"),
383 ("Set initial line number for the internal editor"));
384 return mc_args__loc__usage_string;
387 /* --------------------------------------------------------------------------------------------- */
389 static void
390 mc_args_add_extended_info_to_help (void)
392 mc_args__loc__footer_string = g_strdup_printf ("%s",
394 ("\n"
395 "Please send any bug reports (including the output of `mc -V')\n"
396 "as tickets at www.midnight-commander.org\n"));
397 mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
399 #if GLIB_CHECK_VERSION(2,12,0)
400 g_option_context_set_description (context, mc_args__loc__footer_string);
401 g_option_context_set_summary (context, mc_args__loc__header_string);
402 #endif
405 /* --------------------------------------------------------------------------------------------- */
407 static void
408 mc_setup_by_args (int argc, char *argv[])
410 const char *base;
411 char *tmp;
413 if (mc_args__nomouse)
414 use_mouse_p = MOUSE_DISABLED;
416 if (mc_args__netfs_logfile != NULL)
418 #ifdef ENABLE_VFS_FTP
419 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
420 #endif /* ENABLE_VFS_FTP */
421 #ifdef ENABLE_VFS_SMB
422 smbfs_set_debugf (mc_args__netfs_logfile);
423 #endif /* ENABLE_VFS_SMB */
426 #ifdef ENABLE_VFS_SMB
427 if (mc_args__debug_level != 0)
428 smbfs_set_debug (mc_args__debug_level);
429 #endif /* ENABLE_VFS_SMB */
431 base = x_basename (argv[0]);
432 tmp = (argc > 0) ? argv[1] : NULL;
434 if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
436 /* mce* or vi is link to mc */
438 mc_run_param0 = g_strdup ("");
439 if (tmp != NULL)
442 * Check for filename:lineno, followed by an optional colon.
443 * This format is used by many programs (especially compilers)
444 * in error messages and warnings. It is supported so that
445 * users can quickly copy and paste file locations.
447 char *end, *p;
449 end = tmp + strlen (tmp);
450 p = end;
452 if (p > tmp && p[-1] == ':')
453 p--;
454 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
455 p--;
456 if (tmp < p && p < end && p[-1] == ':')
458 char *fname;
459 struct stat st;
461 fname = g_strndup (tmp, p - 1 - tmp);
463 * Check that the file before the colon actually exists.
464 * If it doesn't exist, revert to the old behavior.
466 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1)
468 mc_run_param0 = fname;
469 mc_args__edit_start_line = atoi (p);
471 else
473 g_free (fname);
474 goto try_plus_filename;
477 else
479 try_plus_filename:
480 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
482 int start_line;
484 start_line = atoi (tmp);
487 * If start_line is zero, position the cursor at the
488 * beginning of the file as other editors (vi, nano)
490 if (start_line == 0)
491 start_line++;
493 if (start_line > 0)
495 char *file;
497 file = (argc > 1) ? argv[2] : NULL;
498 if (file != NULL)
500 tmp = file;
501 mc_args__edit_start_line = start_line;
505 mc_run_param0 = g_strdup (tmp);
508 mc_global.mc_run_mode = MC_RUN_EDITOR;
510 else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
512 /* mcv* or view is link to mc */
514 if (tmp != NULL)
515 mc_run_param0 = g_strdup (tmp);
516 else
518 fprintf (stderr, "%s\n", _("No arguments given to the viewer."));
519 exit (EXIT_FAILURE);
521 mc_global.mc_run_mode = MC_RUN_VIEWER;
523 #ifdef USE_DIFF_VIEW
524 else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
526 /* mcd* or diff is link to mc */
528 if (argc < 3)
530 fprintf (stderr, "%s\n", _("Two files are required to evoke the diffviewer."));
531 exit (EXIT_FAILURE);
534 if (tmp != NULL)
536 mc_run_param0 = g_strdup (tmp);
537 tmp = (argc > 1) ? argv[2] : NULL;
538 if (tmp != NULL)
539 mc_run_param1 = g_strdup (tmp);
540 mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
543 #endif /* USE_DIFF_VIEW */
544 else
546 /* MC is run as mc */
548 switch (mc_global.mc_run_mode)
550 case MC_RUN_EDITOR:
551 case MC_RUN_VIEWER:
552 /* mc_run_param0 is set up in parse_mc_e_argument() and parse_mc_v_argument() */
553 break;
555 case MC_RUN_DIFFVIEWER:
556 /* not implemented yet */
557 break;
559 case MC_RUN_FULL:
560 default:
561 /* sets the current dir and the other dir */
562 if (tmp != NULL)
564 mc_run_param0 = g_strdup (tmp);
565 tmp = (argc > 1) ? argv[2] : NULL;
566 if (tmp != NULL)
567 mc_run_param1 = g_strdup (tmp);
569 mc_global.mc_run_mode = MC_RUN_FULL;
570 break;
575 /* --------------------------------------------------------------------------------------------- */
577 static gboolean
578 mc_args_process (int argc, char *argv[])
580 if (mc_args__show_version)
582 show_version ();
583 return FALSE;
585 if (mc_args__show_datadirs)
587 printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
588 return FALSE;
591 if (mc_args__show_datadirs_extended)
593 show_datadirs_extended ();
594 return FALSE;
597 if (mc_args__show_configure_opts)
599 show_configure_options ();
600 return FALSE;
603 if (mc_args__force_colors)
604 mc_global.args.disable_colors = FALSE;
606 #ifdef HAVE_SUBSHELL_SUPPORT
607 if (mc_args__nouse_subshell)
608 mc_global.tty.use_subshell = FALSE;
609 #endif /* HAVE_SUBSHELL_SUPPORT */
611 mc_setup_by_args (argc, argv);
613 return TRUE;
616 /* --------------------------------------------------------------------------------------------- */
618 static gchar *
619 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message,
620 const gchar * help_str)
622 GString *buffer = g_string_new ("");
623 GIConv conv = g_iconv_open (charset, "UTF-8");
624 gchar *full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message, help_str);
626 str_convert (conv, full_help_str, buffer);
628 g_free (full_help_str);
629 g_iconv_close (conv);
631 return g_string_free (buffer, FALSE);
634 /* --------------------------------------------------------------------------------------------- */
636 static gboolean
637 parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
639 (void) option_name;
640 (void) data;
641 (void) error;
643 mc_global.mc_run_mode = MC_RUN_EDITOR;
644 mc_run_param0 = g_strdup (value);
646 return TRUE;
649 /* --------------------------------------------------------------------------------------------- */
651 static gboolean
652 parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data, GError ** error)
654 (void) option_name;
655 (void) data;
656 (void) error;
658 mc_global.mc_run_mode = MC_RUN_VIEWER;
659 mc_run_param0 = g_strdup (value);
661 return TRUE;
664 /* --------------------------------------------------------------------------------------------- */
666 /*** public functions ****************************************************************************/
668 /* --------------------------------------------------------------------------------------------- */
670 gboolean
671 mc_args_handle (int argc, char **argv, const char *translation_domain)
673 GError *error = NULL;
674 const gchar *_system_codepage = str_detect_termencoding ();
676 #ifdef ENABLE_NLS
677 if (!str_isutf8 (_system_codepage))
678 bind_textdomain_codeset ("mc", "UTF-8");
679 #endif
681 context = g_option_context_new (mc_args_add_usage_info ());
683 g_option_context_set_ignore_unknown_options (context, FALSE);
685 mc_args_add_extended_info_to_help ();
687 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
689 g_option_group_add_entries (main_group, argument_main_table);
690 g_option_context_set_main_group (context, main_group);
691 g_option_group_set_translation_domain (main_group, translation_domain);
693 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
694 _("Terminal options"), NULL, NULL);
696 g_option_group_add_entries (terminal_group, argument_terminal_table);
697 g_option_context_add_group (context, terminal_group);
698 g_option_group_set_translation_domain (terminal_group, translation_domain);
700 color_group = mc_args_new_color_group ();
702 g_option_group_add_entries (color_group, argument_color_table);
703 g_option_context_add_group (context, color_group);
704 g_option_group_set_translation_domain (color_group, translation_domain);
706 if (!g_option_context_parse (context, &argc, &argv, &error))
708 if (error != NULL)
710 gchar *full_help_str;
711 gchar *help_str;
713 #if GLIB_CHECK_VERSION(2,14,0)
714 help_str = g_option_context_get_help (context, TRUE, NULL);
715 #else
716 help_str = g_strdup ("");
717 #endif
718 if (!str_isutf8 (_system_codepage))
719 full_help_str =
720 mc_args__convert_help_to_syscharset (_system_codepage, error->message,
721 help_str);
722 else
723 full_help_str = g_strdup_printf ("%s\n\n%s\n", error->message, help_str);
725 fprintf (stderr, "%s", full_help_str);
727 g_free (help_str);
728 g_free (full_help_str);
729 g_error_free (error);
731 g_option_context_free (context);
732 mc_args_clean_temp_help_strings ();
733 return FALSE;
736 g_option_context_free (context);
737 mc_args_clean_temp_help_strings ();
739 #ifdef ENABLE_NLS
740 if (!str_isutf8 (_system_codepage))
741 bind_textdomain_codeset ("mc", _system_codepage);
742 #endif
744 return mc_args_process (argc, argv);
747 /* --------------------------------------------------------------------------------------------- */