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-2019
   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 static gboolean mc_args__show_configure_opts = FALSE;
  99 
 100 static GOptionGroup *main_group;
 101 
 102 static const GOptionEntry argument_main_table[] = {
 103     /* *INDENT-OFF* */
 104     /* generic options */
 105     {
 106      "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
 107      &mc_args__show_version,
 108      N_("Displays the current version"),
 109      NULL
 110     },
 111 
 112     /* options for wrappers */
 113     {
 114      "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
 115      &mc_args__show_datadirs,
 116      N_("Print data directory"),
 117      NULL
 118     },
 119 
 120     /* show extended information about used data directories */
 121     {
 122      "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
 123      &mc_args__show_datadirs_extended,
 124      N_("Print extended info about used data directories"),
 125      NULL
 126     },
 127 
 128     /* show configure options */
 129     {
 130      "configure-options", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
 131      &mc_args__show_configure_opts,
 132      N_("Print configure options"),
 133      NULL
 134     },
 135 
 136     {
 137      "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
 138      &mc_args__last_wd_file,
 139      N_("Print last working directory to specified file"),
 140      N_("<file>")
 141     },
 142 
 143 #ifdef ENABLE_SUBSHELL
 144     {
 145      "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
 146      &mc_global.tty.use_subshell,
 147      N_("Enables subshell support (default)"),
 148      NULL
 149     },
 150 
 151     {
 152      "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
 153      &mc_args__nouse_subshell,
 154      N_("Disables subshell support"),
 155      NULL
 156     },
 157 #endif
 158 
 159     /* debug options */
 160 #ifdef ENABLE_VFS_FTP
 161     {
 162      "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
 163      &mc_args__netfs_logfile,
 164      N_("Log ftp dialog to specified file"),
 165      N_("<file>")
 166     },
 167 #endif /* ENABLE_VFS_FTP */
 168 #ifdef ENABLE_VFS_SMB
 169     {
 170      "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
 171      &mc_args__debug_level,
 172      N_("Set debug level"),
 173      N_("<integer>")
 174     },
 175 #endif /* ENABLE_VFS_SMB */
 176 
 177     {
 178      /* handle arguments manually */
 179      "view", 'v', G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
 180      (gpointer) parse_mc_v_argument,
 181      N_("Launches the file viewer on a file"),
 182      N_("<file>")
 183     },
 184 
 185     {
 186      /* handle arguments manually */
 187      "edit", 'e', G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
 188      (gpointer) parse_mc_e_argument,
 189      N_("Edit files"),
 190      N_("<file> ...") },
 191 
 192     {
 193      NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
 194     }
 195     /* *INDENT-ON* */
 196 };
 197 
 198 static GOptionGroup *terminal_group;
 199 #define ARGS_TERM_OPTIONS 0
 200 static const GOptionEntry argument_terminal_table[] = {
 201     /* *INDENT-OFF* */
 202     /* terminal options */
 203     {
 204      "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 205      &mc_args__force_xterm,
 206      N_("Forces xterm features"),
 207      NULL
 208     },
 209 
 210     {
 211      "no-x11", 'X', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 212      &mc_global.tty.disable_x11,
 213      N_("Disable X11 support"),
 214      NULL
 215     },
 216 
 217     {
 218      "oldmouse", 'g', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 219      &mc_global.tty.old_mouse,
 220      N_("Tries to use an old highlight mouse tracking"),
 221      NULL
 222     },
 223 
 224     {
 225      "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 226      &mc_args__nomouse,
 227      N_("Disable mouse support in text version"),
 228      NULL
 229     },
 230 
 231 #ifdef HAVE_SLANG
 232     {
 233      "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 234      &SLtt_Try_Termcap,
 235      N_("Tries to use termcap instead of terminfo"),
 236      NULL
 237     },
 238 #endif
 239 
 240     {
 241      "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 242      &mc_global.tty.slow_terminal,
 243      N_("To run on slow terminals"),
 244      NULL
 245     },
 246 
 247     {
 248      "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 249      &mc_global.tty.ugly_line_drawing,
 250      N_("Use stickchars to draw"),
 251      NULL
 252     },
 253 
 254 #ifdef HAVE_SLANG
 255     {
 256      "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 257      &reset_hp_softkeys,
 258      N_("Resets soft keys on HP terminals"),
 259      NULL
 260     },
 261 #endif
 262 
 263     {
 264      "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
 265      &mc_args__keymap_file,
 266      N_("Load definitions of key bindings from specified file"),
 267      N_("<file>")
 268     },
 269 
 270     {
 271      "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
 272      &mc_args__nokeymap,
 273      N_("Don't load definitions of key bindings from file, use defaults"),
 274      NULL
 275     },
 276 
 277     {
 278      NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
 279     }
 280     /* *INDENT-ON* */
 281 };
 282 
 283 #undef ARGS_TERM_OPTIONS
 284 
 285 static GOptionGroup *color_group;
 286 #define ARGS_COLOR_OPTIONS 0
 287 /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
 288 static const GOptionEntry argument_color_table[] = {
 289     /* *INDENT-OFF* */
 290     /* color options */
 291     {
 292      "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
 293      &mc_global.tty.disable_colors,
 294      N_("Requests to run in black and white"),
 295      NULL
 296     },
 297 
 298     {
 299      "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
 300      &mc_args__force_colors,
 301      N_("Request to run in color mode"),
 302      NULL
 303     },
 304 
 305     {
 306      "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
 307      &mc_global.tty.command_line_colors,
 308      N_("Specifies a color configuration"),
 309      N_("<string>")
 310     },
 311 
 312     {
 313      "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
 314      &mc_global.tty.skin,
 315      N_("Show mc with specified skin"),
 316      N_("<string>")
 317     },
 318 
 319     {
 320      NULL, '\0', 0, 0, NULL, NULL, NULL /* Complete struct initialization */
 321     }
 322     /* *INDENT-ON* */
 323 };
 324 
 325 #undef ARGS_COLOR_OPTIONS
 326 
 327 static gchar *mc_args__loc__colors_string = NULL;
 328 static gchar *mc_args__loc__footer_string = NULL;
 329 static gchar *mc_args__loc__header_string = NULL;
 330 static gchar *mc_args__loc__usage_string = NULL;
 331 
 332 /*** file scope functions ************************************************************************/
 333 
 334 /* --------------------------------------------------------------------------------------------- */
 335 static void
 336 mc_args_clean_temp_help_strings (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 *
 347 mc_args_new_color_group (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 *
 389 mc_args_add_usage_info (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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
 419 mc_args_add_extended_info_to_help (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
 427 
 428     g_option_context_set_description (context, mc_args__loc__footer_string);
 429     g_option_context_set_summary (context, mc_args__loc__header_string);
 430 }
 431 
 432 /* --------------------------------------------------------------------------------------------- */
 433 
 434 static gchar *
 435 mc_args__convert_help_to_syscharset (const gchar * charset, const gchar * error_message_str,
     /* [previous][next][first][last][top][bottom][index][help]  */
 436                                      const gchar * help_str)
 437 {
 438     GString *buffer;
 439     GIConv conv;
 440     gchar *full_help_str;
 441 
 442     buffer = g_string_new ("");
 443     conv = g_iconv_open (charset, "UTF-8");
 444     full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message_str, help_str);
 445 
 446     str_convert (conv, full_help_str, buffer);
 447 
 448     g_free (full_help_str);
 449     g_iconv_close (conv);
 450 
 451     return g_string_free (buffer, FALSE);
 452 }
 453 
 454 /* --------------------------------------------------------------------------------------------- */
 455 
 456 static gboolean
 457 parse_mc_e_argument (const gchar * option_name, const gchar * value, gpointer data,
     /* [previous][next][first][last][top][bottom][index][help]  */
 458                      GError ** mcerror)
 459 {
 460     (void) option_name;
 461     (void) value;
 462     (void) data;
 463 
 464     mc_return_val_if_error (mcerror, FALSE);
 465 
 466     mc_global.mc_run_mode = MC_RUN_EDITOR;
 467 
 468     return TRUE;
 469 }
 470 
 471 /* --------------------------------------------------------------------------------------------- */
 472 
 473 static gboolean
 474 parse_mc_v_argument (const gchar * option_name, const gchar * value, gpointer data,
     /* [previous][next][first][last][top][bottom][index][help]  */
 475                      GError ** mcerror)
 476 {
 477     (void) option_name;
 478     (void) value;
 479     (void) data;
 480 
 481     mc_return_val_if_error (mcerror, FALSE);
 482 
 483     mc_global.mc_run_mode = MC_RUN_VIEWER;
 484 
 485     return TRUE;
 486 }
 487 
 488 /* --------------------------------------------------------------------------------------------- */
 489 /**
 490  * Create mcedit_arg_t object from vfs_path_t object and the line number.
 491  *
 492  * @param file_vpath  file path object
 493  * @param line_number line number. If value is 0, try to restore saved position.
 494  * @return mcedit_arg_t object
 495  */
 496 
 497 static mcedit_arg_t *
 498 mcedit_arg_vpath_new (vfs_path_t * file_vpath, long line_number)
     /* [previous][next][first][last][top][bottom][index][help]  */
 499 {
 500     mcedit_arg_t *arg;
 501 
 502     arg = g_new (mcedit_arg_t, 1);
 503     arg->file_vpath = file_vpath;
 504     arg->line_number = line_number;
 505 
 506     return arg;
 507 }
 508 
 509 /* --------------------------------------------------------------------------------------------- */
 510 /**
 511  * Create mcedit_arg_t object from file name and the line number.
 512  *
 513  * @param file_name   file name
 514  * @param line_number line number. If value is 0, try to restore saved position.
 515  * @return mcedit_arg_t object
 516  */
 517 
 518 static mcedit_arg_t *
 519 mcedit_arg_new (const char *file_name, long line_number)
     /* [previous][next][first][last][top][bottom][index][help]  */
 520 {
 521     return mcedit_arg_vpath_new (vfs_path_from_str (file_name), line_number);
 522 }
 523 
 524 /* --------------------------------------------------------------------------------------------- */
 525 /**
 526  * Get list of filenames (and line numbers) from command line, when mc called as editor
 527  *
 528  * @param argc count of all arguments
 529  * @param argv array of strings, contains arguments
 530  * @return list of mcedit_arg_t objects
 531  */
 532 
 533 static GList *
 534 parse_mcedit_arguments (int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help]  */
 535 {
 536     GList *flist = NULL;
 537     int i;
 538     long first_line_number = -1;
 539 
 540     for (i = 0; i < argc; i++)
 541     {
 542         char *tmp;
 543         char *end, *p;
 544         mcedit_arg_t *arg;
 545 
 546         tmp = argv[i];
 547 
 548         /*
 549          * First, try to get line number as +lineno.
 550          */
 551         if (*tmp == '+')
 552         {
 553             long lineno;
 554             char *error;
 555 
 556             lineno = strtol (tmp + 1, &error, 10);
 557 
 558             if (*error == '\0')
 559             {
 560                 /* this is line number */
 561                 first_line_number = lineno;
 562                 continue;
 563             }
 564             /* this is file name */
 565         }
 566 
 567         /*
 568          * Check for filename:lineno, followed by an optional colon.
 569          * This format is used by many programs (especially compilers)
 570          * in error messages and warnings. It is supported so that
 571          * users can quickly copy and paste file locations.
 572          */
 573         end = tmp + strlen (tmp);
 574         p = end;
 575 
 576         if (p > tmp && p[-1] == ':')
 577             p--;
 578         while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
 579             p--;
 580 
 581         if (tmp < p && p < end && p[-1] == ':')
 582         {
 583             char *fname;
 584             vfs_path_t *tmp_vpath, *fname_vpath;
 585             struct stat st;
 586 
 587             fname = g_strndup (tmp, p - 1 - tmp);
 588             tmp_vpath = vfs_path_from_str (tmp);
 589             fname_vpath = vfs_path_from_str (fname);
 590 
 591             /*
 592              * Check that the file before the colon actually exists.
 593              * If it doesn't exist, create new file.
 594              */
 595             if (mc_stat (tmp_vpath, &st) == -1 && mc_stat (fname_vpath, &st) != -1)
 596             {
 597                 arg = mcedit_arg_vpath_new (fname_vpath, atoi (p));
 598                 vfs_path_free (tmp_vpath);
 599             }
 600             else
 601             {
 602                 arg = mcedit_arg_vpath_new (tmp_vpath, 0);
 603                 vfs_path_free (fname_vpath);
 604             }
 605 
 606             g_free (fname);
 607         }
 608         else
 609             arg = mcedit_arg_new (tmp, 0);
 610 
 611         flist = g_list_prepend (flist, arg);
 612     }
 613 
 614     if (flist == NULL)
 615         flist = g_list_prepend (flist, mcedit_arg_new (NULL, 0));
 616     else if (first_line_number != -1)
 617     {
 618         /* overwrite line number for first file */
 619         GList *l;
 620 
 621         l = g_list_last (flist);
 622         ((mcedit_arg_t *) l->data)->line_number = first_line_number;
 623     }
 624 
 625     return flist;
 626 }
 627 
 628 /* --------------------------------------------------------------------------------------------- */
 629 /*** public functions ****************************************************************************/
 630 /* --------------------------------------------------------------------------------------------- */
 631 
 632 void
 633 mc_setup_run_mode (char **argv)
     /* [previous][next][first][last][top][bottom][index][help]  */
 634 {
 635     const char *base;
 636 
 637     base = x_basename (argv[0]);
 638 
 639     if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
 640     {
 641         /* mce* or vi is link to mc */
 642         mc_global.mc_run_mode = MC_RUN_EDITOR;
 643     }
 644     else if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
 645     {
 646         /* mcv* or view is link to mc */
 647         mc_global.mc_run_mode = MC_RUN_VIEWER;
 648     }
 649 #ifdef USE_DIFF_VIEW
 650     else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
 651     {
 652         /* mcd* or diff is link to mc */
 653         mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
 654     }
 655 #endif /* USE_DIFF_VIEW */
 656 }
 657 
 658 gboolean
 659 mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 660 {
 661     const gchar *_system_codepage;
 662     gboolean ok = TRUE;
 663 
 664     mc_return_val_if_error (mcerror, FALSE);
 665 
 666     _system_codepage = str_detect_termencoding ();
 667 
 668 #ifdef ENABLE_NLS
 669     if (!str_isutf8 (_system_codepage))
 670         bind_textdomain_codeset ("mc", "UTF-8");
 671 #endif
 672 
 673     context = g_option_context_new (mc_args_add_usage_info ());
 674 
 675     g_option_context_set_ignore_unknown_options (context, FALSE);
 676 
 677     mc_args_add_extended_info_to_help ();
 678 
 679     main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
 680 
 681     g_option_group_add_entries (main_group, argument_main_table);
 682     g_option_context_set_main_group (context, main_group);
 683     g_option_group_set_translation_domain (main_group, translation_domain);
 684 
 685     terminal_group = g_option_group_new ("terminal", _("Terminal options"),
 686                                          _("Terminal options"), NULL, NULL);
 687 
 688     g_option_group_add_entries (terminal_group, argument_terminal_table);
 689     g_option_context_add_group (context, terminal_group);
 690     g_option_group_set_translation_domain (terminal_group, translation_domain);
 691 
 692     color_group = mc_args_new_color_group ();
 693 
 694     g_option_group_add_entries (color_group, argument_color_table);
 695     g_option_context_add_group (context, color_group);
 696     g_option_group_set_translation_domain (color_group, translation_domain);
 697 
 698     if (!g_option_context_parse (context, argc, argv, mcerror))
 699     {
 700         if (*mcerror == NULL)
 701             mc_propagate_error (mcerror, 0, "%s\n", _("Arguments parse error!"));
 702         else
 703         {
 704             gchar *help_str;
 705 
 706             help_str = g_option_context_get_help (context, TRUE, NULL);
 707 
 708             if (str_isutf8 (_system_codepage))
 709                 mc_replace_error (mcerror, (*mcerror)->code, "%s\n\n%s\n", (*mcerror)->message,
 710                                   help_str);
 711             else
 712             {
 713                 gchar *full_help_str;
 714 
 715                 full_help_str =
 716                     mc_args__convert_help_to_syscharset (_system_codepage, (*mcerror)->message,
 717                                                          help_str);
 718                 mc_replace_error (mcerror, (*mcerror)->code, "%s", full_help_str);
 719                 g_free (full_help_str);
 720             }
 721             g_free (help_str);
 722         }
 723 
 724         ok = FALSE;
 725     }
 726 
 727     g_option_context_free (context);
 728     mc_args_clean_temp_help_strings ();
 729 
 730 #ifdef ENABLE_NLS
 731     if (!str_isutf8 (_system_codepage))
 732         bind_textdomain_codeset ("mc", _system_codepage);
 733 #endif
 734 
 735     return ok;
 736 }
 737 
 738 /* --------------------------------------------------------------------------------------------- */
 739 
 740 gboolean
 741 mc_args_show_info (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 742 {
 743     if (mc_args__show_version)
 744     {
 745         show_version ();
 746         return FALSE;
 747     }
 748 
 749     if (mc_args__show_datadirs)
 750     {
 751         printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
 752         return FALSE;
 753     }
 754 
 755     if (mc_args__show_datadirs_extended)
 756     {
 757         show_datadirs_extended ();
 758         return FALSE;
 759     }
 760 
 761     if (mc_args__show_configure_opts)
 762     {
 763         show_configure_options ();
 764         return FALSE;
 765     }
 766 
 767     return TRUE;
 768 }
 769 
 770 /* --------------------------------------------------------------------------------------------- */
 771 
 772 gboolean
 773 mc_setup_by_args (int argc, char **argv, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 774 {
 775     char *tmp;
 776 
 777     mc_return_val_if_error (mcerror, FALSE);
 778 
 779     if (mc_args__force_colors)
 780         mc_global.tty.disable_colors = FALSE;
 781 
 782 #ifdef ENABLE_SUBSHELL
 783     if (mc_args__nouse_subshell)
 784         mc_global.tty.use_subshell = FALSE;
 785 #endif /* ENABLE_SUBSHELL */
 786 
 787 #ifdef ENABLE_VFS_SMB
 788     if (mc_args__debug_level != 0)
 789         smbfs_set_debug (mc_args__debug_level);
 790 #endif /* ENABLE_VFS_SMB */
 791 
 792     if (mc_args__netfs_logfile != NULL)
 793     {
 794         vfs_path_t *vpath;
 795 #ifdef ENABLE_VFS_FTP
 796         vpath = vfs_path_from_str ("ftp://");
 797         mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
 798         vfs_path_free (vpath);
 799 #endif /* ENABLE_VFS_FTP */
 800 #ifdef ENABLE_VFS_SMB
 801         vpath = vfs_path_from_str ("smb://");
 802         mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
 803         vfs_path_free (vpath);
 804 #endif /* ENABLE_VFS_SMB */
 805         (void) vpath;
 806     }
 807 
 808     tmp = (argc > 0) ? argv[1] : NULL;
 809 
 810     switch (mc_global.mc_run_mode)
 811     {
 812     case MC_RUN_EDITOR:
 813         mc_run_param0 = parse_mcedit_arguments (argc - 1, &argv[1]);
 814         break;
 815 
 816     case MC_RUN_VIEWER:
 817         if (tmp == NULL)
 818         {
 819             mc_propagate_error (mcerror, 0, "%s\n", _("No arguments given to the viewer."));
 820             return FALSE;
 821         }
 822 
 823         mc_run_param0 = g_strdup (tmp);
 824         break;
 825 
 826 #ifdef USE_DIFF_VIEW
 827     case MC_RUN_DIFFVIEWER:
 828         if (argc < 3)
 829         {
 830             mc_propagate_error (mcerror, 0, "%s\n",
 831                                 _("Two files are required to envoke the diffviewer."));
 832             return FALSE;
 833         }
 834         MC_FALLTHROUGH;
 835 #endif /* USE_DIFF_VIEW */
 836 
 837     case MC_RUN_FULL:
 838     default:
 839         /* set the current dir and the other dir for filemanager,
 840            or two files for diff viewer */
 841         if (tmp != NULL)
 842         {
 843             mc_run_param0 = g_strdup (tmp);
 844             tmp = (argc > 1) ? argv[2] : NULL;
 845             if (tmp != NULL)
 846                 mc_run_param1 = g_strdup (tmp);
 847         }
 848         break;
 849     }
 850 
 851     return TRUE;
 852 }
 853 
 854 /* --------------------------------------------------------------------------------------------- */
 855 /**
 856  * Free the mcedit_arg_t object.
 857  *
 858  * @param arg mcedit_arg_t object
 859  */
 860 
 861 void
 862 mcedit_arg_free (mcedit_arg_t * arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
 863 {
 864     vfs_path_free (arg->file_vpath);
 865     g_free (arg);
 866 }
 867 
 868 /* --------------------------------------------------------------------------------------------- */

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