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

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