root/src/args.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_args_clean_temp_help_strings
  2. mc_args_new_color_group
  3. mc_args_add_usage_info
  4. mc_args_add_extended_info_to_help
  5. mc_args__convert_help_to_syscharset
  6. parse_mc_e_argument
  7. parse_mc_v_argument
  8. mcedit_arg_vpath_new
  9. mcedit_arg_new
  10. parse_mcedit_arguments
  11. mc_setup_run_mode
  12. mc_args_parse
  13. mc_args_show_info
  14. mc_setup_by_args
  15. mcedit_arg_free

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

/* [previous][next][first][last][top][bottom][index][help]  */