1 /*
2    Handle command line arguments.
3 
4    Copyright (C) 2009-2021
5    Free Software Foundation, Inc.
6 
7    Written by:
8    Slava Zanko <slavazanko@gmail.com>, 2009.
9    Andrew Borodin <aborodin@vmail.ru>, 2011, 2012.
10 
11    This file is part of the Midnight Commander.
12 
13    The Midnight Commander is free software: you can redistribute it
14    and/or modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation, either version 3 of the License,
16    or (at your option) any later version.
17 
18    The Midnight Commander is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #include <config.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 
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() */
36 
37 #ifdef ENABLE_VFS_SMB
38 #include "src/vfs/smbfs/smbfs.h"        /* smbfs_set_debugf()  */
39 #endif
40 
41 #include "src/textconf.h"
42 
43 #include "src/args.h"
44 
45 /*** external variables **************************************************************************/
46 
47 /*** global variables ****************************************************************************/
48 
49 /* If true, assume we are running on an xterm terminal */
50 gboolean mc_args__force_xterm = FALSE;
51 
52 gboolean mc_args__nomouse = FALSE;
53 
54 /* Force colors, only used by Slang */
55 gboolean mc_args__force_colors = FALSE;
56 
57 /* Don't load keymap from file and use default one */
58 gboolean mc_args__nokeymap = FALSE;
59 
60 char *mc_args__last_wd_file = NULL;
61 
62 /* when enabled NETCODE, use folowing file as logfile */
63 char *mc_args__netfs_logfile = NULL;
64 
65 /* keymap file */
66 char *mc_args__keymap_file = NULL;
67 
68 /* Debug level */
69 #ifdef ENABLE_VFS_SMB
70 int mc_args__debug_level = 0;
71 #endif
72 
73 void *mc_run_param0 = NULL;
74 char *mc_run_param1 = NULL;
75 
76 /*** file scope macro definitions ****************************************************************/
77 
78 /*** file scope type declarations ****************************************************************/
79 
80 /*** file scope variables ************************************************************************/
81 
82 /* If true, show version info and exit */
83 static gboolean mc_args__show_version = FALSE;
84 
85 /* forward declarations */
86 static gboolean parse_mc_e_argument (const gchar * option_name, const gchar * value,
87                                      gpointer data, GError ** mcerror);
88 static gboolean parse_mc_v_argument (const gchar * option_name, const gchar * value,
89                                      gpointer data, GError ** mcerror);
90 
91 static GOptionContext *context;
92 
93 #ifdef ENABLE_SUBSHELL
94 static gboolean mc_args__nouse_subshell = FALSE;
95 #endif /* ENABLE_SUBSHELL */
96 static gboolean mc_args__show_datadirs = FALSE;
97 static gboolean mc_args__show_datadirs_extended = FALSE;
98 #ifdef ENABLE_CONFIGURE_ARGS
99 static gboolean mc_args__show_configure_opts = FALSE;
100 #endif
101 
102 static GOptionGroup *main_group;
103 
104 static const GOptionEntry argument_main_table[] = {
105     /* *INDENT-OFF* */
106     /* generic options */
107     {
108      "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
109      &mc_args__show_version,
110      N_("Displays the current version"),
111      NULL
112     },
113 
114     /* options for wrappers */
115     {
116      "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
117      &mc_args__show_datadirs,
118      N_("Print data directory"),
119      NULL
120     },
121 
122     /* show extended information about used data directories */
123     {
124      "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
125      &mc_args__show_datadirs_extended,
126      N_("Print extended info about used data directories"),
127      NULL
128     },
129 
130 #ifdef ENABLE_CONFIGURE_ARGS
131     /* show configure options */
132     {
133      "configure-options", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
134      &mc_args__show_configure_opts,
135      N_("Print configure options"),
136      NULL
137     },
138 #endif
139 
140     {
141      "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
142      &mc_args__last_wd_file,
143      N_("Print last working directory to specified file"),
144      N_("<file>")
145     },
146 
147 #ifdef ENABLE_SUBSHELL
148     {
149      "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
150      &mc_global.tty.use_subshell,
151      N_("Enables subshell support (default)"),
152      NULL
153     },
154 
155     {
156      "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
157      &mc_args__nouse_subshell,
158      N_("Disables subshell support"),
159      NULL
160     },
161 #endif
162 
163     /* debug options */
164 #ifdef ENABLE_VFS_FTP
165     {
166      "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
167      &mc_args__netfs_logfile,
168      N_("Log ftp dialog to specified file"),
169      N_("<file>")
170     },
171 #endif /* ENABLE_VFS_FTP */
172 #ifdef ENABLE_VFS_SMB
173     {
174      "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
175      &mc_args__debug_level,
176      N_("Set debug level"),
177      N_("<integer>")
178     },
179 #endif /* ENABLE_VFS_SMB */
180 
181     {
182      /* handle arguments manually */
183      "view", 'v', G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
184      (gpointer) parse_mc_v_argument,
185      N_("Launches the file viewer on a file"),
186      N_("<file>")
187     },
188 
189     {
190      /* handle arguments manually */
191      "edit", 'e', G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
192      (gpointer) parse_mc_e_argument,
193      N_("Edit files"),
194      N_("<file> ...")
195     },
196 
197     G_OPTION_ENTRY_NULL
198     /* *INDENT-ON* */
199 };
200 
201 static GOptionGroup *terminal_group;
202 #define ARGS_TERM_OPTIONS 0
203 static const GOptionEntry argument_terminal_table[] = {
204     /* *INDENT-OFF* */
205     /* terminal options */
206     {
207      "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
208      &mc_args__force_xterm,
209      N_("Forces xterm features"),
210      NULL
211     },
212 
213     {
214      "no-x11", 'X', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
215      &mc_global.tty.disable_x11,
216      N_("Disable X11 support"),
217      NULL
218     },
219 
220     {
221      "oldmouse", 'g', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
222      &mc_global.tty.old_mouse,
223      N_("Tries to use an old highlight mouse tracking"),
224      NULL
225     },
226 
227     {
228      "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
229      &mc_args__nomouse,
230      N_("Disable mouse support in text version"),
231      NULL
232     },
233 
234 #ifdef HAVE_SLANG
235     {
236      "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
237      &SLtt_Try_Termcap,
238      N_("Tries to use termcap instead of terminfo"),
239      NULL
240     },
241 #endif
242 
243     {
244      "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
245      &mc_global.tty.slow_terminal,
246      N_("To run on slow terminals"),
247      NULL
248     },
249 
250     {
251      "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
252      &mc_global.tty.ugly_line_drawing,
253      N_("Use stickchars to draw"),
254      NULL
255     },
256 
257 #ifdef HAVE_SLANG
258     {
259      "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
260      &reset_hp_softkeys,
261      N_("Resets soft keys on HP terminals"),
262      NULL
263     },
264 #endif
265 
266     {
267      "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
268      &mc_args__keymap_file,
269      N_("Load definitions of key bindings from specified file"),
270      N_("<file>")
271     },
272 
273     {
274      "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
275      &mc_args__nokeymap,
276      N_("Don't load definitions of key bindings from file, use defaults"),
277      NULL
278     },
279 
280     G_OPTION_ENTRY_NULL
281     /* *INDENT-ON* */
282 };
283 
284 #undef ARGS_TERM_OPTIONS
285 
286 static GOptionGroup *color_group;
287 #define ARGS_COLOR_OPTIONS 0
288 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
289 static const GOptionEntry argument_color_table[] = {
290     /* *INDENT-OFF* */
291     /* color options */
292     {
293      "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
294      &mc_global.tty.disable_colors,
295      N_("Requests to run in black and white"),
296      NULL
297     },
298 
299     {
300      "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
301      &mc_args__force_colors,
302      N_("Request to run in color mode"),
303      NULL
304     },
305 
306     {
307      "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
308      &mc_global.tty.command_line_colors,
309      N_("Specifies a color configuration"),
310      N_("<string>")
311     },
312 
313     {
314      "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
315      &mc_global.tty.skin,
316      N_("Show mc with specified skin"),
317      N_("<string>")
318     },
319 
320     G_OPTION_ENTRY_NULL
321     /* *INDENT-ON* */
322 };
323 
324 #undef ARGS_COLOR_OPTIONS
325 
326 static gchar *mc_args__loc__colors_string = NULL;
327 static gchar *mc_args__loc__footer_string = NULL;
328 static gchar *mc_args__loc__header_string = NULL;
329 static gchar *mc_args__loc__usage_string = NULL;
330 
331 /* --------------------------------------------------------------------------------------------- */
332 /*** file scope functions ************************************************************************/
333 /* --------------------------------------------------------------------------------------------- */
334 
335 static void
mc_args_clean_temp_help_strings(void)336 mc_args_clean_temp_help_strings (void)
337 {
338     MC_PTR_FREE (mc_args__loc__colors_string);
339     MC_PTR_FREE (mc_args__loc__footer_string);
340     MC_PTR_FREE (mc_args__loc__header_string);
341     MC_PTR_FREE (mc_args__loc__usage_string);
342 }
343 
344 /* --------------------------------------------------------------------------------------------- */
345 
346 static GOptionGroup *
mc_args_new_color_group(void)347 mc_args_new_color_group (void)
348 {
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, editbg, editframe, editframeactive\n"
366                                                      "                 editframedrag\n"
367                                                      "   Viewer:       viewnormal,viewbold, viewunderline, viewselected\n"
368                                                      "   Help:         helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
369                                                    /* TRANSLATORS: don't translate color names and attributes */
370                                                    _("Standard Colors:\n"
371                                                     "   black, gray, red, brightred, green, brightgreen, brown,\n"
372                                                     "   yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
373                                                     "   brightcyan, lightgray and white\n\n"
374                                                     "Extended colors, when 256 colors are available:\n"
375                                                     "   color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
376                                                     "Attributes:\n"
377                                                     "   bold, italic, underline, reverse, blink; append more with '+'\n")
378                                                     );
379     /* *INDENT-ON* */
380 
381     return g_option_group_new ("color", mc_args__loc__colors_string,
382                                _("Color options"), NULL, NULL);
383 
384 }
385 
386 /* --------------------------------------------------------------------------------------------- */
387 
388 static gchar *
mc_args_add_usage_info(void)389 mc_args_add_usage_info (void)
390 {
391     gchar *s;
392 
393     switch (mc_global.mc_run_mode)
394     {
395     case MC_RUN_EDITOR:
396         s = g_strdup_printf ("%s\n", _("[+lineno] file1[:lineno] [file2[:lineno]...]"));
397         break;
398     case MC_RUN_VIEWER:
399         s = g_strdup_printf ("%s\n", _("file"));
400         break;
401 #ifdef USE_DIFF_VIEW
402     case MC_RUN_DIFFVIEWER:
403         s = g_strdup_printf ("%s\n", _("file1 file2"));
404         break;
405 #endif /* USE_DIFF_VIEW */
406     case MC_RUN_FULL:
407     default:
408         s = g_strdup_printf ("%s\n", _("[this_dir] [other_panel_dir]"));
409     }
410 
411     mc_args__loc__usage_string = s;
412 
413     return mc_args__loc__usage_string;
414 }
415 
416 /* --------------------------------------------------------------------------------------------- */
417 
418 static void
mc_args_add_extended_info_to_help(void)419 mc_args_add_extended_info_to_help (void)
420 {
421     mc_args__loc__footer_string = g_strdup_printf ("%s",
422                                                    _
423                                                    ("\n"
424                                                     "Please send any bug reports (including the output of 'mc -V')\n"
425                                                     "as tickets at www.midnight-commander.org\n"));
426     mc_args__loc__header_string =
427         g_strdup_printf (_("GNU Midnight Commander %s\n"), mc_global.mc_version);
428 
429     g_option_context_set_description (context, mc_args__loc__footer_string);
430     g_option_context_set_summary (context, mc_args__loc__header_string);
431 }
432 
433 /* --------------------------------------------------------------------------------------------- */
434 
435 static gchar *
mc_args__convert_help_to_syscharset(const gchar * charset,const gchar * error_message_str,const gchar * help_str)436 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message_str,
437                                      const gchar * help_str)
438 {
439     GString *buffer;
440     GIConv conv;
441     gchar *full_help_str;
442 
443     buffer = g_string_new ("");
444     conv = g_iconv_open (charset, "UTF-8");
445     full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message_str, help_str);
446 
447     str_convert (conv, full_help_str, buffer);
448 
449     g_free (full_help_str);
450     g_iconv_close (conv);
451 
452     return g_string_free (buffer, FALSE);
453 }
454 
455 /* --------------------------------------------------------------------------------------------- */
456 
457 static gboolean
parse_mc_e_argument(const gchar * option_name,const gchar * value,gpointer data,GError ** mcerror)458 parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data,
459                      GError ** mcerror)
460 {
461     (void) option_name;
462     (void) value;
463     (void) data;
464 
465     mc_return_val_if_error (mcerror, FALSE);
466 
467     mc_global.mc_run_mode = MC_RUN_EDITOR;
468 
469     return TRUE;
470 }
471 
472 /* --------------------------------------------------------------------------------------------- */
473 
474 static gboolean
parse_mc_v_argument(const gchar * option_name,const gchar * value,gpointer data,GError ** mcerror)475 parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data,
476                      GError ** mcerror)
477 {
478     (void) option_name;
479     (void) value;
480     (void) data;
481 
482     mc_return_val_if_error (mcerror, FALSE);
483 
484     mc_global.mc_run_mode = MC_RUN_VIEWER;
485 
486     return TRUE;
487 }
488 
489 /* --------------------------------------------------------------------------------------------- */
490 /**
491  * Create mcedit_arg_t object from vfs_path_t object and the line number.
492  *
493  * @param file_vpath  file path object
494  * @param line_number line number. If value is 0, try to restore saved position.
495  * @return mcedit_arg_t object
496  */
497 
498 static mcedit_arg_t *
mcedit_arg_vpath_new(vfs_path_t * file_vpath,long line_number)499 mcedit_arg_vpath_new (vfs_path_t * file_vpath, long line_number)
500 {
501     mcedit_arg_t *arg;
502 
503     arg = g_new (mcedit_arg_t, 1);
504     arg->file_vpath = file_vpath;
505     arg->line_number = line_number;
506 
507     return arg;
508 }
509 
510 /* --------------------------------------------------------------------------------------------- */
511 /**
512  * Create mcedit_arg_t object from file name and the line number.
513  *
514  * @param file_name   file name
515  * @param line_number line number. If value is 0, try to restore saved position.
516  * @return mcedit_arg_t object
517  */
518 
519 static mcedit_arg_t *
mcedit_arg_new(const char * file_name,long line_number)520 mcedit_arg_new (const char *file_name, long line_number)
521 {
522     return mcedit_arg_vpath_new (vfs_path_from_str (file_name), line_number);
523 }
524 
525 /* --------------------------------------------------------------------------------------------- */
526 /**
527  * Get list of filenames (and line numbers) from command line, when mc called as editor
528  *
529  * @param argc count of all arguments
530  * @param argv array of strings, contains arguments
531  * @return list of mcedit_arg_t objects
532  */
533 
534 static GList *
parse_mcedit_arguments(int argc,char ** argv)535 parse_mcedit_arguments (int argc, char **argv)
536 {
537     GList *flist = NULL;
538     int i;
539     long first_line_number = -1;
540 
541     for (i = 0; i < argc; i++)
542     {
543         char *tmp;
544         char *end, *p;
545         mcedit_arg_t *arg;
546 
547         tmp = argv[i];
548 
549         /*
550          * First, try to get line number as +lineno.
551          */
552         if (*tmp == '+')
553         {
554             long lineno;
555             char *error;
556 
557             lineno = strtol (tmp + 1, &error, 10);
558 
559             if (*error == '\0')
560             {
561                 /* this is line number */
562                 first_line_number = lineno;
563                 continue;
564             }
565             /* this is file name */
566         }
567 
568         /*
569          * Check for filename:lineno, followed by an optional colon.
570          * This format is used by many programs (especially compilers)
571          * in error messages and warnings. It is supported so that
572          * users can quickly copy and paste file locations.
573          */
574         end = tmp + strlen (tmp);
575         p = end;
576 
577         if (p > tmp && p[-1] == ':')
578             p--;
579         while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
580             p--;
581 
582         if (tmp < p && p < end && p[-1] == ':')
583         {
584             char *fname;
585             vfs_path_t *tmp_vpath, *fname_vpath;
586             struct stat st;
587 
588             fname = g_strndup (tmp, p - 1 - tmp);
589             tmp_vpath = vfs_path_from_str (tmp);
590             fname_vpath = vfs_path_from_str (fname);
591 
592             /*
593              * Check that the file before the colon actually exists.
594              * If it doesn't exist, create new file.
595              */
596             if (mc_stat (tmp_vpath, &st) == -1 && mc_stat (fname_vpath, &st) != -1)
597             {
598                 arg = mcedit_arg_vpath_new (fname_vpath, atoi (p));
599                 vfs_path_free (tmp_vpath, TRUE);
600             }
601             else
602             {
603                 arg = mcedit_arg_vpath_new (tmp_vpath, 0);
604                 vfs_path_free (fname_vpath, TRUE);
605             }
606 
607             g_free (fname);
608         }
609         else
610             arg = mcedit_arg_new (tmp, 0);
611 
612         flist = g_list_prepend (flist, arg);
613     }
614 
615     if (flist == NULL)
616         flist = g_list_prepend (flist, mcedit_arg_new (NULL, 0));
617     else if (first_line_number != -1)
618     {
619         /* overwrite line number for first file */
620         GList *l;
621 
622         l = g_list_last (flist);
623         ((mcedit_arg_t *) l->data)->line_number = first_line_number;
624     }
625 
626     return flist;
627 }
628 
629 /* --------------------------------------------------------------------------------------------- */
630 /*** public functions ****************************************************************************/
631 /* --------------------------------------------------------------------------------------------- */
632 
633 void
mc_setup_run_mode(char ** argv)634 mc_setup_run_mode (char **argv)
635 {
636     const char *base;
637 
638     base = x_basename (argv[0]);
639 
640     if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
641     {
642         /* mce* or vi is link to mc */
643         mc_global.mc_run_mode = MC_RUN_EDITOR;
644     }
645     else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
646     {
647         /* mcv* or view is link to mc */
648         mc_global.mc_run_mode = MC_RUN_VIEWER;
649     }
650 #ifdef USE_DIFF_VIEW
651     else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
652     {
653         /* mcd* or diff is link to mc */
654         mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
655     }
656 #endif /* USE_DIFF_VIEW */
657 }
658 
659 gboolean
mc_args_parse(int * argc,char *** argv,const char * translation_domain,GError ** mcerror)660 mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError ** mcerror)
661 {
662     const gchar *_system_codepage;
663     gboolean ok = TRUE;
664 
665     mc_return_val_if_error (mcerror, FALSE);
666 
667     _system_codepage = str_detect_termencoding ();
668 
669 #ifdef ENABLE_NLS
670     if (!str_isutf8 (_system_codepage))
671         bind_textdomain_codeset ("mc", "UTF-8");
672 #endif
673 
674     context = g_option_context_new (mc_args_add_usage_info ());
675 
676     g_option_context_set_ignore_unknown_options (context, FALSE);
677 
678     mc_args_add_extended_info_to_help ();
679 
680     main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
681 
682     g_option_group_add_entries (main_group, argument_main_table);
683     g_option_context_set_main_group (context, main_group);
684     g_option_group_set_translation_domain (main_group, translation_domain);
685 
686     terminal_group = g_option_group_new ("terminal", _("Terminal options"),
687                                          _("Terminal options"), NULL, NULL);
688 
689     g_option_group_add_entries (terminal_group, argument_terminal_table);
690     g_option_context_add_group (context, terminal_group);
691     g_option_group_set_translation_domain (terminal_group, translation_domain);
692 
693     color_group = mc_args_new_color_group ();
694 
695     g_option_group_add_entries (color_group, argument_color_table);
696     g_option_context_add_group (context, color_group);
697     g_option_group_set_translation_domain (color_group, translation_domain);
698 
699     if (!g_option_context_parse (context, argc, argv, mcerror))
700     {
701         if (*mcerror == NULL)
702             mc_propagate_error (mcerror, 0, "%s\n", _("Arguments parse error!"));
703         else
704         {
705             gchar *help_str;
706 
707             help_str = g_option_context_get_help (context, TRUE, NULL);
708 
709             if (str_isutf8 (_system_codepage))
710                 mc_replace_error (mcerror, (*mcerror)->code, "%s\n\n%s\n", (*mcerror)->message,
711                                   help_str);
712             else
713             {
714                 gchar *full_help_str;
715 
716                 full_help_str =
717                     mc_args__convert_help_to_syscharset (_system_codepage, (*mcerror)->message,
718                                                          help_str);
719                 mc_replace_error (mcerror, (*mcerror)->code, "%s", full_help_str);
720                 g_free (full_help_str);
721             }
722             g_free (help_str);
723         }
724 
725         ok = FALSE;
726     }
727 
728     g_option_context_free (context);
729     mc_args_clean_temp_help_strings ();
730 
731 #ifdef ENABLE_NLS
732     if (!str_isutf8 (_system_codepage))
733         bind_textdomain_codeset ("mc", _system_codepage);
734 #endif
735 
736     return ok;
737 }
738 
739 /* --------------------------------------------------------------------------------------------- */
740 
741 gboolean
mc_args_show_info(void)742 mc_args_show_info (void)
743 {
744     if (mc_args__show_version)
745     {
746         show_version ();
747         return FALSE;
748     }
749 
750     if (mc_args__show_datadirs)
751     {
752         printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
753         return FALSE;
754     }
755 
756     if (mc_args__show_datadirs_extended)
757     {
758         show_datadirs_extended ();
759         return FALSE;
760     }
761 
762 #ifdef ENABLE_CONFIGURE_ARGS
763     if (mc_args__show_configure_opts)
764     {
765         show_configure_options ();
766         return FALSE;
767     }
768 #endif
769 
770     return TRUE;
771 }
772 
773 /* --------------------------------------------------------------------------------------------- */
774 
775 gboolean
mc_setup_by_args(int argc,char ** argv,GError ** mcerror)776 mc_setup_by_args (int argc, char **argv, GError ** mcerror)
777 {
778     char *tmp;
779 
780     mc_return_val_if_error (mcerror, FALSE);
781 
782     if (mc_args__force_colors)
783         mc_global.tty.disable_colors = FALSE;
784 
785 #ifdef ENABLE_SUBSHELL
786     if (mc_args__nouse_subshell)
787         mc_global.tty.use_subshell = FALSE;
788 #endif /* ENABLE_SUBSHELL */
789 
790 #ifdef ENABLE_VFS_SMB
791     if (mc_args__debug_level != 0)
792         smbfs_set_debug (mc_args__debug_level);
793 #endif /* ENABLE_VFS_SMB */
794 
795     if (mc_args__netfs_logfile != NULL)
796     {
797         vfs_path_t *vpath;
798 #ifdef ENABLE_VFS_FTP
799         vpath = vfs_path_from_str ("ftp://");
800         mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
801         vfs_path_free (vpath, TRUE);
802 #endif /* ENABLE_VFS_FTP */
803 #ifdef ENABLE_VFS_SMB
804         vpath = vfs_path_from_str ("smb://");
805         mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
806         vfs_path_free (vpath, TRUE);
807 #endif /* ENABLE_VFS_SMB */
808         (void) vpath;
809     }
810 
811     tmp = (argc > 0) ? argv[1] : NULL;
812 
813     switch (mc_global.mc_run_mode)
814     {
815     case MC_RUN_EDITOR:
816         mc_run_param0 = parse_mcedit_arguments (argc - 1, &argv[1]);
817         break;
818 
819     case MC_RUN_VIEWER:
820         if (tmp == NULL)
821         {
822             mc_propagate_error (mcerror, 0, "%s\n", _("No arguments given to the viewer."));
823             return FALSE;
824         }
825 
826         mc_run_param0 = g_strdup (tmp);
827         break;
828 
829 #ifdef USE_DIFF_VIEW
830     case MC_RUN_DIFFVIEWER:
831         if (argc < 3)
832         {
833             mc_propagate_error (mcerror, 0, "%s\n",
834                                 _("Two files are required to envoke the diffviewer."));
835             return FALSE;
836         }
837         MC_FALLTHROUGH;
838 #endif /* USE_DIFF_VIEW */
839 
840     case MC_RUN_FULL:
841     default:
842         /* set the current dir and the other dir for filemanager,
843            or two files for diff viewer */
844         if (tmp != NULL)
845         {
846             mc_run_param0 = g_strdup (tmp);
847             tmp = (argc > 1) ? argv[2] : NULL;
848             if (tmp != NULL)
849                 mc_run_param1 = g_strdup (tmp);
850         }
851         break;
852     }
853 
854     return TRUE;
855 }
856 
857 /* --------------------------------------------------------------------------------------------- */
858 /**
859  * Free the mcedit_arg_t object.
860  *
861  * @param arg mcedit_arg_t object
862  */
863 
864 void
mcedit_arg_free(mcedit_arg_t * arg)865 mcedit_arg_free (mcedit_arg_t * arg)
866 {
867     vfs_path_free (arg->file_vpath, TRUE);
868     g_free (arg);
869 }
870 
871 /* --------------------------------------------------------------------------------------------- */
872